elm-review/tests/NoTodoComment.elm

82 lines
2.3 KiB
Elm
Raw Normal View History

2020-03-17 01:03:19 +03:00
module NoTodoComment exposing (rule)
import Elm.Syntax.Node as Node exposing (Node)
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.newModuleRuleSchema "NoTodoComment" ()
|> Rule.withSimpleCommentsVisitor commentsVisitor
|> Rule.fromModuleRuleSchema
2020-03-17 01:03:19 +03:00
commentsVisitor : List (Node String) -> List (Error {})
2020-03-17 01:03:19 +03:00
commentsVisitor comments =
comments
|> List.concatMap
(\commentNode ->
String.indexes "TODO" (Node.value commentNode)
2020-03-18 19:35:11 +03:00
|> List.map (errorAtPosition commentNode)
2020-03-17 01:03:19 +03:00
)
errorAtPosition : Node String -> Int -> Error {}
2020-03-18 19:35:11 +03:00
errorAtPosition node index =
2020-03-17 01:03:19 +03:00
Rule.error
{ message = "TODO needs to be handled"
, details = [ "At fruits.com, we prefer not to have lingering TODO comments. Either fix the TODO now or create an issue for it." ]
}
-- Here you would ideally only target the TODO keyword
2020-03-18 01:28:47 +03:00
-- or the rest of the line it appears on,
2020-03-17 01:03:19 +03:00
-- so you would change `range` using `index`.
2020-03-18 19:35:11 +03:00
(untilEndOfLine node index)
untilEndOfLine : Node String -> Int -> Range
untilEndOfLine node index =
let
range : Range
range =
Node.range node
linesBeforeComment : List String
linesBeforeComment =
node
|> Node.value
2020-03-19 00:31:33 +03:00
|> String.left index
2020-03-18 19:35:11 +03:00
|> String.split "\n"
startColumn : Int
startColumn =
if List.length linesBeforeComment == 1 then
range.start.column + index
else
linesBeforeComment
|> List.reverse
|> List.head
|> Maybe.withDefault ""
|> String.length
|> (+) 1
endColumn : Int
endColumn =
node
|> Node.value
|> String.dropLeft index
|> String.split "\n"
|> List.head
|> Maybe.withDefault ""
|> String.length
|> (+) startColumn
startRow : Int
startRow =
range.start.row + (List.length linesBeforeComment - 1)
in
{ start = { row = startRow, column = startColumn }
, end = { row = startRow, column = endColumn }
}