-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathNoNegationInIfCondition.elm
More file actions
120 lines (87 loc) · 3.48 KB
/
NoNegationInIfCondition.elm
File metadata and controls
120 lines (87 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
module NoNegationInIfCondition exposing (rule)
{-|
@docs rule
-}
import Elm.Syntax.Expression as Expression exposing (Expression)
import Elm.Syntax.Node as Node exposing (Node(..))
import Elm.Syntax.Range exposing (Range)
import Review.Fix as Fix
import Review.Rule as Rule exposing (Rule)
{-| Reports when negations are used in
config =
[ NoNegationInIfCondition.rule
]
## Fail
a =
if not condition then
1
else
2
## Success
a =
if condition then
1
else
2
## When (not) to enable this rule
This rule is not meant to be published. It is primarily meant to be a test rule for `withSourceCodeExtractor` and
an example for its usage.
I don't think it's a good rule to enforce, and I personally like to put the "happy" path/branch first,
instead of the non-negated one. As such, I have left this rule incomplete meaning there are more cases that should be
handled for this rule to do what it is meant to do.
-}
rule : Rule
rule =
Rule.newModuleRuleSchemaUsingContextCreator "NoNegationInIfCondition" initialContext
|> Rule.withExpressionEnterVisitor expressionVisitor
|> Rule.providesFixesForModuleRule
|> Rule.fromModuleRuleSchema
type alias Context =
{ extractSourceCode : Range -> String
}
initialContext : Rule.ContextCreator () Context
initialContext =
Rule.initContextCreator
(\extractSourceCode () -> { extractSourceCode = extractSourceCode })
|> Rule.withSourceCodeExtractor
expressionVisitor : Node Expression -> Context -> ( List (Rule.Error {}), Context )
expressionVisitor node context =
case Node.value node of
Expression.IfBlock condition thenBranch elseBranch ->
case Node.value condition of
Expression.Application ((Node notRange (Expression.FunctionOrValue [] "not")) :: _) ->
if isIfExpr elseBranch then
( [], context )
else
let
elseKeywordString : String
elseKeywordString =
context.extractSourceCode { start = (Node.range thenBranch).end, end = (Node.range elseBranch).start }
in
( [ Rule.errorWithFix
{ message = "Don't use if expressions with negated conditions"
, details = [ "We at fruits.com think that if expressions are more readable when the condition is not negated." ]
}
notRange
[ Fix.removeRange notRange
, Fix.removeRange { start = (Node.range thenBranch).start, end = (Node.range elseBranch).start }
, Fix.insertAt
(Node.range elseBranch).end
(elseKeywordString ++ context.extractSourceCode (Node.range thenBranch))
]
]
, context
)
_ ->
( [], context )
_ ->
( [], context )
isIfExpr : Node Expression -> Bool
isIfExpr node =
case Node.value node of
Expression.ParenthesizedExpression expr ->
isIfExpr expr
Expression.IfBlock _ _ _ ->
True
_ ->
False