2019-11-04 20:44:49 +03:00
|
|
|
module NoHtmlButton exposing (rule)
|
|
|
|
|
|
|
|
import Elm.Syntax.Expression exposing (Expression(..))
|
|
|
|
import Elm.Syntax.Node as Node exposing (Node)
|
|
|
|
import Review.Rule as Rule exposing (Direction, Error, Rule)
|
2020-03-27 20:35:53 +03:00
|
|
|
import Review.Scope as Scope
|
2019-11-04 20:44:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
rule : Rule
|
|
|
|
rule =
|
2020-01-19 22:37:19 +03:00
|
|
|
Rule.newModuleRuleSchema "NoHtmlButton" initialContext
|
2020-03-28 13:40:24 +03:00
|
|
|
-- Scope.addModuleVisitors should be added before your own visitors
|
2020-03-27 19:43:16 +03:00
|
|
|
|> Scope.addModuleVisitors
|
2019-11-04 20:44:49 +03:00
|
|
|
|> Rule.withExpressionVisitor expressionVisitor
|
2020-01-19 22:37:19 +03:00
|
|
|
|> Rule.fromModuleRuleSchema
|
2020-03-28 13:40:24 +03:00
|
|
|
|> Rule.ignoreErrorsForFiles [ "src/Button.elm" ]
|
|
|
|
|
|
|
|
|
|
|
|
type alias Context =
|
|
|
|
-- Scope expects a context with a record, containing the `scope` field.
|
|
|
|
{ scope : Scope.ModuleContext
|
|
|
|
}
|
2019-11-04 20:44:49 +03:00
|
|
|
|
|
|
|
|
2019-11-21 16:46:32 +03:00
|
|
|
initialContext : Context
|
|
|
|
initialContext =
|
2020-03-27 19:43:16 +03:00
|
|
|
{ scope = Scope.initialModuleContext
|
2019-11-04 20:44:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-25 20:02:37 +03:00
|
|
|
expressionVisitor : Node Expression -> Direction -> Context -> ( List (Error {}), Context )
|
2019-11-04 20:44:49 +03:00
|
|
|
expressionVisitor node direction context =
|
2020-03-28 13:40:24 +03:00
|
|
|
case ( direction, Node.value node ) of
|
|
|
|
( Rule.OnEnter, FunctionOrValue moduleName "button" ) ->
|
|
|
|
if Scope.realModuleName 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 )
|
|
|
|
|
|
|
|
_ ->
|
2019-11-04 20:44:49 +03:00
|
|
|
( [], context )
|