Fix files not being evaluated when they're ignored in project rules without a folder

This commit is contained in:
Jeroen Engels 2020-09-09 18:11:40 +02:00
parent 02625de8d3
commit e925d0a25a
2 changed files with 191 additions and 10 deletions

View File

@ -3384,11 +3384,10 @@ runProjectVisitor name projectVisitor maybePreviousCache exceptions project node
errors =
case projectVisitor.traversalAndFolder of
TraverseAllModulesInParallel _ ->
errorsFromCache newCache
Exceptions.apply exceptions (accessInternalError >> .filePath) (errorsFromCache newCache)
TraverseImportedModulesFirst _ ->
errorsFromCache newCache
|> Exceptions.apply exceptions (accessInternalError >> .filePath)
Exceptions.apply exceptions (accessInternalError >> .filePath) (errorsFromCache newCache)
in
{ errors = List.map (setRuleName name) errors
, rule =
@ -3609,10 +3608,15 @@ computeModules projectVisitor ( moduleVisitor, moduleContextCreator ) project ex
moduleNameLookupTables =
Review.Project.Internal.moduleNameLookupTables project
moduleToAnalyze : List ProjectModule
moduleToAnalyze =
modulesToAnalyze : List ProjectModule
modulesToAnalyze =
case projectVisitor.traversalAndFolder of
TraverseAllModulesInParallel _ ->
TraverseAllModulesInParallel (Just _) ->
Review.Project.modules project
TraverseAllModulesInParallel Nothing ->
-- Performance: avoid visiting modules when they're ignored and they
-- can't influence the rest of the review.
Exceptions.apply
exceptions
.path
@ -3623,7 +3627,7 @@ computeModules projectVisitor ( moduleVisitor, moduleContextCreator ) project ex
projectModulePaths : Set String
projectModulePaths =
moduleToAnalyze
modulesToAnalyze
|> List.map .path
|> Set.fromList
@ -3637,12 +3641,11 @@ computeModules projectVisitor ( moduleVisitor, moduleContextCreator ) project ex
dict
)
Dict.empty
moduleToAnalyze
modulesToAnalyze
newStartCache : Dict String (CacheEntry projectContext)
newStartCache =
startCache
|> Dict.filter (\path _ -> Set.member path projectModulePaths)
Dict.filter (\path _ -> Set.member path projectModulePaths) startCache
computeModule : Dict String (CacheEntry projectContext) -> List ProjectModule -> ProjectModule -> CacheEntry projectContext
computeModule cache importedModules module_ =

View File

@ -0,0 +1,178 @@
module Review.Rule.ProjectVisitTest exposing (all)
import Review.Rule as Rule exposing (Error)
import Review.Test
import Set exposing (Set)
import Test exposing (Test, test)
type alias ProjectContext =
{ aModuleKey : Maybe Rule.ModuleKey
, moduleNames : Set String
}
initialContext : ProjectContext
initialContext =
{ aModuleKey = Nothing
, moduleNames = Set.empty
}
baseRule : Rule.ProjectRuleSchema { hasAtLeastOneVisitor : (), withModuleContext : Rule.Forbidden } ProjectContext ()
baseRule =
Rule.newProjectRuleSchema "Visitor order" initialContext
|> Rule.withModuleVisitor (Rule.withSimpleModuleDefinitionVisitor (always []))
|> Rule.withModuleContextUsingContextCreator
{ fromProjectToModule = Rule.initContextCreator (\_ -> ())
, fromModuleToProject = fromModuleToProject
, foldProjectContexts = foldProjectContexts
}
|> Rule.withFinalProjectEvaluation finalEvaluation
fromModuleToProject : Rule.ContextCreator a ProjectContext
fromModuleToProject =
Rule.initContextCreator
(\moduleKey metadata _ ->
{ aModuleKey = Just moduleKey
, moduleNames = Set.singleton (Rule.moduleNameFromMetadata metadata |> String.join ".")
}
)
|> Rule.withModuleKey
|> Rule.withMetadata
foldProjectContexts : ProjectContext -> ProjectContext -> ProjectContext
foldProjectContexts newContext previousContext =
{ aModuleKey = newContext.aModuleKey
, moduleNames = Set.union newContext.moduleNames previousContext.moduleNames
}
finalEvaluation : ProjectContext -> List (Error { useErrorForModule : () })
finalEvaluation context =
case context.aModuleKey of
Just moduleKey ->
[ Rule.errorForModule moduleKey
{ message = Set.toList context.moduleNames |> String.join " - "
, details = [ "details" ]
}
{ start = { row = 1, column = 1 }
, end = { row = 1, column = 7 }
}
]
Nothing ->
[]
all : Test
all =
Test.describe "Visitor order"
[ Test.describe "using withContextFromImportedModules"
[ test "should visit every file in a project rule" <|
\() ->
[ """module A exposing (..)
a = 1"""
, """module B exposing (..)
import A
a = 1"""
, """module C exposing (..)
import A
a = 1"""
]
|> Review.Test.runOnModules
(baseRule
|> Rule.withContextFromImportedModules
|> Rule.fromProjectRuleSchema
)
|> Review.Test.expectErrorsForModules
[ ( "C"
, [ Review.Test.error
{ message = "A - B - C"
, details = [ "details" ]
, under = "module"
}
]
)
]
, test "should visit every file in a project rule, even when some of them are ignored" <|
\() ->
[ """module A exposing (..)
a = 1"""
, """module B exposing (..)
import A
a = 1"""
, """module C exposing (..)
import A
a = 1"""
]
|> Review.Test.runOnModules
(baseRule
|> Rule.withContextFromImportedModules
|> Rule.fromProjectRuleSchema
|> Rule.ignoreErrorsForFiles [ "src/File_1.elm" ]
)
|> Review.Test.expectErrorsForModules
[ ( "C"
, [ Review.Test.error
{ message = "A - B - C"
, details = [ "details" ]
, under = "module"
}
]
)
]
]
, Test.describe "without using withContextFromImportedModules"
[ test "should visit every file in a project rule" <|
\() ->
[ """module A exposing (..)
a = 1"""
, """module B exposing (..)
import A
a = 1"""
, """module C exposing (..)
import A
a = 1"""
]
|> Review.Test.runOnModules (Rule.fromProjectRuleSchema baseRule)
|> Review.Test.expectErrorsForModules
[ ( "C"
, [ Review.Test.error
{ message = "A - B - C"
, details = [ "details" ]
, under = "module"
}
]
)
]
, test "should visit every file in a project rule, even when some of them are ignored" <|
\() ->
[ """module A exposing (..)
a = 1"""
, """module B exposing (..)
import A
a = 1"""
, """module C exposing (..)
import A
a = 1"""
]
|> Review.Test.runOnModules
(baseRule
|> Rule.fromProjectRuleSchema
|> Rule.ignoreErrorsForFiles [ "src/File_1.elm" ]
)
|> Review.Test.expectErrorsForModules
[ ( "C"
, [ Review.Test.error
{ message = "A - B - C"
, details = [ "details" ]
, under = "module"
}
]
)
]
]
]