Use lookup table in ScopeExample

This commit is contained in:
Jeroen Engels 2020-08-22 10:23:14 +02:00
parent 9c40b95394
commit 7455bca8a8

View File

@ -2,35 +2,38 @@ module ScopeExample exposing (rule)
import Elm.Syntax.Expression as Expression exposing (Expression)
import Elm.Syntax.Node as Node exposing (Node)
import Review.ModuleNameLookupTable as ModuleNameLookupTable exposing (ModuleNameLookupTable)
import Review.Rule as Rule exposing (Direction, Error, Rule)
import Scope
rule : Rule
rule =
Rule.newModuleRuleSchema "NoHtmlButton" initialContext
|> Scope.addModuleVisitors
Rule.newModuleRuleSchemaUsingContextCreator "NoHtmlButton" initialContext
|> Rule.withExpressionEnterVisitor expressionVisitor
|> Rule.fromModuleRuleSchema
|> Rule.ignoreErrorsForFiles [ "src/Button.elm" ]
type alias Context =
{ scope : Scope.ModuleContext
{ lookupTable : ModuleNameLookupTable
}
initialContext : Context
initialContext : Rule.ContextCreator () Context
initialContext =
{ scope = Scope.initialModuleContext
Rule.initContextCreator
(\lookupTable () ->
{ lookupTable = lookupTable
}
)
|> Rule.withModuleNameLookupTable
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
Expression.FunctionOrValue _ "button" ->
if ModuleNameLookupTable.moduleNameFor context.lookupTable node == Just [ "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." ]