Make it not possible to use withModuleContext twice

This commit is contained in:
Jeroen Engels 2020-03-19 22:42:24 +01:00
parent 9cf4525650
commit a83c0a4dfe
5 changed files with 69 additions and 10 deletions

View File

@ -1,12 +1,6 @@
module NoElmJsonVisitorForModuleVisitorInProjectRule exposing (rule)
{-| We want to forbid module visitors from using `withElmJsonModuleVisitor`.
# Rule
@docs rule
-}
import Review.Rule as Rule exposing (Rule)

View File

@ -2,8 +2,8 @@
This function cannot handle the argument sent through the (|>) pipe:
30| schema
31| |> Rule.withElmJsonModuleVisitor (\_ () -> ())
24| schema
25| |> Rule.withElmJsonModuleVisitor (\_ () -> ())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The argument is:

View File

@ -0,0 +1,30 @@
module WithModuleContext_AtMostOnce exposing (rule)
{-| We want to forbid `Rule.withModuleContext` from being called twice.
-}
import Review.Rule as Rule exposing (Rule)
rule : Rule
rule =
Rule.newProjectRuleSchema "WithModuleContext_AtMostOnce" ()
|> Rule.withModuleVisitor moduleVisitor
|> Rule.withModuleContext
{ fromProjectToModule = \_ _ () -> ()
, fromModuleToProject = \_ _ () -> ()
, foldProjectContexts = \_ () -> ()
}
|> Rule.withModuleContext
{ fromProjectToModule = \_ _ () -> ()
, fromModuleToProject = \_ _ () -> ()
, foldProjectContexts = \_ () -> ()
}
|> Rule.withFinalProjectEvaluation (\_ -> [])
|> Rule.fromProjectRuleSchema
moduleVisitor : Rule.ModuleRuleSchema {} () -> Rule.ModuleRuleSchema { hasAtLeastOneVisitor : () } ()
moduleVisitor schema =
schema
|> Rule.withModuleDefinitionVisitor (\_ () -> ( [], () ))

View File

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

View File

@ -9,6 +9,7 @@ module Review.Rule exposing
, 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.
@ -923,7 +924,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 schemaState
newProjectRuleSchema : String -> projectContext -> ProjectRuleSchema projectContext moduleContext { withModuleContext : Required }
newProjectRuleSchema name_ initialProjectContext =
ProjectRuleSchema
{ name = name_
@ -979,6 +980,12 @@ withModuleVisitor visitor (ProjectRuleSchema schema) =
ProjectRuleSchema { schema | moduleVisitor = HasVisitors (visitor :: previousModuleVisitors) }
{-| TODO Documentation
-}
type Required
= Required
{-| TODO Documentation
-}
withModuleContext :
@ -986,7 +993,7 @@ withModuleContext :
, fromModuleToProject : ModuleKey -> Node ModuleName -> moduleContext -> projectContext
, foldProjectContexts : projectContext -> projectContext -> projectContext
}
-> ProjectRuleSchema projectContext moduleContext schemaState
-> ProjectRuleSchema projectContext moduleContext { schemaState | withModuleContext : Required }
-> ProjectRuleSchema projectContext moduleContext schemaState
withModuleContext moduleContext (ProjectRuleSchema schema) =
let