Add withModuleDocumentation

This commit is contained in:
Jeroen Engels 2022-08-07 21:30:18 +02:00
parent 62abe67a47
commit fc8d1aac9f
2 changed files with 90 additions and 2 deletions

View File

@ -14,7 +14,7 @@ module Review.Rule exposing
, withFinalModuleEvaluation
, withElmJsonModuleVisitor, withReadmeModuleVisitor, withDependenciesModuleVisitor
, ProjectRuleSchema, newProjectRuleSchema, fromProjectRuleSchema, withModuleVisitor, withModuleContext, withModuleContextUsingContextCreator, withElmJsonProjectVisitor, withReadmeProjectVisitor, withDependenciesProjectVisitor, withFinalProjectEvaluation, withContextFromImportedModules
, ContextCreator, initContextCreator, withModuleName, withModuleNameNode, withIsInSourceDirectories, withFilePath, withModuleNameLookupTable, withModuleKey, withSourceCodeExtractor, withFullAst
, ContextCreator, initContextCreator, withModuleName, withModuleNameNode, withIsInSourceDirectories, withFilePath, withModuleNameLookupTable, withModuleKey, withSourceCodeExtractor, withFullAst, withModuleDocumentation
, Metadata, withMetadata, moduleNameFromMetadata, moduleNameNodeFromMetadata, isInSourceDirectories
, Error, error, errorWithFix, ModuleKey, errorForModule, errorForModuleWithFix, ElmJsonKey, errorForElmJson, errorForElmJsonWithFix, ReadmeKey, errorForReadme, errorForReadmeWithFix
, globalError, configurationError
@ -227,7 +227,7 @@ first, as they are in practice a simpler version of project rules.
## Requesting more information
@docs ContextCreator, initContextCreator, withModuleName, withModuleNameNode, withIsInSourceDirectories, withFilePath, withModuleNameLookupTable, withModuleKey, withSourceCodeExtractor, withFullAst
@docs ContextCreator, initContextCreator, withModuleName, withModuleNameNode, withIsInSourceDirectories, withFilePath, withModuleNameLookupTable, withModuleKey, withSourceCodeExtractor, withFullAst, withModuleDocumentation
### Requesting more information (DEPRECATED)
@ -5311,6 +5311,28 @@ withFullAst (ContextCreator fn requested) =
requested
{-| Request the module documentation. Modules don't always have a documentation.
When that is the case, the module documentation will be `Nothing`.
contextCreator : Rule.ContextCreator () Context
contextCreator =
Rule.initContextCreator
(\moduleDocumentation () ->
{ moduleDocumentation = moduleDocumentation
-- ...other fields
}
)
|> Rule.withModuleDocumentation
-}
withModuleDocumentation : ContextCreator (Maybe (Node String)) (from -> to) -> ContextCreator from to
withModuleDocumentation (ContextCreator fn requested) =
ContextCreator
(\data -> fn data (findModuleDocumentation data.ast))
requested
{-| Request the [module key](#ModuleKey) for this module.
rule : Rule

View File

@ -0,0 +1,66 @@
module Review.Rule.WithModuleDocumentationTest exposing (all)
import Elm.Syntax.Node as Node exposing (Node)
import Review.Rule as Rule exposing (Rule)
import Review.Test
import Test exposing (Test, test)
type alias Context =
{ moduleDocumentation : Maybe (Node String)
}
rule : Rule
rule =
Rule.newModuleRuleSchemaUsingContextCreator "TestRule" initContext
|> Rule.withModuleDefinitionVisitor
(\node context ->
( [ Rule.error
{ message =
case context.moduleDocumentation of
Just moduleDocumentation ->
Node.value moduleDocumentation
Nothing ->
"MISSING DOCUMENTATION"
, details = [ "No details" ]
}
(Node.range node)
]
, context
)
)
|> Rule.fromModuleRuleSchema
initContext : Rule.ContextCreator () Context
initContext =
Rule.initContextCreator
(\moduleDocumentation _ ->
{ moduleDocumentation = moduleDocumentation
}
)
|> Rule.withModuleDocumentation
all : Test
all =
test "should pass the module documentation if there is one" <|
\() ->
"""module ModuleName exposing (a)
{-| module documentation
-}
{-| function doc
-}
a = 1
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "{-| module documentation\n-}"
, details = [ "No details" ]
, under = "module ModuleName exposing (a)"
}
]