2020-04-03 17:22:22 +03:00
|
|
|
module MiscRules.ReadmeStartsWithProjectTitle exposing (rule)
|
2020-03-18 22:32:31 +03:00
|
|
|
|
|
|
|
import Elm.Package
|
|
|
|
import Elm.Project
|
2020-03-19 00:49:42 +03:00
|
|
|
import Elm.Syntax.Range exposing (Range)
|
2020-03-18 22:32:31 +03:00
|
|
|
import Review.Rule as Rule exposing (Error, Rule)
|
2020-06-20 10:16:35 +03:00
|
|
|
import Review.Rule3 as Rule3
|
2020-03-18 22:32:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
rule : Rule
|
|
|
|
rule =
|
2020-06-20 10:16:35 +03:00
|
|
|
Rule3.newProjectRuleSchema "ReadmeStartsWithProjectTitle" initialProjectContext
|
|
|
|
|> Rule3.withElmJsonProjectVisitor elmJsonVisitor
|
|
|
|
|> Rule3.withReadmeProjectVisitor readmeVisitor
|
|
|
|
|> Rule3.fromProjectRuleSchema
|
2020-03-18 22:32:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
type alias ProjectContext =
|
|
|
|
{ projectTitle : Maybe String
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-19 19:55:44 +03:00
|
|
|
initialProjectContext : ProjectContext
|
|
|
|
initialProjectContext =
|
2020-03-18 22:32:31 +03:00
|
|
|
{ projectTitle = Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- elm.json VISITOR
|
|
|
|
|
|
|
|
|
2020-03-19 01:08:06 +03:00
|
|
|
elmJsonVisitor : Maybe { a | project : Elm.Project.Project } -> ProjectContext -> ( List nothing, ProjectContext )
|
2020-03-18 22:32:31 +03:00
|
|
|
elmJsonVisitor maybeProject projectContext =
|
|
|
|
case maybeProject |> Maybe.map .project of
|
|
|
|
Just (Elm.Project.Package pkg) ->
|
2020-03-19 01:08:06 +03:00
|
|
|
( [], { projectTitle = Just <| Elm.Package.toString pkg.name } )
|
2020-03-18 22:32:31 +03:00
|
|
|
|
|
|
|
_ ->
|
2020-03-19 01:08:06 +03:00
|
|
|
( [], projectContext )
|
2020-03-18 22:32:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- README VISITOR
|
|
|
|
|
|
|
|
|
2020-03-25 20:02:37 +03:00
|
|
|
readmeVisitor : Maybe { readmeKey : Rule.ReadmeKey, content : String } -> ProjectContext -> ( List (Error scope), ProjectContext )
|
2020-03-18 22:32:31 +03:00
|
|
|
readmeVisitor maybeReadme context =
|
2020-03-19 00:49:42 +03:00
|
|
|
case ( maybeReadme, context.projectTitle ) of
|
|
|
|
( Just { readmeKey, content }, Just projectName ) ->
|
2020-03-19 01:25:17 +03:00
|
|
|
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-18 22:32:31 +03:00
|
|
|
|
2020-03-19 00:49:42 +03:00
|
|
|
_ ->
|
2020-03-18 22:32:31 +03:00
|
|
|
( [], context )
|