@eslint-sh/eslint-plugin 0.13.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 +27 -10
- package/dist/api.d.ts +10 -0
- package/dist/api.js +8 -0
- package/dist/eslint-plugin-api.d.ts +40 -1
- package/dist/language/command-resolution.d.ts +8 -0
- package/dist/language/shell-language.js +2 -0
- package/dist/language/shell-source-code.d.ts +11 -1
- package/dist/language/shell-source-code.js +11 -1
- 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 +18 -1
- package/dist/rules/compat/bash-builtin-destination.js +81 -2
- package/dist/rules/compat/no-mapfile.js +36 -1
- package/dist/rules/compat/no-printf-time-format.js +19 -2
- package/dist/rules/compat/no-test-v.js +14 -10
- package/dist/rules/no-unchecked-cd.js +11 -0
- package/dist/rules/require-tty-guard.js +17 -3
- package/docs/custom-rule-tutorial.md +140 -150
- package/docs/disable-justification.md +71 -0
- package/docs/examples/eslint.config.js +11 -0
- package/docs/examples/no-curl-in-functions-rule.mjs +15 -20
- 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/compat/no-mapfile.md +4 -1
- package/docs/rules/compat/no-printf-time-format.md +1 -1
- package/docs/rules/compat/no-test-v.md +1 -1
- package/docs/rules/no-unchecked-cd.md +2 -1
- package/docs/rules/require-tty-guard.md +1 -1
- package/docs/rules-about-commands.md +78 -0
- package/docs/selectors.md +8 -4
- package/package.json +1 -1
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Require reasons for ESLint disable directives
|
|
2
|
+
|
|
3
|
+
Select the `sh/require-disable-justification` processor to require a written reason after
|
|
4
|
+
`--` on every block, same-line, and next-line disable directive in a shell file.
|
|
5
|
+
A justification must contain text beyond whitespace. One reason applies to the whole
|
|
6
|
+
comment, including a comma-separated list of rule IDs.
|
|
7
|
+
`eslint-enable` needs no reason. The processor uses the shell language's configured
|
|
8
|
+
`directivePrefix`.
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
import sh from 'eslint-sh';
|
|
12
|
+
|
|
13
|
+
export default [
|
|
14
|
+
{
|
|
15
|
+
files: ['**/*.sh'],
|
|
16
|
+
plugins: { sh },
|
|
17
|
+
language: 'sh/sh',
|
|
18
|
+
processor: 'sh/require-disable-justification',
|
|
19
|
+
},
|
|
20
|
+
];
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The processor reports an error at the comment after ESLint applies inline suppression and
|
|
24
|
+
checks for unused directives. A directive can still suppress the policy rule it names.
|
|
25
|
+
ESLint can still report and fix unused directives. The processor checks written
|
|
26
|
+
directives even when `linterOptions.noInlineConfig` prevents them from taking effect.
|
|
27
|
+
|
|
28
|
+
## Options
|
|
29
|
+
|
|
30
|
+
Use `createDisableJustificationProcessor({ rules })` to select which rule IDs need reasons.
|
|
31
|
+
Each entry is an exact rule ID or a literal prefix ending in `*`. For example, `policy/*`
|
|
32
|
+
matches every ID starting with `policy/`. A directive is in scope if it disables any
|
|
33
|
+
selected rule. A directive without rule IDs disables all rules, so it is in scope for any
|
|
34
|
+
nonempty selection. Omit `rules` to check all directives or use `[]` to select none.
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
import sh, { createDisableJustificationProcessor } from 'eslint-sh';
|
|
38
|
+
|
|
39
|
+
export default [
|
|
40
|
+
{
|
|
41
|
+
files: ['**/*.sh'],
|
|
42
|
+
plugins: { sh },
|
|
43
|
+
language: 'sh/sh',
|
|
44
|
+
processor: createDisableJustificationProcessor({ rules: ['policy/*', 'sh/no-eval'] }),
|
|
45
|
+
},
|
|
46
|
+
];
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Examples
|
|
50
|
+
|
|
51
|
+
These comments need reasons with the default processor.
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
# eslint-disable
|
|
55
|
+
run_legacy_task
|
|
56
|
+
# eslint-enable
|
|
57
|
+
|
|
58
|
+
run_legacy_task # eslint-disable-line
|
|
59
|
+
# eslint-disable-next-line policy/no-legacy-task, sh/no-eval
|
|
60
|
+
run_legacy_task
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
This comment supplies a reason.
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
# eslint-disable-next-line policy/no-legacy-task -- The compatibility test exercises the legacy entry point.
|
|
67
|
+
run_legacy_task
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Enable the processor per file group. ESLint selects one processor for a file, so a
|
|
71
|
+
file group that already uses a processor needs to choose which processor to apply.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import sh from '@eslint-sh/eslint-plugin';
|
|
2
|
+
import noSampleCommand from './no-sample-command-rule.mjs';
|
|
3
|
+
|
|
4
|
+
export default [
|
|
5
|
+
{
|
|
6
|
+
files: ['**/*.sh'],
|
|
7
|
+
plugins: { sh, local: { rules: { 'no-sample-command': noSampleCommand } } },
|
|
8
|
+
language: 'sh/sh',
|
|
9
|
+
rules: { 'local/no-sample-command': 'warn' },
|
|
10
|
+
},
|
|
11
|
+
];
|
|
@@ -1,17 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The "every command in the file" custom rule from the custom-rule tutorial
|
|
3
|
-
* (`../custom-rule-tutorial.md`, step 5). Plain, directly-runnable
|
|
4
|
-
* JavaScript; this package's test suite imports this exact file and
|
|
5
|
-
* asserts the tutorial's code fence matches it byte-for-byte.
|
|
6
|
-
*
|
|
7
|
-
* It reports `curl` run anywhere inside a shell function, including through a
|
|
8
|
-
* wrapper (`sudo curl …`) or inside a command substitution in that function.
|
|
9
|
-
* Instead of tracking "am I inside a function?" while visiting nodes, it reads
|
|
10
|
-
* every command once from `context.sourceCode.getCommandInventory().commands`, whose
|
|
11
|
-
* `context` frames (outermost first) already say how each command is reached.
|
|
12
|
-
*
|
|
13
|
-
* @see ../custom-rule-tutorial.md — the tutorial this rule backs
|
|
14
|
-
*/
|
|
15
1
|
import { defineShellRule } from '@eslint-sh/eslint-plugin';
|
|
16
2
|
|
|
17
3
|
const noCurlInFunctions = defineShellRule({
|
|
@@ -30,10 +16,19 @@ const noCurlInFunctions = defineShellRule({
|
|
|
30
16
|
return {
|
|
31
17
|
File() {
|
|
32
18
|
for (const site of context.sourceCode.getCommandInventory().commands) {
|
|
33
|
-
const { effective } = site.resolveInvokedCommand();
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
19
|
+
const { effective, chainIndices } = site.resolveInvokedCommand();
|
|
20
|
+
const index = chainIndices.at(-1);
|
|
21
|
+
if (index === undefined || (!effective.static && chainIndices.length > 1)) continue;
|
|
22
|
+
|
|
23
|
+
const facts = site.argvFacts.at(index);
|
|
24
|
+
if (facts?.resolution !== effective) continue;
|
|
25
|
+
|
|
26
|
+
const text = effective.static ? effective.text : undefined;
|
|
27
|
+
const name = facts.basename.known
|
|
28
|
+
? facts.basename.text
|
|
29
|
+
: text?.slice(text.lastIndexOf('/') + 1);
|
|
30
|
+
const command = site.node.args.at(index);
|
|
31
|
+
if (name !== 'curl' || !command) continue;
|
|
37
32
|
|
|
38
33
|
// Frames run outermost first, so the last `function` frame is the
|
|
39
34
|
// function that directly contains this command.
|
|
@@ -41,9 +36,9 @@ const noCurlInFunctions = defineShellRule({
|
|
|
41
36
|
if (fn) {
|
|
42
37
|
const [name] = fn.names;
|
|
43
38
|
if (typeof name === 'string') {
|
|
44
|
-
context.report({ node:
|
|
39
|
+
context.report({ node: command, messageId: 'curlInFunction', data: { name } });
|
|
45
40
|
} else {
|
|
46
|
-
context.report({ node:
|
|
41
|
+
context.report({ node: command, messageId: 'curlInUnnamedFunction' });
|
|
47
42
|
}
|
|
48
43
|
}
|
|
49
44
|
}
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The TypeScript custom rule from the custom-rule tutorial
|
|
3
|
-
* (`../custom-rule-tutorial.md`, step 10). This package's test suite
|
|
4
|
-
* asserts the tutorial's code fence matches this file byte-for-byte,
|
|
5
|
-
* type-checks it against the package's published declarations, and runs it
|
|
6
|
-
* through a real `eslint` `Linter`.
|
|
7
|
-
*
|
|
8
|
-
* It reports any command named in its `commands` option, including through a
|
|
9
|
-
* wrapper such as `sudo`. Every type comes from `@eslint-sh/eslint-plugin`:
|
|
10
|
-
* the options and message ids passed to `defineShellRule` flow into
|
|
11
|
-
* `context.options` and `context.report`, and `ShellRuleContext` types the
|
|
12
|
-
* helper that reports outside `create()`.
|
|
13
|
-
*
|
|
14
|
-
* @see ../custom-rule-tutorial.md — the tutorial this rule backs
|
|
15
|
-
*/
|
|
16
1
|
import { defineShellRule } from '@eslint-sh/eslint-plugin';
|
|
17
2
|
import type { ShellCommandSite, ShellRuleContext } from '@eslint-sh/eslint-plugin';
|
|
18
3
|
|
|
19
4
|
/** The rule's one options object; `commands` defaults to `[]`. */
|
|
20
5
|
type Options = [{ commands?: string[] }?];
|
|
21
6
|
|
|
22
|
-
/** Reports the
|
|
7
|
+
/** Reports the effective command when its basename or full static path is listed. */
|
|
23
8
|
function reportIfListed(
|
|
24
9
|
context: ShellRuleContext<Options, 'listed'>,
|
|
25
10
|
site: ShellCommandSite,
|
|
26
11
|
listed: ReadonlySet<string>,
|
|
27
12
|
): void {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
13
|
+
// The policy also matches the interpreter itself when it runs a command string.
|
|
14
|
+
const { effective, chainIndices } = site.resolveInvokedCommand({
|
|
15
|
+
embeddedCommandInterpreters: [],
|
|
16
|
+
});
|
|
17
|
+
const index = chainIndices.at(-1);
|
|
18
|
+
if (index === undefined || (!effective.static && chainIndices.length > 1)) return;
|
|
19
|
+
|
|
20
|
+
const facts = site.argvFacts.at(index);
|
|
21
|
+
if (facts?.resolution !== effective) return;
|
|
22
|
+
|
|
23
|
+
const text = effective.static ? effective.text : undefined;
|
|
24
|
+
const basename = facts.basename.known
|
|
25
|
+
? facts.basename.text
|
|
26
|
+
: text?.slice(text.lastIndexOf('/') + 1);
|
|
27
|
+
const name = [text, basename].find((entry) => entry !== undefined && listed.has(entry));
|
|
28
|
+
const command = site.node.args.at(index);
|
|
29
|
+
if (name !== undefined && command) {
|
|
30
|
+
context.report({ node: command, messageId: 'listed', data: { name } });
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
|
|
@@ -1,40 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The complete "flag `rm -rf`" custom rule from the custom-rule tutorial
|
|
3
|
-
* (`../custom-rule-tutorial.md`). Plain, directly-runnable JavaScript (no
|
|
4
|
-
* compile step) — this file *is* the tutorial's rule-module code fence,
|
|
5
|
-
* copy-paste-verbatim; this package's test suite imports this
|
|
6
|
-
* exact file (not a reconstruction) and asserts the markdown fence matches
|
|
7
|
-
* it byte-for-byte, so the two can't silently drift.
|
|
8
|
-
*
|
|
9
|
-
* Built with `defineShellRule`, which declares `meta.languages: ['sh/sh']`
|
|
10
|
-
* for it, so ESLint refuses to run the rule on a file of any other language.
|
|
11
|
-
*
|
|
12
|
-
* Built on the `:command` launch class from the selector cookbook
|
|
13
|
-
* (`../selectors.md`) — `:command` matches calls with a command word, so `create()`
|
|
14
|
-
* narrows further by inspecting the command word and its flag arguments,
|
|
15
|
-
* exactly the pattern the cookbook's intro describes ("writing a rule as a
|
|
16
|
-
* selector string rather than a `create()` full of manual type checks" —
|
|
17
|
-
* here the selector does the node-type narrowing, `create()` only does the
|
|
18
|
-
* domain-specific check).
|
|
19
|
-
*
|
|
20
|
-
* Flag words are resolved with `context.sourceCode.resolveWord`
|
|
21
|
-
* (backed by `sh-analysis`) rather than a
|
|
22
|
-
* hand-rolled scan: a word is only treated as a short-option cluster
|
|
23
|
-
* when it is *statically* that string (bare or
|
|
24
|
-
* quoted literal concatenations). Recursive (`r`/`R`) and force (`f`)
|
|
25
|
-
* flags accumulate across words before `--`; uppercase `F` is not force.
|
|
26
|
-
* Options are matched anywhere before `--`, GNU-style, even after operands.
|
|
27
|
-
* This teaching example does not model long options or option precedence.
|
|
28
|
-
* A word containing a `ParamExp`/`CmdSubst`/`ArithmExp`/
|
|
29
|
-
* `ProcSubst` resolves to `{ static: false, reason }` (`'expansion'`,
|
|
30
|
-
* `'tilde'`, `'glob'`, …) rather than being silently erased into a static
|
|
31
|
-
* match — see `ShellSourceCode.resolveWord`'s API docs. The source instance
|
|
32
|
-
* comes from ESLint, so the rule needs no separate `sh-ast` install or
|
|
33
|
-
* dependency-hoisting reliance.
|
|
34
|
-
*
|
|
35
|
-
* @see ../custom-rule-tutorial.md — the tutorial this rule backs
|
|
36
|
-
* @see ../selectors.md — the `:command` launch class
|
|
37
|
-
*/
|
|
38
1
|
import { defineShellRule } from '@eslint-sh/eslint-plugin';
|
|
39
2
|
|
|
40
3
|
const noRmRf = defineShellRule({
|
|
@@ -50,35 +13,43 @@ const noRmRf = defineShellRule({
|
|
|
50
13
|
},
|
|
51
14
|
create(context) {
|
|
52
15
|
return {
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
});
|
|
16
|
+
File() {
|
|
17
|
+
for (const site of context.sourceCode.getCommandInventory().commands) {
|
|
18
|
+
const { effective, chainIndices } = site.resolveInvokedCommand();
|
|
19
|
+
const index = chainIndices.at(-1);
|
|
20
|
+
if (index === undefined || (!effective.static && chainIndices.length > 1)) continue;
|
|
59
21
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
22
|
+
const facts = site.argvFacts.at(index);
|
|
23
|
+
if (facts?.resolution !== effective) continue;
|
|
24
|
+
|
|
25
|
+
const text = effective.static ? effective.text : undefined;
|
|
26
|
+
const name = facts.basename.known
|
|
27
|
+
? facts.basename.text
|
|
28
|
+
: text?.slice(text.lastIndexOf('/') + 1);
|
|
29
|
+
const command = site.node.args.at(index);
|
|
30
|
+
if (name !== 'rm' || !command) continue;
|
|
31
|
+
|
|
32
|
+
const words = site.argv.slice(index + 1).map((word) => (word.static ? word.text : null));
|
|
63
33
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
34
|
+
// `rm -- -rf` and `rm build -- -rf` are not `rm -rf` — everything
|
|
35
|
+
// after a `--` end-of-options marker is an operand (a filename),
|
|
36
|
+
// never a flag (POSIX Utility Syntax Guidelines, guideline 10).
|
|
37
|
+
const endOfOptions = words.indexOf('--');
|
|
38
|
+
const flagCandidates = words.slice(0, endOfOptions === -1 ? undefined : endOfOptions);
|
|
69
39
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
40
|
+
// Only static short-option clusters contribute flags; operands,
|
|
41
|
+
// long options, and unresolved words supply no evidence. Recursive
|
|
42
|
+
// and force may occur in the same word or in separate words.
|
|
43
|
+
const shortOptions = flagCandidates.filter(
|
|
44
|
+
(word) => word !== null && /^-[a-zA-Z]+$/.test(word),
|
|
45
|
+
);
|
|
46
|
+
const hasRecursive = shortOptions.some((word) => /[rR]/.test(word));
|
|
47
|
+
const hasForce = shortOptions.some((word) => word.includes('f'));
|
|
48
|
+
const isRecursiveForce = hasRecursive && hasForce;
|
|
79
49
|
|
|
80
|
-
|
|
81
|
-
|
|
50
|
+
if (isRecursiveForce) {
|
|
51
|
+
context.report({ node: command, messageId: 'rmRf' });
|
|
52
|
+
}
|
|
82
53
|
}
|
|
83
54
|
},
|
|
84
55
|
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { defineShellRule } from '@eslint-sh/eslint-plugin';
|
|
2
|
+
|
|
3
|
+
export default defineShellRule({
|
|
4
|
+
meta: {
|
|
5
|
+
type: 'suggestion',
|
|
6
|
+
schema: [],
|
|
7
|
+
messages: { match: 'This script invokes sample-tool.' },
|
|
8
|
+
},
|
|
9
|
+
create(context) {
|
|
10
|
+
return {
|
|
11
|
+
File() {
|
|
12
|
+
const { commands } = context.sourceCode.getCommandInventory();
|
|
13
|
+
for (const site of commands) {
|
|
14
|
+
const { effective, chainIndices } = site.resolveInvokedCommand();
|
|
15
|
+
const index = chainIndices.at(-1);
|
|
16
|
+
if (index === undefined || (!effective.static && chainIndices.length > 1)) continue;
|
|
17
|
+
|
|
18
|
+
const facts = site.argvFacts.at(index);
|
|
19
|
+
if (facts?.resolution !== effective) continue;
|
|
20
|
+
|
|
21
|
+
const text = effective.static ? effective.text : undefined;
|
|
22
|
+
const name = facts.basename.known
|
|
23
|
+
? facts.basename.text
|
|
24
|
+
: text?.slice(text.lastIndexOf('/') + 1);
|
|
25
|
+
const word = site.node.args.at(index);
|
|
26
|
+
if (name === 'sample-tool' && word) {
|
|
27
|
+
context.report({ node: word, messageId: 'match' });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
});
|
package/docs/reading-nodes.md
CHANGED
|
@@ -38,6 +38,13 @@ node type. **Narrow it on `type`** and the node's own fields appear, typed:
|
|
|
38
38
|
union of every `type` value. Field access that does not exist is a compile error, so a typo or a
|
|
39
39
|
field from another node type is caught by the compiler.
|
|
40
40
|
|
|
41
|
+
## Walking to a parent
|
|
42
|
+
|
|
43
|
+
Inside a listener, `context.sourceCode.getParent(node)` returns the immediate lint-view parent,
|
|
44
|
+
or `undefined` for the root `File`. For example, the parent of a command argument `Word` is
|
|
45
|
+
its `CallExpr`. Narrow the parent on `type` before accessing fields specific to that node kind.
|
|
46
|
+
Call `getParent` again to walk outward one level at a time.
|
|
47
|
+
|
|
41
48
|
## Where the fields come from
|
|
42
49
|
|
|
43
50
|
The nodes mirror [mvdan/sh](https://github.com/mvdan/sh)'s syntax tree, as `sh-ast` exposes it.
|
|
@@ -51,7 +58,7 @@ rules that turn an upstream record into a lint-view node are fixed:
|
|
|
51
58
|
| a slice (`CallExpr.Args`) | a read-only array of nodes; **always present**, empty when none |
|
|
52
59
|
| a string (`Lit.Value`) | a JavaScript `string` (decoded from Go bytes) |
|
|
53
60
|
| a boolean (`Stmt.Background`) | a `boolean` |
|
|
54
|
-
| an operator (`BinaryCmd.Op`) | a number
|
|
61
|
+
| an operator (`BinaryCmd.Op`) | a number with [named constants](#operator-constants) |
|
|
55
62
|
| a position (`Stmt.Semicolon`) | none: use `range` and `loc` |
|
|
56
63
|
|
|
57
64
|
A child that may be absent is an **optional** property, so the compiler makes you check it before
|
|
@@ -61,6 +68,24 @@ use (`stmt.cmd?.type`). A slice is never absent and never holds a missing entry,
|
|
|
61
68
|
To find a field, hover the narrowed node in your editor, or read the `sh-ast` record of the same
|
|
62
69
|
name: its field list is the node's field list, minus positions, lowercased.
|
|
63
70
|
|
|
71
|
+
## Operator constants
|
|
72
|
+
|
|
73
|
+
Import named constants from `sh-ast`. The lint view preserves their numeric values in
|
|
74
|
+
`BinaryCmd.op` (`BinCmdOperator`) and `Redirect.op` (`RedirOperator`). To locate an
|
|
75
|
+
operator in the source, see the [tutorial's operator rule](./custom-rule-tutorial.md#6-report-on-an-operator).
|
|
76
|
+
It reads the operator text at `OpPos` and reports on the `&&` token.
|
|
77
|
+
|
|
78
|
+
| Node | Shell tokens | Constants from `sh-ast` (in token order) |
|
|
79
|
+
| --- | --- | --- |
|
|
80
|
+
| `BinaryCmd` | `&&`, `\|\|`, `\|`, `\|&` | `AndStmt`, `OrStmt`, `Pipe`, `PipeAll` |
|
|
81
|
+
| `Redirect` | `>`, `>>`, `<`, `<>` | `RdrOut`, `AppOut`, `RdrIn`, `RdrInOut` |
|
|
82
|
+
| `Redirect` | `<&`, `>&` | `DplIn`, `DplOut` |
|
|
83
|
+
| `Redirect` | `>\|`, `>>\|` | `RdrClob`, `AppClob` |
|
|
84
|
+
| `Redirect` | `<<`, `<<-`, `<<<` | `Hdoc`, `DashHdoc`, `WordHdoc` |
|
|
85
|
+
| `Redirect` | `&>`, `&>\|`, `&>>`, `&>>\|` | `RdrAll`, `RdrAllClob`, `AppAll`, `AppAllClob` |
|
|
86
|
+
|
|
87
|
+
The available tokens depend on the shell dialect. For example, `>>|` is a Zsh redirect.
|
|
88
|
+
|
|
64
89
|
## What the view adapts
|
|
65
90
|
|
|
66
91
|
The types describe the view as it is, not the raw parse:
|
|
@@ -40,7 +40,10 @@ mapfile -t lines <<< "$text"
|
|
|
40
40
|
|
|
41
41
|
The loop intentionally leaves `REPLY` set to the empty string. Use the suggestion
|
|
42
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`.
|
|
43
|
+
The destination must be a static array name other than `REPLY`. The suggestion is
|
|
44
|
+
also withheld when a `declare`, `typeset`, `local` or `readonly` in the file names
|
|
45
|
+
`REPLY` or has an operand that is not static: an integer `REPLY` reads as `0` at end
|
|
46
|
+
of input, so the loop would never end, and other attributes change what it stores.
|
|
44
47
|
|
|
45
48
|
A here-string supplies shell text. Files, pipes, process substitutions, and
|
|
46
49
|
inherited stdin can contain NUL bytes, which `read` and `mapfile` handle differently.
|
|
@@ -11,7 +11,7 @@ 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, including through `builtin` or `command`. The format is the first argument after an optional `-v name` or `-vname` and an optional `--`.
|
|
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 `--`. An expanded destination attached to a literal `-v`, as in `"-v$name"` or `"-va[$i]"`, is read the same way. Bash rejects an invalid destination such as `-v1` or `-v 'a[1][2]'` before evaluating the format, so the rule stays quiet there.
|
|
15
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.
|
|
16
16
|
|
|
17
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.
|
|
@@ -15,7 +15,7 @@ The `-v` test arrived in Bash 4.2 (Bash NEWS for 4.2). In Bash 4.1 and older, `[
|
|
|
15
15
|
The rule reports the `-v` test in two places:
|
|
16
16
|
|
|
17
17
|
- inside `[[ … ]]`, where the parser gives the test its own node;
|
|
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,
|
|
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, `! -v name` negates the test, and any other three arguments are a syntax error, so `test -v name trailing` is not reported; 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. Bash evaluates such an expression before it rejects arguments left over after it, so a `-v` there is still reported.
|
|
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
21
|
|
|
@@ -76,7 +76,8 @@ are reported as usual. `run cd x` is not reported either, as before: `run` is no
|
|
|
76
76
|
wrappers this rule follows, so the command it sees is `run`.
|
|
77
77
|
|
|
78
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
|
|
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.
|
|
80
81
|
|
|
81
82
|
<!-- transform: {"kind": "suggestion"} -->
|
|
82
83
|
|
|
@@ -58,7 +58,7 @@ The rule is honest about what it cannot see:
|
|
|
58
58
|
or with a dynamic name is not matched.
|
|
59
59
|
- A CR on a delimiter-like heredoc line can make the guard suggestion end inside the body.
|
|
60
60
|
|
|
61
|
-
Zsh `read -k` can open the terminal even when stdin is redirected. An explicit descriptor or coprocess option supplies input instead.
|
|
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
62
|
|
|
63
63
|
The guard test replaces the current exit status and pipeline status. The rule withholds the
|
|
64
64
|
wrapper when it sees a reference to `$?`, Bash's `PIPESTATUS`, or Zsh's `status` or
|
|
@@ -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/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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",
|