@eslint-sh/eslint-plugin 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -2
- package/dist/api.d.ts +1 -1
- package/dist/command-harvest/harvest-commands.d.ts +5 -3
- package/dist/command-harvest/harvest-commands.js +34 -12
- package/dist/eslint-plugin-api.d.ts +25 -11
- package/dist/language/command-resolution.d.ts +16 -6
- package/dist/language/command-resolution.js +44 -17
- package/dist/language/shell-source-code.d.ts +18 -10
- package/dist/language/shell-source-code.js +24 -25
- package/dist/language/word-resolution.d.ts +18 -12
- package/dist/language/word-resolution.js +133 -3
- package/dist/rules/compat/bash-builtin-destination.d.ts +2 -0
- package/dist/rules/compat/bash-builtin-destination.js +4 -0
- package/dist/rules/compat/declaration-options.d.ts +2 -2
- package/dist/rules/compat/declaration-options.js +23 -17
- package/dist/rules/compat/no-case-attributes.js +2 -4
- package/dist/rules/compat/no-mapfile.js +16 -19
- package/dist/rules/compat/no-negative-array-index.d.ts +3 -8
- package/dist/rules/compat/no-negative-array-index.js +7 -30
- package/dist/rules/compat/no-nounset-empty-array.js +10 -7
- package/dist/rules/compat/no-parameter-transformation.js +2 -1
- package/dist/rules/compat/no-printf-time-format.js +38 -16
- package/dist/rules/compat/no-source.d.ts +3 -8
- package/dist/rules/compat/no-source.js +3 -40
- package/dist/rules/compat/no-test-v.js +243 -61
- package/dist/rules/compat/no-wait-n.js +43 -12
- package/dist/rules/compat/nounset-array-proof.d.ts +3 -1
- package/dist/rules/compat/nounset-array-proof.js +84 -63
- package/dist/rules/compat/posix-absent-command.js +3 -2
- package/dist/rules/compat/shopt-options.js +3 -2
- package/dist/rules/create-wrapper-rule.js +16 -9
- package/dist/rules/no-dynamic-invocation.js +8 -9
- package/dist/rules/no-eval.js +19 -8
- package/dist/rules/no-masked-exit-status.js +29 -17
- package/dist/rules/no-secret-flags.js +4 -3
- package/dist/rules/no-unchecked-cd.d.ts +8 -3
- package/dist/rules/no-unchecked-cd.js +54 -25
- package/dist/rules/no-unused-declarations.js +51 -19
- package/dist/rules/prefer-local.js +136 -16
- package/dist/rules/require-strict-mode.d.ts +2 -2
- package/dist/rules/require-strict-mode.js +27 -18
- package/dist/rules/require-tty-guard.d.ts +2 -0
- package/dist/rules/require-tty-guard.js +197 -45
- package/dist/rules/set-options.d.ts +3 -1
- package/dist/rules/set-options.js +5 -1
- package/dist/rules/strict-mode-prologue.d.ts +9 -2
- package/dist/rules/strict-mode-prologue.js +45 -8
- package/docs/custom-rule-tutorial.md +10 -7
- package/docs/error-codes.md +4 -0
- package/docs/examples/no-curl-in-functions-rule.mjs +7 -5
- package/docs/rules/README.md +4 -4
- package/docs/rules/compat/no-mapfile.md +11 -20
- package/docs/rules/compat/no-negative-array-index.md +9 -22
- package/docs/rules/compat/no-nounset-empty-array.md +5 -5
- package/docs/rules/compat/no-printf-time-format.md +2 -1
- package/docs/rules/compat/no-source.md +8 -17
- package/docs/rules/compat/no-test-v.md +7 -1
- package/docs/rules/compat/no-wait-n.md +3 -1
- package/docs/rules/no-dynamic-invocation.md +3 -0
- package/docs/rules/no-eval.md +22 -1
- package/docs/rules/no-masked-exit-status.md +2 -1
- package/docs/rules/no-secret-flags.md +22 -2
- package/docs/rules/no-unchecked-cd.md +2 -1
- package/docs/rules/no-undeclared-commands.md +1 -1
- package/docs/rules/no-unused-declarations.md +10 -6
- package/docs/rules/prefer-local.md +4 -0
- package/docs/rules/require-network-wrapper.md +1 -1
- package/docs/rules/require-strict-mode.md +19 -11
- package/docs/rules/require-tty-guard.md +26 -4
- package/docs/wrapper-rules.md +1 -1
- package/package.json +4 -4
|
@@ -23,6 +23,7 @@ const noCurlInFunctions = defineShellRule({
|
|
|
23
23
|
schema: [],
|
|
24
24
|
messages: {
|
|
25
25
|
curlInFunction: 'Function `{{name}}` runs `curl`; fetch data before calling it.',
|
|
26
|
+
curlInUnnamedFunction: 'This function runs `curl`; fetch data before calling it.',
|
|
26
27
|
},
|
|
27
28
|
},
|
|
28
29
|
create(context) {
|
|
@@ -38,11 +39,12 @@ const noCurlInFunctions = defineShellRule({
|
|
|
38
39
|
// function that directly contains this command.
|
|
39
40
|
const fn = site.context.findLast((frame) => frame.kind === 'function');
|
|
40
41
|
if (fn) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
messageId: 'curlInFunction',
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
const [name] = fn.names;
|
|
43
|
+
if (typeof name === 'string') {
|
|
44
|
+
context.report({ node: site.node, messageId: 'curlInFunction', data: { name } });
|
|
45
|
+
} else {
|
|
46
|
+
context.report({ node: site.node, messageId: 'curlInUnnamedFunction' });
|
|
47
|
+
}
|
|
46
48
|
}
|
|
47
49
|
}
|
|
48
50
|
},
|
package/docs/rules/README.md
CHANGED
|
@@ -26,12 +26,12 @@ See the [canonical quickstart](https://github.com/mike-north/eslint-sh/blob/main
|
|
|
26
26
|
| [`sh/compat/no-let`](./compat/no-let.md) | `posix-portable` | No | No | Disallow `let` when the target shell is POSIX sh, pointing at arithmetic expansion. |
|
|
27
27
|
| [`sh/compat/no-mapfile`](./compat/no-mapfile.md) | `bash-3.2`, `posix-portable` | No | Yes | Disallow `mapfile` / `readarray` when the target shell lacks them, suggesting a read loop. |
|
|
28
28
|
| [`sh/compat/no-nameref`](./compat/no-nameref.md) | `bash-3.2`, `posix-portable` | No | No | Disallow namerefs (`declare -n`, `local -n`) when the target shell lacks them. |
|
|
29
|
-
| [`sh/compat/no-negative-array-index`](./compat/no-negative-array-index.md) | `bash-3.2`, `posix-portable` | No |
|
|
29
|
+
| [`sh/compat/no-negative-array-index`](./compat/no-negative-array-index.md) | `bash-3.2`, `posix-portable` | No | No | Disallow negative array subscripts when the target shell lacks them. |
|
|
30
30
|
| [`sh/compat/no-nounset-empty-array`](./compat/no-nounset-empty-array.md) | None (opt-in) | No | Yes | Disallow whole-array expansions under `set -u` when the target shell aborts on an empty array, suggesting a guarded form. |
|
|
31
31
|
| [`sh/compat/no-parameter-transformation`](./compat/no-parameter-transformation.md) | `bash-3.2`, `posix-portable` | No | No | Disallow `${v@op}` transformations when the target shell lacks the operator. |
|
|
32
32
|
| [`sh/compat/no-pipe-all`](./compat/no-pipe-all.md) | `bash-3.2`, `posix-portable` | No | Yes | Disallow the stderr-merging pipe (pipe-ampersand) when the target shell lacks it, suggesting an explicit `2>&1` before the pipe. |
|
|
33
33
|
| [`sh/compat/no-printf-time-format`](./compat/no-printf-time-format.md) | `bash-3.2`, `posix-portable` | No | No | Disallow `printf`'s `%(fmt)T` date format when the target shell lacks it. |
|
|
34
|
-
| [`sh/compat/no-source`](./compat/no-source.md) | `posix-portable` | No |
|
|
34
|
+
| [`sh/compat/no-source`](./compat/no-source.md) | `posix-portable` | No | No | Disallow `source` when the target shell is POSIX sh. |
|
|
35
35
|
| [`sh/compat/no-test-v`](./compat/no-test-v.md) | `bash-3.2`, `posix-portable` | No | No | Disallow the `-v name` test (`[[ -v x ]]`, `test -v x`) when the target shell lacks it. |
|
|
36
36
|
| [`sh/compat/no-unicode-escape`](./compat/no-unicode-escape.md) | `bash-3.2`, `posix-portable` | No | No | Disallow `\u` / `\U` escapes in `$'…'` when the target shell does not decode them to UTF-8. |
|
|
37
37
|
| [`sh/compat/no-wait-n`](./compat/no-wait-n.md) | `bash-3.2`, `posix-portable` | No | No | Disallow `wait -n` when the target shell lacks it. |
|
|
@@ -48,13 +48,13 @@ See the [canonical quickstart](https://github.com/mike-north/eslint-sh/blob/main
|
|
|
48
48
|
| [`sh/quote-expansions`](./quote-expansions.md) | `recommended`, `agent-authored` | No | Yes | Require double quotes around expansions in words the shell splits and globs. |
|
|
49
49
|
| [`sh/require-network-wrapper`](./require-network-wrapper.md) | `agent-authored` | No | No | Require network commands to run through your configured wrapper. |
|
|
50
50
|
| [`sh/require-shebang`](./require-shebang.md) | `recommended`, `agent-authored` | Yes (missing shebang only) | No | Require a recognized shell interpreter on line 1. |
|
|
51
|
-
| [`sh/require-strict-mode`](./require-strict-mode.md) | `recommended`, `agent-authored` |
|
|
51
|
+
| [`sh/require-strict-mode`](./require-strict-mode.md) | `recommended`, `agent-authored` | No | Yes | Require strict-mode options near the start of the script. |
|
|
52
52
|
| [`sh/require-tty-guard`](./require-tty-guard.md) | `agent-authored` | No | Yes | Require `read`, `select` and configured interactive commands to be guarded by a TTY test or supplied input. |
|
|
53
53
|
| [`sh/shebang-matches-dialect`](./shebang-matches-dialect.md) | `recommended`, `agent-authored`, `posix-portable` | No | No | Require the shebang to name the same kind of shell (bash or POSIX) as `languageOptions.dialect`. |
|
|
54
54
|
|
|
55
55
|
`recommended` enables `no-secret-flags` and `quote-expansions` at `warn` and its other rules at
|
|
56
56
|
`error`. `agent-authored` enables every rule above except the gated `compat/*` rules, all at `error`. `compat/no-append-all-redirect`, `compat/no-mapfile`,
|
|
57
|
-
`compat/no-
|
|
57
|
+
`compat/no-nounset-empty-array`, `compat/no-pipe-all`, `no-masked-exit-status`, `no-unchecked-cd`, `quote-expansions`, `require-strict-mode` and `require-tty-guard`
|
|
58
58
|
offer editor suggestions, which you apply one at a time from your editor; the Autofix column
|
|
59
59
|
describes edits available through `eslint --fix`. `quote-expansions` suggests quoting instead of
|
|
60
60
|
fixing because only the script's author knows whether the splitting was intended.
|
|
@@ -27,33 +27,24 @@ command.
|
|
|
27
27
|
|
|
28
28
|
### Suggestion
|
|
29
29
|
|
|
30
|
-
For `mapfile -t NAME` or `readarray -t NAME
|
|
31
|
-
|
|
32
|
-
the same array the same way:
|
|
30
|
+
For a direct `mapfile -t [NAME]` or `readarray -t [NAME]` in Bash or Bats, the rule
|
|
31
|
+
can suggest a read loop when a single trailing here-string feeds stdin.
|
|
33
32
|
|
|
34
33
|
<!-- transform: {"kind": "suggestion", "dialect": "bash", "minBashVersion": "3.2"} -->
|
|
35
34
|
|
|
36
35
|
```sh
|
|
37
|
-
mapfile -t lines
|
|
36
|
+
mapfile -t lines <<< "$text"
|
|
38
37
|
# becomes
|
|
39
|
-
{ lines=(); while IFS= read -r || [ -n "$REPLY" ]; do lines+=("$REPLY"); done; }
|
|
38
|
+
{ lines=(); while IFS= read -r || [ -n "$REPLY" ]; do lines+=("$REPLY"); done; } <<< "$text"
|
|
40
39
|
```
|
|
41
40
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
- The brace group keeps the replacement a single command, so a redirection (`< in.txt`,
|
|
50
|
-
`<<< "$text"`, `< <(cmd)`) or a pipeline (`cmd | mapfile -t lines`) still feeds the loop.
|
|
51
|
-
- With no `NAME`, `mapfile` fills `MAPFILE`, and so does the loop.
|
|
52
|
-
|
|
53
|
-
Other forms are reported without a suggestion, because no loop reproduces them exactly or the
|
|
54
|
-
target has no arrays at all: `mapfile` without `-t` (each element keeps its newline), any other
|
|
55
|
-
option (`-d`, `-n`, `-O`, `-s`, `-u`, `-C`), an array name that is not static, a leading
|
|
56
|
-
assignment, a wrapper, and every file under `dialect: "posix"`.
|
|
41
|
+
The loop intentionally leaves `REPLY` set to the empty string. Use the suggestion
|
|
42
|
+
when that change is acceptable. With no array name, both forms fill `MAPFILE`.
|
|
43
|
+
The destination must be a static array name other than `REPLY`.
|
|
44
|
+
|
|
45
|
+
A here-string supplies shell text. Files, pipes, process substitutions, and
|
|
46
|
+
inherited stdin can contain NUL bytes, which `read` and `mapfile` handle differently.
|
|
47
|
+
Those inputs and leading redirections get a diagnostic without a suggestion.
|
|
57
48
|
|
|
58
49
|
## Options
|
|
59
50
|
|
|
@@ -6,35 +6,22 @@ whose target shell lacks them: Bash older than 4.3, including the Bash 3.2 that
|
|
|
6
6
|
|
|
7
7
|
| Configs | Fixable | Languages |
|
|
8
8
|
| ------- | ------- | --------- |
|
|
9
|
-
| `bash-3.2`, `posix-portable` |
|
|
9
|
+
| `bash-3.2`, `posix-portable` | not fixable | `sh/sh` |
|
|
10
10
|
|
|
11
11
|
## Rule details
|
|
12
12
|
|
|
13
13
|
Negative subscripts count back from the end of an indexed array. Bash NEWS for 4.3 introduces assigning, referencing and unsetting elements this way; Bash 4.2 already reads `${arr[-1]}`, but rejects the element length `${#arr[-1]}`, assigning and unsetting with `bad array subscript` (see the [evidence table](../../compat.md#evidence)). The rule uses 4.3, the first release where every use works, so a plain `${arr[-1]}` read at `minBashVersion: "4.2"` is reported although 4.2 accepts it. Bash 3.2 fails with `bad array subscript`.
|
|
14
14
|
|
|
15
|
-
The rule reports a subscript that is a minus sign followed by a nonzero static integer (decimal, octal, hexadecimal, or Bash `base#digits` for bases 2–64), on an expansion (`${arr[-1]}`, `${#arr[-1]}`) or on an assignment (`arr[-1]=x`). It also reports unsetting an element, `unset arr[-1]` (quoted or not, also after options such as `-v` and after `--`), which Bash NEWS for 4.3 covers
|
|
15
|
+
The rule reports a subscript that is a minus sign followed by a nonzero static integer (decimal, octal, hexadecimal, or Bash `base#digits` for bases 2–64), on an expansion (`${arr[-1]}`, `${#arr[-1]}`) or on an assignment (`arr[-1]=x`). It also reports unsetting an element, `unset arr[-1]` (quoted or not, also after options such as `-v` and after `--`), which Bash NEWS for 4.3 covers. It does not evaluate arithmetic expressions or machine-integer overflow. Invalid integer spellings such as `08`, `2#2`, or a leading-zero base prefix (`02#1`, `08#1`) are not evidence of a negative index. It does not report zero in any base (`-0`, `-00`, `-0x0`, `-2#0`, which Bash 3.2 accepts), `${v: -1}` (a substring offset, not a subscript), a computed `-$n`, or a parenthesized `-(1)`. An associative array's `-1` key is a plain string; the rule cannot tell and reports it, but associative arrays need Bash 4.0 anyway.
|
|
16
16
|
|
|
17
|
-
###
|
|
17
|
+
### Choosing a portable index
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
# becomes
|
|
26
|
-
echo "${arr[$((${#arr[@]}-1))]}"
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
Negative subscripts count back from the highest assigned index plus one, while `${#arr[@]}` counts set elements, so the two agree only for an array without holes. Other forms are reported without a suggestion.
|
|
30
|
-
|
|
31
|
-
This is a version-compat rule. Whether it reports depends only on `languageOptions`, as
|
|
32
|
-
described in [version compatibility](../../compat.md):
|
|
33
|
-
|
|
34
|
-
- `minBashVersion` unset: no Bash version constraint, so no report under `bash` or `bats`.
|
|
35
|
-
- `dialect: "bash"` (the default) or `"bats"`: reports when `minBashVersion` is older than `4.3`.
|
|
36
|
-
- `dialect: "posix"`: reports nothing in practice, because POSIX sh has no arrays, because the POSIX parser rejects it with a parse error before any rule runs.
|
|
37
|
-
- `dialect: "mksh"` or `"zsh"`: never reports.
|
|
19
|
+
A negative subscript counts back from the highest assigned index plus one.
|
|
20
|
+
For a dense zero-based array, that value equals the array length, so
|
|
21
|
+
`${arr[$((${#arr[@]}-1))]}` is equivalent to `${arr[-1]}`. For a sparse array
|
|
22
|
+
such as `arr=([0]=first [9]=last)`, the length is two and the last assigned
|
|
23
|
+
index is nine. The rule cannot prove density and leaves the choice of a
|
|
24
|
+
replacement index to the author.
|
|
38
25
|
|
|
39
26
|
## Options
|
|
40
27
|
|
|
@@ -19,16 +19,16 @@ The rule considers an expansion when both of these hold:
|
|
|
19
19
|
|
|
20
20
|
The rule suppresses a report only when a narrow local proof establishes safety:
|
|
21
21
|
|
|
22
|
-
- a preceding
|
|
23
|
-
- an ordinary assignment such as `a=(x)` or `a=("")` guarantees a member. `FUNCNAME` is excluded because it does not obey ordinary assignment semantics.
|
|
22
|
+
- a preceding `set +u` or `set +o nounset` leaves nounset off. A later word with a fixed non-option prefix and one field ends option processing;
|
|
23
|
+
- an ordinary assignment such as `a=(x)` or `a=("")` guarantees a member. `FUNCNAME` is excluded because it does not obey ordinary assignment semantics. At least one element must be guaranteed to yield a field, and the assignment must preserve interpreter state. Quoted empty strings and locale-dependent ANSI-C quotes count; an unmatched glob with `nullglob` can supply zero fields;
|
|
24
24
|
- `[ "${#a[@]}" -eq 0 ] && return` exits the current function on the empty path, or the same zero-length check uses `exit`. `test` and `[[ ... ]]` spellings are also recognized. With `[` or `test`, the length operand must be an ordinary double-quoted word: unquoted counts can split under a changed `IFS`, making the test fail without proving the array non-empty. An optional literal numeric return/exit status is allowed;
|
|
25
25
|
- the expansion is in the then arm of a single positive length test: `[ "${#a[@]}" -gt 0 ]`, `[[ ${#a[@]} -ne 0 ]]`, or `(( ${#a[@]} ))`, for example.
|
|
26
26
|
|
|
27
|
-
Facts stay in one sequential statement list. A simple assignment invalidates the fact for its target, so `a=(x); a=()` still reports. Static `set` commands apply option changes in order, so re-enabling nounset removes the off proof. Other calls, including function calls, `unset`, and `shopt`, and compound statements discard facts. Assignments and option changes in a function, loop, branch, or subshell do not establish facts in the surrounding list. A loop's word list can use facts from immediately before the loop; its body starts afresh. Redirections, background statements, prefix assignments on the expanding command, arithmetic expansions, indirect expansions, localized strings, or other potentially mutating argument expansions prevent a proof.
|
|
27
|
+
Facts stay in one sequential statement list. A simple assignment invalidates the fact for its target, so `a=(x); a=()` still reports. Static `set` commands apply option changes in order, so re-enabling nounset removes the off proof. An inert `test` or `[` with proven single-field operands preserves facts. Other calls, including function calls, `unset`, and `shopt`, and compound statements discard facts. Assignments and option changes in a function, loop, branch, or subshell do not establish facts in the surrounding list. A loop's word list can use facts from immediately before the loop; its body starts afresh. Redirections, background statements, prefix assignments on the expanding command, arithmetic expansions, indirect expansions, localized strings, or other potentially mutating argument expansions prevent a proof.
|
|
28
28
|
|
|
29
29
|
A top-level `return` is not an exit proof: when the script is executed rather than sourced, Bash rejects it and continues to the expansion. For example, `set -u; a=(); [ "${#a[@]}" -eq 0 ] && return; echo "${a[@]}"` still reports. Put that guard inside a function or use `exit` when the intended behavior is to exit the script.
|
|
30
30
|
|
|
31
|
-
The rule does not interpret function bodies at call sites, follow sourced files, or prove data returned by other commands. Dynamic command names, actual calls to `eval`, `source`, `.`, `trap`, `alias`, `unalias`, `enable`, `fc`, `read`, `mapfile`, `readarray`, `getopts`, `unset`, or `let`, indirect expansions (including key/name forms), localized strings, writes or `for`/`select` bindings to special `BASH_*` variables, the executable `PS4` trace prefix, or writable intrinsic integers (`OPTIND`, `RANDOM`, `SECONDS`), definitions overriding proof builtins, function names outside plain shell identifiers, and unrecognized wrapper flags disable local proofs for the file, including when reached through `builtin` or `command` wrappers. Wrapped declaration commands, and declarations using readonly, integer, associative-array or reference attributes or unmodeled arguments, disable local proofs for the file. A reference or integer variable can write interpreter arrays indirectly, so a later `set +u` is not trusted either. Ordinary `local a` and `local -a a` declarations do not block a subsequent literal assignment proof. Unmodeled arithmetic (including numeric `[[ ... ]]` operands, array indices, slices and `let`), `[[ -v ... ]]`, and coprocesses also disable local proofs because they can evaluate variable text or change special arrays. Direct array lengths and literal nonnegative integers remain supported in arithmetic tests. A `printf` call is considered inert only with a static format made of literal text, `%%`, and the simple string conversions `%s`, `%b`, `%q`, or `%c`; `-v` requires a plain ordinary variable name. Other formats, interpreter-backed or unknown destinations and `%n` can write interpreter state, so they disable local proofs, including through wrappers. Command-based `[` and `test` forms require fixed non-name-lookup operators and operands
|
|
31
|
+
The rule does not interpret function bodies at call sites, follow sourced files, or prove data returned by other commands. Dynamic command names, actual calls to `eval`, `source`, `.`, `trap`, `alias`, `unalias`, `enable`, `fc`, `read`, `mapfile`, `readarray`, `getopts`, `unset`, or `let`, indirect expansions (including key/name forms), localized strings, writes or `for`/`select` bindings to special `BASH_*` variables, the executable `PS4` trace prefix, or writable intrinsic integers (`OPTIND`, `RANDOM`, `SECONDS`), definitions overriding proof builtins, function names outside plain shell identifiers, and unrecognized wrapper flags disable local proofs for the file, including when reached through `builtin` or `command` wrappers. Wrapped declaration commands, and declarations using readonly, integer, associative-array or reference attributes or unmodeled arguments, disable local proofs for the file. A reference or integer variable can write interpreter arrays indirectly, so a later `set +u` is not trusted either. Ordinary `local a` and `local -a a` declarations do not block a subsequent literal assignment proof. Unmodeled arithmetic (including numeric `[[ ... ]]` operands, array indices, slices and `let`), `[[ -v ... ]]`, and coprocesses also disable local proofs because they can evaluate variable text or change special arrays. Direct array lengths and literal nonnegative integers remain supported in arithmetic tests. A `printf` call is considered inert only with a static format made of literal text, `%%`, and the simple string conversions `%s`, `%b`, `%q`, or `%c`; `-v` requires a plain ordinary variable name. Other formats, interpreter-backed or unknown destinations and `%n` can write interpreter state, so they disable local proofs, including through wrappers. Command-based `[` and `test` forms require fixed non-name-lookup operators and operands guaranteed to produce one field. `-v`/`-R`, unknown operators, multiword expansions and unmodeled compound tests disable local proofs: their argument parsing can reach arithmetic variable-name evaluation. These restrictions avoid trusting an assignment that can fail, write through a reference, or run under changed interpreter behavior. Ordinary builtin behavior and ordinary variables are assumed when the file contains no evidence to the contrary; this is not a model of arbitrary startup or sourced-caller state.
|
|
32
32
|
|
|
33
33
|
Consequently, guarded code can still report where the proof would require another execution path, an intervening call, a caller contract, or one of the excluded constructs. The message says the expansion is not proven safe and remains conditional on the array actually being empty and nounset being on.
|
|
34
34
|
|
|
@@ -39,7 +39,7 @@ The rule offers a guarded spelling as a suggestion when the word is exactly the
|
|
|
39
39
|
- `"${a[@]}"` becomes `${a[@]+"${a[@]}"}`, which gives no words for an empty array and the same words otherwise;
|
|
40
40
|
- `"${a[*]}"` becomes `"${a[*]-}"`, which gives the one empty word that `"${a[*]}"` gives for an empty array on Bash 4.4, and the same joined word otherwise.
|
|
41
41
|
|
|
42
|
-
An unquoted expansion,
|
|
42
|
+
An unquoted expansion, one inside a longer word, or an expansion whose local proof is blocked solely by locale or tilde uncertainty reports with no suggestion. POSIX sh has no arrays; under `dialect: "posix"` the parser rejects array syntax, so this rule never sees one there.
|
|
43
43
|
|
|
44
44
|
This is a version-compat rule. Whether it reports depends only on `languageOptions`, as
|
|
45
45
|
described in [version compatibility](../../compat.md):
|
|
@@ -11,7 +11,8 @@ Disallow `printf`'s `%(fmt)T` date format in scripts that must support a shell l
|
|
|
11
11
|
|
|
12
12
|
The `%(fmt)T` directive arrived in Bash 4.2 (Bash NEWS for 4.2). Bash 4.1 rejects it with ``printf: `(': invalid format character``, so the timestamp is never printed.
|
|
13
13
|
|
|
14
|
-
The rule reads `printf` commands
|
|
14
|
+
The rule reads `printf` commands, including through `builtin` or `command`. The format is the first argument after an optional `-v name` or `-vname` and an optional `--`. Bash rejects a literal invalid destination such as `-v1` before evaluating the format.
|
|
15
|
+
The rule reads the exact text or the known literal edges of a proven single format field. For locale- or tilde-dependent formats it can recognize a time directive in a literal suffix such as `$'\u00e9''%(%F)T'`. Its format scan handles `%%`, flags, width, and precision within the known text. An unresolved prefix may affect how `printf` reads the first directive in a suffix. A fully computed format such as `printf "$fmt"` provides no literal directive to check.
|
|
15
16
|
|
|
16
17
|
The portable alternative for the current time is `date '+fmt'`, which takes the same `strftime` format. `printf '%(fmt)T' N` formats an arbitrary epoch time `N`, which has no portable `date` spelling (GNU `date -d @N` and BSD `date -r N` differ), so convert those by hand.
|
|
17
18
|
|
|
@@ -4,7 +4,7 @@ Disallow the `source` builtin in scripts that target POSIX sh, where the portabl
|
|
|
4
4
|
|
|
5
5
|
| Configs | Fixable | Languages |
|
|
6
6
|
| ------- | ------- | --------- |
|
|
7
|
-
| `posix-portable` |
|
|
7
|
+
| `posix-portable` | not fixable | `sh/sh` |
|
|
8
8
|
|
|
9
9
|
## Rule details
|
|
10
10
|
|
|
@@ -23,23 +23,14 @@ A function named `source` anywhere in the file exempts a call that names it dire
|
|
|
23
23
|
|
|
24
24
|
A call through a wrapper gets no exemption and is reported. `command source ./lib.sh` skips the function and fails with status 127, as does `env source ./lib.sh`, and dash has no `builtin` at all.
|
|
25
25
|
|
|
26
|
-
The portable alternative is the `.` special built-in
|
|
26
|
+
The portable alternative is to use the POSIX `.` special built-in with a path containing `/`.
|
|
27
|
+
In dash, this changes a failed command lookup into execution of a readable shell file in the
|
|
28
|
+
current shell. The rule reports without an edit because it cannot establish that the file is
|
|
29
|
+
readable at runtime. Before replacing `source ./lib.sh` with `. ./lib.sh`, check that the file is
|
|
30
|
+
readable, the path expands to one field, and the script does not rely on extra positional arguments
|
|
31
|
+
or on the scope of a leading assignment.
|
|
27
32
|
|
|
28
|
-
-
|
|
29
|
-
- It has no leading assignment. `FOO=1 . ./lib.sh` leaves `FOO` set afterwards on a POSIX shell, while Bash outside POSIX mode scopes `FOO=1 source ./lib.sh` to that command.
|
|
30
|
-
- It has exactly one operand. dash ignores further operands of `.`, where `source` would set the positional parameters.
|
|
31
|
-
- The operand has a literal `/`, outside any expansion. With no slash, `.` searches `PATH` and does not look in the current directory.
|
|
32
|
-
|
|
33
|
-
<!-- transform: {"kind": "suggestion", "dialect": "posix"} -->
|
|
34
|
-
```sh
|
|
35
|
-
source ./lib.sh
|
|
36
|
-
# becomes
|
|
37
|
-
. ./lib.sh
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
Other forms get the report without a suggestion.
|
|
41
|
-
|
|
42
|
-
Those conditions do not cover a file that cannot be read, where the rewrite behaves differently. POSIX says a non-interactive shell aborts when [`.`](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#dot) finds no readable file. `. ./missing || echo fallback` ends the script with status 2 on dash and BusyBox ash and status 1 under `bash --posix`, and the fallback never runs. Bash outside POSIX mode returns status 1 from `source` and runs the fallback, and on dash `source ./missing || echo fallback` also runs it, because `source` is not found. To keep a fallback, test first:
|
|
33
|
+
POSIX says a non-interactive shell aborts when [`.`](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#dot) finds no readable file. `. ./missing || echo fallback` ends the script with status 2 on dash and BusyBox ash and status 1 under `bash --posix`, and the fallback never runs. Bash outside POSIX mode returns status 1 from `source` and runs the fallback, and on dash `source ./missing || echo fallback` also runs it, because `source` is not found. To keep a fallback, test first:
|
|
43
34
|
|
|
44
35
|
```sh
|
|
45
36
|
if [ -r ./lib.sh ]; then . ./lib.sh; else echo fallback; fi
|
|
@@ -17,7 +17,13 @@ The rule reports the `-v` test in two places:
|
|
|
17
17
|
- inside `[[ … ]]`, where the parser gives the test its own node;
|
|
18
18
|
- in a `test` or `[` command (also through `builtin` or `command`), where `-v` is an ordinary argument. The rule reads the arguments the way Bash's `test` does: with two arguments a leading `-v` is the operator; with three, the middle one is a binary operator if it can be one, and `! -v name` negates the test; with four, a leading `!` or surrounding parentheses peel off; anything longer goes through `test`'s own precedence rules, where `-v` is the operator only where a primary is expected and another argument follows it.
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
A `-v` used as an operand stays clean, as in `[ "$a" = -v ]`, `[ -n -v ]`, or `[[ $a == -v ]]`. A single field can still spell an operator. The rule reports a literal `-v` when its operator role is established across the possible argument spellings. Known text such as the prefix in `PATH=a:~/bin` can rule out operators. The report points to the `[[` test or the `-v` word.
|
|
21
|
+
|
|
22
|
+
In a straight-line prologue, a plain `set --` establishes that a separate `"$@"` argument
|
|
23
|
+
contributes zero fields. The proof ends at a statement that can evaluate code, including an
|
|
24
|
+
array subscript, a command substitution, a `printf -v` destination, or an opaque command.
|
|
25
|
+
In longer tests, the rule stops at an earlier operand that could make Bash reject the test.
|
|
26
|
+
Unresolved fields after a proven unary expression leave that operator's role unchanged.
|
|
21
27
|
|
|
22
28
|
The portable alternative is `[ -n "${name+set}" ]`. `${name+set}` expands to `set` when the variable is set, even to the empty string, and to nothing when it is unset, and it is safe under `set -u`. Under `dialect: "posix"` the parser does not read `[[` as a test, so only `test` and `[` report there; `[[` itself in a POSIX script is outside this rule.
|
|
23
29
|
|
|
@@ -11,7 +11,9 @@ Bash 3.2 that macOS ships as `/bin/bash`, and POSIX sh.
|
|
|
11
11
|
|
|
12
12
|
`wait -n`, which waits for the next job to finish, arrived in Bash 4.3 (Bash NEWS for 4.3). Bash 4.2 and older reject the option with `wait: -n: invalid option` and return 2 at once. A job pool that throttles itself with `wait -n` therefore stops waiting, and every job runs at the same time, with no error that stops the script.
|
|
13
13
|
|
|
14
|
-
The rule reads `wait` commands (also through `builtin` or `command`). It reports an option word, up to the first operand or `--`, that is a cluster containing `n` (`-n`, `-fn`); the report is on that word. The
|
|
14
|
+
The rule reads `wait` commands (also through `builtin` or `command`). It reports an option word, up to the first operand or `--`, that is a cluster containing `n` (`-n`, `-fn`); the report is on that word. The rule checks the option cluster and any literal destination for validity before reporting
|
|
15
|
+
`-n`. For example, Bash rejects `wait -n!` and `wait -p1 -n`. Bash 5.1 introduced `-p` with a destination variable. Both `-p var` and `-pvar` consume that name as data. When a later expansion can supply more than
|
|
16
|
+
one field, the rule keeps a finding on an already recognized `-n`. `-n` of another command (`echo -n`) and an `-n` after an operand are not read.
|
|
15
17
|
|
|
16
18
|
A script that defines its own `wait` function still reports: the rule reads the name `wait` and does not follow shell function definitions.
|
|
17
19
|
|
|
@@ -77,6 +77,9 @@ See [migration](../transparent-wrappers.md#migration).
|
|
|
77
77
|
}
|
|
78
78
|
```
|
|
79
79
|
|
|
80
|
+
Zsh's multi-name function declarations match any configured name after quote removal.
|
|
81
|
+
For example, `function one tw\o { "$@"; }` matches `allowInFunctions: ['two']`.
|
|
82
|
+
|
|
80
83
|
## Examples
|
|
81
84
|
|
|
82
85
|
### Incorrect
|
package/docs/rules/no-eval.md
CHANGED
|
@@ -45,7 +45,7 @@ The rule deliberately ignores:
|
|
|
45
45
|
- a command word that is itself dynamic (`"$cmd" args`), and command strings passed to another
|
|
46
46
|
shell (`bash -c "$x"`, `sh -c …`).
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
The rule uses the selected shell dialect when analyzing command words.
|
|
49
49
|
|
|
50
50
|
## Options
|
|
51
51
|
|
|
@@ -60,6 +60,27 @@ This rule has no options.
|
|
|
60
60
|
}
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
+
## Analysis limits
|
|
64
|
+
|
|
65
|
+
In mksh mode, the analyzer conservatively treats command arguments with unquoted
|
|
66
|
+
`=` as potentially subject to tilde expansion. Dash and default Zsh pass
|
|
67
|
+
`PATH=a:~/bin` literally in this position. The Zsh result assumes
|
|
68
|
+
`MAGIC_EQUAL_SUBST` is off.
|
|
69
|
+
|
|
70
|
+
With `dialect: 'zsh'`, this command can report `dynamicSource` even though Zsh uses the literal filename `file(.)`.
|
|
71
|
+
|
|
72
|
+
```zsh
|
|
73
|
+
unsetopt GLOB; . ./file(.)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
The plugin has no per-command shell option state for this rule. The analyzer treats the parentheses as possible glob syntax even after Zsh disables filename generation.
|
|
77
|
+
|
|
78
|
+
In Bash, the following script can report `dynamicSource`. When Bash starts in `C.UTF-8`, it reads `./é/lib.sh`. Bash parses the line before executing the assignment, so the escape can produce different bytes when the shell starts in `C`. The analyzer treats the path as locale-dependent.
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
LC_ALL=C.UTF-8; . ./$'\u00e9'/lib.sh
|
|
82
|
+
```
|
|
83
|
+
|
|
63
84
|
## Examples
|
|
64
85
|
|
|
65
86
|
### Incorrect
|
|
@@ -89,7 +89,8 @@ The suggestion is withheld where the split would not mean the same thing, and th
|
|
|
89
89
|
would read the new, empty local after the split. `export PATH="$(…):$PATH"` is safe to split,
|
|
90
90
|
because `export` creates no new scope. For every builtin it also includes the other operands of
|
|
91
91
|
the same declaration. Any mention of such a name in the value counts, because a command can read a
|
|
92
|
-
variable by name
|
|
92
|
+
variable by name. An unresolved operand can name it too, as in `local x=$(test -v "$key" && echo set)`.
|
|
93
|
+
`local x=$([ -v x ] && echo set)`, `local x=$(declare -p x)` and
|
|
93
94
|
`export A=1 B=$(printenv A)` are withheld. So can an unrelated word, as in
|
|
94
95
|
`local name=$(git config user.name)`. Under `-i` the builtin evaluates each value as arithmetic,
|
|
95
96
|
so this reaches the values without a substitution too: `local -i a=$(cmd) b=a+1` is withheld. So
|
|
@@ -25,8 +25,7 @@ recognizes two forms:
|
|
|
25
25
|
|
|
26
26
|
A value is literal only when its whole word is static. Any parameter expansion, command
|
|
27
27
|
substitution, or other dynamic part makes the value fine: `--token="$TOKEN"` and
|
|
28
|
-
`--password=$(cat f)` are the correct pattern.
|
|
29
|
-
resolution (`ShellSourceCode#resolveWordText`), so quoting that resolves to fixed text
|
|
28
|
+
`--password=$(cat f)` are the correct pattern. The plugin resolves each word in its AST position, so quoting that resolves to fixed text
|
|
30
29
|
(`"--password"='hunter2'`) is still literal.
|
|
31
30
|
|
|
32
31
|
Only command arguments are matched. Assignment values (`X="--token=abc"`, `export`, `local`),
|
|
@@ -56,6 +55,27 @@ with the `additionalPatterns` option.
|
|
|
56
55
|
Expect some false positives: a name such as `--token-type` still matches. Suppress a single false
|
|
57
56
|
positive with a disable comment that says why, or narrow the `patterns` option for your project.
|
|
58
57
|
|
|
58
|
+
## Analysis limits
|
|
59
|
+
|
|
60
|
+
With `dialect: 'zsh'`, this command can miss `secretFlag` even though Zsh passes the literal value `secret(.)`.
|
|
61
|
+
|
|
62
|
+
```zsh
|
|
63
|
+
unsetopt GLOB; printf '%s\n' --token secret(.)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The plugin has no per-command shell option state for this rule. The analyzer treats the parentheses as possible glob syntax even after Zsh disables filename generation.
|
|
67
|
+
|
|
68
|
+
In mksh mode, the analyzer conservatively treats command arguments with unquoted
|
|
69
|
+
`=` as potentially subject to tilde expansion. Dash and default Zsh pass
|
|
70
|
+
`PATH=a:~/bin` literally in this position. The Zsh result assumes
|
|
71
|
+
`MAGIC_EQUAL_SUBST` is off.
|
|
72
|
+
|
|
73
|
+
In Bash, the following script can miss `secretFlag`. When Bash starts in `C.UTF-8`, it passes the literal UTF-8 value `é`. Bash parses the line before executing the assignment, so the escape can produce different bytes when the shell starts in `C`. The analyzer treats the value as locale-dependent.
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
LC_ALL=C.UTF-8; printf '%s\n' --token $'\u00e9'
|
|
77
|
+
```
|
|
78
|
+
|
|
59
79
|
## Options
|
|
60
80
|
|
|
61
81
|
- `patterns` (`string[]`). Regular-expression sources tested against each flag's
|
|
@@ -75,7 +75,8 @@ any other function of the same file, and a `cd` in a file read under another dia
|
|
|
75
75
|
are reported as usual. `run cd x` is not reported either, as before: `run` is not one of the
|
|
76
76
|
wrappers this rule follows, so the command it sees is `run`.
|
|
77
77
|
|
|
78
|
-
**Suggestion.** The editor suggestion appends ` || exit` after the command and its redirections.
|
|
78
|
+
**Suggestion.** The editor suggestion appends ` || exit` after the command and its redirections. When the rule accounts for `errexit` and a tracked `set` has an unresolved option, the rule withholds the suggestion because `errexit` may already handle failure. The rule also withholds the suggestion when a literal or computed command name could invoke
|
|
79
|
+
`trap` with an `ERR` or `ZERR` operand. Adding `|| exit` can suppress an error trap.
|
|
79
80
|
|
|
80
81
|
<!-- transform: {"kind": "suggestion"} -->
|
|
81
82
|
|
|
@@ -59,7 +59,7 @@ the convention has a `declarationHint`. With `commentPragma`:
|
|
|
59
59
|
|
|
60
60
|
In a file without a shebang, the message says to add the line as the first line of the file
|
|
61
61
|
instead. A convention without a hint ends the message with the generic sentence
|
|
62
|
-
"Declare it with the project's declaration convention." A command run by path (`./build.sh`) is reported by that path.
|
|
62
|
+
"Declare it with the project's declaration convention." A command run by a static path (`./build.sh`) is reported by that path. When only the basename is known, as in `"$dir"/build.sh`, the rule reports the basename.
|
|
63
63
|
|
|
64
64
|
## Shared settings
|
|
65
65
|
|
|
@@ -34,6 +34,10 @@ replace them with literal commands:
|
|
|
34
34
|
(`for t in jq curl; do "$t" --version; done`), `"${tools[@]}"`, or `$(pick-tool) x`. This
|
|
35
35
|
also applies behind a wrapper (`sudo "$prog"`) or behind a wrapper flag the rule doesn't
|
|
36
36
|
recognize;
|
|
37
|
+
- an argument whose text the analyzer cannot resolve. Any command can use its arguments as
|
|
38
|
+
program names or paths unless a basename is guaranteed. For example, with
|
|
39
|
+
`find . -exec "$dir"/sample-tool \;` the rule preserves declarations for
|
|
40
|
+
`sample-tool` and paths ending in `sample-tool`, and can remove unrelated declarations;
|
|
37
41
|
- a command string run by a shell: `bash -c '…'`, `sh -c '…'`, or `env -S '…'`, also when the
|
|
38
42
|
shell or `env` is run by path (`/bin/bash -c '…'`, `/usr/bin/env -S '…'`);
|
|
39
43
|
- a shell given a command string as the argument of another command: a word after the command
|
|
@@ -66,7 +70,7 @@ replace them with literal commands:
|
|
|
66
70
|
`echo jqfilter` doesn't use `jq`.
|
|
67
71
|
- **A path to it is used.** A command or argument word written as a path also uses its basename:
|
|
68
72
|
`/usr/bin/jq` and `./bin/jq` use `jq`. A declared path, such as `./bin/jq`, is used only by
|
|
69
|
-
that exact path.
|
|
73
|
+
that exact path when the word is static. When only the basename is known, the rule keeps declared paths with that basename.
|
|
70
74
|
|
|
71
75
|
Not counted:
|
|
72
76
|
|
|
@@ -74,7 +78,7 @@ Not counted:
|
|
|
74
78
|
top-level calls after an unconditional definition of the same name when it sees no command
|
|
75
79
|
that can remove functions. It preserves possibly external dependencies when it sees `unset`
|
|
76
80
|
that may remove functions, or Zsh’s `unfunction` or `disable -f` with operands, even inside
|
|
77
|
-
another function. Bash `unset -v` only targets variables
|
|
81
|
+
another function. Bash `unset -v` only targets variables.
|
|
78
82
|
Calls before definitions, after conditional
|
|
79
83
|
definitions, or inside compound commands or functions can also refer to an external program.
|
|
80
84
|
`# uses: cd` is stale even in a
|
|
@@ -185,8 +189,8 @@ With `commentPragma` bound and `settings: { sh: { transparentWrappers: ['with-re
|
|
|
185
189
|
# uses: jq curl docker # docker is never run
|
|
186
190
|
# uses: cd # cd is a builtin, never a declared dependency
|
|
187
191
|
# uses: lint # lint is a function defined below
|
|
188
|
-
lint() { shellcheck
|
|
189
|
-
curl -s
|
|
192
|
+
lint() { shellcheck ./setup.sh; }
|
|
193
|
+
curl -s https://example.com/data.json | jq .
|
|
190
194
|
```
|
|
191
195
|
|
|
192
196
|
### Correct
|
|
@@ -195,9 +199,9 @@ curl -s "$URL" | jq .
|
|
|
195
199
|
```sh
|
|
196
200
|
#!/usr/bin/env bash
|
|
197
201
|
# uses: jq curl git sudo date with-retry
|
|
198
|
-
with-retry curl -s
|
|
202
|
+
with-retry curl -s https://example.com/data.json | jq . # curl runs through the configured wrapper
|
|
199
203
|
sudo git pull # git runs through sudo
|
|
200
|
-
|
|
204
|
+
date +%s # date runs directly
|
|
201
205
|
```
|
|
202
206
|
|
|
203
207
|
## When not to use it
|
|
@@ -43,6 +43,10 @@ What the rule does not report:
|
|
|
43
43
|
pipeline then runs in the current shell, and the rule checks it like any other statement. Earlier
|
|
44
44
|
elements are still skipped.
|
|
45
45
|
|
|
46
|
+
In mksh, `read -p` and `read -up` read from a coprocess. The rule checks these targets when a
|
|
47
|
+
straight-line launch can supply the pipe at the read. A trap or other command that could
|
|
48
|
+
start a coprocess does not establish that the pipe is available.
|
|
49
|
+
|
|
46
50
|
Each function is its own scope. A function defined inside another function is checked on its own.
|
|
47
51
|
Only the **first** assignment to a name in a function is reported, since one declaration covers the
|
|
48
52
|
rest.
|
|
@@ -24,7 +24,7 @@ when the wrapper encloses it, directly (`retry curl x`) or through transparent w
|
|
|
24
24
|
wrapped, and neither is the `curl` in `retry echo "$(curl x)"`: the command substitution is its
|
|
25
25
|
own command. See [Wrapper rules](../wrapper-rules.md) for the full matching rules.
|
|
26
26
|
|
|
27
|
-
A command run by path is matched by its basename, so `/usr/bin/curl x` and `./bin/curl x` report
|
|
27
|
+
A command run by path is matched by its known basename, including when the directory varies, so `/usr/bin/curl x` and `./bin/curl x` report
|
|
28
28
|
as `curl`. A wrapper run by path is the wrapper it names: `/usr/local/bin/retry curl x` is wrapped.
|
|
29
29
|
|
|
30
30
|
`git` counts only for its network subcommands, `fetch`, `clone`, `pull`, and `push`. git's global
|
|
@@ -5,7 +5,7 @@ Require a script to turn on its strict-mode shell options (`set -euo pipefail` b
|
|
|
5
5
|
|
|
6
6
|
| Configs | Fixable | Languages |
|
|
7
7
|
| ------- | ------- | --------- |
|
|
8
|
-
| `recommended`, `agent-authored` |
|
|
8
|
+
| `recommended`, `agent-authored` | suggestions | `sh/sh` |
|
|
9
9
|
|
|
10
10
|
## Rule details
|
|
11
11
|
|
|
@@ -47,12 +47,16 @@ How the rule reads the file:
|
|
|
47
47
|
an argument's value is only known at run time (`set -e "$flags"`), the rule stops reading that
|
|
48
48
|
`set` command there. Options turned on before that argument still count.
|
|
49
49
|
|
|
50
|
+
The rule counts option enables from a plain `set` without redirections. Even a descriptor
|
|
51
|
+
close can fail if the shell needs another descriptor to restore the original. A redirected
|
|
52
|
+
`set` can still disable an option when it runs.
|
|
53
|
+
|
|
50
54
|
The report is placed at line 1, column 1 and lists the required options that are not on.
|
|
51
55
|
|
|
52
|
-
**
|
|
56
|
+
**Suggestion.** An unresolved `set` operand in the prologue, such as `set "$opts"`, may already set the required options. The rule withholds the edit in that case. The suggestion inserts the prologue on its own line right after the shebang, or at line 1 when
|
|
53
57
|
the file has no shebang:
|
|
54
58
|
|
|
55
|
-
<!-- transform: {"kind": "
|
|
59
|
+
<!-- transform: {"kind": "suggestion"} -->
|
|
56
60
|
|
|
57
61
|
```sh
|
|
58
62
|
#!/bin/bash
|
|
@@ -63,10 +67,14 @@ set -euo pipefail
|
|
|
63
67
|
cd "$1"
|
|
64
68
|
```
|
|
65
69
|
|
|
66
|
-
|
|
67
|
-
|
|
70
|
+
Applying the suggestion changes the script's shell options. With the defaults,
|
|
71
|
+
a failed command or pipeline may stop the script, and expanding an unset variable
|
|
72
|
+
can abort under `nounset`. Apply it when those effects are intended.
|
|
73
|
+
|
|
74
|
+
The prologue uses the body’s line ending. With CRLF, the suggestion adds a trailing `#` so the shell
|
|
75
|
+
reads the carriage return as comment text. The suggestion is
|
|
68
76
|
offered only when inserting the prologue would satisfy the rule. For example, `set -e` followed by
|
|
69
|
-
`set +e` in the window is reported with no
|
|
77
|
+
`set +e` in the window is reported with no suggestion, because the `set +e` would turn `errexit` off
|
|
70
78
|
again. Decide what that `set +e` is for, then edit the file by hand.
|
|
71
79
|
|
|
72
80
|
### Dialects
|
|
@@ -79,7 +87,7 @@ When `requiredOptions` is not configured, the required prologue depends on `lang
|
|
|
79
87
|
| `posix` | `set -eu` |
|
|
80
88
|
|
|
81
89
|
`pipefail` became part of POSIX only in POSIX.1-2024, and older `dash` and BusyBox `ash` builds
|
|
82
|
-
still reject `set -o pipefail`.
|
|
90
|
+
still reject `set -o pipefail`. Inserting it would break a working `#!/bin/sh` script
|
|
83
91
|
on those shells, so under `posix` the default requires and inserts only `set -eu`. A `posix` file
|
|
84
92
|
may still turn `pipefail` on; the rule just doesn't require it.
|
|
85
93
|
|
|
@@ -98,11 +106,11 @@ This rule takes one options object:
|
|
|
98
106
|
- `requiredOptions` (`string[]`, dialect-aware default above): a nonempty list of unique
|
|
99
107
|
long option names from the supported `set -o` domain (`errexit`, `nounset`, `pipefail`,
|
|
100
108
|
`noclobber`, …). ESLint rejects strings, empty lists, duplicates, unknown names and short
|
|
101
|
-
aliases as configuration errors. The
|
|
109
|
+
aliases as configuration errors. The suggestion builds a `set` command from this list:
|
|
102
110
|
options with a one-letter flag go into one cluster in list order, the first long-only option
|
|
103
111
|
follows that cluster with `o`, and any others follow as `-o <name>`.
|
|
104
112
|
|
|
105
|
-
<!-- transform: {"kind": "
|
|
113
|
+
<!-- transform: {"kind": "suggestion", "options": {"requiredOptions": ["errexit", "nounset", "pipefail"]}} -->
|
|
106
114
|
|
|
107
115
|
```sh
|
|
108
116
|
echo hi
|
|
@@ -111,7 +119,7 @@ This rule takes one options object:
|
|
|
111
119
|
echo hi
|
|
112
120
|
```
|
|
113
121
|
|
|
114
|
-
<!-- transform: {"kind": "
|
|
122
|
+
<!-- transform: {"kind": "suggestion", "options": {"requiredOptions": ["errexit", "nounset"]}} -->
|
|
115
123
|
|
|
116
124
|
```sh
|
|
117
125
|
echo hi
|
|
@@ -215,7 +223,7 @@ set -eu
|
|
|
215
223
|
<!-- example: {"findings": 1, "messageIds": ["missingStrictMode"]} -->
|
|
216
224
|
```sh
|
|
217
225
|
#!/bin/bash
|
|
218
|
-
# reported: the set +e in the window turns errexit off again (no
|
|
226
|
+
# reported: the set +e in the window turns errexit off again (no suggestion)
|
|
219
227
|
set -euo pipefail
|
|
220
228
|
set +e
|
|
221
229
|
```
|