From 0c1e2bbd4dd17f2f0bf927b588e807cf37ffd768 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 29 Oct 2017 15:41:37 -0700 Subject: [PATCH] Warn when using directives in front of elif and case items (#1036) --- ShellCheck/Parser.hs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/ShellCheck/Parser.hs b/ShellCheck/Parser.hs index becb2633e..b036bb205 100644 --- a/ShellCheck/Parser.hs +++ b/ShellCheck/Parser.hs @@ -906,7 +906,7 @@ prop_readAnnotation3 = isOk readAnnotation "# shellcheck disable=SC1234 source=/ prop_readAnnotation4 = isWarning readAnnotation "# shellcheck cats=dogs disable=SC1234\n" prop_readAnnotation5 = isOk readAnnotation "# shellcheck disable=SC2002 # All cats are precious\n" prop_readAnnotation6 = isOk readAnnotation "# shellcheck disable=SC1234 # shellcheck foo=bar\n" -readAnnotation = called "shellcheck annotation" $ do +readAnnotation = called "shellcheck directive" $ do try readAnnotationPrefix many1 linewhitespace values <- many1 (readDisable <|> readSourceOverride <|> readShellOverride <|> anyKey) @@ -937,7 +937,7 @@ readAnnotation = called "shellcheck annotation" $ do forKey s p = do try $ string s - char '=' + char '=' <|> fail "Expected '=' after directive key" value <- p many linewhitespace 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" readAndOr = do aid <- getNextId + apos <- getPosition 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 $ chainr1 readPipeline $ do op <- g_AND_IF <|> g_OR_IF @@ -2258,9 +2263,12 @@ readCaseList = many readCaseItem readCaseItem = called "case item" $ do 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 spacing - pattern <- readPattern + pattern' <- readPattern void g_Rparen <|> do parseProblem ErrorC 1085 "Did you forget to move the ;; after extending this case item?" @@ -2273,7 +2281,7 @@ readCaseItem = called "case item" $ do parseProblemAt pos ErrorC 1074 "Did you forget the ;; after the previous case item?" readLineBreak - return (separator, pattern, list) + return (separator, pattern', list) readCaseSeparator = choice [ tryToken ";;&" (const ()) >> return CaseContinue,