mirror of
https://github.com/jfmengels/elm-review.git
synced 2024-11-23 23:05:35 +03:00
Add ScopeExample
This commit is contained in:
parent
219bb11ff6
commit
9c40b95394
47
tests/ScopeExample.elm
Normal file
47
tests/ScopeExample.elm
Normal file
@ -0,0 +1,47 @@
|
||||
module ScopeExample exposing (rule)
|
||||
|
||||
import Elm.Syntax.Expression as Expression exposing (Expression)
|
||||
import Elm.Syntax.Node as Node exposing (Node)
|
||||
import Review.Rule as Rule exposing (Direction, Error, Rule)
|
||||
import Scope
|
||||
|
||||
|
||||
rule : Rule
|
||||
rule =
|
||||
Rule.newModuleRuleSchema "NoHtmlButton" initialContext
|
||||
|> Scope.addModuleVisitors
|
||||
|> Rule.withExpressionEnterVisitor expressionVisitor
|
||||
|> Rule.fromModuleRuleSchema
|
||||
|> Rule.ignoreErrorsForFiles [ "src/Button.elm" ]
|
||||
|
||||
|
||||
type alias Context =
|
||||
{ scope : Scope.ModuleContext
|
||||
}
|
||||
|
||||
|
||||
initialContext : Context
|
||||
initialContext =
|
||||
{ scope = Scope.initialModuleContext
|
||||
}
|
||||
|
||||
|
||||
expressionVisitor : Node Expression -> Context -> ( List (Error {}), Context )
|
||||
expressionVisitor node context =
|
||||
case Node.value node of
|
||||
Expression.FunctionOrValue moduleName "button" ->
|
||||
if Scope.moduleNameForValue context.scope "button" moduleName == [ "Html" ] then
|
||||
( [ Rule.error
|
||||
{ message = "Do not use `Html.button` directly"
|
||||
, details = [ "At fruits.com, we've built a nice `Button` module that suits our needs better. Using this module instead of `Html.button` ensures we have a consistent button experience across the website." ]
|
||||
}
|
||||
(Node.range node)
|
||||
]
|
||||
, context
|
||||
)
|
||||
|
||||
else
|
||||
( [], context )
|
||||
|
||||
_ ->
|
||||
( [], context )
|
Loading…
Reference in New Issue
Block a user