mirror of
https://github.com/jfmengels/elm-review.git
synced 2024-08-16 19:40:22 +03:00
Add Rule.withModuleDocumentationVisitor (#132)
This commit is contained in:
parent
d14f04d4bf
commit
350300e0c9
@ -4,6 +4,7 @@ module Review.Rule exposing
|
||||
, withSimpleModuleDefinitionVisitor, withSimpleCommentsVisitor, withSimpleImportVisitor, withSimpleDeclarationVisitor, withSimpleExpressionVisitor
|
||||
, newModuleRuleSchemaUsingContextCreator
|
||||
, withModuleDefinitionVisitor
|
||||
, withModuleDocumentationVisitor
|
||||
, withCommentsVisitor
|
||||
, withImportVisitor
|
||||
, Direction(..), withDeclarationEnterVisitor, withDeclarationExitVisitor, withDeclarationVisitor, withDeclarationListVisitor
|
||||
@ -164,6 +165,7 @@ The traversal of a module rule is the following:
|
||||
- The definition for dependencies, visited by [`withDependenciesModuleVisitor`](#withDependenciesModuleVisitor)
|
||||
- Visit the Elm module (in the following order)
|
||||
- The module definition, visited by [`withSimpleModuleDefinitionVisitor`](#withSimpleModuleDefinitionVisitor) and [`withModuleDefinitionVisitor`](#withModuleDefinitionVisitor)
|
||||
- The module documentation, visited by [`withModuleDocumentationVisitor`](#withModuleDocumentationVisitor)
|
||||
- The module's list of comments, visited by [`withSimpleCommentsVisitor`](#withSimpleCommentsVisitor) and [`withCommentsVisitor`](#withCommentsVisitor)
|
||||
- Each import, visited by [`withSimpleImportVisitor`](#withSimpleImportVisitor) and [`withImportVisitor`](#withImportVisitor)
|
||||
- The list of declarations, visited by [`withDeclarationListVisitor`](#withDeclarationListVisitor)
|
||||
@ -192,6 +194,7 @@ Evaluating/visiting a node means two things:
|
||||
|
||||
@docs newModuleRuleSchemaUsingContextCreator
|
||||
@docs withModuleDefinitionVisitor
|
||||
@docs withModuleDocumentationVisitor
|
||||
@docs withCommentsVisitor
|
||||
@docs withImportVisitor
|
||||
@docs Direction, withDeclarationEnterVisitor, withDeclarationExitVisitor, withDeclarationVisitor, withDeclarationListVisitor
|
||||
@ -278,6 +281,7 @@ import Elm.Project
|
||||
import Elm.Syntax.Declaration as Declaration exposing (Declaration)
|
||||
import Elm.Syntax.Exposing as Exposing exposing (Exposing, TopLevelExpose)
|
||||
import Elm.Syntax.Expression as Expression exposing (Expression, Function)
|
||||
import Elm.Syntax.File
|
||||
import Elm.Syntax.Import exposing (Import)
|
||||
import Elm.Syntax.Infix as Infix
|
||||
import Elm.Syntax.Module as Module exposing (Module)
|
||||
@ -343,6 +347,7 @@ type alias ModuleRuleSchemaData moduleContext =
|
||||
, initialModuleContext : Maybe moduleContext
|
||||
, moduleContextCreator : ContextCreator () moduleContext
|
||||
, moduleDefinitionVisitors : List (Visitor Module moduleContext)
|
||||
, moduleDocumentationVisitors : List (Maybe (Node String) -> moduleContext -> ( List (Error {}), moduleContext ))
|
||||
, commentsVisitors : List (List (Node String) -> moduleContext -> ( List (Error {}), moduleContext ))
|
||||
, importVisitors : List (Visitor Import moduleContext)
|
||||
, declarationListVisitors : List (List (Node Declaration) -> moduleContext -> ( List (Error {}), moduleContext ))
|
||||
@ -976,6 +981,7 @@ newModuleRuleSchema name initialModuleContext =
|
||||
, initialModuleContext = Just initialModuleContext
|
||||
, moduleContextCreator = initContextCreator (always initialModuleContext)
|
||||
, moduleDefinitionVisitors = []
|
||||
, moduleDocumentationVisitors = []
|
||||
, commentsVisitors = []
|
||||
, importVisitors = []
|
||||
, declarationListVisitors = []
|
||||
@ -1040,6 +1046,7 @@ newModuleRuleSchemaUsingContextCreator name moduleContextCreator =
|
||||
, initialModuleContext = Nothing
|
||||
, moduleContextCreator = moduleContextCreator
|
||||
, moduleDefinitionVisitors = []
|
||||
, moduleDocumentationVisitors = []
|
||||
, commentsVisitors = []
|
||||
, importVisitors = []
|
||||
, declarationListVisitors = []
|
||||
@ -1312,6 +1319,7 @@ mergeModuleVisitorsHelp initialProjectContext moduleContextCreator visitors =
|
||||
, initialModuleContext = Just initialModuleContext
|
||||
, moduleContextCreator = initContextCreator (always initialModuleContext)
|
||||
, moduleDefinitionVisitors = []
|
||||
, moduleDocumentationVisitors = []
|
||||
, commentsVisitors = []
|
||||
, importVisitors = []
|
||||
, declarationListVisitors = []
|
||||
@ -1343,6 +1351,7 @@ mergeModuleVisitorsHelp initialProjectContext moduleContextCreator visitors =
|
||||
fromModuleRuleSchemaToRunnableModuleVisitor : ModuleRuleSchema schemaState moduleContext -> RunnableModuleVisitor moduleContext
|
||||
fromModuleRuleSchemaToRunnableModuleVisitor (ModuleRuleSchema schema) =
|
||||
{ moduleDefinitionVisitors = List.reverse schema.moduleDefinitionVisitors
|
||||
, moduleDocumentationVisitors = List.reverse schema.moduleDocumentationVisitors
|
||||
, commentsVisitors = List.reverse schema.commentsVisitors
|
||||
, importVisitors = List.reverse schema.importVisitors
|
||||
, declarationListVisitors = List.reverse schema.declarationListVisitors
|
||||
@ -1983,17 +1992,17 @@ withSimpleModuleDefinitionVisitor visitor schema =
|
||||
{-| Add a visitor to the [`ModuleRuleSchema`](#ModuleRuleSchema) which will visit the module's comments.
|
||||
|
||||
This visitor will give you access to the list of comments (in source order) in
|
||||
the module all at once. Note that comments that are parsed as doc comments by
|
||||
the module all at once. Note that comments that are parsed as documentation comments by
|
||||
[`elm-syntax`](https://package.elm-lang.org/packages/stil4m/elm-syntax/latest/)
|
||||
are not included in this list.
|
||||
|
||||
As such, the following comments are included (✅) / excluded (❌):
|
||||
|
||||
- ✅ Module doc comments (`{-| -}`)
|
||||
- ✅ Port doc comments (`{-| -}`)
|
||||
- ✅ Module documentation (`{-| -}`)
|
||||
- ✅ Port documentation comments (`{-| -}`)
|
||||
- ✅ Top-level comments not internal to a function/type/etc.
|
||||
- ✅ Comments internal to a function/type/etc.
|
||||
- ❌ Function/type/type alias doc comments (`{-| -}`)
|
||||
- ❌ Function/type/type alias documentation comments (`{-| -}`)
|
||||
|
||||
The following example forbids words like "TODO" appearing in a comment.
|
||||
|
||||
@ -2353,27 +2362,42 @@ withModuleDefinitionVisitor visitor (ModuleRuleSchema schema) =
|
||||
the `context` and/or report patterns.
|
||||
|
||||
This visitor will give you access to the list of comments (in source order) in
|
||||
the module all at once. Note that comments that are parsed as doc comments by
|
||||
the module all at once. Note that comments that are parsed as documentation comments by
|
||||
[`elm-syntax`](https://package.elm-lang.org/packages/stil4m/elm-syntax/latest/)
|
||||
are not included in this list.
|
||||
|
||||
As such, the following comments are included (✅) / excluded (❌):
|
||||
|
||||
- ✅ Module doc comments (`{-| -}`)
|
||||
- ✅ Port doc comments (`{-| -}`)
|
||||
- ✅ Module documentation (`{-| -}`)
|
||||
- ✅ Port documentation comments (`{-| -}`)
|
||||
- ✅ Top-level comments not internal to a function/type/etc.
|
||||
- ✅ Comments internal to a function/type/etc.
|
||||
- ❌ Function/type/type alias doc comments (`{-| -}`)
|
||||
- ❌ Function/type/type alias documentation comments (`{-| -}`)
|
||||
|
||||
Tip: If you do not need to collect data in this visitor, you may wish to use the
|
||||
simpler [`withSimpleCommentsVisitor`](#withSimpleCommentsVisitor) function.
|
||||
|
||||
Tip: If you only need to access the module documentation, you should use
|
||||
[`withModuleDocumentationVisitor`](#withModuleDocumentationVisitor) instead.
|
||||
|
||||
-}
|
||||
withCommentsVisitor : (List (Node String) -> moduleContext -> ( List (Error {}), moduleContext )) -> ModuleRuleSchema schemaState moduleContext -> ModuleRuleSchema { schemaState | hasAtLeastOneVisitor : () } moduleContext
|
||||
withCommentsVisitor visitor (ModuleRuleSchema schema) =
|
||||
ModuleRuleSchema { schema | commentsVisitors = visitor :: schema.commentsVisitors }
|
||||
|
||||
|
||||
{-| Add a visitor to the [`ModuleRuleSchema`](#ModuleRuleSchema) which will visit the module's documentation, collect data in
|
||||
the `context` and/or report patterns.
|
||||
|
||||
This visitor will give you access to the module documentation comment. Modules don't always have a documentation.
|
||||
When that is the case, the visitor will be called with the `Nothing` as the module documentation.
|
||||
|
||||
-}
|
||||
withModuleDocumentationVisitor : (Maybe (Node String) -> moduleContext -> ( List (Error {}), moduleContext )) -> ModuleRuleSchema schemaState moduleContext -> ModuleRuleSchema { schemaState | hasAtLeastOneVisitor : () } moduleContext
|
||||
withModuleDocumentationVisitor visitor (ModuleRuleSchema schema) =
|
||||
ModuleRuleSchema { schema | moduleDocumentationVisitors = visitor :: schema.moduleDocumentationVisitors }
|
||||
|
||||
|
||||
{-| Add a visitor to the [`ModuleRuleSchema`](#ModuleRuleSchema) which will visit the module's
|
||||
[import statements](https://package.elm-lang.org/packages/stil4m/elm-syntax/7.2.1/Elm-Syntax-Import)
|
||||
(`import Html as H exposing (div)`) in order of their definition, collect data
|
||||
@ -3933,6 +3957,7 @@ type alias RunnableProjectVisitor projectContext moduleContext =
|
||||
|
||||
type alias RunnableModuleVisitor moduleContext =
|
||||
{ moduleDefinitionVisitors : List (Visitor Module moduleContext)
|
||||
, moduleDocumentationVisitors : List (Maybe (Node String) -> moduleContext -> ( List (Error {}), moduleContext ))
|
||||
, commentsVisitors : List (List (Node String) -> moduleContext -> ( List (Error {}), moduleContext ))
|
||||
, importVisitors : List (Visitor Import moduleContext)
|
||||
, declarationListVisitors : List (List (Node Declaration) -> moduleContext -> ( List (Error {}), moduleContext ))
|
||||
@ -4496,6 +4521,8 @@ visitModuleForProjectRule : RunnableModuleVisitor moduleContext -> moduleContext
|
||||
visitModuleForProjectRule schema initialContext module_ =
|
||||
( [], initialContext )
|
||||
|> accumulateWithListOfVisitors schema.moduleDefinitionVisitors module_.ast.moduleDefinition
|
||||
-- TODO When `elm-syntax` integrates the module documentation by default, then we should use that instead of this.
|
||||
|> accumulateModuleDocumentationVisitor schema.moduleDocumentationVisitors module_.ast
|
||||
|> accumulateWithListOfVisitors schema.commentsVisitors module_.ast.comments
|
||||
|> accumulateList schema.importVisitors module_.ast.imports
|
||||
|> accumulateWithListOfVisitors schema.declarationListVisitors module_.ast.declarations
|
||||
@ -4982,6 +5009,59 @@ accumulateWithListOfVisitors visitors element initialErrorsAndContext =
|
||||
visitors
|
||||
|
||||
|
||||
accumulateModuleDocumentationVisitor :
|
||||
List (Maybe (Node String) -> context -> ( List (Error {}), context ))
|
||||
-> Elm.Syntax.File.File
|
||||
-> ( List (Error {}), context )
|
||||
-> ( List (Error {}), context )
|
||||
accumulateModuleDocumentationVisitor visitors ast initialErrorsAndContext =
|
||||
if List.isEmpty visitors then
|
||||
initialErrorsAndContext
|
||||
|
||||
else
|
||||
let
|
||||
cutOffLine : Int
|
||||
cutOffLine =
|
||||
case ast.imports of
|
||||
firstImport :: _ ->
|
||||
(Node.range firstImport).start.row
|
||||
|
||||
[] ->
|
||||
case ast.declarations of
|
||||
firstDeclaration :: _ ->
|
||||
(Node.range firstDeclaration).start.row
|
||||
|
||||
[] ->
|
||||
-- Should not happen, as every module should have at least one declaration
|
||||
0
|
||||
|
||||
moduleDocumentation : Maybe (Node String)
|
||||
moduleDocumentation =
|
||||
findModuleDocumentation cutOffLine ast.comments
|
||||
in
|
||||
List.foldl
|
||||
(\visitor errorsAndContext -> accumulate (visitor moduleDocumentation) errorsAndContext)
|
||||
initialErrorsAndContext
|
||||
visitors
|
||||
|
||||
|
||||
findModuleDocumentation : Int -> List (Node String) -> Maybe (Node String)
|
||||
findModuleDocumentation cutOffLine comments =
|
||||
case comments of
|
||||
[] ->
|
||||
Nothing
|
||||
|
||||
((Node range content) as comment) :: restOfComments ->
|
||||
if range.start.row > cutOffLine then
|
||||
Nothing
|
||||
|
||||
else if String.startsWith "{-|" content then
|
||||
Just comment
|
||||
|
||||
else
|
||||
findModuleDocumentation cutOffLine restOfComments
|
||||
|
||||
|
||||
accumulateList : List (a -> context -> ( List (Error {}), context )) -> List a -> ( List (Error {}), context ) -> ( List (Error {}), context )
|
||||
accumulateList visitor elements errorAndContext =
|
||||
List.foldl (visitWithListOfVisitors visitor) errorAndContext elements
|
||||
|
@ -104,7 +104,7 @@ rule configuration =
|
||||
Rule.newModuleRuleSchema "Docs.NoMissing" initialContext
|
||||
|> Rule.withElmJsonModuleVisitor elmJsonVisitor
|
||||
|> Rule.withModuleDefinitionVisitor (moduleDefinitionVisitor configuration.from)
|
||||
|> Rule.withCommentsVisitor commentsVisitor
|
||||
|> Rule.withModuleDocumentationVisitor moduleDocumentationVisitor
|
||||
|> Rule.withDeclarationEnterVisitor (declarationVisitor configuration.document)
|
||||
|> Rule.fromModuleRuleSchema
|
||||
|
||||
@ -268,18 +268,13 @@ collectExposing node =
|
||||
|
||||
|
||||
|
||||
-- COMMENTS VISITOR
|
||||
-- MODULE DOCUMENTATION VISITOR
|
||||
|
||||
|
||||
commentsVisitor : List (Node String) -> Context -> ( List (Error {}), Context )
|
||||
commentsVisitor comments context =
|
||||
moduleDocumentationVisitor : Maybe (Node String) -> Context -> ( List (Rule.Error {}), Context )
|
||||
moduleDocumentationVisitor moduleDocumentation context =
|
||||
if context.shouldBeReported then
|
||||
let
|
||||
documentation : Maybe (Node String)
|
||||
documentation =
|
||||
findFirst (Node.value >> String.startsWith "{-|") comments
|
||||
in
|
||||
( checkModuleDocumentation documentation context.moduleNameNode
|
||||
( checkModuleDocumentation moduleDocumentation context.moduleNameNode
|
||||
, context
|
||||
)
|
||||
|
||||
@ -287,20 +282,6 @@ commentsVisitor comments context =
|
||||
( [], context )
|
||||
|
||||
|
||||
findFirst : (a -> Bool) -> List a -> Maybe a
|
||||
findFirst predicate list =
|
||||
case list of
|
||||
[] ->
|
||||
Nothing
|
||||
|
||||
a :: rest ->
|
||||
if predicate a then
|
||||
Just a
|
||||
|
||||
else
|
||||
findFirst predicate rest
|
||||
|
||||
|
||||
|
||||
-- DECLARATION VISITOR
|
||||
|
||||
|
@ -94,7 +94,7 @@ rule =
|
||||
Rule.newModuleRuleSchema "Docs.ReviewAtDocs" initialContext
|
||||
|> Rule.withElmJsonModuleVisitor elmJsonVisitor
|
||||
|> Rule.withModuleDefinitionVisitor moduleDefinitionVisitor
|
||||
|> Rule.withCommentsVisitor commentsVisitor
|
||||
|> Rule.withModuleDocumentationVisitor moduleDocumentationVisitor
|
||||
|> Rule.withDeclarationListVisitor (\nodes context -> ( declarationListVisitor nodes context, context ))
|
||||
|> Rule.fromModuleRuleSchema
|
||||
|
||||
@ -152,12 +152,12 @@ moduleDefinitionVisitor node context =
|
||||
|
||||
|
||||
|
||||
-- COMMENTS VISITOR
|
||||
-- MODULE DOCUMENTATION VISITOR
|
||||
|
||||
|
||||
commentsVisitor : List (Node String) -> Context -> ( List (Rule.Error {}), Context )
|
||||
commentsVisitor nodes context =
|
||||
case find (Node.value >> String.startsWith "{-|") nodes of
|
||||
moduleDocumentationVisitor : Maybe (Node String) -> Context -> ( List (Rule.Error {}), Context )
|
||||
moduleDocumentationVisitor moduleDocumentation context =
|
||||
case moduleDocumentation of
|
||||
Just (Node range comment) ->
|
||||
case String.lines comment of
|
||||
firstLine :: restOfLines ->
|
||||
@ -450,20 +450,6 @@ topLevelExposeName (Node range topLevelExpose) =
|
||||
Node range name
|
||||
|
||||
|
||||
find : (a -> Bool) -> List a -> Maybe a
|
||||
find predicate list =
|
||||
case list of
|
||||
[] ->
|
||||
Nothing
|
||||
|
||||
first :: rest ->
|
||||
if predicate first then
|
||||
Just first
|
||||
|
||||
else
|
||||
find predicate rest
|
||||
|
||||
|
||||
indexedConcatMap : (Int -> a -> List b) -> List a -> List b
|
||||
indexedConcatMap function list =
|
||||
List.foldl
|
||||
|
@ -234,7 +234,7 @@ moduleVisitor : Rule.ModuleRuleSchema schemaState ModuleContext -> Rule.ModuleRu
|
||||
moduleVisitor schema =
|
||||
schema
|
||||
|> Rule.withModuleDefinitionVisitor moduleDefinitionVisitor
|
||||
|> Rule.withCommentsVisitor commentsVisitor
|
||||
|> Rule.withModuleDocumentationVisitor moduleDocumentationVisitor
|
||||
|> Rule.withDeclarationListVisitor declarationListVisitor
|
||||
|
||||
|
||||
@ -351,37 +351,34 @@ exposedName node =
|
||||
|
||||
|
||||
|
||||
-- COMMENTS VISITOR
|
||||
-- MODULE DOCUMENTATION VISITOR
|
||||
|
||||
|
||||
commentsVisitor : List (Node String) -> ModuleContext -> ( List nothing, ModuleContext )
|
||||
commentsVisitor comments context =
|
||||
moduleDocumentationVisitor : Maybe (Node String) -> ModuleContext -> ( List (Rule.Error {}), ModuleContext )
|
||||
moduleDocumentationVisitor moduleDocumentation context =
|
||||
let
|
||||
docs : List (Node String)
|
||||
docs =
|
||||
List.filter (Node.value >> String.startsWith "{-|") comments
|
||||
|
||||
sectionsAndLinks : List { titleSections : List SectionWithRange, links : List MaybeExposedLink }
|
||||
sectionsAndLinks : { titleSections : List SectionWithRange, links : List MaybeExposedLink }
|
||||
sectionsAndLinks =
|
||||
List.map
|
||||
(\doc ->
|
||||
case moduleDocumentation of
|
||||
Just (Node range content) ->
|
||||
findSectionsAndLinks
|
||||
context.moduleName
|
||||
context.isModuleExposed
|
||||
{ content = Node.value doc, startRow = (Node.range doc).start.row }
|
||||
)
|
||||
docs
|
||||
{ content = content, startRow = range.start.row }
|
||||
|
||||
Nothing ->
|
||||
{ titleSections = [], links = [] }
|
||||
in
|
||||
( []
|
||||
, { isModuleExposed = context.isModuleExposed
|
||||
, exposedElements = context.exposedElements
|
||||
, moduleName = context.moduleName
|
||||
, commentSections = List.concatMap .titleSections sectionsAndLinks
|
||||
, commentSections = sectionsAndLinks.titleSections
|
||||
, sections =
|
||||
List.append
|
||||
(List.concatMap (.titleSections >> List.map removeRangeFromSection) sectionsAndLinks)
|
||||
(List.map removeRangeFromSection sectionsAndLinks.titleSections)
|
||||
context.sections
|
||||
, links = List.append (List.concatMap .links sectionsAndLinks) context.links
|
||||
, links = List.append sectionsAndLinks.links context.links
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -202,7 +202,7 @@ foldProjectContexts newContext previousContext =
|
||||
moduleVisitor : StableConfiguration -> Rule.ModuleRuleSchema schemaState ModuleContext -> Rule.ModuleRuleSchema { schemaState | hasAtLeastOneVisitor : () } ModuleContext
|
||||
moduleVisitor configuration schema =
|
||||
schema
|
||||
|> Rule.withCommentsVisitor (\comments context -> ( [], commentsVisitor configuration comments context ))
|
||||
|> Rule.withModuleDocumentationVisitor (\moduleDocumentation context -> ( [], moduleDocumentationVisitor configuration moduleDocumentation context ))
|
||||
|> Rule.withDeclarationListVisitor (\nodes context -> ( [], declarationListVisitor configuration nodes context ))
|
||||
|> Rule.withDeclarationEnterVisitor (\node context -> ( declarationVisitor configuration node context, context ))
|
||||
|> Rule.withExpressionEnterVisitor (\node context -> ( expressionVisitor configuration node context, context ))
|
||||
@ -485,13 +485,13 @@ registerDeprecatedThings (StableConfiguration configuration) module_ acc =
|
||||
}
|
||||
|
||||
|
||||
commentsVisitor : StableConfiguration -> List (Node String) -> ModuleContext -> ModuleContext
|
||||
commentsVisitor (StableConfiguration configuration) comments moduleContext =
|
||||
moduleDocumentationVisitor : StableConfiguration -> Maybe (Node String) -> ModuleContext -> ModuleContext
|
||||
moduleDocumentationVisitor (StableConfiguration configuration) maybeModuleDocumentation moduleContext =
|
||||
if moduleContext.isModuleDeprecated then
|
||||
moduleContext
|
||||
|
||||
else
|
||||
case find (\(Node _ comment) -> String.startsWith "{-|" comment) comments of
|
||||
case maybeModuleDocumentation of
|
||||
Just (Node _ moduleDocumentation) ->
|
||||
{ moduleContext | isModuleDeprecated = configuration.documentationPredicate moduleDocumentation }
|
||||
|
||||
@ -998,23 +998,3 @@ error origin range =
|
||||
, details = details
|
||||
}
|
||||
range
|
||||
|
||||
|
||||
{-| Find the first element that satisfies a predicate and return
|
||||
Just that element. If none match, return Nothing.
|
||||
|
||||
find (\num -> num > 5) [ 2, 4, 6, 8 ] == Just 6
|
||||
|
||||
-}
|
||||
find : (a -> Bool) -> List a -> Maybe a
|
||||
find predicate list =
|
||||
case list of
|
||||
[] ->
|
||||
Nothing
|
||||
|
||||
first :: rest ->
|
||||
if predicate first then
|
||||
Just first
|
||||
|
||||
else
|
||||
find predicate rest
|
||||
|
@ -71,7 +71,7 @@ moduleVisitor : Rule.ModuleRuleSchema {} ModuleContext -> Rule.ModuleRuleSchema
|
||||
moduleVisitor schema =
|
||||
schema
|
||||
|> Rule.withModuleDefinitionVisitor (\project context -> ( [], moduleDefinitionVisitor project context ))
|
||||
|> Rule.withCommentsVisitor (\project context -> ( [], commentsVisitor project context ))
|
||||
|> Rule.withModuleDocumentationVisitor (\project context -> ( [], moduleDocumentationVisitor project context ))
|
||||
|> Rule.withImportVisitor (\project context -> ( [], importVisitor project context ))
|
||||
|> Rule.withDeclarationListVisitor (\project context -> ( [], declarationListVisitor project context ))
|
||||
|> Rule.withExpressionEnterVisitor (\project context -> ( [], expressionVisitor project context ))
|
||||
@ -358,21 +358,21 @@ moduleDefinitionVisitor moduleNode moduleContext =
|
||||
{ moduleContext | rawExposed = list }
|
||||
|
||||
|
||||
commentsVisitor : List (Node String) -> ModuleContext -> ModuleContext
|
||||
commentsVisitor nodes moduleContext =
|
||||
moduleDocumentationVisitor : Maybe (Node String) -> ModuleContext -> ModuleContext
|
||||
moduleDocumentationVisitor maybeModuleDocumentation moduleContext =
|
||||
if List.isEmpty moduleContext.rawExposed then
|
||||
moduleContext
|
||||
|
||||
else
|
||||
let
|
||||
comments : List ( Int, String )
|
||||
comments =
|
||||
case List.Extra.find (\(Node _ comment) -> String.startsWith "{-|" comment) nodes of
|
||||
Just (Node range comment) ->
|
||||
docsReferences : List ( Int, String )
|
||||
docsReferences =
|
||||
case maybeModuleDocumentation of
|
||||
Just (Node range moduleDocumentation) ->
|
||||
let
|
||||
lines : List String
|
||||
lines =
|
||||
comment
|
||||
moduleDocumentation
|
||||
|> String.lines
|
||||
|> List.drop 1
|
||||
in
|
||||
@ -392,12 +392,12 @@ commentsVisitor nodes moduleContext =
|
||||
[]
|
||||
in
|
||||
{ moduleContext
|
||||
| exposed = collectExposedElements comments moduleContext.rawExposed
|
||||
| exposed = collectExposedElements docsReferences moduleContext.rawExposed
|
||||
}
|
||||
|
||||
|
||||
collectExposedElements : List ( Int, String ) -> List (Node Exposing.TopLevelExpose) -> Dict String ExposedElement
|
||||
collectExposedElements comments nodes =
|
||||
collectExposedElements docsReferences nodes =
|
||||
let
|
||||
listWithPreviousRange : List (Maybe Range)
|
||||
listWithPreviousRange =
|
||||
@ -424,7 +424,7 @@ collectExposedElements comments nodes =
|
||||
Just
|
||||
( name
|
||||
, { range = untilEndOfVariable name range
|
||||
, rangesToRemove = getRangesToRemove comments nodes name index maybePreviousRange range nextRange
|
||||
, rangesToRemove = getRangesToRemove docsReferences nodes name index maybePreviousRange range nextRange
|
||||
, elementType = Function
|
||||
}
|
||||
)
|
||||
@ -433,7 +433,7 @@ collectExposedElements comments nodes =
|
||||
Just
|
||||
( name
|
||||
, { range = untilEndOfVariable name range
|
||||
, rangesToRemove = getRangesToRemove comments nodes name index maybePreviousRange range nextRange
|
||||
, rangesToRemove = getRangesToRemove docsReferences nodes name index maybePreviousRange range nextRange
|
||||
, elementType = TypeOrTypeAlias
|
||||
}
|
||||
)
|
||||
|
@ -29,20 +29,22 @@ all =
|
||||
|> Rule.withDependenciesModuleVisitor (\_ context -> context ++ "\n3.2 - withDependenciesModuleVisitor")
|
||||
|> Rule.withModuleDefinitionVisitor (\_ context -> ( [], context ++ "\n4.1 - withModuleDefinitionVisitor" ))
|
||||
|> Rule.withModuleDefinitionVisitor (\_ context -> ( [], context ++ "\n4.2 - withModuleDefinitionVisitor" ))
|
||||
|> Rule.withCommentsVisitor (\_ context -> ( [], context ++ "\n5.1 - withCommentsVisitor" ))
|
||||
|> Rule.withCommentsVisitor (\_ context -> ( [], context ++ "\n5.2 - withCommentsVisitor" ))
|
||||
|> Rule.withImportVisitor (\_ context -> ( [], context ++ "\n6.1 - withImportVisitor" ))
|
||||
|> Rule.withImportVisitor (\_ context -> ( [], context ++ "\n6.2 - withImportVisitor" ))
|
||||
|> Rule.withDeclarationListVisitor (\_ context -> ( [], context ++ "\n7.1 - withDeclarationListVisitor" ))
|
||||
|> Rule.withDeclarationListVisitor (\_ context -> ( [], context ++ "\n7.2 - withDeclarationListVisitor" ))
|
||||
|> Rule.withDeclarationEnterVisitor (\_ context -> ( [], context ++ "\n8.1 - withDeclarationEnterVisitor" ))
|
||||
|> Rule.withDeclarationEnterVisitor (\_ context -> ( [], context ++ "\n8.2 - withDeclarationEnterVisitor" ))
|
||||
|> Rule.withDeclarationExitVisitor (\_ context -> ( [], context ++ "\n11.2 - withDeclarationExitVisitor" ))
|
||||
|> Rule.withDeclarationExitVisitor (\_ context -> ( [], context ++ "\n11.1 - withDeclarationExitVisitor" ))
|
||||
|> Rule.withExpressionEnterVisitor (\_ context -> ( [], context ++ "\n9.1 - withExpressionEnterVisitor" ))
|
||||
|> Rule.withExpressionEnterVisitor (\_ context -> ( [], context ++ "\n9.2 - withExpressionEnterVisitor" ))
|
||||
|> Rule.withExpressionExitVisitor (\_ context -> ( [], context ++ "\n10.2 - withExpressionExitVisitor" ))
|
||||
|> Rule.withExpressionExitVisitor (\_ context -> ( [], context ++ "\n10.1 - withExpressionExitVisitor" ))
|
||||
|> Rule.withModuleDocumentationVisitor (\_ context -> ( [], context ++ "\n5.1 - withModuleDocumentationVisitor" ))
|
||||
|> Rule.withModuleDocumentationVisitor (\_ context -> ( [], context ++ "\n5.2 - withModuleDocumentationVisitor" ))
|
||||
|> Rule.withCommentsVisitor (\_ context -> ( [], context ++ "\n6.1 - withCommentsVisitor" ))
|
||||
|> Rule.withCommentsVisitor (\_ context -> ( [], context ++ "\n6.2 - withCommentsVisitor" ))
|
||||
|> Rule.withImportVisitor (\_ context -> ( [], context ++ "\n7.1 - withImportVisitor" ))
|
||||
|> Rule.withImportVisitor (\_ context -> ( [], context ++ "\n7.2 - withImportVisitor" ))
|
||||
|> Rule.withDeclarationListVisitor (\_ context -> ( [], context ++ "\n8.1 - withDeclarationListVisitor" ))
|
||||
|> Rule.withDeclarationListVisitor (\_ context -> ( [], context ++ "\n8.2 - withDeclarationListVisitor" ))
|
||||
|> Rule.withDeclarationEnterVisitor (\_ context -> ( [], context ++ "\n9.1 - withDeclarationEnterVisitor" ))
|
||||
|> Rule.withDeclarationEnterVisitor (\_ context -> ( [], context ++ "\n9.2 - withDeclarationEnterVisitor" ))
|
||||
|> Rule.withDeclarationExitVisitor (\_ context -> ( [], context ++ "\n12.2 - withDeclarationExitVisitor" ))
|
||||
|> Rule.withDeclarationExitVisitor (\_ context -> ( [], context ++ "\n12.1 - withDeclarationExitVisitor" ))
|
||||
|> Rule.withExpressionEnterVisitor (\_ context -> ( [], context ++ "\n10.1 - withExpressionEnterVisitor" ))
|
||||
|> Rule.withExpressionEnterVisitor (\_ context -> ( [], context ++ "\n10.2 - withExpressionEnterVisitor" ))
|
||||
|> Rule.withExpressionExitVisitor (\_ context -> ( [], context ++ "\n11.2 - withExpressionExitVisitor" ))
|
||||
|> Rule.withExpressionExitVisitor (\_ context -> ( [], context ++ "\n11.1 - withExpressionExitVisitor" ))
|
||||
|> Rule.withFinalModuleEvaluation finalEvaluation
|
||||
|> Rule.fromModuleRuleSchema
|
||||
|
||||
@ -71,20 +73,22 @@ a = 1
|
||||
3.2 - withDependenciesModuleVisitor
|
||||
4.1 - withModuleDefinitionVisitor
|
||||
4.2 - withModuleDefinitionVisitor
|
||||
5.1 - withCommentsVisitor
|
||||
5.2 - withCommentsVisitor
|
||||
6.1 - withImportVisitor
|
||||
6.2 - withImportVisitor
|
||||
7.1 - withDeclarationListVisitor
|
||||
7.2 - withDeclarationListVisitor
|
||||
8.1 - withDeclarationEnterVisitor
|
||||
8.2 - withDeclarationEnterVisitor
|
||||
9.1 - withExpressionEnterVisitor
|
||||
9.2 - withExpressionEnterVisitor
|
||||
10.1 - withExpressionExitVisitor
|
||||
10.2 - withExpressionExitVisitor
|
||||
11.1 - withDeclarationExitVisitor
|
||||
11.2 - withDeclarationExitVisitor"""
|
||||
5.1 - withModuleDocumentationVisitor
|
||||
5.2 - withModuleDocumentationVisitor
|
||||
6.1 - withCommentsVisitor
|
||||
6.2 - withCommentsVisitor
|
||||
7.1 - withImportVisitor
|
||||
7.2 - withImportVisitor
|
||||
8.1 - withDeclarationListVisitor
|
||||
8.2 - withDeclarationListVisitor
|
||||
9.1 - withDeclarationEnterVisitor
|
||||
9.2 - withDeclarationEnterVisitor
|
||||
10.1 - withExpressionEnterVisitor
|
||||
10.2 - withExpressionEnterVisitor
|
||||
11.1 - withExpressionExitVisitor
|
||||
11.2 - withExpressionExitVisitor
|
||||
12.1 - withDeclarationExitVisitor
|
||||
12.2 - withDeclarationExitVisitor"""
|
||||
, details = [ "details" ]
|
||||
, under = "module"
|
||||
}
|
||||
@ -118,9 +122,10 @@ a = 1
|
||||
}
|
||||
]
|
||||
in
|
||||
Review.Test.run rule """module A exposing (..)
|
||||
"""module A exposing (..)
|
||||
a = 1
|
||||
"""
|
||||
|> Review.Test.run rule
|
||||
|> Review.Test.expectErrors
|
||||
[ Review.Test.error
|
||||
{ message = """
|
||||
@ -163,9 +168,10 @@ Exit A"""
|
||||
}
|
||||
]
|
||||
in
|
||||
Review.Test.run rule """module A exposing (..)
|
||||
"""module A exposing (..)
|
||||
a = 1
|
||||
"""
|
||||
|> Review.Test.run rule
|
||||
|> Review.Test.expectErrors
|
||||
[ Review.Test.error
|
||||
{ message = """
|
||||
|
@ -11,7 +11,7 @@ all =
|
||||
describe "Review.Rule.withCommentsVisitor"
|
||||
[ test "passes the list of comments in source order to the rule" <|
|
||||
\() ->
|
||||
Review.Test.run rule """port module ModuleName exposing (a)
|
||||
"""port module ModuleName exposing (a)
|
||||
{-| Module
|
||||
|
||||
@docs a
|
||||
@ -47,6 +47,7 @@ port output : Json.Encode.Value -> Cmd msg
|
||||
|
||||
-- Last comment
|
||||
"""
|
||||
|> Review.Test.run rule
|
||||
|> Review.Test.expectErrors
|
||||
[ Review.Test.error
|
||||
{ message = "comments"
|
||||
|
@ -12,12 +12,13 @@ all =
|
||||
describe "Review.Rule.withDeclarationListVisitor"
|
||||
[ test "passes the list of declarations to the rule" <|
|
||||
\() ->
|
||||
Review.Test.run rule """port module ModuleName exposing (b)
|
||||
"""port module ModuleName exposing (b)
|
||||
type A = Bar | Baz
|
||||
a_ = 1
|
||||
b_ = 2
|
||||
port output : Json.Encode.Value -> Cmd msg
|
||||
port input : (Json.Decode.Value -> msg) -> Sub msg"""
|
||||
|> Review.Test.run rule
|
||||
|> Review.Test.expectErrors
|
||||
[ Review.Test.error
|
||||
{ message = "Declarations"
|
||||
|
97
tests/Review/Rule/WithModuleDocumentationVisitorTest.elm
Normal file
97
tests/Review/Rule/WithModuleDocumentationVisitorTest.elm
Normal file
@ -0,0 +1,97 @@
|
||||
module Review.Rule.WithModuleDocumentationVisitorTest exposing (all)
|
||||
|
||||
import Elm.Syntax.Node as Node exposing (Node)
|
||||
import Review.Rule as Rule exposing (Error, Rule)
|
||||
import Review.Test
|
||||
import Test exposing (Test, describe, test)
|
||||
|
||||
|
||||
all : Test
|
||||
all =
|
||||
describe "Review.Rule.withModuleDocumentationVisitor"
|
||||
[ test "should pass Nothing if there is no module documentation" <|
|
||||
\() ->
|
||||
"""module ModuleName exposing (a)
|
||||
a = 1
|
||||
"""
|
||||
|> Review.Test.run rule
|
||||
|> Review.Test.expectNoErrors
|
||||
, 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 documentation\n-}"
|
||||
}
|
||||
]
|
||||
, test "should pass the module documentation if there is one, even if it looks like a port documentation" <|
|
||||
\() ->
|
||||
"""port module ModuleName exposing (a)
|
||||
{-| module documentation
|
||||
-}
|
||||
port a : String -> Cmd msg
|
||||
"""
|
||||
|> Review.Test.run rule
|
||||
|> Review.Test.expectErrors
|
||||
[ Review.Test.error
|
||||
{ message = "{-| module documentation\n-}"
|
||||
, details = [ "No details" ]
|
||||
, under = "{-| module documentation\n-}"
|
||||
}
|
||||
]
|
||||
, test "should not mis-categorize the documentation for a port for the module documentation (doc after import)" <|
|
||||
\() ->
|
||||
"""port module ModuleName exposing (a)
|
||||
import A
|
||||
|
||||
{-| port documentation
|
||||
-}
|
||||
port a : String -> Cmd msg
|
||||
"""
|
||||
|> Review.Test.run rule
|
||||
|> Review.Test.expectNoErrors
|
||||
, test "should not mis-categorize the documentation for a port for the module documentation (doc after a declaration)" <|
|
||||
\() ->
|
||||
"""port module ModuleName exposing (a)
|
||||
|
||||
b = 1
|
||||
|
||||
{-| port documentation
|
||||
-}
|
||||
port a : String -> Cmd msg
|
||||
"""
|
||||
|> Review.Test.run rule
|
||||
|> Review.Test.expectNoErrors
|
||||
]
|
||||
|
||||
|
||||
rule : Rule
|
||||
rule =
|
||||
Rule.newModuleRuleSchema "WithModuleDocumentationVisitorTestRule" ()
|
||||
|> Rule.withModuleDocumentationVisitor (\node () -> ( moduleDocumentationVisitor node, () ))
|
||||
|> Rule.fromModuleRuleSchema
|
||||
|
||||
|
||||
moduleDocumentationVisitor : Maybe (Node String) -> List (Error {})
|
||||
moduleDocumentationVisitor maybeModuleDocumentation =
|
||||
case maybeModuleDocumentation of
|
||||
Just moduleDocumentation ->
|
||||
[ Rule.error
|
||||
{ message = Node.value moduleDocumentation
|
||||
, details = [ "No details" ]
|
||||
}
|
||||
(Node.range moduleDocumentation)
|
||||
]
|
||||
|
||||
Nothing ->
|
||||
[]
|
Loading…
Reference in New Issue
Block a user