Improve warnings on backslashes in comments

This commit is contained in:
Vidar Holen 2021-03-20 13:58:37 -07:00
parent a277efdbb1
commit 98952df35b
2 changed files with 18 additions and 7 deletions

View File

@ -1,6 +1,7 @@
## Git ## Git
### Added ### Added
- `disable` directives can now be a range, e.g. `disable=SC3000-SC4000` - `disable` directives can now be a range, e.g. `disable=SC3000-SC4000`
- SC1143: Warn about line continuations in comments
- SC2259/SC2260: Warn when redirections override pipes - SC2259/SC2260: Warn when redirections override pipes
- SC2261: Warn about multiple competing redirections - SC2261: Warn about multiple competing redirections
- SC2262/SC2263: Warn about aliases declared and used in the same parsing unit - SC2262/SC2263: Warn about aliases declared and used in the same parsing unit

View File

@ -87,11 +87,23 @@ extglobStart = oneOf extglobStartChars
unicodeDoubleQuotes = "\x201C\x201D\x2033\x2036" unicodeDoubleQuotes = "\x201C\x201D\x2033\x2036"
unicodeSingleQuotes = "\x2018\x2019" unicodeSingleQuotes = "\x2018\x2019"
prop_spacing = isOk spacing " \\\n # Comment" prop_spacing1 = isOk spacing " \\\n # Comment"
prop_spacing2 = isOk spacing "# We can continue lines with \\"
prop_spacing3 = isWarning spacing " \\\n # --verbose=true \\"
spacing = do spacing = do
x <- many (many1 linewhitespace <|> try (string "\\\n" >> return "")) x <- many (many1 linewhitespace <|> continuation)
optional readComment optional readComment
return $ concat x return $ concat x
where
continuation = do
try (string "\\\n")
-- The line was continued. Warn if this next line is a comment with a trailing \
whitespace <- many linewhitespace
optional $ do
x <- readComment
when ("\\" `isSuffixOf` x) $
parseProblem ErrorC 1143 "This backslash is part of a comment and does not continue the line."
return whitespace
spacing1 = do spacing1 = do
spacing <- spacing spacing <- spacing
@ -1040,13 +1052,9 @@ readComment = do
readAnyComment readAnyComment
prop_readAnyComment = isOk readAnyComment "# Comment" prop_readAnyComment = isOk readAnyComment "# Comment"
prop_readAnyComment1 = not $ isOk readAnyComment "# Comment \\\n"
readAnyComment = do readAnyComment = do
char '#' char '#'
comment <- many $ noneOf "\\\r\n" many $ noneOf "\r\n"
bs <- many $ oneOf "\\"
unless (null bs) (fail "Backslash in or directly after comment")
return comment
prop_readNormalWord = isOk readNormalWord "'foo'\"bar\"{1..3}baz$(lol)" prop_readNormalWord = isOk readNormalWord "'foo'\"bar\"{1..3}baz$(lol)"
prop_readNormalWord2 = isOk readNormalWord "foo**(foo)!!!(@@(bar))" prop_readNormalWord2 = isOk readNormalWord "foo**(foo)!!!(@@(bar))"
@ -1409,6 +1417,8 @@ readNormalEscaped = called "escaped char" $ do
do do
next <- quotable <|> oneOf "?*@!+[]{}.,~#" next <- quotable <|> oneOf "?*@!+[]{}.,~#"
when (next == ' ') $ checkTrailingSpaces pos <|> return () when (next == ' ') $ checkTrailingSpaces pos <|> return ()
-- Check if this line is followed by a commented line with a trailing backslash
when (next == '\n') $ try . lookAhead $ void spacing
return $ if next == '\n' then "" else [next] return $ if next == '\n' then "" else [next]
<|> <|>
do do