elm-review/tests/MiscRules/ReadmeStartsWithProjectTitle.elm

80 lines
2.3 KiB
Elm
Raw Normal View History

2020-04-03 17:22:22 +03:00
module MiscRules.ReadmeStartsWithProjectTitle exposing (rule)
import Elm.Package
import Elm.Project
2020-03-19 00:49:42 +03:00
import Elm.Syntax.Range exposing (Range)
import Review.Rule as Rule exposing (Error, Rule)
rule : Rule
rule =
2020-06-28 08:49:27 +03:00
Rule.newProjectRuleSchema "ReadmeStartsWithProjectTitle" initialProjectContext
|> Rule.withElmJsonProjectVisitor elmJsonVisitor
|> Rule.withReadmeProjectVisitor readmeVisitor
|> Rule.fromProjectRuleSchema
type alias ProjectContext =
{ projectTitle : Maybe String
}
initialProjectContext : ProjectContext
initialProjectContext =
{ projectTitle = Nothing
}
-- elm.json VISITOR
elmJsonVisitor : Maybe { a | project : Elm.Project.Project } -> ProjectContext -> ( List nothing, ProjectContext )
elmJsonVisitor maybeProject projectContext =
case maybeProject |> Maybe.map .project of
Just (Elm.Project.Package pkg) ->
( [], { projectTitle = Just <| Elm.Package.toString pkg.name } )
_ ->
( [], projectContext )
-- README VISITOR
readmeVisitor : Maybe { readmeKey : Rule.ReadmeKey, content : String } -> ProjectContext -> ( List (Error scope), ProjectContext )
readmeVisitor maybeReadme context =
2020-03-19 00:49:42 +03:00
case ( maybeReadme, context.projectTitle ) of
( Just { readmeKey, content }, Just projectName ) ->
if not <| String.startsWith ("# " ++ projectName) content then
2020-03-19 00:49:42 +03:00
let
range : Range
range =
{ start = { row = 1, column = 1 }
, end =
{ row = 1
, column =
String.lines content
|> List.head
|> Maybe.withDefault ""
|> String.length
|> (+) 1
}
}
in
( [ Rule.errorForReadme readmeKey
{ message = "TODO"
, details = [ "TODO" ]
}
range
]
, context
)
else
( [], context )
2020-03-19 00:49:42 +03:00
_ ->
( [], context )