Make SC2230 optional

This commit is contained in:
Vidar Holen 2019-12-07 16:06:34 -08:00
parent 0a4580e234
commit 0f15fa49ba
4 changed files with 37 additions and 10 deletions

View File

@ -9,6 +9,9 @@
- SC2255: Suggest using `$((..))` in `[ 2*3 -eq 6 ]` - SC2255: Suggest using `$((..))` in `[ 2*3 -eq 6 ]`
- SC2256: Warn about translated strings that are known variables - SC2256: Warn about translated strings that are known variables
### Changed
- SC2230: This check is now off by default
## v0.7.0 - 2019-07-28 ## v0.7.0 - 2019-07-28
### Added ### Added
- Precompiled binaries for macOS and Linux aarch64 - Precompiled binaries for macOS and Linux aarch64

View File

@ -275,8 +275,8 @@ Here is an example `.shellcheckrc`:
# Turn on warnings for unassigned uppercase variables # Turn on warnings for unassigned uppercase variables
enable=check-unassigned-uppercase enable=check-unassigned-uppercase
# Allow using `which` since it gives full paths and is common enough # Allow [ ! -z foo ] instead of suggesting -n
disable=SC2230 disable=SC2236
If no `.shellcheckrc` is found in any of the parent directories, ShellCheck If no `.shellcheckrc` is found in any of the parent directories, ShellCheck
will look in `~/.shellcheckrc` followed by the XDG config directory will look in `~/.shellcheckrc` followed by the XDG config directory

View File

@ -35,17 +35,18 @@ analyzeScript spec = newAnalysisResult {
arComments = arComments =
filterByAnnotation spec params . nub $ filterByAnnotation spec params . nub $
runAnalytics spec runAnalytics spec
++ runChecker params (checkers params) ++ runChecker params (checkers spec params)
} }
where where
params = makeParameters spec params = makeParameters spec
checkers params = mconcat $ map ($ params) [ checkers spec params = mconcat $ map ($ params) [
ShellCheck.Checks.Commands.checker, ShellCheck.Checks.Commands.checker spec,
ShellCheck.Checks.Custom.checker, ShellCheck.Checks.Custom.checker,
ShellCheck.Checks.ShellSupport.checker ShellCheck.Checks.ShellSupport.checker
] ]
optionalChecks = mconcat $ [ optionalChecks = mconcat $ [
ShellCheck.Analytics.optionalChecks ShellCheck.Analytics.optionalChecks,
ShellCheck.Checks.Commands.optionalChecks
] ]

View File

@ -21,7 +21,7 @@
{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleContexts #-}
-- This module contains checks that examine specific commands by name. -- This module contains checks that examine specific commands by name.
module ShellCheck.Checks.Commands (checker , ShellCheck.Checks.Commands.runTests) where module ShellCheck.Checks.Commands (checker, optionalChecks, ShellCheck.Checks.Commands.runTests) where
import ShellCheck.AST import ShellCheck.AST
import ShellCheck.ASTLib import ShellCheck.ASTLib
@ -90,13 +90,30 @@ commandChecks = [
,checkMvArguments, checkCpArguments, checkLnArguments ,checkMvArguments, checkCpArguments, checkLnArguments
,checkFindRedirections ,checkFindRedirections
,checkReadExpansions ,checkReadExpansions
,checkWhich
,checkSudoRedirect ,checkSudoRedirect
,checkSudoArgs ,checkSudoArgs
,checkSourceArgs ,checkSourceArgs
,checkChmodDashr ,checkChmodDashr
] ]
optionalChecks = map fst optionalCommandChecks
optionalCommandChecks :: [(CheckDescription, CommandCheck)]
optionalCommandChecks = [
(newCheckDescription {
cdName = "deprecate-which",
cdDescription = "Suggest 'command -v' instead of 'which'",
cdPositive = "which javac",
cdNegative = "command -v javac"
}, checkWhich)
]
optionalCheckMap = Map.fromList $ map (\(desc, check) -> (cdName desc, check)) optionalCommandChecks
prop_verifyOptionalExamples = all check optionalCommandChecks
where
check (desc, check) =
verify check (cdPositive desc)
&& verifyNot check (cdNegative desc)
buildCommandMap :: [CommandCheck] -> Map.Map CommandName (Token -> Analysis) buildCommandMap :: [CommandCheck] -> Map.Map CommandName (Token -> Analysis)
buildCommandMap = foldl' addCheck Map.empty buildCommandMap = foldl' addCheck Map.empty
where where
@ -128,8 +145,14 @@ getChecker list = Checker {
map = buildCommandMap list map = buildCommandMap list
checker :: Parameters -> Checker checker :: AnalysisSpec -> Parameters -> Checker
checker params = getChecker commandChecks checker spec params = getChecker $ commandChecks ++ optionals
where
keys = asOptionalChecks spec
optionals =
if "all" `elem` keys
then map snd optionalCommandChecks
else mapMaybe (\x -> Map.lookup x optionalCheckMap) keys
prop_checkTr1 = verify checkTr "tr [a-f] [A-F]" prop_checkTr1 = verify checkTr "tr [a-f] [A-F]"
prop_checkTr2 = verify checkTr "tr 'a-z' 'A-Z'" prop_checkTr2 = verify checkTr "tr 'a-z' 'A-Z'"