Filter out files that were not requested

This commit is contained in:
Jeroen Engels 2023-05-31 10:57:35 +02:00
parent 08647ec79d
commit 3816ab1074
2 changed files with 35 additions and 1 deletions

View File

@ -1110,7 +1110,7 @@ fromModuleRuleSchema ((ModuleRuleSchema schema) as moduleVisitor) =
{ name = schema.name
, initialProjectContext = initialModuleContext
, elmJsonVisitor = compactProjectDataVisitors (Maybe.map .project) schema.elmJsonVisitor
, arbitraryFilesVisitor = compactProjectDataVisitors identity (Maybe.map Tuple.first schema.arbitraryFilesVisitor)
, arbitraryFilesVisitor = compactArbitraryFilesVisitor schema.arbitraryFilesVisitor
, arbitraryFileRequest = Maybe.map Tuple.second schema.arbitraryFilesVisitor |> Maybe.withDefault []
, readmeVisitor = compactProjectDataVisitors (Maybe.map .content) schema.readmeVisitor
, directDependenciesVisitor = compactProjectDataVisitors identity schema.directDependenciesVisitor
@ -1156,6 +1156,21 @@ compactProjectDataVisitors getData maybeVisitor =
Just (\rawData moduleContext -> ( [], visitor (getData rawData) moduleContext ))
compactArbitraryFilesVisitor : Maybe ( List { a | path : String } -> moduleContext -> moduleContext, List String ) -> Maybe (List { a | path : String } -> moduleContext -> ( List nothing, moduleContext ))
compactArbitraryFilesVisitor maybeArbitraryFilesVisitor =
case maybeArbitraryFilesVisitor of
Just ( arbitraryFilesVisitor, requestedFiles ) ->
let
predicate : { a | path : String } -> Bool
predicate file =
List.member file.path requestedFiles
in
Just (\files moduleContext -> ( [], arbitraryFilesVisitor (List.filter predicate files) moduleContext ))
Nothing ->
Nothing
-- PROJECT RULES

View File

@ -19,6 +19,25 @@ all =
in
"""module A exposing (a)
a = 1
"""
|> Review.Test.runWithProjectData (Project.addArbitraryFiles arbitraryFiles Project.new) rule
|> Review.Test.expectGlobalErrors
[ { message = "Found these files"
, details = [ "foo/some-file.css" ]
}
]
, test "filters out files that were not requested" <|
\() ->
let
arbitraryFiles : List { path : String, content : String }
arbitraryFiles =
[ { path = "foo/some-file.css", content = "#thing { color: red; }" }
, { path = "foo/some-other-file.css", content = "#thing { color: red; }" }
, { path = "bar/some-file.css", content = "#thing { color: red; }" }
]
in
"""module A exposing (a)
a = 1
"""
|> Review.Test.runWithProjectData (Project.addArbitraryFiles arbitraryFiles Project.new) rule
|> Review.Test.expectGlobalErrors