mirror of
https://github.com/koalaman/shellcheck
synced 2025-08-20 21:33:50 -07:00
x-prefix: add support for "!=" and X (capital x)
Changes checkComparisonWithLeadingX to: 1) Work with the "!=" operator in addition to "=" and "==". 2) Support prefixing with "x" and "X". This is helpful since some scripts have comparisons like [ "X$var" = "X" ] and the like
This commit is contained in:
parent
20d11c1c33
commit
08329b0698
1 changed files with 15 additions and 13 deletions
|
@ -4320,11 +4320,12 @@ prop_checkComparisonWithLeadingX5 = verify checkComparisonWithLeadingX "[ \"x$fo
|
||||||
prop_checkComparisonWithLeadingX6 = verify checkComparisonWithLeadingX "[ x\"$foo\" = x'lol' ]"
|
prop_checkComparisonWithLeadingX6 = verify checkComparisonWithLeadingX "[ x\"$foo\" = x'lol' ]"
|
||||||
checkComparisonWithLeadingX params t =
|
checkComparisonWithLeadingX params t =
|
||||||
case t of
|
case t of
|
||||||
TC_Binary id typ op lhs rhs | op == "=" || op == "==" ->
|
TC_Binary id typ op lhs rhs
|
||||||
|
| op `elem` ["=", "==", "!="] ->
|
||||||
check lhs rhs
|
check lhs rhs
|
||||||
T_SimpleCommand _ _ [cmd, lhs, op, rhs] |
|
T_SimpleCommand _ _ [cmd, lhs, op, rhs]
|
||||||
getLiteralString cmd == Just "test" &&
|
| getLiteralString cmd == Just "test" &&
|
||||||
getLiteralString op `elem` [Just "=", Just "=="] ->
|
getLiteralString op `elem` [Just "=", Just "==", Just "!="] ->
|
||||||
check lhs rhs
|
check lhs rhs
|
||||||
_ -> return ()
|
_ -> return ()
|
||||||
where
|
where
|
||||||
|
@ -4336,18 +4337,19 @@ checkComparisonWithLeadingX params t =
|
||||||
|
|
||||||
fixLeadingX token =
|
fixLeadingX token =
|
||||||
case getWordParts token of
|
case getWordParts token of
|
||||||
T_Literal id ('x':_):_ ->
|
T_Literal id (c:_):_ | toLower c == 'x' ->
|
||||||
case token of
|
case token of
|
||||||
-- The side is a single, unquoted x, so we have to quote
|
-- The side is a single, unquoted x or X, so we have to quote
|
||||||
T_NormalWord _ [T_Literal id "x"] ->
|
T_NormalWord _ [T_Literal id [c]] ->
|
||||||
return $ replaceStart id params 1 "\"\""
|
return $ replaceStart id params 1 "\"\""
|
||||||
-- Otherwise we can just delete it
|
-- Otherwise we can just delete it
|
||||||
_ -> return $ replaceStart id params 1 ""
|
_ -> return $ replaceStart id params 1 ""
|
||||||
T_SingleQuoted id ('x':_):_ ->
|
T_SingleQuoted id (c:rest):_ | toLower c == 'x' ->
|
||||||
-- Replace the single quote and x
|
-- Replace the single quote and the character x or X
|
||||||
return $ replaceStart id params 2 "'"
|
return $ replaceStart id params 2 "'"
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
|
|
||||||
|
|
||||||
prop_checkAssignToSelf1 = verify checkAssignToSelf "x=$x"
|
prop_checkAssignToSelf1 = verify checkAssignToSelf "x=$x"
|
||||||
prop_checkAssignToSelf2 = verify checkAssignToSelf "x=${x}"
|
prop_checkAssignToSelf2 = verify checkAssignToSelf "x=${x}"
|
||||||
prop_checkAssignToSelf3 = verify checkAssignToSelf "x=\"$x\""
|
prop_checkAssignToSelf3 = verify checkAssignToSelf "x=\"$x\""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue