Improve the range for NoTodoComment

This commit is contained in:
Jeroen Engels 2020-03-18 17:35:11 +01:00
parent d1e77c29bf
commit c619e2c484
3 changed files with 183 additions and 4 deletions

View File

@ -13,6 +13,7 @@ when inside the directory containing this file.
import NoDebugLog
import NoDebugTodoOrToString
import NoTodoComment
import NoUnused.CustomTypeConstructors2
import NoUnused.Variables
import NoUnusedDependencies
@ -31,5 +32,6 @@ config =
, NoUnusedDependencies.rule
, NoUnusedExports.rule
, NoUnusedModules.rule
, NoTodoComment.rule
]
|> List.map (Rule.ignoreErrorsForDirectories [ "src/Vendor/" ])

View File

@ -18,12 +18,12 @@ commentsVisitor comments =
|> List.concatMap
(\commentNode ->
String.indexes "TODO" (Node.value commentNode)
|> List.map (errorAtPosition (Node.range commentNode))
|> List.map (errorAtPosition commentNode)
)
errorAtPosition : Range -> Int -> Error
errorAtPosition range index =
errorAtPosition : Node String -> Int -> Error
errorAtPosition node index =
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." ]
@ -31,4 +31,51 @@ errorAtPosition range index =
-- Here you would ideally only target the TODO keyword
-- or the rest of the line it appears on,
-- so you would change `range` using `index`.
range
(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
|> String.slice 0 index
|> 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 }
}

130
tests/NoTodoCommentTest.elm Normal file
View File

@ -0,0 +1,130 @@
module NoTodoCommentTest exposing (all)
import NoTodoComment exposing (rule)
import Review.Test
import Test exposing (Test, describe, test)
message : String
message =
"TODO needs to be handled"
details : List String
details =
[ "At fruits.com, we prefer not to have lingering TODO comments. Either fix the TODO now or create an issue for it."
]
all : Test
all =
describe "NoTodoComment"
[ test "should not regular comments" <|
\() ->
"""
module A exposing (..)
-- Some comment
a = 1
"""
|> Review.Test.run rule
|> Review.Test.expectNoErrors
, test "should report TODO comment, and report the TODO until the end of the line" <|
\() ->
"""
module A exposing (..)
-- TODO Do this
a = 1
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
, details = details
, under = "TODO Do this"
}
]
, test "should report TODO comment, and report the TODO until the end of the line (comment is indented)" <|
\() ->
"""
module A exposing (..)
-- TODO Do this
a = 1
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
, details = details
, under = "TODO Do this"
}
]
, test "should report TODO comment, and report the TODO until the end of the line, not the following lines" <|
\() ->
"""
module A exposing (..)
-- TODO Do this
-- because there is a good reason
a = 1
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
, details = details
, under = "TODO Do this"
}
]
, test "should report TODO comment, and report the TODO until the end of the line, not the following lines (multiline comment)" <|
\() ->
"""
module A exposing (..)
{- TODO Do this
because there is a good reason
-}
a = 1
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
, details = details
, under = "TODO Do this"
}
]
, test "should report TODO comment, and report the TODO until the end of the line, not the following lines (multiline comment, TODO doesn't start at the beginning)" <|
\() ->
"""
module A exposing (..)
{- There is something
TODO Do this
because there is a good reason
-}
a = 1
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
, details = details
, under = "TODO Do this"
}
]
, test "should report TODO comment, and report the TODO until the end of the line, not the following lines (indented multiline comment)" <|
\() ->
"""
module A exposing (..)
{- There is something
TODO Do this
because there is a good reason
-}
a = 1
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = message
, details = details
, under = "TODO Do this"
}
]
]