Visit modules in project rule more efficiently and only if necessary

This commit is contained in:
Jeroen Engels 2020-03-19 00:21:45 +01:00
parent f37b19d6ed
commit 8b1050b2f8

View File

@ -1069,10 +1069,6 @@ type alias ProjectRuleCache projectContext =
runProjectRule : ProjectRuleSchema projectContext moduleContext -> ProjectRuleCache projectContext -> Exceptions -> Project -> List (Graph.NodeContext ModuleName ()) -> ( List Error, Rule ) runProjectRule : ProjectRuleSchema projectContext moduleContext -> ProjectRuleCache projectContext -> Exceptions -> Project -> List (Graph.NodeContext ModuleName ()) -> ( List Error, Rule )
runProjectRule ((ProjectRuleSchema schema) as wrappedSchema) startCache exceptions project nodeContexts = runProjectRule ((ProjectRuleSchema schema) as wrappedSchema) startCache exceptions project nodeContexts =
let let
graph : Graph ModuleName ()
graph =
Review.Project.moduleGraph project
elmJsonData : Maybe { elmJsonKey : ElmJsonKey, project : Elm.Project.Project } elmJsonData : Maybe { elmJsonKey : ElmJsonKey, project : Elm.Project.Project }
elmJsonData = elmJsonData =
Review.Project.elmJson project Review.Project.elmJson project
@ -1099,6 +1095,46 @@ runProjectRule ((ProjectRuleSchema schema) as wrappedSchema) startCache exceptio
|> accumulateWithListOfVisitors schema.readmeVisitors readmeData |> accumulateWithListOfVisitors schema.readmeVisitors readmeData
|> accumulateWithListOfVisitors schema.dependenciesVisitors (Review.Project.dependencies project) |> accumulateWithListOfVisitors schema.dependenciesVisitors (Review.Project.dependencies project)
newCache : ProjectRuleCache projectContext
newCache =
if List.isEmpty schema.moduleVisitorCreators then
startCache
else
computeModules wrappedSchema project initialContext nodeContexts startCache
contextsAndErrorsPerFile : List ( List Error, projectContext )
contextsAndErrorsPerFile =
newCache
|> Dict.values
|> List.map (\cacheEntry -> ( cacheEntry.errors, cacheEntry.context ))
errors : List Error
errors =
[ projectRelatedErrors
, List.concatMap Tuple.first contextsAndErrorsPerFile
, errorsFromFinalEvaluationForProject wrappedSchema initialContext contextsAndErrorsPerFile
]
|> List.concat
|> Exceptions.apply exceptions errorFilePath
in
( errors, Rule schema.name exceptions (runProjectRule wrappedSchema newCache) )
computeModules : ProjectRuleSchema projectContext moduleContext -> Project -> projectContext -> List (Graph.NodeContext ModuleName ()) -> ProjectRuleCache projectContext -> ProjectRuleCache projectContext
computeModules (ProjectRuleSchema schema) project initialContext nodeContexts startCache =
let
graph : Graph ModuleName ()
graph =
Review.Project.moduleGraph project
projectModulePaths : Set String
projectModulePaths =
project
|> Review.Project.modules
|> List.map .path
|> Set.fromList
modules : Dict ModuleName ProjectModule modules : Dict ModuleName ProjectModule
modules = modules =
project project
@ -1112,12 +1148,10 @@ runProjectRule ((ProjectRuleSchema schema) as wrappedSchema) startCache exceptio
) )
Dict.empty Dict.empty
projectModulePaths : Set String newStartCache : ProjectRuleCache projectContext
projectModulePaths = newStartCache =
project startCache
|> Review.Project.modules |> Dict.filter (\path _ -> Set.member path projectModulePaths)
|> List.map .path
|> Set.fromList
dummyInitialContext : moduleContext dummyInitialContext : moduleContext
dummyInitialContext = dummyInitialContext =
@ -1182,37 +1216,13 @@ runProjectRule ((ProjectRuleSchema schema) as wrappedSchema) startCache exceptio
moduleNameNode_ moduleNameNode_
context context
} }
in
newStartCache : ProjectRuleCache projectContext
newStartCache =
startCache
|> Dict.filter (\path _ -> Set.member path projectModulePaths)
newCache : ProjectRuleCache projectContext
newCache =
List.foldl List.foldl
(computeModuleAndCacheResult schema.traversalType modules graph computeModule) (computeModuleAndCacheResult schema.traversalType modules graph computeModule)
( newStartCache, Set.empty ) ( newStartCache, Set.empty )
nodeContexts nodeContexts
|> Tuple.first |> Tuple.first
contextsAndErrorsPerFile : List ( List Error, projectContext )
contextsAndErrorsPerFile =
newCache
|> Dict.values
|> List.map (\cacheEntry -> ( cacheEntry.errors, cacheEntry.context ))
errors : List Error
errors =
[ projectRelatedErrors
, List.concatMap Tuple.first contextsAndErrorsPerFile
, errorsFromFinalEvaluationForProject wrappedSchema initialContext contextsAndErrorsPerFile
]
|> List.concat
|> Exceptions.apply exceptions errorFilePath
in
( errors, Rule schema.name exceptions (runProjectRule wrappedSchema newCache) )
setRuleName : String -> Error -> Error setRuleName : String -> Error -> Error
setRuleName ruleName (Error err) = setRuleName ruleName (Error err) =