jfmengels/elm-review-simplify

Simplify `if x then (_, True) else (_,False)` (and other conditionals that differ only by a boolean literal)

Open

#11 aperta il 17 lug 2021

Vedi su GitHub
 (4 commenti) (0 reazioni) (0 assegnatari)Elm (10 fork)github user discovery
enhancementhelp wanted

Metriche repository

Star
 (22 star)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

Haven't put a ton of thought into how difficult it would be to implement as a rule, but I ran into it in my code today and noticed it wasn't caught by elm-review-simplify currently.

if bar then
    ( foo, True )
else
    ( foo, False )

can be simplified to just:

( foo, bar )

Honestly this could be extended to basically any two expressions that differ only by a Boolean between conditions, but it might be challenging to check for that.

E.g.

if bar then
    { a = foo, b = True }
else
    { a = foo, b = False }
if bar then
    func foo True
else
    func foo False

Off the top of my head, one could maybe descend into the two Node Expressions of an IfBlock and check for the presence of a boolean literal, then compare the ASTs for equality with the literal swapped?

Guida contributor