Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
55 lines (41 sloc)
1.69 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
] | |
_ -> | |
[] | |
_ -> | |
[] | |
_ -> | |
[] | |
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`." | |
] | |
} |