From 46a3019ed758d64a4f1052b2e8c623dcd24ba4ed Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Wed, 17 Jan 2018 19:20:10 -0800 Subject: [PATCH] Fix annotations for here documents (fixes #1071) --- CHANGELOG.md | 3 +++ ShellCheck/Parser.hs | 33 ++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30a5599..775f205 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ - SC2223: Quote warning specific to `: ${var=value}` - SC1127: Warn about C-style comments +### Fixed +- Annotations intended for a command's here documents now work + ### Changed - SC1073: 'else if' is now parsed correctly and not like 'elif' diff --git a/ShellCheck/Parser.hs b/ShellCheck/Parser.hs index af9bd36..0408b38 100644 --- a/ShellCheck/Parser.hs +++ b/ShellCheck/Parser.hs @@ -143,7 +143,7 @@ data Context = deriving (Show) data HereDocContext = - HereDocPending Token -- on linefeed, read this T_HereDoc + HereDocPending Token [Context] -- on linefeed, read this T_HereDoc deriving (Show) data UserState = UserState { @@ -194,9 +194,10 @@ addToHereDocMap id list = do addPendingHereDoc t = do state <- getState + context <- getCurrentContexts let docs = pendingHereDocs state putState $ state { - pendingHereDocs = HereDocPending t : docs + pendingHereDocs = HereDocPending t context : docs } popPendingHereDocs = do @@ -205,9 +206,7 @@ popPendingHereDocs = do putState $ state { pendingHereDocs = [] } - return . map extract . reverse $ pendingHereDocs state - where - extract (HereDocPending t) = t + return . reverse $ pendingHereDocs state getMap = positionMap <$> getState getParseNotes = parseNotes <$> getState @@ -372,15 +371,16 @@ acceptButWarn parser level code note = parseProblemAt pos level code note ) -withContext entry p = do - pushContext entry - do - v <- p - popContext - return v - <|> do -- p failed without consuming input, abort context - v <- popContext - fail "" +parsecBracket before after op = do + val <- before + (op val <* optional (after val)) <|> (after val *> fail "") + +swapContext contexts p = + parsecBracket (getCurrentContexts <* setCurrentContexts contexts) + setCurrentContexts + (const p) + +withContext entry p = parsecBracket (pushContext entry) (const popContext) (const p) called s p = do pos <- getPosition @@ -1594,6 +1594,8 @@ prop_readHereDoc14= isWarning readScript "cat << foo\nbar\nfoo \n" prop_readHereDoc15= isWarning readScript "cat < rawLine `reluctantlyTill` do linewhitespace `reluctantlyTill` string endToken -- GitLab