alex-tan/elm-review-opinionated-simplify/1.0.0

elm-review-opinionated-simplify

Provides elm-review rules for opinionated simplification of Elm code.

This package was generated with Claude Code. There may be bugs -- pull requests are welcome.

Provided rules

  • PreferMaybeChaining - Reports case expressions on Maybe that can be simplified using Maybe.map, Maybe.andThen, Maybe.withDefault, or Maybe.Extra.withDefaultLazy.
  • SimplifyLambdaToComposition - Reports lambdas like (\x -> func x.field) that can be simplified to function composition (func << .field).

Configuration

module ReviewConfig exposing (config)

import PreferMaybeChaining
import Review.Rule exposing (Rule)
import SimplifyLambdaToComposition

config : List Rule
config =
    [ PreferMaybeChaining.rule
    , SimplifyLambdaToComposition.rule
    ]

Examples

PreferMaybeChaining

Note: This rule suggests using Maybe.Extra.withDefaultLazy for complex default expressions. Make sure elm-community/maybe-extra is installed in your project.

Maybe.map

-- Before
case maybeValue of
    Nothing -> Nothing
    Just x -> Just (f x)

-- After
maybeValue
    |> Maybe.map (\x -> f x)

Maybe.andThen

-- Before
case maybeId of
    Nothing -> Nothing
    Just id -> Store.findById id store

-- After
maybeId
    |> Maybe.andThen (\id -> Store.findById id store)

Pipe chains are flattened:

-- Before
case maybeId of
    Nothing -> Nothing
    Just id -> lookup id |> Maybe.map .name

-- After
maybeId
    |> Maybe.andThen (\id -> lookup id)
    |> Maybe.map .name

Maybe.withDefault

-- Before
case maybeValue of
    Nothing -> 0
    Just x -> x

-- After
maybeValue
    |> Maybe.withDefault 0

Maybe.Extra.withDefaultLazy

Used when the default expression is not a simple literal or variable:

-- Before
case maybeValue of
    Nothing -> computeDefault model
    Just x -> x

-- After
maybeValue
    |> Maybe.Extra.withDefaultLazy (\() -> computeDefault model)

SimplifyLambdaToComposition

-- Before
List.sortBy (\todo -> Time.posixToMillis todo.createdAt) todos

-- After
List.sortBy (Time.posixToMillis << .createdAt) todos

Nested function applications are also handled:

-- Before
List.map (\x -> f (g x.field)) list

-- After
List.map (f << g << .field) list

Try it out

You can try the example configuration above out by running the following command:

elm-review --template alex-tan/elm-review-opinionated-simplify/example