Allow specifying external-sources=true in shellcheckrc (fixes #1818)

This commit is contained in:
Vidar Holen 2021-09-15 18:02:37 -07:00
parent 64733cc110
commit 4e703e5c61
12 changed files with 145 additions and 31 deletions

View file

@ -987,9 +987,9 @@ prop_readAnnotation7 = isOk readAnnotation "# shellcheck disable=SC1000,SC2000-S
readAnnotation = called "shellcheck directive" $ do
try readAnnotationPrefix
many1 linewhitespace
readAnnotationWithoutPrefix
readAnnotationWithoutPrefix True
readAnnotationWithoutPrefix = do
readAnnotationWithoutPrefix sandboxed = do
values <- many1 readKey
optional readAnyComment
void linefeed <|> eof <|> do
@ -1035,6 +1035,21 @@ readAnnotationWithoutPrefix = do
"This shell type is unknown. Use e.g. sh or bash."
return [ShellOverride shell]
"external-sources" -> do
pos <- getPosition
value <- many1 letter
case value of
"true" ->
if sandboxed
then do
parseNoteAt pos ErrorC 1144 "external-sources can only be enabled in .shellcheckrc, not in individual files."
return []
else return [ExternalSources True]
"false" -> return [ExternalSources False]
_ -> do
parseNoteAt pos ErrorC 1145 "Unknown external-sources value. Expected true/false."
return []
_ -> do
parseNoteAt keyPos WarningC 1107 "This directive is unknown. It will be ignored."
anyChar `reluctantlyTill` whitespace
@ -2176,10 +2191,12 @@ readSource t@(T_Redirecting _ _ (T_SimpleCommand cmdId _ (cmd:file':rest'))) = d
if filename == "/dev/null" -- always allow /dev/null
then return (Right "", filename)
else do
allAnnotations <- getCurrentAnnotations True
currentScript <- Mr.asks currentFilename
paths <- mapMaybe getSourcePath <$> getCurrentAnnotations True
resolved <- system $ siFindSource sys currentScript paths filename
contents <- system $ siReadFile sys resolved
let paths = mapMaybe getSourcePath allAnnotations
let externalSources = listToMaybe $ mapMaybe getExternalSources allAnnotations
resolved <- system $ siFindSource sys currentScript externalSources paths filename
contents <- system $ siReadFile sys externalSources resolved
return (contents, resolved)
case input of
Left err -> do
@ -2213,6 +2230,11 @@ readSource t@(T_Redirecting _ _ (T_SimpleCommand cmdId _ (cmd:file':rest'))) = d
SourcePath x -> Just x
_ -> Nothing
getExternalSources t =
case t of
ExternalSources b -> Just b
_ -> Nothing
-- If the word has a single expansion as the directory, try stripping it
-- This affects `$foo/bar` but not `${foo}-dir/bar` or `/foo/$file`
stripDynamicPrefix word =
@ -3202,7 +3224,7 @@ prop_readConfigKVs4 = isOk readConfigKVs "\n\n\n\n\t \n"
prop_readConfigKVs5 = isOk readConfigKVs "# shellcheck accepts annotation-like comments in rc files\ndisable=1234"
readConfigKVs = do
anySpacingOrComment
annotations <- many (readAnnotationWithoutPrefix <* anySpacingOrComment)
annotations <- many (readAnnotationWithoutPrefix False <* anySpacingOrComment)
eof
return $ concat annotations
anySpacingOrComment =