mirror of
https://github.com/jfmengels/elm-review.git
synced 2024-08-16 19:40:22 +03:00
Compare commits
11 Commits
bcdf5834de
...
700a1de78f
Author | SHA1 | Date | |
---|---|---|---|
|
700a1de78f | ||
|
211358d8d9 | ||
|
77e9a1d702 | ||
|
c9d4a1a5e5 | ||
|
dddff57491 | ||
|
adde2af518 | ||
|
e908da1209 | ||
|
932140be6e | ||
|
1337bb97e3 | ||
|
312dff67f9 | ||
|
828318cccb |
@ -1,4 +1,4 @@
|
||||
module Review.RequestedData exposing (RequestedData(..), combine, combineJust, none)
|
||||
module Review.RequestedData exposing (RequestedData(..), combine, combineJust, none, withFiles)
|
||||
|
||||
|
||||
type RequestedData
|
||||
@ -6,6 +6,7 @@ type RequestedData
|
||||
{ moduleNameLookupTable : Bool
|
||||
, sourceCodeExtractor : Bool
|
||||
, ignoredFiles : Bool
|
||||
, files : List String
|
||||
}
|
||||
|
||||
|
||||
@ -15,6 +16,7 @@ none =
|
||||
{ moduleNameLookupTable = False
|
||||
, sourceCodeExtractor = False
|
||||
, ignoredFiles = False
|
||||
, files = []
|
||||
}
|
||||
|
||||
|
||||
@ -33,10 +35,20 @@ combine maybeA maybeB =
|
||||
a
|
||||
|
||||
|
||||
withFiles : List String -> RequestedData -> RequestedData
|
||||
withFiles files ((RequestedData requested) as untouched) =
|
||||
if List.isEmpty files then
|
||||
untouched
|
||||
|
||||
else
|
||||
RequestedData { requested | files = files }
|
||||
|
||||
|
||||
combineJust : RequestedData -> RequestedData -> RequestedData
|
||||
combineJust (RequestedData a) (RequestedData b) =
|
||||
RequestedData
|
||||
{ moduleNameLookupTable = a.moduleNameLookupTable || b.moduleNameLookupTable
|
||||
, sourceCodeExtractor = a.sourceCodeExtractor || b.sourceCodeExtractor
|
||||
, ignoredFiles = a.ignoredFiles || b.ignoredFiles
|
||||
, files = a.files ++ b.files
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ module Review.Rule exposing
|
||||
, ReviewError, errorRuleName, errorMessage, errorDetails, errorRange, errorFilePath, errorTarget, errorFixes, errorFixFailure
|
||||
, ignoreErrorsForDirectories, ignoreErrorsForFiles, filterErrorsForFiles
|
||||
, withDataExtractor, preventExtract
|
||||
, reviewV3, reviewV2, review, ProjectData, ruleName, ruleProvidesFixes, ruleKnowsAboutIgnoredFiles, withRuleId, getConfigurationError
|
||||
, reviewV3, reviewV2, review, ProjectData, ruleName, ruleProvidesFixes, ruleKnowsAboutIgnoredFiles, ruleRequestedFiles, withRuleId, getConfigurationError
|
||||
, Required, Forbidden
|
||||
)
|
||||
|
||||
@ -286,7 +286,7 @@ find the tools to extract data below.
|
||||
|
||||
# Running rules
|
||||
|
||||
@docs reviewV3, reviewV2, review, ProjectData, ruleName, ruleProvidesFixes, ruleKnowsAboutIgnoredFiles, withRuleId, getConfigurationError
|
||||
@docs reviewV3, reviewV2, review, ProjectData, ruleName, ruleProvidesFixes, ruleKnowsAboutIgnoredFiles, ruleRequestedFiles, withRuleId, getConfigurationError
|
||||
|
||||
|
||||
# Internals
|
||||
@ -397,13 +397,17 @@ type alias ModuleRuleSchemaData moduleContext =
|
||||
|
||||
-- Project visitors
|
||||
, elmJsonVisitor : Maybe (Maybe Elm.Project.Project -> moduleContext -> moduleContext)
|
||||
, arbitraryFilesVisitor : Maybe (List { path : String, content : String } -> moduleContext -> moduleContext)
|
||||
, arbitraryFilesVisitor : Maybe ( List { path : String, content : String } -> moduleContext -> moduleContext, ArbitraryFileRequest )
|
||||
, readmeVisitor : Maybe (Maybe String -> moduleContext -> moduleContext)
|
||||
, dependenciesVisitor : Maybe (Dict String Review.Project.Dependency.Dependency -> moduleContext -> moduleContext)
|
||||
, directDependenciesVisitor : Maybe (Dict String Review.Project.Dependency.Dependency -> moduleContext -> moduleContext)
|
||||
}
|
||||
|
||||
|
||||
type alias ArbitraryFileRequest =
|
||||
List String
|
||||
|
||||
|
||||
|
||||
-- REVIEWING
|
||||
|
||||
@ -872,6 +876,17 @@ ruleKnowsAboutIgnoredFiles (Rule rule) =
|
||||
requestedData.ignoredFiles
|
||||
|
||||
|
||||
{-| REPLACEME
|
||||
-}
|
||||
ruleRequestedFiles : Rule -> List String
|
||||
ruleRequestedFiles (Rule rule) =
|
||||
let
|
||||
(RequestedData requestedData) =
|
||||
rule.requestedData
|
||||
in
|
||||
requestedData.files
|
||||
|
||||
|
||||
{-| Assign an id to a rule. This id should be unique.
|
||||
|
||||
config =
|
||||
@ -1110,7 +1125,8 @@ fromModuleRuleSchema ((ModuleRuleSchema schema) as moduleVisitor) =
|
||||
{ name = schema.name
|
||||
, initialProjectContext = initialModuleContext
|
||||
, elmJsonVisitor = compactProjectDataVisitors (Maybe.map .project) schema.elmJsonVisitor
|
||||
, arbitraryFilesVisitor = compactProjectDataVisitors identity 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
|
||||
, dependenciesVisitor = compactProjectDataVisitors identity schema.dependenciesVisitor
|
||||
@ -1130,6 +1146,7 @@ fromModuleRuleSchema ((ModuleRuleSchema schema) as moduleVisitor) =
|
||||
, initialProjectContext = ()
|
||||
, elmJsonVisitor = Nothing
|
||||
, arbitraryFilesVisitor = Nothing
|
||||
, arbitraryFileRequest = []
|
||||
, readmeVisitor = Nothing
|
||||
, directDependenciesVisitor = Nothing
|
||||
, dependenciesVisitor = Nothing
|
||||
@ -1154,6 +1171,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
|
||||
|
||||
@ -1173,6 +1205,7 @@ type alias ProjectRuleSchemaData projectContext moduleContext =
|
||||
, initialProjectContext : projectContext
|
||||
, elmJsonVisitor : Maybe (Maybe { elmJsonKey : ElmJsonKey, project : Elm.Project.Project } -> projectContext -> ( List (Error {}), projectContext ))
|
||||
, arbitraryFilesVisitor : Maybe (List { path : String, content : String } -> projectContext -> ( List (Error {}), projectContext ))
|
||||
, arbitraryFileRequest : ArbitraryFileRequest
|
||||
, readmeVisitor : Maybe (Maybe { readmeKey : ReadmeKey, content : String } -> projectContext -> ( List (Error {}), projectContext ))
|
||||
, directDependenciesVisitor : Maybe (Dict String Review.Project.Dependency.Dependency -> projectContext -> ( List (Error {}), projectContext ))
|
||||
, dependenciesVisitor : Maybe (Dict String Review.Project.Dependency.Dependency -> projectContext -> ( List (Error {}), projectContext ))
|
||||
@ -1234,6 +1267,7 @@ newProjectRuleSchema name initialProjectContext =
|
||||
, initialProjectContext = initialProjectContext
|
||||
, elmJsonVisitor = Nothing
|
||||
, arbitraryFilesVisitor = Nothing
|
||||
, arbitraryFileRequest = []
|
||||
, readmeVisitor = Nothing
|
||||
, directDependenciesVisitor = Nothing
|
||||
, dependenciesVisitor = Nothing
|
||||
@ -1259,6 +1293,7 @@ fromProjectRuleSchema (ProjectRuleSchema schema) =
|
||||
RequestedData.combine
|
||||
(Maybe.map requestedDataFromContextCreator schema.moduleContextCreator)
|
||||
(Maybe.map (.fromModuleToProject >> requestedDataFromContextCreator) schema.folder)
|
||||
|> RequestedData.withFiles schema.arbitraryFileRequest
|
||||
, providesFixes = schema.providesFixes
|
||||
, ruleProjectVisitor =
|
||||
Ok
|
||||
@ -2294,11 +2329,22 @@ withElmJsonModuleVisitor visitor (ModuleRuleSchema schema) =
|
||||
the project's `README.md` file.
|
||||
-}
|
||||
withArbitraryFilesModuleVisitor :
|
||||
(List { path : String, content : String } -> moduleContext -> moduleContext)
|
||||
ArbitraryFileRequest
|
||||
-> (List { path : String, content : String } -> moduleContext -> moduleContext)
|
||||
-> ModuleRuleSchema { schemaState | canCollectProjectData : () } moduleContext
|
||||
-> ModuleRuleSchema { schemaState | canCollectProjectData : () } moduleContext
|
||||
withArbitraryFilesModuleVisitor visitor (ModuleRuleSchema schema) =
|
||||
ModuleRuleSchema { schema | arbitraryFilesVisitor = Just (combineContextOnlyVisitor visitor schema.arbitraryFilesVisitor) }
|
||||
withArbitraryFilesModuleVisitor newRequestedFiles newVisitor (ModuleRuleSchema schema) =
|
||||
ModuleRuleSchema { schema | arbitraryFilesVisitor = Just (combineArbitraryFilesModuleVisitor newRequestedFiles newVisitor schema) }
|
||||
|
||||
|
||||
combineArbitraryFilesModuleVisitor : ArbitraryFileRequest -> (a -> context -> context) -> { b | arbitraryFilesVisitor : Maybe ( a -> context -> context, ArbitraryFileRequest ) } -> ( a -> context -> context, ArbitraryFileRequest )
|
||||
combineArbitraryFilesModuleVisitor newRequestedFiles newVisitor schema =
|
||||
case schema.arbitraryFilesVisitor of
|
||||
Just ( existingVisitor, existingRequestedFiles ) ->
|
||||
( combineContextOnlyVisitor newVisitor (Just existingVisitor), newRequestedFiles ++ existingRequestedFiles )
|
||||
|
||||
Nothing ->
|
||||
( newVisitor, newRequestedFiles )
|
||||
|
||||
|
||||
{-| Add a visitor to the [`ModuleRuleSchema`](#ModuleRuleSchema) which will visit
|
||||
|
@ -58,6 +58,16 @@ rule : List String -> Rule
|
||||
rule exceptions =
|
||||
Rule.newModuleRuleSchema "NoImportingEverything" ()
|
||||
|> Rule.withSimpleImportVisitor (importVisitor <| exceptionsToSet exceptions)
|
||||
|> Rule.withArbitraryFilesModuleVisitor
|
||||
[ "CHANGELOG.md" ]
|
||||
(\files context ->
|
||||
let
|
||||
-- TODO FIX NOT GETTING ANY FILE HERE
|
||||
_ =
|
||||
Debug.log "files" files
|
||||
in
|
||||
context
|
||||
)
|
||||
|> Rule.fromModuleRuleSchema
|
||||
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
module Review.Rule.WithArbitraryFilesVisitorTest exposing (all)
|
||||
|
||||
import Elm.Syntax.Node as Node exposing (Node)
|
||||
import Review.Project as Project
|
||||
import Review.Rule as Rule exposing (Error, Rule)
|
||||
import Review.Test
|
||||
@ -20,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
|
||||
@ -37,7 +55,7 @@ type alias Context =
|
||||
rule : Rule
|
||||
rule =
|
||||
Rule.newModuleRuleSchema "WithCommentsVisitorTestRule" []
|
||||
|> Rule.withArbitraryFilesModuleVisitor arbitraryFilesModuleVisitor
|
||||
|> Rule.withArbitraryFilesModuleVisitor [ "foo/some-file.css" ] arbitraryFilesModuleVisitor
|
||||
|> Rule.withModuleDefinitionVisitor (\_ context -> ( [], context ))
|
||||
|> Rule.withFinalModuleEvaluation finalEvaluation
|
||||
|> Rule.fromModuleRuleSchema
|
||||
|
Loading…
Reference in New Issue
Block a user