Skip to content

Commit

Permalink
Warn about comments/blanks before shebang. Fixes #844
Browse files Browse the repository at this point in the history
  • Loading branch information
koalaman committed Jan 21, 2018
1 parent 7b3c402 commit c868854
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
- SC2227: Warn about redirections in the middle of 'find' commands
- SC2224,SC2225,SC2226: Warn when using mv/cp/ln without a destination
- SC2223: Quote warning specific to `: ${var=value}`
- SC1128: Warn about blanks/comments before shebang
- SC1127: Warn about C-style comments

### Fixed
Expand Down
30 changes: 22 additions & 8 deletions ShellCheck/Parser.hs
Expand Up @@ -2654,20 +2654,23 @@ prop_readShebang1 = isOk readShebang "#!/bin/sh\n"
prop_readShebang2 = isWarning readShebang "!# /bin/sh\n"
prop_readShebang3 = isNotOk readShebang "#shellcheck shell=/bin/sh\n"
prop_readShebang4 = isWarning readShebang "! /bin/sh"
prop_readShebang5 = isWarning readShebang "\n#!/bin/sh"
prop_readShebang6 = isWarning readShebang " # Copyright \n!#/bin/bash"
prop_readShebang7 = isNotOk readShebang "# Copyright \nfoo\n#!/bin/bash"
readShebang = do
choice $ map try [
readCorrect,
readSwapped,
readTooManySpaces,
readMissingHash,
readMissingBang
]
anyShebang <|> try readMissingBang <|> withHeader
many linewhitespace
str <- many $ noneOf "\r\n"
optional carriageReturn
optional linefeed
return str
where
anyShebang = choice $ map try [
readCorrect,
readSwapped,
readTooManySpaces,
readMissingHash
]
readCorrect = void $ string "#!"

readSwapped = do
Expand Down Expand Up @@ -2705,11 +2708,22 @@ readShebang = do
parseProblemAt pos ErrorC 1113
"Use #!, not just #, for the shebang."


ensurePathAhead = lookAhead $ do
many linewhitespace
char '/'

withHeader = try $ do
many1 headerLine
pos <- getPosition
anyShebang <*
parseProblemAt pos ErrorC 1128 "The shebang must be on the first line. Delete blanks and move comments."

headerLine = do
notFollowedBy2 anyShebang
many linewhitespace
optional readAnyComment
linefeed

verifyEof = eof <|> choice [
ifParsable g_Lparen $
parseProblem ErrorC 1088 "Parsing stopped here. Invalid use of parentheses?",
Expand Down

0 comments on commit c868854

Please sign in to comment.