Merge branch 'slycordinator-leading_X'

This commit is contained in:
Vidar Holen 2025-07-17 08:41:07 -07:00
commit 2ae0aeaff9

View file

@ -4318,14 +4318,16 @@ prop_checkComparisonWithLeadingX3 = verifyNot checkComparisonWithLeadingX "[ $fo
prop_checkComparisonWithLeadingX4 = verifyNot checkComparisonWithLeadingX "test $foo = xbar" prop_checkComparisonWithLeadingX4 = verifyNot checkComparisonWithLeadingX "test $foo = xbar"
prop_checkComparisonWithLeadingX5 = verify checkComparisonWithLeadingX "[ \"x$foo\" = 'xlol' ]" prop_checkComparisonWithLeadingX5 = verify checkComparisonWithLeadingX "[ \"x$foo\" = 'xlol' ]"
prop_checkComparisonWithLeadingX6 = verify checkComparisonWithLeadingX "[ x\"$foo\" = x'lol' ]" prop_checkComparisonWithLeadingX6 = verify checkComparisonWithLeadingX "[ x\"$foo\" = x'lol' ]"
prop_checkComparisonWithLeadingX7 = verify checkComparisonWithLeadingX "[ X$foo != Xbar ]"
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
check lhs rhs | op `elem` ["=", "==", "!="] ->
T_SimpleCommand _ _ [cmd, lhs, op, rhs] | check lhs rhs
getLiteralString cmd == Just "test" && T_SimpleCommand _ _ [cmd, lhs, op, rhs]
getLiteralString op `elem` [Just "=", Just "=="] -> | getLiteralString cmd == Just "test" &&
check lhs rhs getLiteralString op `elem` [Just "=", Just "==", Just "!="] ->
check lhs rhs
_ -> return () _ -> return ()
where where
msg = "Avoid x-prefix in comparisons as it no longer serves a purpose." msg = "Avoid x-prefix in comparisons as it no longer serves a purpose."
@ -4335,19 +4337,20 @@ checkComparisonWithLeadingX params t =
return $ styleWithFix (getId lhs) 2268 msg $ fixWith [l, r] return $ styleWithFix (getId lhs) 2268 msg $ fixWith [l, r]
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\""