mirror of
https://github.com/jfmengels/elm-review.git
synced 2024-09-11 16:16:09 +03:00
Add a way to get the module context using a module context creator
This commit is contained in:
parent
35eab49acd
commit
619850016a
@ -15,6 +15,7 @@ module Review.Rule exposing
|
||||
, ignoreErrorsForDirectories, ignoreErrorsForFiles
|
||||
, review
|
||||
, Required, Forbidden
|
||||
, ModuleMetadata, initContextCreator, moduleNameFromMetadata, moduleNameNodeFromMetadata, withModuleContext2, withModuleMetadata
|
||||
)
|
||||
|
||||
{-| This module contains functions that are used for writing rules.
|
||||
@ -1124,7 +1125,7 @@ withModuleContext functions (ProjectRuleSchema schema) =
|
||||
projectContext
|
||||
)
|
||||
|> withModuleKey
|
||||
|> withMetadata
|
||||
|> withModuleMetadata
|
||||
in
|
||||
ProjectRuleSchema
|
||||
{ schema
|
||||
@ -1134,7 +1135,26 @@ withModuleContext functions (ProjectRuleSchema schema) =
|
||||
{ fromModuleToProject =
|
||||
initContextCreator (\moduleKey metadata moduleContext -> functions.fromModuleToProject moduleKey (moduleNameNodeFromMetadata metadata) moduleContext)
|
||||
|> withModuleKey
|
||||
|> withMetadata
|
||||
|> withModuleMetadata
|
||||
, foldProjectContexts = functions.foldProjectContexts
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
withModuleContext2 :
|
||||
{ fromProjectToModule : ContextCreator projectContext moduleContext
|
||||
, fromModuleToProject : ContextCreator moduleContext projectContext
|
||||
, foldProjectContexts : projectContext -> projectContext -> projectContext
|
||||
}
|
||||
-> ProjectRuleSchema { schemaState | canAddModuleVisitor : (), withModuleContext : Required } projectContext moduleContext
|
||||
-> ProjectRuleSchema { schemaState | hasAtLeastOneVisitor : (), withModuleContext : Forbidden } projectContext moduleContext
|
||||
withModuleContext2 functions (ProjectRuleSchema schema) =
|
||||
ProjectRuleSchema
|
||||
{ schema
|
||||
| moduleContextCreator = Just functions.fromProjectToModule
|
||||
, folder =
|
||||
Just
|
||||
{ fromModuleToProject = functions.fromModuleToProject
|
||||
, foldProjectContexts = functions.foldProjectContexts
|
||||
}
|
||||
}
|
||||
@ -3189,7 +3209,7 @@ computeModules projectVisitor ( moduleVisitor, moduleContextCreator ) project ex
|
||||
moduleKey =
|
||||
ModuleKey module_.path
|
||||
|
||||
metadata : Metadata
|
||||
metadata : ModuleMetadata
|
||||
metadata =
|
||||
createMetadata { moduleNameNode = moduleNameNode module_.ast.moduleDefinition }
|
||||
|
||||
@ -3618,22 +3638,12 @@ type ContextCreator from to
|
||||
= ContextCreator (AvailableData -> from -> to) RequestedData
|
||||
|
||||
|
||||
type Metadata
|
||||
= Metadata
|
||||
{ moduleNameNode : Node ModuleName
|
||||
type RequestedData
|
||||
= RequestedData
|
||||
{ metadata : Bool
|
||||
}
|
||||
|
||||
|
||||
createMetadata : { moduleNameNode : Node ModuleName } -> Metadata
|
||||
createMetadata data =
|
||||
Metadata data
|
||||
|
||||
|
||||
moduleNameNodeFromMetadata : Metadata -> Node ModuleName
|
||||
moduleNameNodeFromMetadata (Metadata metadata) =
|
||||
metadata.moduleNameNode
|
||||
|
||||
|
||||
initContextCreator : (from -> to) -> ContextCreator from to
|
||||
initContextCreator fromProjectToModule =
|
||||
ContextCreator
|
||||
@ -3641,14 +3651,13 @@ initContextCreator fromProjectToModule =
|
||||
(RequestedData { metadata = False })
|
||||
|
||||
|
||||
type RequestedData
|
||||
= RequestedData
|
||||
{ metadata : Bool
|
||||
}
|
||||
applyContextCreator : AvailableData -> ContextCreator from to -> from -> to
|
||||
applyContextCreator data (ContextCreator fn _) from =
|
||||
fn data from
|
||||
|
||||
|
||||
withMetadata : ContextCreator Metadata (from -> to) -> ContextCreator from to
|
||||
withMetadata (ContextCreator fn (RequestedData requested)) =
|
||||
withModuleMetadata : ContextCreator ModuleMetadata (from -> to) -> ContextCreator from to
|
||||
withModuleMetadata (ContextCreator fn (RequestedData requested)) =
|
||||
ContextCreator
|
||||
(\data -> fn data data.metadata)
|
||||
(RequestedData { requested | metadata = True })
|
||||
@ -3662,16 +3671,31 @@ withModuleKey (ContextCreator fn (RequestedData requested)) =
|
||||
|
||||
|
||||
type alias AvailableData =
|
||||
{ metadata : Metadata
|
||||
{ metadata : ModuleMetadata
|
||||
, moduleKey : ModuleKey
|
||||
}
|
||||
|
||||
|
||||
applyContextCreator : AvailableData -> ContextCreator from to -> from -> to
|
||||
applyContextCreator data (ContextCreator fn _) from =
|
||||
fn data from
|
||||
|
||||
-- METADATA
|
||||
|
||||
|
||||
requestedData : ContextCreator from to -> RequestedData
|
||||
requestedData (ContextCreator _ requested) =
|
||||
requested
|
||||
type ModuleMetadata
|
||||
= ModuleMetadata
|
||||
{ moduleNameNode : Node ModuleName
|
||||
}
|
||||
|
||||
|
||||
createMetadata : { moduleNameNode : Node ModuleName } -> ModuleMetadata
|
||||
createMetadata data =
|
||||
ModuleMetadata data
|
||||
|
||||
|
||||
moduleNameNodeFromMetadata : ModuleMetadata -> Node ModuleName
|
||||
moduleNameNodeFromMetadata (ModuleMetadata metadata) =
|
||||
metadata.moduleNameNode
|
||||
|
||||
|
||||
moduleNameFromMetadata : ModuleMetadata -> ModuleName
|
||||
moduleNameFromMetadata (ModuleMetadata metadata) =
|
||||
Node.value metadata.moduleNameNode
|
||||
|
@ -58,9 +58,9 @@ rule =
|
||||
Rule.newProjectRuleSchema "NoMissingSubscriptionsCall" initialProjectContext
|
||||
|> Scope.addProjectVisitors
|
||||
|> Rule.withModuleVisitor moduleVisitor
|
||||
|> Rule.withModuleContext
|
||||
{ fromProjectToModule = fromProjectToModule
|
||||
, fromModuleToProject = fromModuleToProject
|
||||
|> Rule.withModuleContext2
|
||||
{ fromProjectToModule = Rule.initContextCreator fromProjectToModule
|
||||
, fromModuleToProject = Rule.initContextCreator fromModuleToProject |> Rule.withModuleMetadata
|
||||
, foldProjectContexts = foldProjectContexts
|
||||
}
|
||||
|> Rule.withContextFromImportedModules
|
||||
@ -101,8 +101,8 @@ initialProjectContext =
|
||||
}
|
||||
|
||||
|
||||
fromProjectToModule : Rule.ModuleKey -> Node ModuleName -> ProjectContext -> ModuleContext
|
||||
fromProjectToModule _ _ projectContext =
|
||||
fromProjectToModule : ProjectContext -> ModuleContext
|
||||
fromProjectToModule projectContext =
|
||||
{ scope = Scope.fromProjectToModule projectContext.scope
|
||||
, modulesThatExposeSubscriptionsAndUpdate = projectContext.modulesThatExposeSubscriptionsAndUpdate
|
||||
, definesUpdate = False
|
||||
@ -112,12 +112,12 @@ fromProjectToModule _ _ projectContext =
|
||||
}
|
||||
|
||||
|
||||
fromModuleToProject : Rule.ModuleKey -> Node ModuleName -> ModuleContext -> ProjectContext
|
||||
fromModuleToProject _ moduleName moduleContext =
|
||||
{ scope = Scope.fromModuleToProject moduleName moduleContext.scope
|
||||
fromModuleToProject : Rule.ModuleMetadata -> ModuleContext -> ProjectContext
|
||||
fromModuleToProject metadata moduleContext =
|
||||
{ scope = Scope.fromModuleToProject (Rule.moduleNameNodeFromMetadata metadata) moduleContext.scope
|
||||
, modulesThatExposeSubscriptionsAndUpdate =
|
||||
if moduleContext.definesSubscriptions && moduleContext.definesUpdate then
|
||||
Set.singleton (Node.value moduleName)
|
||||
Set.singleton (Rule.moduleNameFromMetadata metadata)
|
||||
|
||||
else
|
||||
Set.empty
|
||||
|
Loading…
Reference in New Issue
Block a user