Add way to access the dependencies from a Rule

This commit is contained in:
Jeroen Engels 2019-11-20 10:45:13 +01:00
parent 75bb616686
commit e3003128c5
2 changed files with 28 additions and 5 deletions

View File

@ -66,6 +66,7 @@ rule =
, fileKey = Nothing , fileKey = Nothing
} }
, elmJsonVisitor = Just elmJsonVisitor , elmJsonVisitor = Just elmJsonVisitor
, dependenciesVisitor = Nothing
, fileVisitor = fileVisitor , fileVisitor = fileVisitor
, mergeContexts = , mergeContexts =
\contextA contextB -> \contextA contextB ->

View File

@ -4,7 +4,7 @@ module Review.Rule exposing
, newSchema, fromSchema , newSchema, fromSchema
, withSimpleModuleDefinitionVisitor, withSimpleImportVisitor, withSimpleDeclarationVisitor, withSimpleExpressionVisitor , withSimpleModuleDefinitionVisitor, withSimpleImportVisitor, withSimpleDeclarationVisitor, withSimpleExpressionVisitor
, withInitialContext, withModuleDefinitionVisitor, withImportVisitor, Direction(..), withDeclarationVisitor, withDeclarationListVisitor, withExpressionVisitor, withFinalEvaluation , withInitialContext, withModuleDefinitionVisitor, withImportVisitor, Direction(..), withDeclarationVisitor, withDeclarationListVisitor, withExpressionVisitor, withFinalEvaluation
, withElmJsonVisitor , withElmJsonVisitor, withDependenciesVisitor
, withFixes , withFixes
, Error, error, parsingError, errorRuleName, errorMessage, errorDetails, errorRange, errorFixes, errorFilePath , Error, error, parsingError, errorRuleName, errorMessage, errorDetails, errorRange, errorFixes, errorFilePath
, newMultiSchema, fromMultiSchema, newFileVisitorSchema , newMultiSchema, fromMultiSchema, newFileVisitorSchema
@ -25,6 +25,7 @@ contents of the file to analyze to the rule. The order in which things get passe
- Read project-related info (only collect data in these steps) - Read project-related info (only collect data in these steps)
- The `elm.json` file, visited by [`withElmJsonVisitor`](#withElmJsonVisitor) - The `elm.json` file, visited by [`withElmJsonVisitor`](#withElmJsonVisitor)
- The definition for dependencies, visited by [`withDependenciesVisitor`](#withDependenciesVisitor)
- Visit the file (in the following order) - Visit the file (in the following order)
- The module definition, visited by [`withSimpleModuleDefinitionVisitor`](#withSimpleModuleDefinitionVisitor) and [`withModuleDefinitionVisitor`](#withModuleDefinitionVisitor) - The module definition, visited by [`withSimpleModuleDefinitionVisitor`](#withSimpleModuleDefinitionVisitor) and [`withModuleDefinitionVisitor`](#withModuleDefinitionVisitor)
- Each import, visited by [`withSimpleImportVisitor`](#withSimpleImportVisitor) and [`withImportVisitor`](#withImportVisitor) - Each import, visited by [`withSimpleImportVisitor`](#withSimpleImportVisitor) and [`withImportVisitor`](#withImportVisitor)
@ -174,7 +175,7 @@ patterns you would want to forbid, but that are not handled by the example.
## Builder functions to analyze the project's data ## Builder functions to analyze the project's data
@docs withElmJsonVisitor @docs withElmJsonVisitor, withDependenciesVisitor
## Automatic fixing ## Automatic fixing
@ -198,6 +199,7 @@ For more information on automatic fixing, read the documentation for [`Review.Fi
-} -}
import Dict exposing (Dict) import Dict exposing (Dict)
import Elm.Docs
import Elm.Project import Elm.Project
import Elm.Syntax.Declaration exposing (Declaration(..)) import Elm.Syntax.Declaration exposing (Declaration(..))
import Elm.Syntax.Expression exposing (Expression(..), Function, LetDeclaration(..)) import Elm.Syntax.Expression exposing (Expression(..), Function, LetDeclaration(..))
@ -264,6 +266,7 @@ type
, initialContext : context , initialContext : context
, fileKeyVisitor : Maybe (FileKey -> context -> context) , fileKeyVisitor : Maybe (FileKey -> context -> context)
, elmJsonVisitor : Maybe Elm.Project.Project -> context -> context , elmJsonVisitor : Maybe Elm.Project.Project -> context -> context
, dependenciesVisitor : Dict String Elm.Docs.Module -> context -> context
, moduleDefinitionVisitor : Node Module -> context -> ( List Error, context ) , moduleDefinitionVisitor : Node Module -> context -> ( List Error, context )
, importVisitor : Node Import -> context -> ( List Error, context ) , importVisitor : Node Import -> context -> ( List Error, context )
, declarationListVisitor : List (Node Declaration) -> context -> ( List Error, context ) , declarationListVisitor : List (Node Declaration) -> context -> ( List Error, context )
@ -467,6 +470,7 @@ computeErrors (Schema schema) project =
initialContext = initialContext =
schema.initialContext schema.initialContext
|> schema.elmJsonVisitor (Review.Project.elmJson project) |> schema.elmJsonVisitor (Review.Project.elmJson project)
|> schema.dependenciesVisitor (Review.Project.modules project)
in in
\file -> \file ->
initialContext initialContext
@ -494,6 +498,7 @@ type MultiSchema context
{ name : String { name : String
, initialContext : context , initialContext : context
, elmJsonVisitor : Maybe (Maybe Elm.Project.Project -> context -> context) , elmJsonVisitor : Maybe (Maybe Elm.Project.Project -> context -> context)
, dependenciesVisitor : Maybe (Dict String Elm.Docs.Module -> context -> context)
, mergeContexts : context -> context -> context , mergeContexts : context -> context -> context
, fileVisitor : context -> Schema ForLookingAtSeveralFiles { hasAtLeastOneVisitor : () } context , fileVisitor : context -> Schema ForLookingAtSeveralFiles { hasAtLeastOneVisitor : () } context
, finalEvaluationFn : context -> List Error , finalEvaluationFn : context -> List Error
@ -505,16 +510,18 @@ newMultiSchema :
-> ->
{ initialContext : context { initialContext : context
, elmJsonVisitor : Maybe (Maybe Elm.Project.Project -> context -> context) , elmJsonVisitor : Maybe (Maybe Elm.Project.Project -> context -> context)
, dependenciesVisitor : Maybe (Dict String Elm.Docs.Module -> context -> context)
, fileVisitor : context -> Schema ForLookingAtSeveralFiles { hasAtLeastOneVisitor : () } context , fileVisitor : context -> Schema ForLookingAtSeveralFiles { hasAtLeastOneVisitor : () } context
, mergeContexts : context -> context -> context , mergeContexts : context -> context -> context
, finalEvaluation : context -> List Error , finalEvaluation : context -> List Error
} }
-> MultiSchema context -> MultiSchema context
newMultiSchema name_ { initialContext, elmJsonVisitor, fileVisitor, mergeContexts, finalEvaluation } = newMultiSchema name_ { initialContext, elmJsonVisitor, dependenciesVisitor, fileVisitor, mergeContexts, finalEvaluation } =
MultiSchema MultiSchema
{ name = name_ { name = name_
, initialContext = initialContext , initialContext = initialContext
, elmJsonVisitor = elmJsonVisitor , elmJsonVisitor = elmJsonVisitor
, dependenciesVisitor = dependenciesVisitor
, mergeContexts = mergeContexts , mergeContexts = mergeContexts
, fileVisitor = fileVisitor , fileVisitor = fileVisitor
, finalEvaluationFn = finalEvaluation , finalEvaluationFn = finalEvaluation
@ -549,14 +556,23 @@ type alias MultiRuleCache context =
runMulti : MultiSchema context -> MultiRuleCache context -> Project -> List ParsedFile -> ( List Error, Rule ) runMulti : MultiSchema context -> MultiRuleCache context -> Project -> List ParsedFile -> ( List Error, Rule )
runMulti (MultiSchema schema) startCache project = runMulti (MultiSchema schema) startCache project =
let let
initialContext : context contextWithElmJson : context
initialContext = contextWithElmJson =
case schema.elmJsonVisitor of case schema.elmJsonVisitor of
Just elmJsonVisitor -> Just elmJsonVisitor ->
elmJsonVisitor (Review.Project.elmJson project) schema.initialContext elmJsonVisitor (Review.Project.elmJson project) schema.initialContext
Nothing -> Nothing ->
schema.initialContext schema.initialContext
initialContext : context
initialContext =
case schema.dependenciesVisitor of
Just dependenciesVisitor ->
dependenciesVisitor (Review.Project.modules project) contextWithElmJson
Nothing ->
contextWithElmJson
in in
\files -> \files ->
let let
@ -937,6 +953,7 @@ emptySchema name_ initialContext =
, initialContext = initialContext , initialContext = initialContext
, fileKeyVisitor = Nothing , fileKeyVisitor = Nothing
, elmJsonVisitor = \elmJson context -> context , elmJsonVisitor = \elmJson context -> context
, dependenciesVisitor = \modules context -> context
, moduleDefinitionVisitor = \node context -> ( [], context ) , moduleDefinitionVisitor = \node context -> ( [], context )
, importVisitor = \node context -> ( [], context ) , importVisitor = \node context -> ( [], context )
, declarationListVisitor = \declarationNodes context -> ( [], context ) , declarationListVisitor = \declarationNodes context -> ( [], context )
@ -1014,6 +1031,11 @@ withElmJsonVisitor visitor (Schema schema) =
Schema { schema | elmJsonVisitor = visitor } Schema { schema | elmJsonVisitor = visitor }
withDependenciesVisitor : (Dict String Elm.Docs.Module -> context -> context) -> Schema anyType anything context -> Schema anyType anything context
withDependenciesVisitor visitor (Schema schema) =
Schema { schema | dependenciesVisitor = visitor }
{-| Add a visitor to the [`Schema`](#Schema) which will visit the `File`'s {-| Add a visitor to the [`Schema`](#Schema) which will visit the `File`'s
[module definition](https://package.elm-lang.org/packages/stil4m/elm-syntax/latest/Elm-Syntax-Module) (`module SomeModuleName exposing (a, b)`), collect data in the `context` and/or report patterns. [module definition](https://package.elm-lang.org/packages/stil4m/elm-syntax/latest/Elm-Syntax-Module) (`module SomeModuleName exposing (a, b)`), collect data in the `context` and/or report patterns.