Warn when using directives in front of elif and case items (#1036)

This commit is contained in:
Vidar Holen 2017-10-29 15:41:37 -07:00
parent 5d9cb81008
commit 0c1e2bbd4d
1 changed files with 12 additions and 4 deletions

View File

@ -906,7 +906,7 @@ prop_readAnnotation3 = isOk readAnnotation "# shellcheck disable=SC1234 source=/
prop_readAnnotation4 = isWarning readAnnotation "# shellcheck cats=dogs disable=SC1234\n" prop_readAnnotation4 = isWarning readAnnotation "# shellcheck cats=dogs disable=SC1234\n"
prop_readAnnotation5 = isOk readAnnotation "# shellcheck disable=SC2002 # All cats are precious\n" prop_readAnnotation5 = isOk readAnnotation "# shellcheck disable=SC2002 # All cats are precious\n"
prop_readAnnotation6 = isOk readAnnotation "# shellcheck disable=SC1234 # shellcheck foo=bar\n" prop_readAnnotation6 = isOk readAnnotation "# shellcheck disable=SC1234 # shellcheck foo=bar\n"
readAnnotation = called "shellcheck annotation" $ do readAnnotation = called "shellcheck directive" $ do
try readAnnotationPrefix try readAnnotationPrefix
many1 linewhitespace many1 linewhitespace
values <- many1 (readDisable <|> readSourceOverride <|> readShellOverride <|> anyKey) values <- many1 (readDisable <|> readSourceOverride <|> readShellOverride <|> anyKey)
@ -937,7 +937,7 @@ readAnnotation = called "shellcheck annotation" $ do
forKey s p = do forKey s p = do
try $ string s try $ string s
char '=' char '=' <|> fail "Expected '=' after directive key"
value <- p value <- p
many linewhitespace many linewhitespace
return value return value
@ -1937,8 +1937,13 @@ prop_readAndOr1 = isOk readAndOr "# shellcheck disable=1\nfoo"
prop_readAndOr2 = isOk readAndOr "# shellcheck disable=1\n# lol\n# shellcheck disable=3\nfoo" prop_readAndOr2 = isOk readAndOr "# shellcheck disable=1\n# lol\n# shellcheck disable=3\nfoo"
readAndOr = do readAndOr = do
aid <- getNextId aid <- getNextId
apos <- getPosition
annotations <- readAnnotations annotations <- readAnnotations
unless (null annotations) $ optional $ do
try . lookAhead $ readKeyword
parseProblemAt apos ErrorC 1123 "ShellCheck directives are only valid in front of complete compound commands, like 'if', not e.g. individual 'elif' branches."
andOr <- withAnnotations annotations $ andOr <- withAnnotations annotations $
chainr1 readPipeline $ do chainr1 readPipeline $ do
op <- g_AND_IF <|> g_OR_IF op <- g_AND_IF <|> g_OR_IF
@ -2258,9 +2263,12 @@ readCaseList = many readCaseItem
readCaseItem = called "case item" $ do readCaseItem = called "case item" $ do
notFollowedBy2 g_Esac notFollowedBy2 g_Esac
optional $ do
try . lookAhead $ readAnnotationPrefix
parseProblem ErrorC 1124 "ShellCheck directives are only valid in front of complete commands like 'case' statements, not individual case branches."
optional g_Lparen optional g_Lparen
spacing spacing
pattern <- readPattern pattern' <- readPattern
void g_Rparen <|> do void g_Rparen <|> do
parseProblem ErrorC 1085 parseProblem ErrorC 1085
"Did you forget to move the ;; after extending this case item?" "Did you forget to move the ;; after extending this case item?"
@ -2273,7 +2281,7 @@ readCaseItem = called "case item" $ do
parseProblemAt pos ErrorC 1074 parseProblemAt pos ErrorC 1074
"Did you forget the ;; after the previous case item?" "Did you forget the ;; after the previous case item?"
readLineBreak readLineBreak
return (separator, pattern, list) return (separator, pattern', list)
readCaseSeparator = choice [ readCaseSeparator = choice [
tryToken ";;&" (const ()) >> return CaseContinue, tryToken ";;&" (const ()) >> return CaseContinue,