jfmengels / elm-review-simplify

Provides elm-review rules to simplify your Elm code
https://package.elm-lang.org/packages/jfmengels/elm-review-simplify/latest/
BSD 3-Clause "New" or "Revised" License
19 stars 9 forks source link

WIP: Add case pattern catch simplifications #103

Closed lue-bird closed 10 months ago

lue-bird commented 1 year ago

The goal is fully implementing #99

known shortcoming A

when simplifying a structure, there are no checks if new patterns together catch all possibilities early:

case ( bool, a ) of
    ( True, _ ) -> ...
    ( False, _ ) -> ...
    _ -> ...

fixed to

case bool of
    True -> ...
    False -> ...
    _ -> ...

which results in a compiler error: "all possibilities exhaustively matched before third case". To check for that, we'd have to effectively re-implement exhaustiveness-checking from the compiler.

In comparison, single premature catch-all cases do lead to the removal of later cases.

case ( bool, a ) of
    ( b, _ ) -> ...
    _ -> ...

fixed to

case bool of
    b -> ...

Personally, I'm fine with fixing to code that throws a compiler error there but maybe you have a better idea.

known shortcoming B

Currently there's no knowledge in CheckInfo about whether a variant is the only one of a type. This means there will be suggested simplifications like

case Toop.T3 ... of
    Toop.T3 O O O ->
        Collage.circle 1

    Toop.T3 O O I ->
        Collage.rectangle 0.5 1.5

    Toop.T3 O I O ->
        Collage.rectangle 1.5 0.5

    Toop.T3 b0 b1 b2 ->
        {- 3 to 8 -}
        Collage.ngon
            (ArraySized.l3 b0 b1 b2
                |> Bits.toN
                |> N.toInt
            )
            1

fixed to

case (...) of
    (O, (O, O)) ->
        Collage.circle 1

    (O, (O, I)) ->
        Collage.rectangle 0.5 1.5

    (O, (I, O)) ->
        Collage.rectangle 1.5 0.5

    (b0, (b1, b2)) ->
        {- 3 to 8 -}
        Collage.ngon
            (ArraySized.l3 b0 b1 b2
                |> Bits.toN
                |> N.toInt
            )
            1

Especially with more descriptive single-variants like User we might wanna poll in slack whether these should be simplified to tuples.