Provides elm-review rules to disallow single-pattern case expressions.
NoSinglePatternCase - Reports case...of expressions that match only a single pattern. Such case expressions typically are either unnecessary or may be written more concisely.module ReviewConfig exposing (config)
import NoSinglePatternCase
import Review.Rule exposing (Rule)
config : List Rule
config =
[ NoSinglePatternCase.rule
NoSinglePatternCase.fixInArgument
]You can try the example configuration above out by running the following command:
elm-review --template SiriusStarr/elm-review-no-single-pattern-case/example/fix-in-argument --rules NoSinglePatternCase2.0.2✨ The rule is now capable of reducing the expressions and patterns of a single-pattern case to find the original binding sites to reduce expressions at. The following improvements have been added:
Reduce parenthesized expressions:
unpack o =
case (o) of
Opaque i -> i
-->
unpack (Opaque i) =
iReduce constructed tupled expressions
add a b =
case ( a, b ) of
( Opaque i1, Opaque i2 ) ->
Opaque <| i1 + i2
-->
add (Opaque i1) (Opaque i2) =
Opaque <| i1 + i2Reduce constructed record expressions
pointless a b c =
case { d = a, e = b, f = c } of
{ d, e, f } -> e
-->
pointless _ e _ =
eReduce unnecessary as patterns
unpack o =
case o of
((Opaque i) as a) -> i
-->
unpack (Opaque i) =
iReduce function application/named patterns
multWrap a b =
case (Tuple a b) of
Tuple i1 i2 ->
Opaque <| i1 * i2
-->
multWrap i1 i2 =
Opaque <| i1 * i2✨ With replaceUnusedBindings, unit patterns are now fixed to ()
instead of _ to be more explicit. (There is an
elm-review rule
to enforce this in your code, should you so desire.)
🚸 The error range reported by the rule now flags just the pattern, instead of the entire case, for less cluttered errors (especially with tooling). No more red-underlining large sections of code!
🐛 Fixed a bug where name clashes were only checked for expressions, not patterns, when generating fixes, leading to the generation of uncompilable code. This probably rarely happened in practice, but for instance this incorrect fix used to be generated:
unpack o =
let
foo i =
0
in
case o of
Opaque i -> i
-->
unpack (Opaque i) =
let
foo i = -- THIS IS A NAME CLASH FOR `i`
0
in
i🐛 Fixed a bug where both subexpressions of a single-pattern case were not
actually checked by this rule. This means that nested single-pattern
cases are now detected properly (previously, only the outermost one would
be flagged). This applies to both those nested following the pattern or within the case...of.
NoUnused.Patterns
to one's config, as the fixes from this rule can sometimes generate nested
as patterns, which will be detected by that rule.2.0.1 -- Bump elm-syntax to v7.2.8, fixing issued with lambda ranges.
Unlikely to affect this rule in practice, however.2.0.0 -- Add fixes, with option to destructure in the argument or in a let1.0.1 -- Fix minor documentation issue1.0.0 -- Initial release