@eslint-sh/eslint-plugin 0.11.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 +10 -4
- package/dist/command-harvest/harvest-commands.js +39 -10
- package/dist/eslint-plugin-api.d.ts +36 -14
- package/dist/language/command-resolution.d.ts +16 -6
- package/dist/language/command-resolution.js +44 -17
- package/dist/language/line-breaks.d.ts +10 -4
- package/dist/language/line-breaks.js +14 -5
- package/dist/language/shell-language.d.ts +2 -0
- package/dist/language/shell-language.js +10 -1
- package/dist/language/shell-source-code.d.ts +29 -13
- package/dist/language/shell-source-code.js +126 -55
- 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 +34 -46
- 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 +127 -30
- package/dist/rules/prefer-local.js +146 -52
- package/dist/rules/require-shebang.js +23 -21
- package/dist/rules/require-strict-mode.d.ts +2 -2
- package/dist/rules/require-strict-mode.js +28 -16
- package/dist/rules/require-tty-guard.d.ts +2 -0
- package/dist/rules/require-tty-guard.js +205 -50
- package/dist/rules/set-options.d.ts +5 -16
- package/dist/rules/set-options.js +17 -45
- package/dist/rules/shebang-interpreter.d.ts +33 -10
- package/dist/rules/shebang-interpreter.js +522 -53
- package/dist/rules/shebang-matches-dialect.js +4 -4
- package/dist/rules/strict-mode-prologue.d.ts +9 -2
- package/dist/rules/strict-mode-prologue.js +45 -8
- package/dist/rules/variable-reads.d.ts +12 -0
- package/dist/rules/variable-reads.js +31 -0
- package/docs/custom-rule-tutorial.md +19 -10
- package/docs/error-codes.md +6 -1
- package/docs/examples/no-curl-in-functions-rule.mjs +7 -5
- package/docs/rules/README.md +5 -5
- 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 +4 -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 +18 -9
- package/docs/rules/prefer-local.md +15 -33
- package/docs/rules/require-network-wrapper.md +1 -1
- package/docs/rules/require-shebang.md +46 -3
- package/docs/rules/require-strict-mode.md +40 -15
- package/docs/rules/require-tty-guard.md +27 -4
- package/docs/rules/shebang-matches-dialect.md +3 -0
- package/docs/shellcheck.md +1 -1
- package/docs/wrapper-rules.md +1 -1
- package/package.json +4 -4
|
@@ -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
|
|
@@ -52,8 +56,7 @@ replace them with literal commands:
|
|
|
52
56
|
|
|
53
57
|
**Used.** Otherwise, a declared name is used when any of the following holds:
|
|
54
58
|
|
|
55
|
-
- **The script runs it as a command.**
|
|
56
|
-
`sh/no-undeclared-commands` does, so a command that rule counts as used is never reported
|
|
59
|
+
- **The script runs it as a command.** A command that `sh/no-undeclared-commands` counts as used is never reported
|
|
57
60
|
here. That covers commands anywhere in the file: the top level, loops, conditionals,
|
|
58
61
|
pipelines, and command substitutions (`$(…)` and backquotes) in any position. It also covers
|
|
59
62
|
function bodies, including a function that is never called. That is deliberately cautious: the
|
|
@@ -67,12 +70,18 @@ replace them with literal commands:
|
|
|
67
70
|
`echo jqfilter` doesn't use `jq`.
|
|
68
71
|
- **A path to it is used.** A command or argument word written as a path also uses its basename:
|
|
69
72
|
`/usr/bin/jq` and `./bin/jq` use `jq`. A declared path, such as `./bin/jq`, is used only by
|
|
70
|
-
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.
|
|
71
74
|
|
|
72
75
|
Not counted:
|
|
73
76
|
|
|
74
|
-
- **Builtins and
|
|
75
|
-
|
|
77
|
+
- **Builtins and calls to earlier top-level functions.** The rule excludes builtins and direct
|
|
78
|
+
top-level calls after an unconditional definition of the same name when it sees no command
|
|
79
|
+
that can remove functions. It preserves possibly external dependencies when it sees `unset`
|
|
80
|
+
that may remove functions, or Zsh’s `unfunction` or `disable -f` with operands, even inside
|
|
81
|
+
another function. Bash `unset -v` only targets variables.
|
|
82
|
+
Calls before definitions, after conditional
|
|
83
|
+
definitions, or inside compound commands or functions can also refer to an external program.
|
|
84
|
+
`# uses: cd` is stale even in a
|
|
76
85
|
script that runs `cd`. Which names are builtins depends on `languageOptions.dialect`; see
|
|
77
86
|
[`sh/no-undeclared-commands`](./no-undeclared-commands.md#rule-details) for the lists.
|
|
78
87
|
- **Commands run on another host are not counted.** In `ssh host 'jq . f'`, the remote host
|
|
@@ -180,8 +189,8 @@ With `commentPragma` bound and `settings: { sh: { transparentWrappers: ['with-re
|
|
|
180
189
|
# uses: jq curl docker # docker is never run
|
|
181
190
|
# uses: cd # cd is a builtin, never a declared dependency
|
|
182
191
|
# uses: lint # lint is a function defined below
|
|
183
|
-
lint() { shellcheck
|
|
184
|
-
curl -s
|
|
192
|
+
lint() { shellcheck ./setup.sh; }
|
|
193
|
+
curl -s https://example.com/data.json | jq .
|
|
185
194
|
```
|
|
186
195
|
|
|
187
196
|
### Correct
|
|
@@ -190,9 +199,9 @@ curl -s "$URL" | jq .
|
|
|
190
199
|
```sh
|
|
191
200
|
#!/usr/bin/env bash
|
|
192
201
|
# uses: jq curl git sudo date with-retry
|
|
193
|
-
with-retry curl -s
|
|
202
|
+
with-retry curl -s https://example.com/data.json | jq . # curl runs through the configured wrapper
|
|
194
203
|
sudo git pull # git runs through sudo
|
|
195
|
-
|
|
204
|
+
date +%s # date runs directly
|
|
196
205
|
```
|
|
197
206
|
|
|
198
207
|
## When not to use it
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
Require variables assigned inside a function to be declared `local`, so a function does not
|
|
4
4
|
overwrite globals its callers rely on.
|
|
5
5
|
|
|
6
|
-
| Configs
|
|
7
|
-
|
|
|
8
|
-
| `agent-authored` |
|
|
6
|
+
| Configs | Fixable | Languages |
|
|
7
|
+
| --------------- | ----------- | --------- |
|
|
8
|
+
| `agent-authored` | not fixable | `sh/sh` |
|
|
9
9
|
|
|
10
10
|
## Rule details
|
|
11
11
|
|
|
@@ -36,12 +36,17 @@ What the rule does not report:
|
|
|
36
36
|
allowed.
|
|
37
37
|
- Anything in a subshell `( … )`, a command substitution `$( … )` or a process substitution, and
|
|
38
38
|
functions whose body is a subshell (`f() ( … )`): those run in a child process, so nothing leaks.
|
|
39
|
+
- Assignments in a background job, a coprocess, or a disowned job are isolated from the enclosing function.
|
|
39
40
|
- Anything in a pipeline element, such as `echo hi | read x` or `ls | while read -r line; do …; done`.
|
|
40
41
|
Pipeline elements run in subshells, so the assignment is lost rather than leaked. The exception is
|
|
41
42
|
a file that enables `shopt -s lastpipe` (anywhere in the file): the **last** element of each
|
|
42
43
|
pipeline then runs in the current shell, and the rule checks it like any other statement. Earlier
|
|
43
44
|
elements are still skipped.
|
|
44
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
|
+
|
|
45
50
|
Each function is its own scope. A function defined inside another function is checked on its own.
|
|
46
51
|
Only the **first** assignment to a name in a function is reported, since one declaration covers the
|
|
47
52
|
rest.
|
|
@@ -49,37 +54,14 @@ rest.
|
|
|
49
54
|
`local` is not part of POSIX, so under `languageOptions: { dialect: 'posix' }` the rule reports
|
|
50
55
|
nothing.
|
|
51
56
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
```sh
|
|
56
|
-
# before
|
|
57
|
-
f() {
|
|
58
|
-
out=$(compute)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
# after
|
|
62
|
-
f() {
|
|
63
|
-
local out
|
|
64
|
-
out=$(compute)
|
|
65
|
-
}
|
|
66
|
-
```
|
|
57
|
+
Choose the variable’s scope when addressing the diagnostic. A `local` declaration changes the
|
|
58
|
+
value that an expansion or a called function can see, and it prevents the caller from observing
|
|
59
|
+
assignments after the function returns. ESLint leaves the source unchanged.
|
|
67
60
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
`
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
The fix declares a variable that was global. If the function _reads_ the global before assigning it
|
|
74
|
-
(`count=$((count + 1))`), declaring it `local` makes that read see an empty value instead, so
|
|
75
|
-
review the function before accepting the fix. Every report in a function carries the same fix, a
|
|
76
|
-
single `local` declaring all of the function's reported names, so `eslint --fix` settles the
|
|
77
|
-
function in one pass however many names it leaks (`a` then `b` becomes `local a b`). Fixes that
|
|
78
|
-
insert at the same point conflict, and ESLint applies one per pass and stops after 10, which is why
|
|
79
|
-
one fix per name would leave a function with more than 10 names unfixed. The declaration therefore
|
|
80
|
-
also covers a name whose report a user suppressed with a directive, so review the function if you
|
|
81
|
-
suppress a name in it. Each report, message and suggestion is still per name. A function whose body
|
|
82
|
-
is not a `{ … }` block (`f() if …; fi`) is reported without a fix.
|
|
61
|
+
If you choose local scope for a command substitution, a separate declaration preserves the
|
|
62
|
+
assignment’s exit status. For example, use `local out` followed by `out=$(compute)`. With
|
|
63
|
+
`local out=$(compute)`, the status belongs to `local`, which can hide a failure from `set -e`
|
|
64
|
+
or `|| handle_error` (ShellCheck SC2155).
|
|
83
65
|
|
|
84
66
|
## Options
|
|
85
67
|
|
|
@@ -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
|
|
@@ -21,8 +21,31 @@ This rule reads line 1 of the file and reports two problems:
|
|
|
21
21
|
accepted two ways:
|
|
22
22
|
- by absolute path: `#!/bin/bash`, `#!/usr/local/bin/bash`, with any arguments after it;
|
|
23
23
|
- through `env` named by absolute path: `#!/usr/bin/env bash`,
|
|
24
|
-
`#!/usr/bin/env -S bash -e`.
|
|
25
|
-
|
|
24
|
+
`#!/usr/bin/env -S bash -e`. Use `-S` to pass options, `NAME=value` assignments,
|
|
25
|
+
or shell arguments. Without it, `env` receives one command operand. A path such as
|
|
26
|
+
`/opt/shell tools/bash` names Bash, while `bash -e` is a literal command name. In a Linux
|
|
27
|
+
shebang, the argument delivered to `env` ends at LF or NUL. Blanks immediately before NUL
|
|
28
|
+
remain part of the argument.
|
|
29
|
+
Split-string forms support static quoting, escapes and comments, including `-vS` and
|
|
30
|
+
nested splitting with `-S` or `--split-string`. GNU env restarts option processing after
|
|
31
|
+
each split, keeping the effects of earlier options.
|
|
32
|
+
GNU `env` options such as `--argv0=shell` and `--default-signal=PIPE` can precede the shell
|
|
33
|
+
name. Unambiguous long-option abbreviations such as `--split-s=bash -e` are accepted.
|
|
34
|
+
An ambiguous abbreviation such as `--ignore` leaves the interpreter unrecognized.
|
|
35
|
+
Signal lists use Linux signal names, numbers, or GNU shell-status operands. An
|
|
36
|
+
explicit disposition for `SIGKILL` or `SIGSTOP` is rejected unless a later bare disposition
|
|
37
|
+
option replaces it. GNU env applies assignments containing a literal `=`, including
|
|
38
|
+
`=yes` and `MODE${SUFFIX}=yes`. After option processing ends, a name made only of
|
|
39
|
+
expansions, as in `-- ${NAME}=yes`, is also recognized as an assignment. While options
|
|
40
|
+
are still being read, an expanded name at the start of a word can introduce another
|
|
41
|
+
option, so the interpreter stays unrecognized. An expansion can fill an attached value,
|
|
42
|
+
as in `--argv0=shell${SUFFIX}`. An expanded `--unset` value can contain an equals sign
|
|
43
|
+
even inside quotes, so it leaves the interpreter unrecognized unless env clears the
|
|
44
|
+
environment and skips unsetting.
|
|
45
|
+
An unquoted expansion of a set variable, even an empty one, makes an adjacent `#` literal.
|
|
46
|
+
An unset variable can instead make it start a comment that ends only the current split
|
|
47
|
+
string. The rule checks both readings and recognizes the interpreter when they have usable
|
|
48
|
+
options and agree on the executable operand.
|
|
26
49
|
|
|
27
50
|
The space-separated long form `#!/usr/bin/env --split-string bash -eu` is reported:
|
|
28
51
|
GNU env receives `--split-string bash -eu` as one option name and rejects it. GNU
|
|
@@ -51,7 +74,8 @@ For `missingShebang`, the fix inserts one line above line 1. The interpreter com
|
|
|
51
74
|
| `zsh` | `#!/usr/bin/env zsh` | `#!/bin/zsh` |
|
|
52
75
|
| `bats` | `#!/usr/bin/env bats` | `#!/bin/bats` |
|
|
53
76
|
|
|
54
|
-
The inserted line
|
|
77
|
+
The inserted line starts at byte zero and ends with LF, including in a CRLF file. The fix removes
|
|
78
|
+
a leading UTF-8 byte order mark and preserves the original body and its line endings. For an empty
|
|
55
79
|
file the fix produces exactly the shebang line and a newline.
|
|
56
80
|
|
|
57
81
|
`unrecognizedInterpreter` has no fix. The file names some other interpreter, and choosing a
|
|
@@ -149,6 +173,25 @@ export default [
|
|
|
149
173
|
|
|
150
174
|
The rule does not detect sourced libraries on its own.
|
|
151
175
|
|
|
176
|
+
## Limitations
|
|
177
|
+
|
|
178
|
+
- The rules model GNU coreutils 9.7 `env` on Linux. Other implementations such as uutils differ on
|
|
179
|
+
numeric signals and option abbreviations. Re-splitting requires statically known input.
|
|
180
|
+
- The GNU env reader shares a work budget across all readings. It allows 64 work units per UTF-16
|
|
181
|
+
code unit in env's original shebang argument, with a minimum of 1,048,576 units to cover
|
|
182
|
+
nested splits within Linux's 256-byte shebang buffer. A much longer header with deeply nested
|
|
183
|
+
splits can exhaust the budget. Exhaustion leaves the interpreter unrecognized and withholds
|
|
184
|
+
shebang startup-option credit.
|
|
185
|
+
- The Linux kernel's shebang byte limit falls outside the model. Recognition uses physical
|
|
186
|
+
LF or NUL, including when the argument contains a Unicode line separator.
|
|
187
|
+
- A bare CRLF shebang such as `#!/bin/dash` or `#!/usr/bin/env dash` reports
|
|
188
|
+
`unrecognizedInterpreter`. Linux looks up `/bin/dash\r` directly in the first case and
|
|
189
|
+
passes `dash\r` to GNU `env` in the second. The diagnostic escapes whitespace that Linux
|
|
190
|
+
retains at the end of the header, such as `\r`, `\v`, `\f`, or `\u00a0`. A CR in the direct
|
|
191
|
+
interpreter path before an argument also remains part of the path.
|
|
192
|
+
- Recognition identifies the executable operand. Executable lookup, working-directory
|
|
193
|
+
availability, and what the interpreter does with its arguments depend on the runtime.
|
|
194
|
+
|
|
152
195
|
## Relationship to shellcheck
|
|
153
196
|
|
|
154
197
|
eslint-sh is a convention and contract linter: a policy layer, where shellcheck is the gotcha
|
|
@@ -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,9 +67,14 @@ set -euo pipefail
|
|
|
63
67
|
cd "$1"
|
|
64
68
|
```
|
|
65
69
|
|
|
66
|
-
|
|
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
|
|
67
76
|
offered only when inserting the prologue would satisfy the rule. For example, `set -e` followed by
|
|
68
|
-
`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
|
|
69
78
|
again. Decide what that `set +e` is for, then edit the file by hand.
|
|
70
79
|
|
|
71
80
|
### Dialects
|
|
@@ -78,7 +87,7 @@ When `requiredOptions` is not configured, the required prologue depends on `lang
|
|
|
78
87
|
| `posix` | `set -eu` |
|
|
79
88
|
|
|
80
89
|
`pipefail` became part of POSIX only in POSIX.1-2024, and older `dash` and BusyBox `ash` builds
|
|
81
|
-
still reject `set -o pipefail`.
|
|
90
|
+
still reject `set -o pipefail`. Inserting it would break a working `#!/bin/sh` script
|
|
82
91
|
on those shells, so under `posix` the default requires and inserts only `set -eu`. A `posix` file
|
|
83
92
|
may still turn `pipefail` on; the rule just doesn't require it.
|
|
84
93
|
|
|
@@ -97,11 +106,11 @@ This rule takes one options object:
|
|
|
97
106
|
- `requiredOptions` (`string[]`, dialect-aware default above): a nonempty list of unique
|
|
98
107
|
long option names from the supported `set -o` domain (`errexit`, `nounset`, `pipefail`,
|
|
99
108
|
`noclobber`, …). ESLint rejects strings, empty lists, duplicates, unknown names and short
|
|
100
|
-
aliases as configuration errors. The
|
|
109
|
+
aliases as configuration errors. The suggestion builds a `set` command from this list:
|
|
101
110
|
options with a one-letter flag go into one cluster in list order, the first long-only option
|
|
102
111
|
follows that cluster with `o`, and any others follow as `-o <name>`.
|
|
103
112
|
|
|
104
|
-
<!-- transform: {"kind": "
|
|
113
|
+
<!-- transform: {"kind": "suggestion", "options": {"requiredOptions": ["errexit", "nounset", "pipefail"]}} -->
|
|
105
114
|
|
|
106
115
|
```sh
|
|
107
116
|
echo hi
|
|
@@ -110,7 +119,7 @@ This rule takes one options object:
|
|
|
110
119
|
echo hi
|
|
111
120
|
```
|
|
112
121
|
|
|
113
|
-
<!-- transform: {"kind": "
|
|
122
|
+
<!-- transform: {"kind": "suggestion", "options": {"requiredOptions": ["errexit", "nounset"]}} -->
|
|
114
123
|
|
|
115
124
|
```sh
|
|
116
125
|
echo hi
|
|
@@ -134,10 +143,12 @@ throw a configuration error naming the shared binding when either rule loads for
|
|
|
134
143
|
with `set` statements in the window. Only spellings the operating system passes to the shell as
|
|
135
144
|
written are read:
|
|
136
145
|
- `#!/usr/bin/env -S bash -euo pipefail`: `env -S` splits the rest of the line into words, so
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
146
|
+
the rule reads the resulting shell arguments. This also handles GNU env options before the
|
|
147
|
+
shell, unambiguous long-option abbreviations, and nested splits of static arguments.
|
|
148
|
+
An unquoted carriage return in a split string separates arguments like a space.
|
|
149
|
+
- `#!/bin/bash -eu`: the kernel passes everything after the interpreter as **one** argument.
|
|
150
|
+
In `#!/bin/bash -e -u`, the single argument contains a space, so the rule withholds startup
|
|
151
|
+
credit.
|
|
141
152
|
- `#!/usr/bin/env bash -e` counts as nothing. Without `-S`, Linux passes `bash -e` to `env` as
|
|
142
153
|
one command name.
|
|
143
154
|
|
|
@@ -149,7 +160,21 @@ Relative interpreters such as `#!env -S bash -eu` receive no credit. Neither doe
|
|
|
149
160
|
`#!/usr/bin/env --split-string bash -eu`: GNU env rejects the space-separated long
|
|
150
161
|
option when invoked through a kernel shebang. GNU's equals form
|
|
151
162
|
`#!/usr/bin/env --split-string=bash -eu` receives credit; macOS env does not support that long
|
|
152
|
-
option. Use an absolute env path and `-S` for both systems.
|
|
163
|
+
option. Use an absolute env path and `-S` for both systems. A carriage return retained in a
|
|
164
|
+
direct shell flag or inside a quoted split-string flag is passed as part of the argument.
|
|
165
|
+
The shared option parser reads the resolved arguments for every interpreter. A single argument
|
|
166
|
+
that contains whitespace would have split into separate tokens under the original shebang
|
|
167
|
+
reader, so the rule withholds startup credit for that argv. Otherwise, an uncertain or
|
|
168
|
+
unrecognized argument preserves the changes already read. GNU env
|
|
169
|
+
`-C` and `--chdir` can redirect a relative script path. GNU env `-a` and `--argv0` can
|
|
170
|
+
change startup identity. If any reading uses one of these options, the rule withholds
|
|
171
|
+
startup-option credit.
|
|
172
|
+
|
|
173
|
+
**Known limitations.** The shared parser can credit `dash -euB` even though dash rejects `-B`,
|
|
174
|
+
and it can credit options when Bash `-t` or BusyBox `sh -o nolog` prevents the body from running.
|
|
175
|
+
Valid startup switches outside the shared option syntax can end option reading before later
|
|
176
|
+
flags. For example, Bash `-O extglob -eu` receives no credit for `-eu`. The shared fixer spells
|
|
177
|
+
`notify` as `-b`, which Zsh rejects in a `set` statement.
|
|
153
178
|
|
|
154
179
|
```js
|
|
155
180
|
// eslint.config.js
|
|
@@ -198,7 +223,7 @@ set -eu
|
|
|
198
223
|
<!-- example: {"findings": 1, "messageIds": ["missingStrictMode"]} -->
|
|
199
224
|
```sh
|
|
200
225
|
#!/bin/bash
|
|
201
|
-
# 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)
|
|
202
227
|
set -euo pipefail
|
|
203
228
|
set +e
|
|
204
229
|
```
|
|
@@ -30,13 +30,19 @@ A command has its input supplied explicitly, and is not reported, when:
|
|
|
30
30
|
|
|
31
31
|
- it, or any statement that encloses it, redirects fd 0 from a file, here-document or
|
|
32
32
|
here-string (`read x < file`, `read x <<< "$v"`, `while read l; do …; done < file`), or from
|
|
33
|
-
|
|
33
|
+
a known nonzero descriptor (`read x <&3`), or closes fd 0 (`read x <&-`). In the `zsh` and
|
|
34
|
+
`mksh` dialects, a target that resolves to `p` takes input from the coprocess (`read x <&p`).
|
|
35
|
+
A computed duplication target can still refer to stdin;
|
|
34
36
|
- it is on the right of a pipe (`echo a | read x`, `cat f | while read l; do …; done`);
|
|
35
37
|
- it is inside a `coproc`, which reads its own input pipe;
|
|
36
|
-
- it is `read -u N`
|
|
37
|
-
|
|
38
|
+
- it is Bash or Zsh `read -u N` with a known nonzero final descriptor, or mksh
|
|
39
|
+
`read -uN` with a known nonzero attached descriptor. Bash accepts zero padding
|
|
40
|
+
and leading whitespace in a `read -u` descriptor. The rule can step past a
|
|
41
|
+
Bash prompt option whose argument is guaranteed to produce one field, as in
|
|
42
|
+
`read -p "$prompt" -u 3 answer`.
|
|
43
|
+
- it is mksh or Zsh `read -p`, which reads from the coprocess. In mksh, `read -up`
|
|
44
|
+
also selects the coprocess. In Bash and dash, `-p` supplies a prompt.
|
|
38
45
|
|
|
39
|
-
A `read` with `-p` is still reported: the prompt does not make it safe without a terminal.
|
|
40
46
|
|
|
41
47
|
### Limits
|
|
42
48
|
|
|
@@ -50,6 +56,23 @@ The rule is honest about what it cannot see:
|
|
|
50
56
|
- **Variables are not tracked.** `is_tty=$([ -t 0 ] && echo 1)` is not a guard.
|
|
51
57
|
- **Command names are read as written.** A command reached through a wrapper (`command read x`)
|
|
52
58
|
or with a dynamic name is not matched.
|
|
59
|
+
- A CR on a delimiter-like heredoc line can make the guard suggestion end inside the body.
|
|
60
|
+
|
|
61
|
+
Zsh `read -k` can open the terminal even when stdin is redirected. An explicit descriptor or coprocess option supplies input instead.
|
|
62
|
+
|
|
63
|
+
The guard test replaces the current exit status and pipeline status. The rule withholds the
|
|
64
|
+
wrapper when it sees a reference to `$?`, Bash's `PIPESTATUS`, or Zsh's `status` or
|
|
65
|
+
`pipestatus` in the statement or its redirections, including expanding here-documents.
|
|
66
|
+
It also withholds the wrapper for Bash indirect parameter expansions that can read status.
|
|
67
|
+
|
|
68
|
+
An unresolved descriptor duplication such as `read answer 0<&"$fd"`, an unsupported
|
|
69
|
+
input target such as `read answer 0<&foo`, or an unresolved option word such as
|
|
70
|
+
`read "$arg" answer` can still report. The rule withholds the wrapper because the command
|
|
71
|
+
could read from an alternate descriptor or fail while setting up input. It also withholds
|
|
72
|
+
the wrapper when a known or unresolved option can fail before input is read,
|
|
73
|
+
when a final `-u 0` follows a nonzero descriptor that Bash checks first, or
|
|
74
|
+
when a duplication target is invalid for the dialect. For example, dash and
|
|
75
|
+
mksh reject `0<&00`, and Bash rejects `read -t nope -u 00 answer`.
|
|
53
76
|
|
|
54
77
|
## Options
|
|
55
78
|
|
|
@@ -25,6 +25,9 @@ interpreter path (`#!/bin/sh`, `#!/usr/local/bin/bash`), or an absolute-path `en
|
|
|
25
25
|
it runs (`#!/usr/bin/env sh`, `#!/usr/bin/env -S bash -e`). A relative interpreter such as
|
|
26
26
|
`#!sh` is not read; `sh/require-shebang` reports it.
|
|
27
27
|
|
|
28
|
+
On Linux, a terminal carriage return on a bare interpreter name stays part of the name.
|
|
29
|
+
`sh/require-shebang` reports that unrecognized interpreter.
|
|
30
|
+
|
|
28
31
|
Nothing else is reported. A file with no shebang is clean here (that is `sh/require-shebang`'s
|
|
29
32
|
concern), as is a shebang that names another interpreter, and any file whose dialect is `mksh`,
|
|
30
33
|
`zsh` or `bats`. A shebang and dialect that already agree are clean: `#!/usr/bin/env bash` under
|
package/docs/shellcheck.md
CHANGED
|
@@ -73,7 +73,7 @@ reason given, and each rule's page has a "Relationship to shellcheck" section. T
|
|
|
73
73
|
| [`sh/no-unchecked-cd`](./rules/no-unchecked-cd.md) | SC2164 | Offers a suggestion and covers `pushd` and `popd` too. shellcheck suppresses SC2164 for shebang errexit flags; `ignoreWhenErrexit` reads only prologue `set` statements, so the rule still reports those unchecked directory changes. |
|
|
74
74
|
| [`sh/require-shebang`](./rules/require-shebang.md) | SC2148, SC1008 | Requires a recognized shebang even when shellcheck infers the shell from a `.bash`, `.bats` or `.ksh` extension, a directive or a command-line flag and emits no SC2148. |
|
|
75
75
|
| [`sh/no-masked-exit-status`](./rules/no-masked-exit-status.md) | SC2155 | `require-strict-mode` makes `set -euo pipefail` a contract, and `local x=$(cmd)` is the most common way a script that honours it still swallows a failure. shellcheck offers no fix; the rule offers the split as an editor suggestion and can be limited to files where errexit is on. It is not in `recommended`, because that preset runs beside shellcheck. |
|
|
76
|
-
| [`sh/prefer-local`](./rules/prefer-local.md) | SC2155 (a related check) | Requires `local` declarations
|
|
76
|
+
| [`sh/prefer-local`](./rules/prefer-local.md) | SC2155 (a related check) | Requires `local` declarations. Declare a variable on a separate line from a command substitution to avoid SC2155. |
|
|
77
77
|
| [`sh/compat/no-associative-arrays`](./rules/compat/no-associative-arrays.md), [`no-case-modification`](./rules/compat/no-case-modification.md), [`no-negative-array-index`](./rules/compat/no-negative-array-index.md), [`no-mapfile`](./rules/compat/no-mapfile.md) | SC3044 (the `declare` or `mapfile` command, not the `-A` option), SC3043 (`local -A`), SC3054 (`${a[-1]}`), SC3059 (case modification) | shellcheck reports these only for scripts it treats as POSIX `sh`. These rules report them for a Bash version you name. |
|
|
78
78
|
| [`sh/compat/no-double-bracket`](./rules/compat/no-double-bracket.md), [`no-let`](./rules/compat/no-let.md), [`no-source`](./rules/compat/no-source.md) | SC3010 (`[[`), SC3039 (`let`), SC3046 (`source`), under `-s sh` only | Under `-s bash` shellcheck reports nothing for `[[` or `source`, and for `let` only SC2219, a style note that prefers `(( ))`. These rules report them for the dialect you configure, because they fail on dash and would otherwise defeat a preset named for portability. |
|
|
79
79
|
| [`sh/compat/no-test-v`](./rules/compat/no-test-v.md), [`no-wait-n`](./rules/compat/no-wait-n.md), [`no-globstar`](./rules/compat/no-globstar.md), [`no-pipe-all`](./rules/compat/no-pipe-all.md), [`no-fd-variable`](./rules/compat/no-fd-variable.md), [`no-unicode-escape`](./rules/compat/no-unicode-escape.md) | SC3016 (`test -v`, with SC3010 for `[[`), SC3045 (`wait -n`), SC3044 (`shopt`), SC3029 (`\|&`), SC3022 (`{fd}`), SC3003 (`$'…'`) | shellcheck reports these Bash features only for scripts it treats as POSIX `sh`. These rules report them for a Bash version you name. |
|
package/docs/wrapper-rules.md
CHANGED
|
@@ -199,7 +199,7 @@ A command word that contains a slash is matched by its basename, so running a co
|
|
|
199
199
|
cannot bypass the policy. `/usr/bin/curl x` and `./bin/curl x` are `curl`, and the message names
|
|
200
200
|
`curl`. The same holds for wrappers: `/usr/local/bin/retry curl x` is `curl` run through `retry`,
|
|
201
201
|
with `retry`'s declared options, and `/usr/bin/sudo curl x` is `curl` run through `sudo`. Only the
|
|
202
|
-
whole basename is compared
|
|
202
|
+
whole basename is compared. A word like `"$dir"/curl` matches when analysis guarantees one field ending in `/curl`. Following a wrapper through an unresolved path or resolving a subcommand after it still requires an analyzable chain.
|
|
203
203
|
|
|
204
204
|
### Subcommands
|
|
205
205
|
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-sh/eslint-plugin",
|
|
3
3
|
"description": "Shell script linting for ESLint, with built-in policy rules, shared presets, and custom rules over a typed shell AST",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.13.0",
|
|
5
5
|
"author": "Mike North <michael.l.north@gmail.com>",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@eslint/core": "^1.2.1",
|
|
8
8
|
"@eslint/plugin-kit": "^0.7.2",
|
|
9
|
-
"sh-ast": "^0.
|
|
10
|
-
"sh-analysis": "^0.
|
|
9
|
+
"sh-ast": "^0.13.0",
|
|
10
|
+
"sh-analysis": "^0.4.0"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"eslint": "^10.
|
|
13
|
+
"eslint": "^10.15.0"
|
|
14
14
|
},
|
|
15
15
|
"peerDependencies": {
|
|
16
16
|
"eslint": "^10.2.0"
|