elm-review/tests/MiscRules/NoHtmlButton.elm
2020-08-21 20:53:33 +02:00

51 lines
1.7 KiB
Elm

module MiscRules.NoHtmlButton exposing (rule)
import Elm.Syntax.Expression exposing (Expression(..))
import Elm.Syntax.Node as Node exposing (Node)
import Review.ModuleNameLookupTable as ModuleNameLookupTable exposing (ModuleNameLookupTable)
import Review.Rule as Rule exposing (Error, Rule)
rule : Rule
rule =
Rule.newModuleRuleSchemaUsingContextCreator "NoHtmlButton" contextCreator
|> Rule.withExpressionEnterVisitor expressionVisitor
|> Rule.fromModuleRuleSchema
|> Rule.ignoreErrorsForFiles [ "src/Button.elm" ]
type alias Context =
{ moduleNameLookupTable : ModuleNameLookupTable
}
contextCreator : Rule.ContextCreator () Context
contextCreator =
Rule.initContextCreator
(\moduleNameLookupTable () ->
{ moduleNameLookupTable = moduleNameLookupTable
}
)
|> Rule.withModuleNameLookupTable
expressionVisitor : Node Expression -> Context -> ( List (Error {}), Context )
expressionVisitor node context =
case Node.value node of
FunctionOrValue _ "button" ->
if ModuleNameLookupTable.moduleNameFor context.moduleNameLookupTable 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." ]
}
(Node.range node)
]
, context
)
else
( [], context )
_ ->
( [], context )