elm-review/tests/MiscRules/NoHtmlButton.elm

57 lines
1.9 KiB
Elm
Raw Normal View History

2020-04-03 17:22:22 +03:00
module MiscRules.NoHtmlButton exposing (rule)
import Elm.Syntax.Expression exposing (Expression(..))
import Elm.Syntax.Node as Node exposing (Node)
2020-08-20 00:26:48 +03:00
import Review.ModuleNameLookupTable exposing (ModuleNameLookupTable)
2020-06-16 00:11:51 +03:00
import Review.Rule as Rule exposing (Error, Rule)
2020-04-22 00:02:26 +03:00
import Scope
rule : Rule
rule =
2020-08-20 00:01:40 +03:00
Rule.newModuleRuleSchemaUsingContextCreator "NoHtmlButton" contextCreator
-- Scope.addModuleVisitors should be added before your own visitors
2020-08-20 00:01:40 +03:00
--|> Scope.addModuleVisitors
2020-06-16 23:03:18 +03:00
|> Rule.withExpressionEnterVisitor expressionVisitor
2020-01-19 22:37:19 +03:00
|> Rule.fromModuleRuleSchema
|> Rule.ignoreErrorsForFiles [ "src/Button.elm" ]
type alias Context =
-- Scope expects a context with a record, containing the `scope` field.
{ scope : Scope.ModuleContext
2020-08-20 00:06:41 +03:00
, moduleNameLookupTable : ModuleNameLookupTable
}
2020-08-20 00:01:40 +03:00
contextCreator : Rule.ContextCreator () Context
contextCreator =
Rule.initContextCreator
2020-08-20 00:06:41 +03:00
(\moduleNameLookupTable () ->
2020-08-20 00:01:40 +03:00
{ scope = Scope.initialModuleContext
2020-08-20 00:06:41 +03:00
, moduleNameLookupTable = moduleNameLookupTable
2020-08-20 00:01:40 +03:00
}
)
2020-08-20 00:06:41 +03:00
|> Rule.withModuleNameLookupTable
2020-06-16 00:11:51 +03:00
expressionVisitor : Node Expression -> Context -> ( List (Error {}), Context )
expressionVisitor node context =
case Node.value node of
FunctionOrValue moduleName "button" ->
2020-05-16 23:33:36 +03:00
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 )