Add Rule.withModuleName

This commit is contained in:
Jeroen Engels 2022-04-28 11:48:55 +02:00
parent 319ecb29a6
commit d17d995f0e
3 changed files with 84 additions and 3 deletions

File diff suppressed because one or more lines are too long

View File

@ -13,7 +13,8 @@ module Review.Rule exposing
, withFinalModuleEvaluation
, withElmJsonModuleVisitor, withReadmeModuleVisitor, withDependenciesModuleVisitor
, ProjectRuleSchema, newProjectRuleSchema, fromProjectRuleSchema, withModuleVisitor, withModuleContext, withModuleContextUsingContextCreator, withElmJsonProjectVisitor, withReadmeProjectVisitor, withDependenciesProjectVisitor, withFinalProjectEvaluation, withContextFromImportedModules
, ContextCreator, Metadata, initContextCreator, isInSourceDirectories, moduleNameFromMetadata, moduleNameNodeFromMetadata, withMetadata, withFilePath, withModuleNameLookupTable, withModuleKey, withSourceCodeExtractor
, ContextCreator, initContextCreator, isInSourceDirectories, withModuleName, withFilePath, withModuleNameLookupTable, withModuleKey, withSourceCodeExtractor
, Metadata, moduleNameFromMetadata, moduleNameNodeFromMetadata, withMetadata
, Error, error, errorWithFix, ModuleKey, errorForModule, errorForModuleWithFix, ElmJsonKey, errorForElmJson, errorForElmJsonWithFix, ReadmeKey, errorForReadme, errorForReadmeWithFix
, globalError, configurationError
, ReviewError, errorRuleName, errorMessage, errorDetails, errorRange, errorFixes, errorFilePath, errorTarget
@ -223,7 +224,8 @@ first, as they are in practice a simpler version of project rules.
## Requesting more information
@docs ContextCreator, Metadata, initContextCreator, isInSourceDirectories, moduleNameFromMetadata, moduleNameNodeFromMetadata, withMetadata, withFilePath, withModuleNameLookupTable, withModuleKey, withSourceCodeExtractor
@docs ContextCreator, initContextCreator, isInSourceDirectories, withModuleName, withFilePath, withModuleNameLookupTable, withModuleKey, withSourceCodeExtractor
@docs Metadata, moduleNameFromMetadata, moduleNameNodeFromMetadata, withMetadata
## Errors
@ -5047,6 +5049,27 @@ withMetadata (ContextCreator fn requestedData) =
requestedData
{-| Request the name of the module.
contextCreator : Rule.ContextCreator () Context
contextCreator =
Rule.initContextCreator
(\moduleName () ->
{ moduleName = moduleName
-- ...other fields
}
)
|> Rule.withModuleName
-}
withModuleName : ContextCreator ModuleName (from -> to) -> ContextCreator from to
withModuleName (ContextCreator fn requestedData) =
ContextCreator
(\data -> fn data (moduleNameFromMetadata data.metadata))
requestedData
{-| Requests the module name lookup table for the types and functions inside a module.
When encountering a `Expression.FunctionOrValue ModuleName String` (among other nodes where we refer to a function or value),

View File

@ -0,0 +1,58 @@
module Review.Rule.WithModuleNameTest exposing (all)
import Elm.Syntax.ModuleName exposing (ModuleName)
import Elm.Syntax.Node as Node
import Review.Rule as Rule exposing (Rule)
import Review.Test
import Test exposing (Test, test)
type alias Context =
{ moduleName : ModuleName
}
rule : Rule
rule =
Rule.newModuleRuleSchemaUsingContextCreator "TestRule" initContext
|> Rule.withModuleDefinitionVisitor
(\node context ->
( [ Rule.error
{ message = "ModuleName: " ++ Debug.toString context.moduleName
, details = [ "No details" ]
}
(Node.range node)
]
, context
)
)
|> Rule.fromModuleRuleSchema
initContext : Rule.ContextCreator () Context
initContext =
Rule.initContextCreator
(\moduleName _ ->
{ moduleName = moduleName
}
)
|> Rule.withModuleName
all : Test
all =
Test.describe "withModuleNameTest"
[ test "should not pass the elmJsonKey if the `elm.json` file does not exist" <|
\() ->
"""module A.B exposing (..)
a = 1
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "ModuleName: [\"A\",\"B\"]"
, details = [ "No details" ]
, under = "module A.B exposing (..)"
}
]
]