Allow specifying ranges in disable directives

This commit is contained in:
Vidar Holen 2020-09-01 16:22:15 -07:00
parent 43191fa71d
commit 58783ab3cc
5 changed files with 13 additions and 6 deletions

View File

@ -1,5 +1,6 @@
## Git
### Added
- `disable` directives can now be a range, e.g. `disable=SC3000-SC4000`
- SC2259/SC2260: Warn when redirections override pipes
- SC2261: Warn about multiple competing redirections
- SC2262/SC2263: Warn about aliases declared and used in the same parsing unit

View File

@ -232,7 +232,8 @@ Valid keys are:
**disable**
: Disables a comma separated list of error codes for the following command.
The command can be a simple command like `echo foo`, or a compound command
like a function definition, subshell block or loop.
like a function definition, subshell block or loop. A range can be
be specified with a dash, e.g. `disable=SC3000-SC4000` to exclude 3xxx.
**enable**
: Enable an optional check by name, as listed with **--list-optional**.

View File

@ -145,7 +145,7 @@ data InnerToken t =
deriving (Show, Eq, Functor, Foldable, Traversable)
data Annotation =
DisableComment Integer
DisableComment Integer Integer -- [from, to)
| EnableComment String
| SourceOverride String
| ShellOverride String

View File

@ -567,5 +567,5 @@ isAnnotationIgnoringCode code t =
T_Annotation _ anns _ -> any hasNum anns
_ -> False
where
hasNum (DisableComment ts) = code == ts
hasNum (DisableComment from to) = code >= from && code < to
hasNum _ = False

View File

@ -270,7 +270,7 @@ contextItemDisablesCode alsoCheckSourced code = disabling alsoCheckSourced
ContextAnnotation list -> any disabling' list
ContextSource _ -> not $ checkSourced
_ -> False
disabling' (DisableComment n) = code == n
disabling' (DisableComment n m) = code >= n && code < m
disabling' _ = False
@ -973,6 +973,7 @@ prop_readAnnotation3 = isOk readAnnotation "# shellcheck disable=SC1234 source=/
prop_readAnnotation4 = isWarning readAnnotation "# shellcheck cats=dogs disable=SC1234\n"
prop_readAnnotation5 = isOk readAnnotation "# shellcheck disable=SC2002 # All cats are precious\n"
prop_readAnnotation6 = isOk readAnnotation "# shellcheck disable=SC1234 # shellcheck foo=bar\n"
prop_readAnnotation7 = isOk readAnnotation "# shellcheck disable=SC1000,SC2000-SC3000,SC1001\n"
readAnnotation = called "shellcheck directive" $ do
try readAnnotationPrefix
many1 linewhitespace
@ -993,12 +994,16 @@ readAnnotationWithoutPrefix = do
key <- many1 (letter <|> char '-')
char '=' <|> fail "Expected '=' after directive key"
annotations <- case key of
"disable" -> readCode `sepBy` char ','
"disable" -> readRange `sepBy` char ','
where
readRange = do
from <- readCode
to <- choice [ char '-' *> readCode, return $ from+1 ]
return $ DisableComment from to
readCode = do
optional $ string "SC"
int <- many1 digit
return $ DisableComment (read int)
return $ read int
"enable" -> readName `sepBy` char ','
where