Warn about elseif or elsif as command names (fixes #1177)

This commit is contained in:
Vidar Holen 2018-04-27 22:23:37 -07:00
parent a1d8947297
commit 5481ccd7f7
2 changed files with 18 additions and 7 deletions

View File

@ -5,6 +5,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}`
- SC1131: Warn when using `elseif` or `elsif`
- SC1128: Warn about blanks/comments before shebang
- SC1127: Warn about C-style comments

View File

@ -1831,21 +1831,22 @@ prop_readSimpleCommand8 = isWarning readSimpleCommand "// Lol"
prop_readSimpleCommand9 = isWarning readSimpleCommand "/* Lolbert */"
prop_readSimpleCommand10 = isWarning readSimpleCommand "/**** Lolbert */"
prop_readSimpleCommand11 = isOk readSimpleCommand "/\\* foo"
prop_readSimpleCommand12 = isWarning readSimpleCommand "elsif foo"
prop_readSimpleCommand13 = isWarning readSimpleCommand "ElseIf foo"
prop_readSimpleCommand14 = isWarning readSimpleCommand "elseif[$i==2]"
readSimpleCommand = called "simple command" $ do
pos <- getPosition
id1 <- getNextId
id2 <- getNextId
prefix <- option [] readCmdPrefix
skipAnnotationAndWarn
cmd <- option Nothing $ do { f <- readCmdName; return $ Just f; }
pos <- getPosition
cmd <- option Nothing $ Just <$> readCmdName
when (null prefix && isNothing cmd) $ fail "Expected a command"
when (cStyleComment cmd) $
parseProblemAt pos ErrorC 1127 "Was this intended as a comment? Use # in sh."
case cmd of
Nothing -> return $ makeSimpleCommand id1 id2 prefix [] []
Just cmd -> do
validateCommand pos cmd
suffix <- option [] $ getParser readCmdSuffix cmd [
(["declare", "export", "local", "readonly", "typeset"], readModifierSuffix),
(["time"], readTimeSuffix),
@ -1868,11 +1869,20 @@ readSimpleCommand = called "simple command" $ do
cStyleComment cmd =
case cmd of
Just (T_NormalWord _ [T_Literal _ "//"]) -> True
Just (T_NormalWord _ (T_Literal _ "/" : T_Glob _ "*" :_)) -> True
_ -> False
validateCommand pos cmd =
case cmd of
(T_NormalWord _ [T_Literal _ "//"]) -> commentWarning pos
(T_NormalWord _ (T_Literal _ "/" : T_Glob _ "*" :_)) -> commentWarning pos
(T_NormalWord _ (T_Literal _ str:_)) -> do
let cmd = map toLower $ takeWhile isAlpha str
when (cmd `elem` ["elsif", "elseif"]) $
parseProblemAt pos ErrorC 1131 "Use 'elif' to start another branch."
_ -> return ()
commentWarning pos =
parseProblemAt pos ErrorC 1127 "Was this intended as a comment? Use # in sh."
readSource :: Monad m => SourcePos -> Token -> SCParser m Token
readSource pos t@(T_Redirecting _ _ (T_SimpleCommand _ _ (cmd:file:_))) = do