lue-bird/elm-review-pattern/1.0.2

NOTE: the latest version is 1.0.3

Review.Pattern.As

Forbid ... as ..variable.. patterns.

forbid : Rule

Forbid ... as ..variable.. patterns.

config =
    [ Review.Pattern.As.forbid
    ]

For example

view ({ windowSize } as model) =
    Html.text "Hello!"

will be fixed to

view model =
    let
        { windowSize } =
            model
    in
    Html.text "Hello!"

This fix doesn't try to merge this destructuring into existing lets. Adding jfmengels/elm-review-simplify will do this for you.

why

  • as can quickly lead to barely readable, long patterns:

    Node _ (Expression.Lambda ({ argumentPatterns, resultExpression, arrowRange } as lambda)) ->
        x

    compared to

    Node _ (Expression.Lambda lambda) ->
        let
            { argumentPatterns, resultExpression, arrowRange } =
                lambda
        in
        x
  • less syntax to learn to use right and keep in your head

  • as in case of patterns can create variables with types that know less than you:

    highestScoreUi ... =
        case computeScores ... of
            [] ->
                textUi "no scores, yet"
    
    
            (head :: _) as scores ->
                case List.maximum scores of
                    -- This can never happen
                    Nothing ->
                        -- so we'll just use the head
                        scoreUi head
    
    
                    Just highestFound ->
                        scoreUi highestFound

    compared to

    highestScoreUi ... =
        case computeScores ... of
            [] ->
                textUi "no scores, yet"
    
    
            scoresHead :: scoresTail ->
                scoreUi (List.Nonempty.maximum ( scoresHead, scoresTail ))

    There are many more reasons for not introducing variables like that, see VariablesBetweenCaseOf.AccessInCases.forbid.