jfmengels/elm-lint/2.0.1

NOTE: this package is not compatible with Elm 0.19.1

Lint

A linter for Elm.

See Lint.Rules for available rules. To define the rules you wish to use:

rules =
    [ Lint.Rules.NoDebug.rule
    , Lint.Rules.NoUnusedVariables
    ]

To run the rules on a source code and get a list of errors:

lint : String -> List String
lint source =
    let
        errors =
            List.concatMap (\rule -> rule source) rules
    in
        if List.isEmpty errors then
            [ "No errors." ]
        else
            List.map (\err -> err.rule ++ ": " ++ err.message) errors

Implementation

List ( Severity, LintRule )
-> String
-> Result (List String) (List ( Severity, LintError ))

Lints a file and gives back the errors raised by the given rules.

errors =
    lintSource rules source

Rule creation functions

lint : List Statement -> LintRuleImplementation context -> List LintError

Lints source code using a given rule implementation, and gives back a list of errors that were found.

rule : LintRule
rule input =
    lint input implementation


implementation : LintRuleImplementation Context
implementation =
    { statementFn = doNothing
    , typeFn = doNothing
    , expressionFn = expressionFn
    , moduleEndFn = (\ctx -> ( [], ctx ))
    , initialContext = Context
    }
doNothing : LintImplementation a context

Basic implementation of a visitor function that does nothing, i.e. return an empty list of errors and an untouched context. This is used to avoid a bit of boilerplate for visitor functions whose node types we are not interested in.

implementation : LintRuleImplementation Context
implementation =
    { statementFn = doNothing
    , typeFn = doNothing
    , expressionFn = expressionFn
    , moduleEndFn = (\ctx -> ( [], ctx ))
    , initialContext = Context
    }
LintRuleImplementation context
-> Expression
-> ( List LintError, context )

Visit an expression using a sub rule implementation. The use of this function is not encouraged, but it can make part of the implementation of complex rules much easier. It gives back a list of errors and a context.

expressionFn : Context -> Direction Expression -> ( List LintError, Context )
expressionFn ctx node =
    case node of
        Enter (Case expr patterns) ->
            visitExpression subimplementation expr
        _ ->
            ( [], ctx )


subimplementation : LintRuleImplementation Subcontext
subimplementation =
    { statementFn = doNothing
    , typeFn = doNothing
    , expressionFn = subexpressionFn
    , moduleEndFn = (\ctx -> ( [], ctx ))
    , initialContext = Subcontext
    }

Internal

countErrors : Severity -> List ( File, List ( Severity, LintError ) ) -> Int

Count the number of errors of a given Severity in the list of errors.

parseSource : String -> Result (List String) (List Statement)

Parse source code into a AST