jfmengels/elm-lint/3.0.0

NOTE: the latest version is 4.1.2

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 : File -> 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 =
    { typeFn = doNothing
    , expressionFn = expressionFn
    , moduleEndFn = \ctx -> ( [], ctx )
    , initialContext = Context
    }
doNothing : context -> Direction (Node a) -> ( List LintError, 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
-> Node 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

parseSource : String -> Result (List String) File

Parse source code into a AST