-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NoUnsafeRegexFromLiteral.elm
65 lines (49 loc) · 2.17 KB
/
NoUnsafeRegexFromLiteral.elm
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
module NoUnsafeRegexFromLiteral exposing (rule)
{-| Forbids misusing the unsafe function `Helpers.Regex.fromLiteral`.
**Note**: This version is too simplistic to be used. Check out the `master` branch
for the final and safe version.
-}
import Elm.Syntax.Expression as Expression exposing (Expression)
import Elm.Syntax.Node as Node exposing (Node)
import Regex
import Review.Rule as Rule exposing (Error, Rule)
rule : Rule
rule =
Rule.newModuleRuleSchema "NoUnsafeRegexFromLiteral" ()
|> Rule.withSimpleExpressionVisitor expressionVisitor
|> Rule.fromModuleRuleSchema
expressionVisitor : Node Expression -> List (Error {})
expressionVisitor node =
case Node.value node of
Expression.Application (function :: argument :: []) ->
case Node.value function of
Expression.FunctionOrValue [ "Helpers", "Regex" ] "fromLiteral" ->
case Node.value argument of
Expression.Literal string ->
case Regex.fromString string of
Just _ ->
[]
Nothing ->
[ Rule.error invalidRegex (Node.range node)
]
_ ->
[ Rule.error nonLiteralValue (Node.range node) ]
_ ->
[]
_ ->
[]
invalidRegex : { message : String, details : List String }
invalidRegex =
{ message = "Helpers.Regex.fromLiteral needs to be called with a valid regex."
, details =
[ "The regex you passed does not evaluate to a valid regex. Please fix it or use `Regex.fromString`."
]
}
nonLiteralValue : { message : String, details : List String }
nonLiteralValue =
{ message = "Helpers.Regex.fromLiteral needs to be called with a static string literal."
, details =
[ "This function serves to give you more guarantees about creating regular expressions, but if the argument is dynamic or too complex, I won't be able to tell you."
, "Either make the argument static or use Regex.fromString."
]
}