Skip to content

Commit

Permalink
Warn about flipped $ and " in $"(cmd)" (fixes koalaman#1517)
Browse files Browse the repository at this point in the history
  • Loading branch information
koalaman committed Mar 21, 2019
1 parent c53c8a5 commit f514f5f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Preliminary support for fix suggestions
- Files containing Bats tests can now be checked
- Directory wide directives can now be placed in a `.shellcheckrc`
- SC2247: Warn about $"(cmd)" and $"{var}"
- SC2246: Warn if a shebang's interpreter ends with /
- SC2245: Warn that Ksh ignores all but the first glob result in `[`
- SC2243/SC2244: Suggest using explicit -n for `[ $foo ]`
Expand Down
14 changes: 14 additions & 0 deletions src/ShellCheck/Analytics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ nodeChecks = [
,checkInvertedStringTest
,checkRedirectionToCommand
,checkNullaryExpansionTest
,checkDollarQuoteParen
]


Expand Down Expand Up @@ -3169,5 +3170,18 @@ checkNullaryExpansionTest params t =
fix = fixWith [replaceStart id params 0 "-n "]
_ -> return ()


prop_checkDollarQuoteParen1 = verify checkDollarQuoteParen "$\"(foo)\""
prop_checkDollarQuoteParen2 = verify checkDollarQuoteParen "$\"{foo}\""
prop_checkDollarQuoteParen3 = verifyNot checkDollarQuoteParen "\"$(foo)\""
prop_checkDollarQuoteParen4 = verifyNot checkDollarQuoteParen "$\"..\""
checkDollarQuoteParen params t =
case t of
T_DollarDoubleQuoted id ((T_Literal _ (c:_)):_) | c `elem` "({" ->
warnWithFix id 2247 "Flip leading $ and \" if this should be a quoted substitution." (fix id)
_ -> return ()
where
fix id = fixWith [replaceStart id params 2 "\"$"]

return []
runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])

0 comments on commit f514f5f

Please sign in to comment.