Change the order of Scope.realModuleName arguments

This commit is contained in:
Jeroen Engels 2020-03-28 11:40:24 +01:00
parent 402eb6f813
commit 0f25a734e5
5 changed files with 38 additions and 61 deletions

View File

@ -1,72 +1,49 @@
module NoHtmlButton exposing (rule)
import Elm.Syntax.Expression exposing (Expression(..))
import Elm.Syntax.Module as Module exposing (Module)
import Elm.Syntax.Node as Node exposing (Node)
import Review.Rule as Rule exposing (Direction, Error, Rule)
import Review.Scope as Scope
type alias Context =
{ scope : Scope.ModuleContext
, allowed : Allowed
}
type Allowed
= HtmlButtonIsAllowed
| HtmlButtonIsForbidden
rule : Rule
rule =
Rule.newModuleRuleSchema "NoHtmlButton" initialContext
-- Scope.addModuleVisitors should be added before your own visitors
|> Scope.addModuleVisitors
|> Rule.withModuleDefinitionVisitor moduleDefinitionVisitor
|> Rule.withExpressionVisitor expressionVisitor
|> Rule.fromModuleRuleSchema
|> Rule.ignoreErrorsForFiles [ "src/Button.elm" ]
type alias Context =
-- Scope expects a context with a record, containing the `scope` field.
{ scope : Scope.ModuleContext
}
initialContext : Context
initialContext =
{ scope = Scope.initialModuleContext
, allowed = HtmlButtonIsForbidden
}
moduleDefinitionVisitor : Node Module -> Context -> ( List nothing, Context )
moduleDefinitionVisitor node context =
if (Node.value node |> Module.moduleName) == [ "Button" ] then
( [], { context | allowed = HtmlButtonIsAllowed } )
else
( [], { context | allowed = HtmlButtonIsForbidden } )
expressionVisitor : Node Expression -> Direction -> Context -> ( List (Error {}), Context )
expressionVisitor node direction context =
case ( direction, context.allowed ) of
( Rule.OnEnter, HtmlButtonIsAllowed ) ->
( [], context )
( Rule.OnEnter, HtmlButtonIsForbidden ) ->
case Node.value node of
FunctionOrValue moduleName name ->
if name == "button" && Scope.realModuleName moduleName name context.scope == [ "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 )
( _, _ ) ->
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 )
_ ->
( [], context )

View File

@ -216,7 +216,7 @@ fromModuleToProject moduleKey moduleName moduleContext =
foldProjectContexts : ProjectContext -> ProjectContext -> ProjectContext
foldProjectContexts newContext previousContext =
{ scope = Scope.foldProjectContexts previousContext.scope newContext.scope
{ scope = Scope.foldProjectContexts newContext.scope previousContext.scope
, exposedModules = previousContext.exposedModules
, exposedConstructors = Dict.union newContext.exposedConstructors previousContext.exposedConstructors
, usedConstructors =
@ -426,7 +426,7 @@ registerUsedFunctionOrValue moduleName name moduleContext =
let
realModuleName : ModuleName
realModuleName =
Scope.realModuleName moduleName name moduleContext.scope
Scope.realModuleName moduleContext.scope name moduleName
in
{ moduleContext
| usedFunctionsOrValues =
@ -579,7 +579,7 @@ collectTypesUsedAsPhantomVariables scope phantomVariables node =
let
realModuleNameOfPhantomContainer : ModuleName
realModuleNameOfPhantomContainer =
Scope.realModuleName moduleNameOfPhantomContainer name scope
Scope.realModuleName scope name moduleNameOfPhantomContainer
typesUsedInThePhantomVariablePosition : List ( ModuleName, CustomTypeName )
typesUsedInThePhantomVariablePosition =
@ -590,7 +590,7 @@ collectTypesUsedAsPhantomVariables scope phantomVariables node =
(\( _, index ) ->
case listAtIndex index params |> Maybe.map Node.value of
Just (TypeAnnotation.Typed (Node.Node _ ( moduleNameOfPhantomVariable, typeName )) _) ->
Just ( Scope.realModuleName moduleNameOfPhantomVariable typeName scope, typeName )
Just ( Scope.realModuleName scope typeName moduleNameOfPhantomVariable, typeName )
_ ->
Nothing

View File

@ -149,7 +149,7 @@ fromModuleToProject moduleKey moduleName moduleContext =
foldProjectContexts : ProjectContext -> ProjectContext -> ProjectContext
foldProjectContexts newContext previousContext =
{ scope = Scope.foldProjectContexts previousContext.scope newContext.scope
{ scope = Scope.foldProjectContexts newContext.scope previousContext.scope
, projectType = previousContext.projectType
, modules = Dict.union previousContext.modules newContext.modules
, used = Set.union newContext.used previousContext.used
@ -407,7 +407,7 @@ testFunctionName scope declaration =
|> Maybe.map (Node.value >> .typeAnnotation >> Node.value)
of
Just (TypeAnnotation.Typed (Node _ ( moduleName, name )) _) ->
if name == "Test" && Scope.realModuleName moduleName name scope == [ "Test" ] then
if name == "Test" && Scope.realModuleName scope name moduleName == [ "Test" ] then
function.declaration
|> Node.value
|> .name
@ -467,7 +467,7 @@ collectTypesFromTypeAnnotation scope node =
collectTypesFromTypeAnnotation scope a ++ collectTypesFromTypeAnnotation scope b
TypeAnnotation.Typed (Node _ ( moduleName, name )) params ->
( Scope.realModuleName moduleName name scope, name )
( Scope.realModuleName scope name moduleName, name )
:: List.concatMap (collectTypesFromTypeAnnotation scope) params
TypeAnnotation.Record list ->
@ -501,7 +501,7 @@ expressionVisitor node direction moduleContext =
( Rule.OnEnter, Expression.FunctionOrValue moduleName name ) ->
( []
, registerAsUsed
( Scope.realModuleName moduleName name moduleContext.scope, name )
( Scope.realModuleName moduleContext.scope name moduleName, name )
moduleContext
)

View File

@ -132,8 +132,8 @@ fromModuleToProject moduleName (ModuleContext moduleContext) =
foldProjectContexts : ProjectContext -> ProjectContext -> ProjectContext
foldProjectContexts (ProjectContext a) (ProjectContext b) =
ProjectContext
{ dependenciesModules = a.dependenciesModules
, modules = Dict.union a.modules b.modules
{ dependenciesModules = Dict.union b.dependenciesModules a.dependenciesModules
, modules = Dict.union b.modules a.modules
}
@ -914,8 +914,8 @@ findInList predicate list =
-- ACCESS
realModuleName : List String -> String -> ModuleContext -> List String
realModuleName moduleName functionOrType (ModuleContext context) =
realModuleName : ModuleContext -> String -> List String -> List String
realModuleName (ModuleContext context) functionOrType moduleName =
if List.length moduleName == 0 then
if isInScope functionOrType context.scopes then
[]

View File

@ -260,7 +260,7 @@ typeAnnotationNames scope typeAnnotation =
realName : String
realName =
case Scope.realModuleName moduleName typeName scope of
case Scope.realModuleName scope typeName moduleName of
[] ->
"<nothing>." ++ typeName
@ -301,7 +301,7 @@ expressionVisitor node direction context =
realName : String
realName =
case Scope.realModuleName moduleName name context.scope of
case Scope.realModuleName context.scope name moduleName of
[] ->
"<nothing>." ++ name