mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-08-08 22:21:04 +08:00
Recognize more invalid shebangs
This commit is contained in:
@@ -2569,7 +2569,13 @@ prop_readShebang2 = isWarning readShebang "!# /bin/sh\n"
|
|||||||
prop_readShebang3 = isNotOk readShebang "#shellcheck shell=/bin/sh\n"
|
prop_readShebang3 = isNotOk readShebang "#shellcheck shell=/bin/sh\n"
|
||||||
prop_readShebang4 = isWarning readShebang "! /bin/sh"
|
prop_readShebang4 = isWarning readShebang "! /bin/sh"
|
||||||
readShebang = do
|
readShebang = do
|
||||||
try readCorrect <|> try readSwapped <|> try readMissingHash
|
choice $ map try [
|
||||||
|
readCorrect,
|
||||||
|
readSwapped,
|
||||||
|
readTooManySpaces,
|
||||||
|
readMissingHash,
|
||||||
|
readMissingBang
|
||||||
|
]
|
||||||
many linewhitespace
|
many linewhitespace
|
||||||
str <- many $ noneOf "\r\n"
|
str <- many $ noneOf "\r\n"
|
||||||
optional carriageReturn
|
optional carriageReturn
|
||||||
@@ -2577,21 +2583,47 @@ readShebang = do
|
|||||||
return str
|
return str
|
||||||
where
|
where
|
||||||
readCorrect = void $ string "#!"
|
readCorrect = void $ string "#!"
|
||||||
|
|
||||||
readSwapped = do
|
readSwapped = do
|
||||||
pos <- getPosition
|
pos <- getPosition
|
||||||
string "!#"
|
string "!#"
|
||||||
parseProblemAt pos ErrorC 1084
|
parseProblemAt pos ErrorC 1084
|
||||||
"Use #!, not !#, for the shebang."
|
"Use #!, not !#, for the shebang."
|
||||||
|
|
||||||
|
skipSpaces = liftM (not . null) $ many linewhitespace
|
||||||
|
readTooManySpaces = do
|
||||||
|
startPos <- getPosition
|
||||||
|
startSpaces <- skipSpaces
|
||||||
|
char '#'
|
||||||
|
middlePos <- getPosition
|
||||||
|
middleSpaces <- skipSpaces
|
||||||
|
char '!'
|
||||||
|
when startSpaces $
|
||||||
|
parseProblemAt startPos ErrorC 1114
|
||||||
|
"Remove leading spaces before the shebang."
|
||||||
|
when middleSpaces $
|
||||||
|
parseProblemAt middlePos ErrorC 1115
|
||||||
|
"Remove spaces between # and ! in the shebang."
|
||||||
|
|
||||||
readMissingHash = do
|
readMissingHash = do
|
||||||
pos <- getPosition
|
pos <- getPosition
|
||||||
char '!'
|
char '!'
|
||||||
lookAhead $ do
|
ensurePathAhead
|
||||||
many linewhitespace
|
|
||||||
char '/'
|
|
||||||
parseProblemAt pos ErrorC 1104
|
parseProblemAt pos ErrorC 1104
|
||||||
"Use #!, not just !, for the shebang."
|
"Use #!, not just !, for the shebang."
|
||||||
|
|
||||||
|
readMissingBang = do
|
||||||
|
char '#'
|
||||||
|
pos <- getPosition
|
||||||
|
ensurePathAhead
|
||||||
|
parseProblemAt pos ErrorC 1113
|
||||||
|
"Use #!, not just #, for the shebang."
|
||||||
|
|
||||||
|
|
||||||
|
ensurePathAhead = lookAhead $ do
|
||||||
|
many linewhitespace
|
||||||
|
char '/'
|
||||||
|
|
||||||
verifyEof = eof <|> choice [
|
verifyEof = eof <|> choice [
|
||||||
ifParsable g_Lparen $
|
ifParsable g_Lparen $
|
||||||
parseProblem ErrorC 1088 "Parsing stopped here. Invalid use of parentheses?",
|
parseProblem ErrorC 1088 "Parsing stopped here. Invalid use of parentheses?",
|
||||||
|
Reference in New Issue
Block a user