Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove empty haddocks and remove empty comments. #1459

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Empty line tests; leading, trailing & double
  • Loading branch information
philderbeast committed Aug 3, 2024
commit 25ce7567c34e0af1d258b33b4d943c6fa06b8966
46 changes: 34 additions & 12 deletions src/Hint/Comment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,36 @@ isSingleSome comm@(L (anchor -> span) _) =
commentLine :: LEpaComment -> Int
commentLine (L (anchor -> span) _) = srcLocLine $ realSrcSpanStart span

-- | Is the next line in a string of empty comments leading up to a non-emtpy
-- comment?
nextLineIsComment :: Int -> [Int] -> [Int] -> Bool
nextLineIsComment x singles somes = Just True == do
next <- find (x <) somes
pure $ [x + 1 .. next] `isInfixOf` singles
-- | Do we have two consecutive empty single comment lines?
doubleEmpty :: [Int] -> [Int] -> Bool
doubleEmpty singles somes = let empties = somes \\ singles in
0 `elem` zipWith (-) (drop 1 $ empties) empties

-- | Do we have trailing empty single comment lines?
trailingEmpty :: [Int] -> [Int] -> Bool
trailingEmpty singles somes = leadingEmpty (reverse singles) (reverse somes)

-- | Do we have leading empty single comment lines?
leadingEmpty :: [Int] -> [Int] -> Bool
leadingEmpty singles somes = let empties = somes \\ singles in
case (empties, somes) of
(_, []) -> True
([], _) -> False
(e : _, s : _) -> e < s

check :: [Int] -> [Int] -> LEpaComment -> [Idea]
check singles somes comm@(L{})
| isHaddockWhitespace comm =
if | isMultiline -> [emptyHaddockMulti comm]
| nextLineIsComment (commentLine comm) singles somes -> []
| otherwise -> [emptyHaddockSingle comm]
| leadingEmpty singles somes -> [leadingEmptyHaddockSingle comm]
| trailingEmpty singles somes -> [trailingEmptyHaddockSingle comm]
| doubleEmpty singles somes -> [doubleEmptyHaddockSingle comm]
| otherwise -> []
| isCommentWhitespace comm =
if | isMultiline -> [emptyCommentMulti comm]
| not $ nextLineIsComment (commentLine comm) singles somes -> [emptyCommentSingle comm]
| leadingEmpty singles somes -> [leadingEmptyCommentSingle comm]
| trailingEmpty singles somes -> [trailingEmptyCommentSingle comm]
| doubleEmpty singles somes -> [doubleEmptyCommentSingle comm]
| otherwise -> []
| isMultiline, null (commentText comm) = [emptyCommentMulti comm]
| isMultiline, "#" `isSuffixOf` s && not ("#" `isPrefixOf` s) = [grab "Fix pragma markup" comm $ '#':s]
Expand All @@ -101,9 +115,17 @@ isCommentWhitespace :: LEpaComment -> Bool
isCommentWhitespace comm@(L (anchor -> span) _ ) =
not (isPointRealSpan span) && isStringWhitespace (commentText comm)

emptyCommentSingle, emptyHaddockSingle :: LEpaComment -> Idea
emptyCommentSingle = emptyComment ("--" ++) "Empty single-line comment"
emptyHaddockSingle = emptyComment ("--" ++) "Empty single-line haddock"
doubleEmptyCommentSingle, doubleEmptyHaddockSingle :: LEpaComment -> Idea
doubleEmptyCommentSingle = emptyComment ("--" ++) "Double empty single-line comment"
doubleEmptyHaddockSingle = emptyComment ("--" ++) "Double empty single-line haddock"

trailingEmptyCommentSingle, trailingEmptyHaddockSingle :: LEpaComment -> Idea
trailingEmptyCommentSingle = emptyComment ("--" ++) "Trailing empty single-line comment"
trailingEmptyHaddockSingle = emptyComment ("--" ++) "Trailing empty single-line haddock"

leadingEmptyCommentSingle, leadingEmptyHaddockSingle :: LEpaComment -> Idea
leadingEmptyCommentSingle = emptyComment ("--" ++) "Leading empty single-line comment"
leadingEmptyHaddockSingle = emptyComment ("--" ++) "Leading empty single-line haddock"

emptyCommentMulti, emptyHaddockMulti :: LEpaComment -> Idea
emptyCommentMulti = emptyComment (\s -> "{-" ++ s ++ "-}") "Empty multi-line comment"
Expand Down
23 changes: 6 additions & 17 deletions tests/empty-comment.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ haddockBelowSingle = 1
-- ^

OUTPUT
tests/empty-comment-single.hs:2:1-4: Suggestion: Empty single-line haddock
tests/empty-comment-single.hs:2:1-4: Suggestion: Leading empty single-line haddock
Found:
-- |
Perhaps you should remove it.

tests/empty-comment-single.hs:6:1-4: Suggestion: Empty single-line haddock
tests/empty-comment-single.hs:6:1-4: Suggestion: Leading empty single-line haddock
Found:
-- ^
Perhaps you should remove it.
Expand All @@ -34,18 +34,7 @@ haddockAboveSingles = 1
haddockAboveDoctest = 1

OUTPUT
tests/empty-comment-singles.hs:2:1-4: Suggestion: Empty single-line haddock
Found:
-- |
Perhaps you should remove it.

tests/empty-comment-singles.hs:3:1-2: Suggestion: Empty single-line comment
Found:
--
Perhaps you should remove it.

2 hints

No hints

---------------------------------------------------------------------
RUN tests/empty-comment-multi.hs
Expand Down Expand Up @@ -178,17 +167,17 @@ data HaddockMulti = HaddockMulti
}

OUTPUT
tests/empty-comment-record.hs:4:3-6: Suggestion: Empty single-line haddock
tests/empty-comment-record.hs:4:3-6: Suggestion: Leading empty single-line haddock
Found:
-- ^
Perhaps you should remove it.

tests/empty-comment-record.hs:5:3-6: Suggestion: Empty single-line haddock
tests/empty-comment-record.hs:5:3-6: Suggestion: Leading empty single-line haddock
Found:
-- |
Perhaps you should remove it.

tests/empty-comment-record.hs:7:35-38: Suggestion: Empty single-line haddock
tests/empty-comment-record.hs:7:35-38: Suggestion: Leading empty single-line haddock
Found:
-- ^
Perhaps you should remove it.
Expand Down