@eslint-sh/eslint-plugin 0.8.0 → 0.9.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.
Files changed (91) hide show
  1. package/README.md +41 -34
  2. package/dist/api.d.ts +92 -0
  3. package/dist/api.js +83 -0
  4. package/dist/command-harvest/harvest-commands.d.ts +2 -2
  5. package/dist/command-harvest/harvest-commands.js +2 -2
  6. package/dist/configs/posix-portable.d.ts +6 -10
  7. package/dist/configs/posix-portable.js +4 -6
  8. package/dist/configuration-message.d.ts +14 -0
  9. package/dist/configuration-message.js +33 -0
  10. package/dist/declaration-conventions/get-declared-commands.d.ts +41 -0
  11. package/dist/declaration-conventions/{read-declared-commands.js → get-declared-commands.js} +43 -43
  12. package/dist/eslint-plugin-api.d.ts +1506 -0
  13. package/dist/eslint-plugin-syntax-analyze.d.ts +3 -3
  14. package/dist/eslint-plugin-syntax.d.ts +3 -4
  15. package/dist/eslint-plugin.d.ts +16 -1560
  16. package/dist/index.d.ts +14 -81
  17. package/dist/index.js +22 -76
  18. package/dist/language/command-resolution.d.ts +5 -5
  19. package/dist/language/command-resolution.js +11 -7
  20. package/dist/language/selector-classes.d.ts +7 -3
  21. package/dist/language/selector-classes.js +13 -4
  22. package/dist/language/shell-language-options.d.ts +3 -3
  23. package/dist/language/shell-language-options.js +12 -17
  24. package/dist/language/shell-language.d.ts +3 -1
  25. package/dist/language/shell-language.js +6 -11
  26. package/dist/language/shell-source-code.d.ts +59 -42
  27. package/dist/language/shell-source-code.js +86 -60
  28. package/dist/language/source-coordinates.d.ts +2 -0
  29. package/dist/language/source-coordinates.js +8 -4
  30. package/dist/language/syntax-view.js +19 -7
  31. package/dist/language/word-resolution.d.ts +2 -1
  32. package/dist/language/word-resolution.js +8 -3
  33. package/dist/rules/create-wrapper-rule.d.ts +1 -1
  34. package/dist/rules/create-wrapper-rule.js +11 -9
  35. package/dist/rules/define-shell-rule.js +1 -1
  36. package/dist/rules/no-masked-exit-status.d.ts +4 -3
  37. package/dist/rules/no-masked-exit-status.js +3 -2
  38. package/dist/rules/no-secret-env-reads.d.ts +2 -0
  39. package/dist/rules/no-secret-env-reads.js +13 -2
  40. package/dist/rules/no-secret-flags.d.ts +2 -0
  41. package/dist/rules/no-secret-flags.js +14 -3
  42. package/dist/rules/no-unchecked-cd.js +3 -2
  43. package/dist/rules/no-undeclared-commands.js +8 -3
  44. package/dist/rules/no-unused-declarations.js +6 -2
  45. package/dist/rules/prefer-local.d.ts +3 -2
  46. package/dist/rules/prefer-local.js +4 -2
  47. package/dist/rules/require-network-wrapper.d.ts +1 -1
  48. package/dist/rules/require-network-wrapper.js +11 -7
  49. package/dist/rules/require-strict-mode.d.ts +5 -46
  50. package/dist/rules/require-strict-mode.js +17 -107
  51. package/dist/rules/require-tty-guard.d.ts +2 -0
  52. package/dist/rules/require-tty-guard.js +8 -1
  53. package/dist/rules/resolve-declaration-convention.d.ts +9 -88
  54. package/dist/rules/resolve-declaration-convention.js +19 -71
  55. package/dist/rules/resolve-network-wrapper.d.ts +1 -2
  56. package/dist/rules/resolve-network-wrapper.js +18 -14
  57. package/dist/rules/resolve-transparent-wrappers.d.ts +2 -7
  58. package/dist/rules/resolve-transparent-wrappers.js +19 -9
  59. package/dist/rules/set-options.d.ts +1 -1
  60. package/dist/rules/set-options.js +1 -1
  61. package/dist/rules/strict-mode-prologue.d.ts +43 -0
  62. package/dist/rules/strict-mode-prologue.js +91 -0
  63. package/dist/rules/user-pattern.d.ts +4 -3
  64. package/dist/rules/user-pattern.js +10 -8
  65. package/dist/shell-dialect.d.ts +8 -0
  66. package/dist/shell-dialect.js +22 -0
  67. package/dist/syntax/analyze.d.ts +3 -3
  68. package/dist/syntax/analyze.js +3 -3
  69. package/dist/syntax/index.d.ts +3 -4
  70. package/dist/syntax/index.js +3 -4
  71. package/dist/version.js +2 -1
  72. package/docs/compat.md +5 -6
  73. package/docs/custom-rule-tutorial.md +22 -18
  74. package/docs/declaration-conventions.md +20 -23
  75. package/docs/error-codes.md +93 -0
  76. package/docs/examples/no-rm-rf-rule.mjs +8 -10
  77. package/docs/examples/sorted-declarations-rule.mjs +6 -9
  78. package/docs/examples/tsconfig.json +10 -0
  79. package/docs/rules/README.md +12 -4
  80. package/docs/rules/compat/no-nounset-empty-array.md +1 -1
  81. package/docs/rules/no-masked-exit-status.md +2 -2
  82. package/docs/rules/no-secret-env-reads.md +13 -3
  83. package/docs/rules/no-secret-flags.md +14 -5
  84. package/docs/rules/no-unchecked-cd.md +15 -2
  85. package/docs/rules/require-network-wrapper.md +5 -5
  86. package/docs/rules/require-strict-mode.md +25 -23
  87. package/docs/rules/require-tty-guard.md +12 -3
  88. package/docs/selectors.md +7 -2
  89. package/docs/wrapper-rules.md +2 -0
  90. package/package.json +3 -3
  91. package/dist/declaration-conventions/read-declared-commands.d.ts +0 -70
@@ -5,8 +5,7 @@
5
5
  */
6
6
  import {
7
7
  defineShellRule,
8
- readDeclaredCommands,
9
- resolveDeclarationConvention,
8
+ getDeclaredCommands,
10
9
  } from '@eslint-sh/eslint-plugin';
11
10
 
12
11
  const sortedDeclarations = defineShellRule({
@@ -16,18 +15,16 @@ const sortedDeclarations = defineShellRule({
16
15
  messages: { unsorted: 'Declare `{{name}}` before `{{previous}}`.' },
17
16
  },
18
17
  create(context) {
19
- // The bound convention, checked exactly as the built-in rules check it;
20
- // `null` when none is bound, and then the rule has nothing to check.
21
- const convention = resolveDeclarationConvention(context);
22
- if (convention === null) {
23
- return {};
24
- }
25
18
  return {
26
19
  File() {
27
20
  const { sourceCode } = context;
28
21
  // The file's declarations, checked as the built-in rules check them.
29
22
  // Only those written in the script have a range to report at.
30
- const declared = readDeclaredCommands(convention, context).filter(
23
+ const declarations = getDeclaredCommands(context);
24
+ if (declarations === null) {
25
+ return;
26
+ }
27
+ const declared = declarations.filter(
31
28
  (each) => each.kind === 'script',
32
29
  );
33
30
  for (let i = 1; i < declared.length; i++) {
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "noEmit": true,
5
+ "composite": false
6
+ },
7
+ "include": [
8
+ "*.ts"
9
+ ]
10
+ }
@@ -27,7 +27,7 @@ See the [canonical quickstart](https://github.com/mike-north/eslint-sh/blob/main
27
27
  | [`sh/compat/no-mapfile`](./compat/no-mapfile.md) | `bash-3.2`, `posix-portable` | No | Yes | Disallow `mapfile` / `readarray` when the target shell lacks them, suggesting a read loop. |
28
28
  | [`sh/compat/no-nameref`](./compat/no-nameref.md) | `bash-3.2`, `posix-portable` | No | No | Disallow namerefs (`declare -n`, `local -n`) when the target shell lacks them. |
29
29
  | [`sh/compat/no-negative-array-index`](./compat/no-negative-array-index.md) | `bash-3.2`, `posix-portable` | No | Yes | Disallow negative array subscripts when the target shell lacks them, suggesting a length-based index. |
30
- | [`sh/compat/no-nounset-empty-array`](./compat/no-nounset-empty-array.md) | `posix-portable` | No | Yes | Disallow whole-array expansions under `set -u` when the target shell aborts on an empty array, suggesting a guarded form. |
30
+ | [`sh/compat/no-nounset-empty-array`](./compat/no-nounset-empty-array.md) | None (opt-in) | No | Yes | Disallow whole-array expansions under `set -u` when the target shell aborts on an empty array, suggesting a guarded form. |
31
31
  | [`sh/compat/no-parameter-transformation`](./compat/no-parameter-transformation.md) | `bash-3.2`, `posix-portable` | No | No | Disallow `${v@op}` transformations when the target shell lacks the operator. |
32
32
  | [`sh/compat/no-pipe-all`](./compat/no-pipe-all.md) | `bash-3.2`, `posix-portable` | No | Yes | Disallow the stderr-merging pipe (pipe-ampersand) when the target shell lacks it, suggesting an explicit `2>&1` before the pipe. |
33
33
  | [`sh/compat/no-printf-time-format`](./compat/no-printf-time-format.md) | `bash-3.2`, `posix-portable` | No | No | Disallow `printf`'s `%(fmt)T` date format when the target shell lacks it. |
@@ -48,7 +48,7 @@ See the [canonical quickstart](https://github.com/mike-north/eslint-sh/blob/main
48
48
  | [`sh/quote-expansions`](./quote-expansions.md) | `recommended`, `agent-authored` | No | Yes | Require double quotes around expansions in words the shell splits and globs. |
49
49
  | [`sh/require-network-wrapper`](./require-network-wrapper.md) | `agent-authored` | No | No | Require network commands to run through your configured wrapper. |
50
50
  | [`sh/require-shebang`](./require-shebang.md) | `recommended`, `agent-authored` | Yes (missing shebang only) | No | Require a recognized shell interpreter on line 1. |
51
- | [`sh/require-strict-mode`](./require-strict-mode.md) | `recommended`, `agent-authored`, `posix-portable` (with set -eu) | Yes (when insertion satisfies the rule) | No | Require strict-mode options near the start of the script. |
51
+ | [`sh/require-strict-mode`](./require-strict-mode.md) | `recommended`, `agent-authored` | Yes (when insertion satisfies the rule) | No | Require strict-mode options near the start of the script. |
52
52
  | [`sh/require-tty-guard`](./require-tty-guard.md) | `agent-authored` | No | Yes | Require `read`, `select` and configured interactive commands to be guarded by a TTY test or supplied input. |
53
53
  | [`sh/shebang-matches-dialect`](./shebang-matches-dialect.md) | `recommended`, `agent-authored`, `posix-portable` | No | No | Require the shebang to name the same kind of shell (bash or POSIX) as `languageOptions.dialect`. |
54
54
 
@@ -64,10 +64,10 @@ shell features that your target shell lacks, as set by `languageOptions.minBashV
64
64
  `dialect`. They report nothing when `minBashVersion` is unset, so the `bash-3.2` preset sets it to `3.2`
65
65
  and the `posix-portable` preset sets `dialect: "posix"`. `bash-3.2` enables all except
66
66
  `compat/no-nounset-empty-array`, which is opt-in for scripts using `set -u` on Bash below 4.4
67
- with arrays that can really be empty; `posix-portable` retains all of them.
67
+ with arrays that can really be empty; `posix-portable` omits that Bash-only check because it cannot fire there.
68
68
  See [version compatibility](../compat.md). `compat/*` means target gates only; `shebang-matches-dialect`
69
69
  compares the file with your config whatever the target, so it lives outside that namespace. `posix-portable` also enables
70
- `shebang-matches-dialect`, `require-strict-mode` (as `set -eu`) and the POSIX-only gates
70
+ `shebang-matches-dialect` (to check the target against the declared interpreter) and the POSIX-only gates
71
71
  `compat/no-double-bracket`, `compat/no-let` and `compat/no-source`, which report those constructs
72
72
  under `dialect: "posix"` because they fail on dash. It does not report `local`, `echo -e` or the
73
73
  other constructs listed under
@@ -80,6 +80,14 @@ To run these rules beside shellcheck, see [shellcheck](../shellcheck.md).
80
80
  [wrapper rules](../wrapper-rules.md)). Each linked rule page documents its options, limitations,
81
81
  and relationship to shellcheck.
82
82
 
83
+ ## Replacement and additive lists
84
+
85
+ For the secret rules, `patterns` replaces the defaults and `additionalPatterns` adds to the
86
+ replacement (or defaults when omitted). For the TTY and network wrapper rules, `commands` and
87
+ `additionalCommands` have the same relationship. An empty replacement removes defaults; an empty
88
+ addition changes nothing. Overlapping entries do not duplicate findings. Lists without defaults,
89
+ such as `allowedSinks`, retain their plain names. Shared wrapper settings follow their own contract.
90
+
83
91
  ## Regular-expression options
84
92
 
85
93
  Every regular-expression source in a rule option is compiled with JavaScript's `u` flag
@@ -6,7 +6,7 @@ ships as `/bin/bash`.
6
6
 
7
7
  | Configs | Fixable | Languages |
8
8
  | ------- | ------- | --------- |
9
- | `posix-portable` | suggestions | `sh/sh` |
9
+ | None (opt-in) | suggestions | `sh/sh` |
10
10
 
11
11
  ## Rule details
12
12
 
@@ -136,7 +136,7 @@ does not depend on `languageOptions`.
136
136
  | Option | Type | Default | Effect |
137
137
  | ----------------- | ---------- | ------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
138
138
  | `onlyWithErrexit` | `boolean` | `false` | Report only where `errexit` is known to be on (below). |
139
- | `allowShebangOnly` | `boolean` | `false` | With `onlyWithErrexit`, count options on the shebang line (`#!/bin/bash -e`, `#!/bin/sh -e`, `#!/usr/bin/env -S bash -e`) as enabled. Same meaning and default as the option of that name in [`require-strict-mode`](./require-strict-mode.md), and read by the same code. |
139
+ | `allowShebangOnly` | `boolean` | `false` | With `onlyWithErrexit`, count options on the shebang line (`#!/bin/bash -e`, `#!/bin/sh -e`, `#!/usr/bin/env -S bash -e`) as enabled. Uses the shared shebang parser. This rule retains its own sequential-state policy; [`require-strict-mode`](./require-strict-mode.md) uses the shared prologue setting. |
140
140
  | `builtins` | `string[]` | `["local", "declare", "typeset", "export", "readonly"]` | Which declaration builtins to check, for example `["local"]` for teams that accept `export X=$(…)` at the top level. |
141
141
 
142
142
  **Where `errexit` is on.** The rule reads the plain top-level `set` statements (`set -e`,
@@ -147,7 +147,7 @@ statements counts for the code after it. Unlike `require-strict-mode` there is n
147
147
  window, because the order is tracked. A function body is read at the point where it is defined, so
148
148
  `set -e` before `f() { … }` covers `f`.
149
149
 
150
- `allowShebangOnly` is off by default for the reason it is off in `require-strict-mode`: options on the
150
+ `allowShebangOnly` is off by default for the reason it is off in the shared prologue definition: options on the
151
151
  shebang line apply only when the script is run directly, and `bash script.sh` ignores them. Turn it
152
152
  on if your scripts are always executed directly.
153
153
 
@@ -69,8 +69,8 @@ The rule matches parameter expansions only. These reads are not reported:
69
69
 
70
70
  ## Options
71
71
 
72
- - `patterns` (`string[]`, at least one entry). Regular-expression sources tested against each
73
- variable's name. They **replace** the default list. Sources use `u` (Unicode, case-sensitive):
72
+ - `patterns` (`string[]`). Regular-expression sources tested against each
73
+ variable's name. They **replace** the default list; an empty list removes all defaults. Sources use `u` (Unicode, case-sensitive):
74
74
  `patterns: ['token']` does not catch `$GITHUB_TOKEN`, while `patterns: ['TOKEN']` does.
75
75
  Lowercase names such as `$sort_key` do not match the uppercase default patterns.
76
76
  See [regular-expression options](./README.md#regular-expression-options) for compilation and
@@ -78,6 +78,10 @@ The rule matches parameter expansions only. These reads are not reported:
78
78
 
79
79
  Default: `["(_TOKEN|_SECRET|_PASSWORD|_KEY)$"]`.
80
80
 
81
+ - `additionalPatterns` (`string[]`, default `[]`). Sources added to `patterns`, or to the
82
+ defaults when `patterns` is omitted. These also compile with `u` and match exact case.
83
+ An empty addition changes nothing; overlapping patterns report each read only once.
84
+
81
85
  - `allowedSinks` (`string[]`). Command names whose own arguments may read a secret-shaped variable. Wrapper-owned
82
86
  operands are not exempt.
83
87
 
@@ -89,12 +93,18 @@ The rule matches parameter expansions only. These reads are not reported:
89
93
  rules: {
90
94
  'sh/no-secret-env-reads': [
91
95
  'error',
92
- { allowedSinks: ['vault', 'gh'], patterns: ['(_TOKEN|_SECRET|_PASSWORD|_KEY|_PAT)$'] },
96
+ { allowedSinks: ['vault', 'gh'], additionalPatterns: ['_PAT$'] },
93
97
  ],
94
98
  },
95
99
  }
96
100
  ```
97
101
 
102
+ With this configuration, both `echo "$GITHUB_TOKEN"` and `echo "$ACCESS_PAT"` report;
103
+ `echo "$access_pat"` stays clean. `{ patterns: ['^PIN$'], additionalPatterns: ['_PAT$'] }`
104
+ reports `$PIN` and `$ACCESS_PAT`, and leaves `$GITHUB_TOKEN` alone. `{ patterns: [] }` checks no names;
105
+ adding `additionalPatterns` to it checks only those added patterns. A lowercase addition
106
+ `['token']` does not catch `$GITHUB_TOKEN`; flag patterns use different case semantics.
107
+
98
108
  Regex syntax errors fail when this rule first runs on a file, naming the rule, option and
99
109
  pattern. `--print-config` will not catch any regex syntax errors. See the
100
110
  [shared compilation contract](./README.md#regular-expression-options).
@@ -51,35 +51,44 @@ digit (`--password2`, `--token1`) or by another `-segment` (`--secret-key`,
51
51
 
52
52
  Other qualifiers aren't matched, so `--my-token` is not reported. A general prefix would also
53
53
  match negations such as `--no-password`, which take no value. Add a project's own flag names
54
- with the `patterns` option.
54
+ with the `additionalPatterns` option.
55
55
 
56
56
  Expect some false positives: a name such as `--token-type` still matches. Suppress a single false
57
57
  positive with a disable comment that says why, or narrow the `patterns` option for your project.
58
58
 
59
59
  ## Options
60
60
 
61
- - `patterns` (`string[]`, at least one entry). Regular-expression sources tested against each flag's
61
+ - `patterns` (`string[]`). Regular-expression sources tested against each flag's
62
62
  name, without `=` or the value. They **replace** the default list. Each source is compiled
63
- with `iu` (Unicode, case-insensitive), like the default. For example, `patterns: ['token']`
63
+ with `iu` (Unicode, case-insensitive), like the default. An empty list removes all defaults. For example, `patterns: ['token']`
64
64
  catches `cli --TOKEN=abc`; capitals in a custom pattern also match lowercase flag names.
65
65
  See [regular-expression options](./README.md#regular-expression-options) for compilation and
66
66
  configuration errors, which name the rule, option index and source.
67
67
 
68
68
  Default: `["^(?!.*-(?:stdin|file)$)--?(?:access-|auth-?)?(?:password|passwd|token|secret|api-?key|auth(?:key|orization)?)(?![a-z])"]`.
69
69
 
70
+ - `additionalPatterns` (`string[]`, default `[]`). Sources added to `patterns`, or to the
71
+ defaults when `patterns` is omitted. These also compile with `iu`. An empty addition changes
72
+ nothing; overlapping patterns report each flag only once.
73
+
70
74
  ```js
71
75
  // eslint.config.js
72
76
  {
73
77
  rules: {
74
78
  'sh/no-secret-flags': [
75
79
  'error',
76
- // Only these exact names, plus a project-specific flag.
77
- { patterns: ['^--?(password|token|api-?key)$', '^--vault-pin$'] },
80
+ // Keep the defaults and also catch --vault-pin (in any case).
81
+ { additionalPatterns: ['^--vault-pin$'] },
78
82
  ],
79
83
  },
80
84
  }
81
85
  ```
82
86
 
87
+ With this configuration, both `cli --token=abc` and `cli --VAULT-PIN=1234` report.
88
+ For a narrower base, `{ patterns: ['^--pin$'], additionalPatterns: ['^--vault-pin$'] }`
89
+ reports `--pin` and `--vault-pin`, and leaves `--token` alone. `{ patterns: [] }` checks no names;
90
+ adding `additionalPatterns` to it checks only those added patterns.
91
+
83
92
  Regex syntax errors fail when this rule first runs on a file, naming the rule, option and
84
93
  pattern. `--print-config` will not catch any regex syntax errors. See the
85
94
  [shared compilation contract](./README.md#regular-expression-options).
@@ -35,8 +35,21 @@ read as `cd`.
35
35
  by itself, so the rule is quiet there. "Prologue" is read exactly as
36
36
  [`require-strict-mode`](./require-strict-mode.md) reads it: the first five top-level statements,
37
37
  tracked in order, so a later `set +e` undoes an earlier `set -e`, and a `set -e` outside that
38
- window does not count. Only `set` statements count; shebang flags do not. Set
39
- `ignoreWhenErrexit` to `false` to report anyway.
38
+ window does not count. Those are the defaults. Both rules read the shared project definition:
39
+
40
+ ```js
41
+ {
42
+ settings: { sh: { strictModePrologue: { allowShebangOnly: true, withinStatements: 10 } } },
43
+ rules: { 'sh/no-unchecked-cd': 'error' },
44
+ }
45
+ ```
46
+
47
+ With this definition, `#!/bin/bash -e` counts and a `set -e` at statement six counts for both
48
+ rules. The defaults are `allowShebangOnly: false` (shebang flags are bypassed by
49
+ `bash script.sh`) and `withinStatements: 5`. Invalid explicit values throw a named shared-setting
50
+ configuration error; the fields are not per-rule options. See
51
+ [the shared prologue definition](./require-strict-mode.md#shared-prologue-definition).
52
+ Set `ignoreWhenErrexit` to `false` to report anyway.
40
53
 
41
54
  **Files run by bats.** With `languageOptions.dialect` set to `"bats"`, a bare `cd` (or `pushd` or
42
55
  `popd`) written directly in the body of a `@test` block, or of a top-level `setup`, `teardown`,
@@ -72,15 +72,15 @@ It does not depend on `languageOptions`.
72
72
  `assignmentOperands`, as in `sh-ast/analyze`'s `WrapperSpec`.
73
73
  - `commands` (`string[]`, default `["curl", "wget", "nc", "ssh", "scp", "rsync", "git"]`): the
74
74
  network commands. Setting it replaces the default list. `git` keeps its subcommand restriction
75
- wherever it is listed.
76
- - `alsoTreatAsNetwork` (`string[]`, default `[]`): more commands to check, added to `commands` (or
77
- to its default).
75
+ wherever it is listed. An empty list removes all built-in commands.
76
+ - `additionalCommands` (`string[]`, default `[]`): more commands to check, added to `commands` (or
77
+ to its default). An empty addition changes nothing; overlapping names report only once.
78
78
 
79
79
  ```js
80
80
  // eslint.config.js
81
81
  {
82
82
  rules: {
83
- 'sh/require-network-wrapper': ['error', { wrapper: 'retry', alsoTreatAsNetwork: ['aws'] }],
83
+ 'sh/require-network-wrapper': ['error', { wrapper: 'retry', additionalCommands: ['aws'] }],
84
84
  },
85
85
  }
86
86
  ```
@@ -164,7 +164,7 @@ wget() { :; }; wget x # a function the file defines, not the clie
164
164
  Leave the rule off if your project has no single wrapper for network calls, or if network
165
165
  resilience is handled outside your scripts (for example by a proxy or by the CI runner). For a
166
166
  policy about commands other than network clients, define your own rule with
167
- [`createWrapperRule`](../wrapper-rules.md) rather than overloading `alsoTreatAsNetwork`.
167
+ [`createWrapperRule`](../wrapper-rules.md) rather than overloading `additionalCommands`.
168
168
 
169
169
  ## Relationship to shellcheck
170
170
 
@@ -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`, `posix-portable` (with set -eu) | autofix | `sh/sh` |
8
+ | `recommended`, `agent-authored` | autofix | `sh/sh` |
9
9
 
10
10
  ## Rule details
11
11
 
@@ -70,7 +70,7 @@ again. Decide what that `set +e` is for, then edit the file by hand.
70
70
 
71
71
  ### Dialects
72
72
 
73
- When `mode` is not configured, the required prologue depends on `languageOptions.dialect`:
73
+ When `requiredOptions` is not configured, the required prologue depends on `languageOptions.dialect`:
74
74
 
75
75
  | `dialect` | Default prologue |
76
76
  | -------------------------------------------- | ------------------- |
@@ -82,7 +82,7 @@ still reject `set -o pipefail`. An autofix that inserted it would break a workin
82
82
  on those shells, so under `posix` the default requires and inserts only `set -eu`. A `posix` file
83
83
  may still turn `pipefail` on; the rule just doesn't require it.
84
84
 
85
- A configured `mode` always wins, whatever the dialect. To require `pipefail` in POSIX scripts too,
85
+ Configured `requiredOptions` always win, whatever the dialect. To require `pipefail` in POSIX scripts too,
86
86
  configure it (see below).
87
87
 
88
88
  The dialect comes from `languageOptions`, not from the shebang. A `#!/bin/sh` file linted under the
@@ -94,20 +94,14 @@ shebang by [`sh/shebang-matches-dialect`](./shebang-matches-dialect.md); set
94
94
 
95
95
  This rule takes one options object:
96
96
 
97
- - `mode` (`string | string[]`, default `"set -euo pipefail"`, or `"set -eu"` under the `posix`
98
- dialect): the required prologue.
99
- - A **string** must be a `set` command that only turns options on, such as `"set -eu"` or
100
- `"set -o errexit -o nounset"`. The options it turns on are what the rule requires, and the
101
- string is the exact text the autofix inserts. A string that is not such a command (for example
102
- `"set +e"` or `"echo hi"`) is a configuration error.
103
- - A **list** names the required options by their long names (`errexit`, `nounset`, `pipefail`,
104
- `noclobber`, …, any name `set -o` accepts). The autofix inserts a `set` command built from the
105
- list. Options with a one-letter flag go into one cluster in list order. The first option with no
106
- letter is attached to that cluster with `o`, and any others follow as `-o <name>`. So
107
- `["errexit", "nounset", "pipefail"]` inserts `set -euo pipefail` and `["errexit", "nounset"]`
108
- inserts `set -eu`.
109
-
110
- <!-- transform: {"kind": "fix", "options": {"mode": ["errexit", "nounset", "pipefail"]}} -->
97
+ - `requiredOptions` (`string[]`, dialect-aware default above): a nonempty list of unique
98
+ long option names from the supported `set -o` domain (`errexit`, `nounset`, `pipefail`,
99
+ `noclobber`, …). ESLint rejects strings, empty lists, duplicates, unknown names and short
100
+ aliases as configuration errors. The autofix always builds a `set` command from this list:
101
+ options with a one-letter flag go into one cluster in list order, the first long-only option
102
+ follows that cluster with `o`, and any others follow as `-o <name>`.
103
+
104
+ <!-- transform: {"kind": "fix", "options": {"requiredOptions": ["errexit", "nounset", "pipefail"]}} -->
111
105
 
112
106
  ```sh
113
107
  echo hi
@@ -116,7 +110,7 @@ This rule takes one options object:
116
110
  echo hi
117
111
  ```
118
112
 
119
- <!-- transform: {"kind": "fix", "options": {"mode": ["errexit", "nounset"]}} -->
113
+ <!-- transform: {"kind": "fix", "options": {"requiredOptions": ["errexit", "nounset"]}} -->
120
114
 
121
115
  ```sh
122
116
  echo hi
@@ -124,6 +118,15 @@ This rule takes one options object:
124
118
  set -eu
125
119
  echo hi
126
120
  ```
121
+
122
+ ### Shared prologue definition
123
+
124
+ Both this rule and [`sh/no-unchecked-cd`](./no-unchecked-cd.md) read
125
+ `settings: { sh: { strictModePrologue: { allowShebangOnly: false, withinStatements: 5 } } }`.
126
+ These fields are shared settings, not rule options. Only an absent value uses defaults;
127
+ malformed objects, unknown fields, non-boolean flags and non-positive or non-integer windows
128
+ throw a configuration error naming the shared binding when either rule loads for a file.
129
+
127
130
  - `withinStatements` (integer ≥ 1, default `5`): how many top-level statements from the top of the
128
131
  file the prologue may take to appear.
129
132
  - `allowShebangOnly` (`boolean`, default `false`): when `true`, options passed to the shell on the
@@ -151,11 +154,10 @@ option. Use an absolute env path and `-S` for both systems.
151
154
  ```js
152
155
  // eslint.config.js
153
156
  {
157
+ settings: { sh: { strictModePrologue: { allowShebangOnly: false, withinStatements: 5 } } },
154
158
  rules: {
155
159
  'sh/require-strict-mode': ['error', {
156
- mode: 'set -euo pipefail',
157
- withinStatements: 5,
158
- allowShebangOnly: false,
160
+ requiredOptions: ['errexit', 'nounset', 'pipefail'],
159
161
  }],
160
162
  },
161
163
  }
@@ -169,7 +171,7 @@ For POSIX `sh` scripts that only run on shells with `pipefail` support, and shou
169
171
  files: ['**/*.sh'],
170
172
  languageOptions: { dialect: 'posix' },
171
173
  rules: {
172
- 'sh/require-strict-mode': ['error', { mode: ['errexit', 'nounset', 'pipefail'] }],
174
+ 'sh/require-strict-mode': ['error', { requiredOptions: ['errexit', 'nounset', 'pipefail'] }],
173
175
  },
174
176
  }
175
177
  ```
@@ -236,7 +238,7 @@ set -e
236
238
  <!-- example: {"dialect": "posix"} -->
237
239
  ```sh
238
240
  #!/bin/sh
239
- # with languageOptions: { dialect: 'posix' } and no configured mode
241
+ # with languageOptions: { dialect: 'posix' } and no configured requiredOptions
240
242
  set -eu
241
243
  ```
242
244
 
@@ -11,7 +11,7 @@ have their input supplied explicitly.
11
11
 
12
12
  A prompt that waits on the terminal hangs forever when a script runs without one: in CI, from
13
13
  cron, or from an agent's tool call. In automation it also reads whatever happens to be on stdin.
14
- This rule reports `read` and `select` (and any command you add with `commands`) that are not
14
+ This rule reports `read` and `select` (and any command you add with `additionalCommands`) that are not
15
15
  guarded.
16
16
 
17
17
  The check is **purely syntactic**. The rule never follows data or control flow, so what it treats
@@ -55,18 +55,27 @@ The rule is honest about what it cannot see:
55
55
 
56
56
  - `commands` (`string[]`, default `['read', 'select']`): the interactive commands to check. A
57
57
  configured list **replaces** the default, as with `sh/require-network-wrapper`; include `read`
58
- and `select` to keep them. `select` is parsed as a loop, not a command, and is checked whenever
58
+ and `select` to keep them. An empty list removes all defaults. `select` is parsed as a loop, not a command, and is checked whenever
59
59
  the list contains `select`.
60
60
 
61
+ - `additionalCommands` (`string[]`, default `[]`): commands added to `commands`, or to
62
+ `read` and `select` when `commands` is omitted. An empty addition changes nothing;
63
+ overlapping names report only once.
64
+
61
65
  ```js
62
66
  // eslint.config.js
63
67
  {
64
68
  rules: {
65
- 'sh/require-tty-guard': ['error', { commands: ['read', 'select', 'confirm'] }],
69
+ 'sh/require-tty-guard': ['error', { additionalCommands: ['confirm'] }],
66
70
  },
67
71
  }
68
72
  ```
69
73
 
74
+ This configuration checks `read`, `select` and `confirm`.
75
+ `{ commands: ['ask'], additionalCommands: ['confirm'] }` checks `ask` and `confirm`,
76
+ leaving `read` and `select` alone. `{ commands: [], additionalCommands: ['confirm'] }`
77
+ checks only `confirm`.
78
+
70
79
  ## Suggestion
71
80
 
72
81
  The rule offers one suggestion per report: wrap the statement in `if [ -t 0 ]; then … fi`:
package/docs/selectors.md CHANGED
@@ -16,13 +16,18 @@ Looking for a complete, runnable `create()` that uses one of these selectors —
16
16
 
17
17
  | Class | Matches |
18
18
  | -------------- | ---------------------------------------------------- |
19
- | `:command` | `CallExpr` |
19
+ | `:command` | `CallExpr` with a command word (nonempty `args`) |
20
20
  | `:loop` | `ForClause`, `WhileClause` |
21
21
  | `:conditional` | `IfClause`, `CaseClause` |
22
22
  | `:expansion` | `ParamExp`, `CmdSubst`, `ArithmExp`, `ProcSubst` |
23
23
  | `:quoted` | `SglQuoted`, `DblQuoted` |
24
24
  | `:function` | `FuncDecl` |
25
25
 
26
+ `:command` matches exactly the calls with an entry in `sourceCode.getCommandSites()`.
27
+ An assignment-only statement such as `FOO=1` has no command site and does not match.
28
+ `FOO=1 jq .`, `true`, `:`, and dynamic invocations such as `"$tool" file` do match.
29
+ Select `CallExpr` directly when you want every call node, including assignment-only calls.
30
+
26
31
  `:loop` deliberately does **not** include `CStyleLoop` — that node is `ForClause.loop`'s nested
27
32
  C-style arithmetic header (`init`/`cond`/`post`), not a loop statement in its own right. A
28
33
  `for ((i=0;i<3;i++))` loop's outer node is still `ForClause`, so `:loop` already reaches it.
@@ -54,7 +59,7 @@ behind rules like "forbid calling `curl` directly, use the wrapped helper instea
54
59
  :function :command
55
60
  ```
56
61
 
57
- A descendant combinator: any `CallExpr` that has a `FuncDecl` ancestor. Useful for rules that only
62
+ A descendant combinator: any command site that has a `FuncDecl` ancestor. Useful for rules that only
58
63
  apply to commands run from within a shell function (e.g. "functions must not shell out to `sudo`").
59
64
 
60
65
  ### Match every command nested inside a conditional
@@ -213,3 +213,5 @@ A `WrapperSpec` may set `shellBuiltin: true` for a wrapper that runs inside the
213
213
  Such entries match exact names: an executable such as `/tmp/command` is not the `command`
214
214
  builtin and its argument words remain opaque. Omitted or false retains utility-wrapper path
215
215
  matching. The same boolean field is accepted in the network-wrapper option and setting.
216
+
217
+ See [error codes](error-codes.md) for stable configuration and helper failure identities.
package/package.json CHANGED
@@ -1,12 +1,12 @@
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.8.0",
4
+ "version": "0.9.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.9.0"
9
+ "sh-ast": "^0.10.0"
10
10
  },
11
11
  "devDependencies": {
12
12
  "eslint": "^10.7.0"
@@ -77,7 +77,7 @@
77
77
  "directory": "packages/eslint-plugin"
78
78
  },
79
79
  "scripts": {
80
- "build": "tsc -p tsconfig.json && node ../../scripts/rollup-declarations.mjs api-extractor.json syntax/api-extractor.json syntax/analyze/api-extractor.json",
80
+ "build": "node ../../scripts/materialize-shared-source.mjs && tsc -p tsconfig.json && node ../../scripts/rollup-declarations.mjs api-extractor.json syntax/api-extractor.json syntax/analyze/api-extractor.json && cp types/eslint-plugin.d.ts dist/eslint-plugin.d.ts",
81
81
  "check": "concurrently -n lint,types,api-report \"pnpm run check:lint-ts\" \"pnpm run check:typecheck\" \"pnpm run check:api-report\"",
82
82
  "check:api-report": "api-extractor run && api-extractor run --config syntax/api-extractor.json && api-extractor run --config syntax/analyze/api-extractor.json",
83
83
  "check:lint-ts": "eslint .",
@@ -1,70 +0,0 @@
1
- /**
2
- * The one path by which a rule reads a bound convention's declarations for
3
- * the file it is linting. The built-in consumers (`sh/no-undeclared-commands`,
4
- * `sh/no-unused-declarations`) and a pack's own rules all call
5
- * {@link readDeclaredCommands}, so they hand the convention the same
6
- * {@link LintedFile} and trust only declarations that passed the same checks.
7
- *
8
- * The checks exist because a declaration's kind decides where a report goes
9
- * and whether a fix is offered. A declaration of an unknown kind, one missing
10
- * its kind's fields, or a script range outside the text would otherwise be
11
- * reported at a made-up location, crash deep inside a rule, or be silently
12
- * accepted by a rule that only reads names. Failing with the declaration and
13
- * the setting in the message makes the convention's bug the thing the user
14
- * sees.
15
- */
16
- import type { ShellSourceCode } from '../language/shell-source-code.js';
17
- import type { DeclarationConvention, DeclaredCommand, LintedFile } from './declaration-convention.js';
18
- /**
19
- * What {@link readDeclaredCommands} reads from a rule's context: the linted
20
- * file and its source code. Any shell rule's `context` satisfies it.
21
- *
22
- * @public
23
- */
24
- export interface DeclarationReadContext extends LintedFile {
25
- /** The source code of the file being linted. */
26
- readonly sourceCode: ShellSourceCode;
27
- }
28
- /**
29
- * The only view of the linted file a convention's hooks receive: `filename`,
30
- * `physicalFilename` and `cwd`, frozen. A rule's `context` must never reach a
31
- * hook, because it would let a convention report, read options or reach other
32
- * rule state.
33
- *
34
- * @internal
35
- */
36
- export declare function toLintedFile(context: LintedFile): LintedFile;
37
- /**
38
- * Returns the declarations `convention` makes for the file `context` is
39
- * linting, after checking each one against its kind. Call it from a rule's
40
- * listener with the convention `resolveDeclarationConvention` returned and
41
- * the rule's own `context`.
42
- *
43
- * The convention receives a frozen {@link LintedFile} holding only
44
- * `filename`, `physicalFilename` and `cwd`, never the whole context, so it
45
- * sees which file it is reading and nothing it could report through.
46
- *
47
- * Throws an `Error` naming the declaration-convention setting and showing the
48
- * offending value when
49
- * `declaredCommands` does not return an array, or returns a declaration with
50
- * a missing or unknown `kind`, without its kind's fields, or with a `range`
51
- * outside the linted text.
52
- *
53
- * @example
54
- * ```ts
55
- * const convention = resolveDeclarationConvention(context);
56
- * if (convention === null) return {};
57
- * return {
58
- * File() {
59
- * for (const declared of readDeclaredCommands(convention, context)) {
60
- * if (declared.kind === 'script') {
61
- * // declared.range is an offset pair into context.sourceCode.text
62
- * }
63
- * }
64
- * },
65
- * };
66
- * ```
67
- *
68
- * @public
69
- */
70
- export declare function readDeclaredCommands(convention: DeclarationConvention, context: DeclarationReadContext): DeclaredCommand[];