Skip to content

Commit

Permalink
Make SC2230 optional
Browse files Browse the repository at this point in the history
  • Loading branch information
koalaman committed Dec 8, 2019
1 parent 0a4580e commit 0f15fa4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
- SC2255: Suggest using `$((..))` in `[ 2*3 -eq 6 ]`
- SC2256: Warn about translated strings that are known variables

### Changed
- SC2230: This check is now off by default

## v0.7.0 - 2019-07-28
### Added
- Precompiled binaries for macOS and Linux aarch64
Expand Down
4 changes: 2 additions & 2 deletions shellcheck.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ Here is an example `.shellcheckrc`:
# Turn on warnings for unassigned uppercase variables
enable=check-unassigned-uppercase

# Allow using `which` since it gives full paths and is common enough
disable=SC2230
# Allow [ ! -z foo ] instead of suggesting -n
disable=SC2236

If no `.shellcheckrc` is found in any of the parent directories, ShellCheck
will look in `~/.shellcheckrc` followed by the XDG config directory
Expand Down
9 changes: 5 additions & 4 deletions src/ShellCheck/Analyzer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ analyzeScript spec = newAnalysisResult {
arComments =
filterByAnnotation spec params . nub $
runAnalytics spec
++ runChecker params (checkers params)
++ runChecker params (checkers spec params)
}
where
params = makeParameters spec

checkers params = mconcat $ map ($ params) [
ShellCheck.Checks.Commands.checker,
checkers spec params = mconcat $ map ($ params) [
ShellCheck.Checks.Commands.checker spec,
ShellCheck.Checks.Custom.checker,
ShellCheck.Checks.ShellSupport.checker
]

optionalChecks = mconcat $ [
ShellCheck.Analytics.optionalChecks
ShellCheck.Analytics.optionalChecks,
ShellCheck.Checks.Commands.optionalChecks
]
31 changes: 27 additions & 4 deletions src/ShellCheck/Checks/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{-# LANGUAGE FlexibleContexts #-}

-- 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.ASTLib
Expand Down Expand Up @@ -90,13 +90,30 @@ commandChecks = [
,checkMvArguments, checkCpArguments, checkLnArguments
,checkFindRedirections
,checkReadExpansions
,checkWhich
,checkSudoRedirect
,checkSudoArgs
,checkSourceArgs
,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 = foldl' addCheck Map.empty
where
Expand Down Expand Up @@ -128,8 +145,14 @@ getChecker list = Checker {
map = buildCommandMap list


checker :: Parameters -> Checker
checker params = getChecker commandChecks
checker :: AnalysisSpec -> Parameters -> Checker
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_checkTr2 = verify checkTr "tr 'a-z' 'A-Z'"
Expand Down

0 comments on commit 0f15fa4

Please sign in to comment.