提交 d3a4c985 编写于 作者: V Vidar Holen

Read any -* as binary/unary op, and warn on unknown.

上级 35b8d58c
...@@ -75,6 +75,7 @@ basicChecks = [ ...@@ -75,6 +75,7 @@ basicChecks = [
,checkQuotedCondRegex ,checkQuotedCondRegex
,checkForInCat ,checkForInCat
,checkFindExec ,checkFindExec
,checkValidCondOps
] ]
modifyMap = modify modifyMap = modify
...@@ -227,7 +228,7 @@ isMagicInQuotes _ = False ...@@ -227,7 +228,7 @@ isMagicInQuotes _ = False
prop_checkShebang1 = verifyFull checkShebang "#!/usr/bin/env bash -x\necho cow" prop_checkShebang1 = verifyFull checkShebang "#!/usr/bin/env bash -x\necho cow"
prop_checkShebang2 = verifyNotFull checkShebang "#! /bin/sh -l " prop_checkShebang2 = verifyNotFull checkShebang "#! /bin/sh -l "
checkShebang (T_Script id sb _) m = checkShebang (T_Script id sb _) m =
if (length $ words sb) > 2 then if (length $ words sb) > 2 then
let note = Note ErrorC $ "On most OS, shebangs can only specify a single parameter." let note = Note ErrorC $ "On most OS, shebangs can only specify a single parameter."
in Map.adjust (\(Metadata pos notes) -> Metadata pos (note:notes)) id m in Map.adjust (\(Metadata pos notes) -> Metadata pos (note:notes)) id m
else m else m
...@@ -514,6 +515,18 @@ checkDollarArithmeticCommand _ = return () ...@@ -514,6 +515,18 @@ checkDollarArithmeticCommand _ = return ()
allModifiedVariables t = snd $ runState (doAnalysis (\x -> modify $ (++) (getModifiedVariables x)) t) [] allModifiedVariables t = snd $ runState (doAnalysis (\x -> modify $ (++) (getModifiedVariables x)) t) []
prop_checkValidCondOps1 = verify checkValidCondOps "[[ a -xz b ]]"
prop_checkValidCondOps2 = verify checkValidCondOps "[ -M a ]"
prop_checkValidCondOps3 = verifyNot checkValidCondOps "[ 1 = 2 -a 3 -ge 4 ]"
prop_checkValidCondOps4 = verifyNot checkValidCondOps "[[ ! -v foo ]]"
checkValidCondOps (TC_Binary id _ s _ _)
| not (s `elem` ["-nt", "-ot", "-ef", "==", "!=", "<=", ">=", "-eq", "-ne", "-lt", "-le", "-gt", "-ge", "=~", ">", "<", "="]) =
warn id "Unknown binary operator."
checkValidCondOps (TC_Unary id _ s _)
| not (s `elem` [ "!", "-a", "-b", "-c", "-d", "-e", "-f", "-g", "-h", "-L", "-k", "-p", "-r", "-s", "-S", "-t", "-u", "-w", "-x", "-O", "-G", "-N", "-z", "-n", "-o", "-v", "-R"]) =
warn id "Unknown unary operator."
checkValidCondOps _ = return ()
--- Context seeking --- Context seeking
getParentTree t = getParentTree t =
......
...@@ -212,13 +212,19 @@ readConditionContents single = do ...@@ -212,13 +212,19 @@ readConditionContents single = do
where where
typ = if single then SingleBracket else DoubleBracket typ = if single then SingleBracket else DoubleBracket
readCondBinaryOp = try $ do readCondBinaryOp = try $ do
op <- choice $ (map tryOp ["-nt", "-ot", "-ef", "==", "!=", "<=", ">=", "-eq", "-ne", "-lt", "-le", "-gt", "-ge", "=~", ">", "<", "="]) id <- getNextId
op <- (choice $ (map tryOp ["==", "!=", "<=", ">=", "=~", ">", "<", "="])) <|> otherOp
hardCondSpacing hardCondSpacing
return op return op
where tryOp s = try $ do where
id <- getNextId tryOp s = try $ do
string s id <- getNextId
return $ TC_Binary id typ s string s
return $ TC_Binary id typ s
otherOp = try $ do
id <- getNextId
s <- readOp
return $ TC_Binary id typ s
readCondUnaryExp = do readCondUnaryExp = do
op <- readCondUnaryOp op <- readCondUnaryOp
...@@ -231,15 +237,15 @@ readConditionContents single = do ...@@ -231,15 +237,15 @@ readConditionContents single = do
fail "oops") fail "oops")
readCondUnaryOp = try $ do readCondUnaryOp = try $ do
op <- choice $ (map tryOp [ "-a", "-b", "-c", "-d", "-e", "-f", "-g", "-h", "-L", "-k", "-p", "-r", "-s", "-S", "-t", "-u", "-w", "-x", "-O", "-G", "-N", id <- getNextId
"-z", "-n", "-o", "-v", "-R" s <- readOp
])
hardCondSpacing hardCondSpacing
return op return $ TC_Unary id typ s
where tryOp s = try $ do
id <- getNextId readOp = try $ do
string s char '-'
return $ TC_Unary id typ s s <- many1 letter
return ('-':s)
readCondWord = do readCondWord = do
notFollowedBy2 (try (spacing >> (string "]"))) notFollowedBy2 (try (spacing >> (string "]")))
...@@ -733,7 +739,7 @@ readDollarDoubleQuote = do ...@@ -733,7 +739,7 @@ readDollarDoubleQuote = do
x <- many doubleQuotedPart x <- many doubleQuotedPart
doubleQuote <?> "end of translated double quoted string" doubleQuote <?> "end of translated double quoted string"
return $ T_DollarDoubleQuoted id x return $ T_DollarDoubleQuoted id x
prop_readDollarArithmetic = isOk readDollarArithmetic "$(( 3 * 4 +5))" prop_readDollarArithmetic = isOk readDollarArithmetic "$(( 3 * 4 +5))"
prop_readDollarArithmetic2 = isOk readDollarArithmetic "$(((3*4)+(1*2+(3-1))))" prop_readDollarArithmetic2 = isOk readDollarArithmetic "$(((3*4)+(1*2+(3-1))))"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册