@eslint-sh/eslint-plugin 0.12.0 → 0.14.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 +32 -12
- package/dist/api.d.ts +11 -1
- package/dist/api.js +8 -0
- 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 +65 -12
- package/dist/language/command-resolution.d.ts +24 -6
- package/dist/language/command-resolution.js +44 -17
- package/dist/language/shell-language.js +2 -0
- package/dist/language/shell-source-code.d.ts +29 -11
- package/dist/language/shell-source-code.js +35 -26
- package/dist/language/word-resolution.d.ts +18 -12
- package/dist/language/word-resolution.js +133 -3
- package/dist/processors/disable-justification.d.ts +20 -0
- package/dist/processors/disable-justification.js +124 -0
- package/dist/rules/compat/bash-builtin-destination.d.ts +19 -0
- package/dist/rules/compat/bash-builtin-destination.js +83 -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 +52 -20
- 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 +55 -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 +257 -71
- 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 +65 -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 +211 -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 +148 -155
- package/docs/disable-justification.md +71 -0
- package/docs/error-codes.md +4 -0
- package/docs/examples/eslint.config.js +11 -0
- package/docs/examples/no-curl-in-functions-rule.mjs +20 -23
- package/docs/examples/no-listed-commands-rule.ts +19 -19
- package/docs/examples/no-rm-rf-rule.mjs +33 -62
- package/docs/examples/no-sample-command-rule.mjs +33 -0
- package/docs/reading-nodes.md +26 -1
- package/docs/rules/README.md +4 -4
- package/docs/rules/compat/no-mapfile.md +14 -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 +8 -2
- 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 +3 -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/rules-about-commands.md +78 -0
- package/docs/selectors.md +8 -4
- package/docs/wrapper-rules.md +1 -1
- package/package.json +4 -4
|
@@ -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,9 @@ 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, and in Zsh when the file defines a `TRAPZERR` function
|
|
80
|
+
(or assigns the `functions` parameter). Adding `|| exit` can suppress an error trap.
|
|
79
81
|
|
|
80
82
|
<!-- transform: {"kind": "suggestion"} -->
|
|
81
83
|
|
|
@@ -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
|
```
|
|
@@ -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
|
|
|
@@ -52,6 +58,22 @@ The rule is honest about what it cannot see:
|
|
|
52
58
|
or with a dynamic name is not matched.
|
|
53
59
|
- A CR on a delimiter-like heredoc line can make the guard suggestion end inside the body.
|
|
54
60
|
|
|
61
|
+
Zsh `read -k` and `read -q` 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`.
|
|
76
|
+
|
|
55
77
|
## Options
|
|
56
78
|
|
|
57
79
|
- `commands` (`string[]`, default `['read', 'select']`): the interactive commands to check. A
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# Rules about commands
|
|
2
|
+
|
|
3
|
+
Start with `context.sourceCode.getCommandInventory()` when a rule depends on which command
|
|
4
|
+
a script invokes. This example reports invocations of the placeholder `sample-tool`, including
|
|
5
|
+
`"sample-tool"`, `\sample-tool`, `/usr/bin/sample-tool`, `env sample-tool`, `sudo sample-tool`,
|
|
6
|
+
`command sample-tool`, and `timeout 5 sample-tool`.
|
|
7
|
+
|
|
8
|
+
Save [`no-sample-command-rule.mjs`](./examples/no-sample-command-rule.mjs) next to your
|
|
9
|
+
[`eslint.config.js`](./custom-rule-tutorial.md#1-run-one-local-rule):
|
|
10
|
+
|
|
11
|
+
```mjs
|
|
12
|
+
import { defineShellRule } from '@eslint-sh/eslint-plugin';
|
|
13
|
+
|
|
14
|
+
export default defineShellRule({
|
|
15
|
+
meta: {
|
|
16
|
+
type: 'suggestion',
|
|
17
|
+
schema: [],
|
|
18
|
+
messages: { match: 'This script invokes sample-tool.' },
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
return {
|
|
22
|
+
File() {
|
|
23
|
+
const { commands } = context.sourceCode.getCommandInventory();
|
|
24
|
+
for (const site of commands) {
|
|
25
|
+
const { effective, chainIndices } = site.resolveInvokedCommand();
|
|
26
|
+
const index = chainIndices.at(-1);
|
|
27
|
+
if (index === undefined || (!effective.static && chainIndices.length > 1)) continue;
|
|
28
|
+
|
|
29
|
+
const facts = site.argvFacts.at(index);
|
|
30
|
+
if (facts?.resolution !== effective) continue;
|
|
31
|
+
|
|
32
|
+
const text = effective.static ? effective.text : undefined;
|
|
33
|
+
const name = facts.basename.known
|
|
34
|
+
? facts.basename.text
|
|
35
|
+
: text?.slice(text.lastIndexOf('/') + 1);
|
|
36
|
+
const word = site.node.args.at(index);
|
|
37
|
+
if (name === 'sample-tool' && word) {
|
|
38
|
+
context.report({ node: word, messageId: 'match' });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The `File` listener reads the inventory once for the whole file. Each command site can
|
|
48
|
+
`resolveInvokedCommand()` through the analyzer's transparent wrappers. The final `chainIndices`
|
|
49
|
+
entry points to the original word associated with `effective`. Check that `effective` is the
|
|
50
|
+
same object as that word's `argvFacts[index].resolution` before using its basename. A synthetic
|
|
51
|
+
result, such as one for an interpreter's `-c` flag, has a different object.
|
|
52
|
+
|
|
53
|
+
The example also skips a non-static word reached through a wrapper. Its role may still be
|
|
54
|
+
unresolved even when the word has a known basename. In `env "A=$v"/sample-tool printf OK`,
|
|
55
|
+
the word with basename `sample-tool` is an environment assignment, and `printf` is the command.
|
|
56
|
+
|
|
57
|
+
- Use the basename fact's `text` when `known` is true. In a direct `"$dir"/sample-tool` call,
|
|
58
|
+
the directory varies and the basename is still `sample-tool`.
|
|
59
|
+
- When the basename fact is unknown and `effective.static` is true, take the last path component
|
|
60
|
+
of `effective.text`. This also covers Zsh `command sample-tool`.
|
|
61
|
+
- Report on `site.node.args[index]` to highlight the original spelling, including quotes and any
|
|
62
|
+
directory prefix.
|
|
63
|
+
|
|
64
|
+
The inventory's `uncertainties` describe opaque executable constructs. In Zsh,
|
|
65
|
+
`echo "${items[(r)$(sample-tool)]}"` has an `echo` site and an `opaque-zsh-subscript`
|
|
66
|
+
uncertainty for the reverse-subscript pattern. Treat such entries as a sign that the
|
|
67
|
+
inventory may be incomplete.
|
|
68
|
+
|
|
69
|
+
For `bash -c "sample-tool arg"`, the inventory
|
|
70
|
+
has a `bash` site and an empty `uncertainties` list. Resolving that site gives
|
|
71
|
+
`{ static: false, reason: 'embedded-command' }`, because the analyzer leaves the command
|
|
72
|
+
string opaque. When neither the basename nor the text is known, as in `"$tool" arg`, this
|
|
73
|
+
example skips the site. A rule that requires every invocation to be reviewable should report
|
|
74
|
+
unresolved sites as well as opaque constructs from `uncertainties`. The inventory can have an
|
|
75
|
+
empty `uncertainties` list even when a site is unresolved.
|
|
76
|
+
|
|
77
|
+
Use a [selector](./selectors.md) for syntax-only rules. For example,
|
|
78
|
+
`:expansion:not(DblQuoted :expansion)` selects unquoted expansions outside double quotes.
|
package/docs/selectors.md
CHANGED
|
@@ -12,6 +12,9 @@ Looking for a complete, runnable `create()` that uses one of these selectors —
|
|
|
12
12
|
`{ meta, create(context) }` rule module, `context.report`, and the `eslint.config.mjs`
|
|
13
13
|
`plugins`/`rules`/`language` wiring? See the [custom-rule tutorial](./custom-rule-tutorial.md).
|
|
14
14
|
|
|
15
|
+
For rules about which command a script invokes, start with
|
|
16
|
+
[Rules about commands](./rules-about-commands.md).
|
|
17
|
+
|
|
15
18
|
## The six launch classes
|
|
16
19
|
|
|
17
20
|
| Class | Matches |
|
|
@@ -43,15 +46,16 @@ upstream field spellings; rule listeners receive the plugin view. Import view ty
|
|
|
43
46
|
|
|
44
47
|
## Recipes
|
|
45
48
|
|
|
46
|
-
### Match a
|
|
49
|
+
### Match the first part of a command word (syntax only)
|
|
47
50
|
|
|
48
51
|
```
|
|
49
52
|
CallExpr[args.0.parts.0.value='jq']
|
|
50
53
|
```
|
|
51
54
|
|
|
52
|
-
`args.0` is the command word
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
`args.0` is the command word. This recipe tests the `value` field of its first syntax part, so
|
|
56
|
+
it matches bare `jq` and can also match a longer word such as `jq"$suffix"`. A double-quoted
|
|
57
|
+
word like `"jq"` has a different shape. For policies based on the resolved program name,
|
|
58
|
+
use the [wrapper-aware command inventory recipe](./rules-about-commands.md).
|
|
55
59
|
|
|
56
60
|
### Match every command nested inside a function
|
|
57
61
|
|
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.14.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"
|