Make it not possible to use withModuleContext without withModuleVisitor

This commit is contained in:
Jeroen Engels 2020-03-19 22:59:58 +01:00
parent a83c0a4dfe
commit 06da24e1aa
4 changed files with 48 additions and 4 deletions

View File

@ -0,0 +1,19 @@
module No_WithModuleContext_Without_WithModuleVisitor exposing (rule)
{-| We want to forbid using `Rule.withModuleContext` when `withModuleVisitor` has
not been used.
-}
import Review.Rule as Rule exposing (Rule)
rule : Rule
rule =
Rule.newProjectRuleSchema "No_WithModuleContext_Without_WithModuleVisitor" ()
|> Rule.withModuleContext
{ fromProjectToModule = \_ _ () -> ()
, fromModuleToProject = \_ _ () -> ()
, foldProjectContexts = \_ () -> ()
}
|> Rule.withFinalProjectEvaluation (\_ -> [])
|> Rule.fromProjectRuleSchema

View File

@ -0,0 +1,22 @@
-- TYPE MISMATCH ---------- ./No_WithModuleContext_Without_WithModuleVisitor.elm
This function cannot handle the argument sent through the (|>) pipe:
12| Rule.newProjectRuleSchema "No_WithModuleContext_Without_WithModuleVisitor" ()
13|> |> Rule.withModuleContext
14|> { fromProjectToModule = \_ _ () -> ()
15|> , fromModuleToProject = \_ _ () -> ()
16|> , foldProjectContexts = \_ () -> ()
17|> }
The argument is:
Rule.ProjectRuleSchema () () {}
But (|>) is piping it to a function that expects:
Rule.ProjectRuleSchema
()
()
{ schemaState | withModuleContext : Rule.Required }

View File

@ -6,10 +6,10 @@ module Review.Rule exposing
, withModuleDefinitionVisitor, withCommentsVisitor, withImportVisitor, Direction(..), withDeclarationVisitor, withDeclarationListVisitor, withExpressionVisitor, withFinalModuleEvaluation
, withElmJsonModuleVisitor, withReadmeModuleVisitor, withDependenciesModuleVisitor
, ProjectRuleSchema, newProjectRuleSchema, fromProjectRuleSchema, withModuleVisitor, withModuleContext, withElmJsonProjectVisitor, withReadmeProjectVisitor, withDependenciesProjectVisitor, withFinalProjectEvaluation, withContextFromImportedModules
, Required
, Error, error, errorRuleName, errorMessage, errorDetails, errorRange, errorFixes, errorFilePath, ModuleKey, errorForModule, ElmJsonKey, errorForElmJson, ReadmeKey, errorForReadme
, withFixes
, ignoreErrorsForDirectories, ignoreErrorsForFiles
, Required
)
{-| This module contains functions that are used for writing rules.
@ -192,6 +192,7 @@ Evaluating/visiting a node means two things:
## Creating a project rule
@docs ProjectRuleSchema, newProjectRuleSchema, fromProjectRuleSchema, withModuleVisitor, withModuleContext, withElmJsonProjectVisitor, withReadmeProjectVisitor, withDependenciesProjectVisitor, withFinalProjectEvaluation, withContextFromImportedModules
@docs Required
## Errors
@ -924,7 +925,7 @@ You can't use [`withElmJsonModuleVisitor`](#withElmJsonModuleVisitor) or [`withD
in project rules. Instead, you should use [`withElmJsonProjectVisitor`](#withElmJsonProjectVisitor) or [`withDependenciesProjectVisitor`](#withDependenciesProjectVisitor).
-}
newProjectRuleSchema : String -> projectContext -> ProjectRuleSchema projectContext moduleContext { withModuleContext : Required }
newProjectRuleSchema : String -> projectContext -> ProjectRuleSchema projectContext moduleContext {}
newProjectRuleSchema name_ initialProjectContext =
ProjectRuleSchema
{ name = name_
@ -962,7 +963,7 @@ fromProjectRuleSchema (ProjectRuleSchema schema) =
withModuleVisitor :
(ModuleRuleSchema {} moduleContext -> ModuleRuleSchema { hasAtLeastOneVisitor : () } moduleContext)
-> ProjectRuleSchema projectContext moduleContext schemaState
-> ProjectRuleSchema projectContext moduleContext schemaState
-> ProjectRuleSchema projectContext moduleContext { schemaState | withModuleContext : Required }
withModuleVisitor visitor (ProjectRuleSchema schema) =
let
previousModuleVisitors : List (ModuleRuleSchema {} moduleContext -> ModuleRuleSchema { hasAtLeastOneVisitor : () } moduleContext)

View File

@ -166,7 +166,9 @@ emptyScope =
}
addProjectVisitors : Rule.ProjectRuleSchema { projectContext | scope : ProjectContext } { moduleContext | scope : ModuleContext } schemaState -> Rule.ProjectRuleSchema { projectContext | scope : ProjectContext } { moduleContext | scope : ModuleContext } schemaState
addProjectVisitors :
Rule.ProjectRuleSchema { projectContext | scope : ProjectContext } { moduleContext | scope : ModuleContext } schemaState
-> Rule.ProjectRuleSchema { projectContext | scope : ProjectContext } { moduleContext | scope : ModuleContext } { schemaState | withModuleContext : Rule.Required }
addProjectVisitors schema =
schema
|> Rule.withContextFromImportedModules