@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
package/README.md CHANGED
@@ -46,16 +46,22 @@ directory it runs in.
46
46
  ## CommonJS config
47
47
 
48
48
  In a package without `"type": "module"` (the ESLint default), `eslint.config.js` is CommonJS, as
49
- is `eslint.config.cjs` in any package. `@eslint-sh/eslint-plugin` is ESM-only, so
50
- `require('@eslint-sh/eslint-plugin')` returns the module namespace and the plugin object is its
51
- `.default`. Named exports such as `commentPragma` are ordinary properties of the `require` result.
52
- Without `.default`, `sh.configs` is `undefined` and ESLint fails with
53
- `Cannot read properties of undefined (reading 'recommended')`.
49
+ is `eslint.config.cjs` in any package. `require('@eslint-sh/eslint-plugin')` returns the plugin object
50
+ directly. Named helpers such as `commentPragma` are properties of that same object;
51
+ `require('@eslint-sh/eslint-plugin').default` also returns it. These helper/default properties are
52
+ non-enumerable, so they do not change ESLint's plugin or config serialization.
53
+ Both packages remain ESM-only and use Node's synchronous `require(ESM)` support
54
+ (Node 22.13+ on the 22.x line, or Node 24+). ESM default and named imports are unchanged.
55
+
56
+ For TypeScript CommonJS consumers, use `import sh = require('@eslint-sh/eslint-plugin')` with
57
+ `module: "node20"` and `moduleResolution: "node16"`, or `module: "nodenext"` and
58
+ `moduleResolution: "nodenext"`. `module: "node16"` models Node without `require(ESM)`
59
+ and rejects this ESM-only package; `skipLibCheck` is not a remedy.
54
60
 
55
61
  ```js
56
62
  // eslint.config.js (CommonJS package) or eslint.config.cjs
57
63
  const { defineConfig } = require('eslint/config');
58
- const sh = require('@eslint-sh/eslint-plugin').default;
64
+ const sh = require('@eslint-sh/eslint-plugin');
59
65
  // Named exports need no `.default`.
60
66
  const { commentPragma } = require('@eslint-sh/eslint-plugin');
61
67
 
@@ -71,24 +77,32 @@ module.exports = defineConfig([
71
77
 
72
78
  ## Public API
73
79
 
80
+ - `module.exports` — Node's quoted interop export makes plain `require()` return the plugin
81
+ with its hidden default and named helpers. ESM consumers use the ordinary exports below.
82
+
74
83
  - `plugin` (default export) — `{ meta: { name, version, namespace: "sh" }, languages: { sh:
75
84
  ShellLanguage }, rules, configs: { recommended, 'agent-authored', 'bash-3.2', 'posix-portable' } }`. `meta.version` always
76
85
  matches this package's `package.json`. Each config is a single config object with no `files`
77
86
  key (`bash-3.2` and `posix-portable` also set `languageOptions`); extend it under your own `files`:
78
87
  `defineConfig({ files: ['**/*.sh'], extends: [sh.configs.recommended] })`.
79
- - `ShellLanguage` — the `@eslint/core` `Language` implementation: `fileType: "text"`,
88
+ - `ShellLanguage` — type-only annotation for `plugin.languages.sh`, the registered runtime
89
+ language. Select it through `language: 'sh/sh'`; construction is internal. The `@eslint/core`
90
+ `Language` implementation has `fileType: "text"`,
80
91
  `lineStart`/`columnStart: 1`, `nodeTypeKey: "type"`, the consumer's schema-derived `visitorKeys`,
81
92
  `validateLanguageOptions`/`normalizeLanguageOptions` for the
82
93
  `{dialect, minBashVersion, directivePrefix}` schema, `parse`/`createSourceCode`, and
83
94
  `matchesSelectorClass` for the six launch esquery selector classes (`:command`, `:loop`,
84
95
  `:conditional`, `:expansion`, `:quoted`, `:function` — see the
85
96
  [selector cookbook](./docs/selectors.md)).
86
- - `ShellSourceCode` — extends `@eslint/plugin-kit`'s `TextSourceCodeBase`; implements `traverse()`
97
+ - `ShellSourceCode` — type-only annotation for the instance ESLint supplies as
98
+ `context.sourceCode`; construction belongs to the language pipeline. It extends `@eslint/plugin-kit`'s `TextSourceCodeBase`; implements `traverse()`
87
99
  (driven by the consumer's `visitorKeys`, yielding two-phase `VisitNodeStep`s), `getParent()`, and
88
100
  the disable-directive surface (`getDisableDirectives()`, `getInlineConfigNodes()`,
89
101
  `applyInlineConfig()`). Rule authors get helpers that delegate to `sh-ast/analyze`, so every rule
90
- gets the same answer: `resolveWordText(word, options?)` (a word's literal text, or `null` if
91
- `resolveWord` does not report it static), `getCommandName(callExpr)` (the static command word,
102
+ gets the same answer: `resolveWord(word, options?)` (the full `WordResolution`) and
103
+ `resolveWordText(word, options?)` (exactly its static text, or `null`). Both use grammar-position
104
+ defaults: command-argument context for `CallExpr.args`, conservative tilde handling elsewhere;
105
+ explicit options override those defaults. Helpers also include `getCommandName(callExpr)` (the static command word,
92
106
  without following wrappers), and `resolveInvokedCommand(callExpr, { transparentWrappers? })` (the
93
107
  full `Argv0Resolution` once wrappers such as `env`, `sudo`, and `exec` are followed, including the
94
108
  wrapper `chain` and the `effective` command or the reason it is unknowable). The `dialect` and
@@ -116,7 +130,7 @@ ShellLanguage }, rules, configs: { recommended, 'agent-authored', 'bash-3.2', 'p
116
130
  which follows wrappers. `ResolveArgv0Options` is that method's option bag (and
117
131
  `ShellSourceCode.resolveInvokedCommand`'s): `transparentWrappers` replaces the wrapper table
118
132
  followed.
119
- - `WordResolution` — what `resolveWord` and a site's `argv` words report: `{ static: true, text }`
133
+ - `WordResolution` — what `ShellSourceCode.resolveWord` and a site's `argv` words report: `{ static: true, text }`
120
134
  when the word's value is known without running the script, or `{ static: false, reason }` when
121
135
  it is not (`reason` is `'expansion'`, `'tilde'`, `'glob'`, `'brace'`, `'locale'` or
122
136
  `'unsupported'`; a new reason can appear in a minor release, so keep a `default` branch). Check
@@ -125,10 +139,6 @@ ShellLanguage }, rules, configs: { recommended, 'agent-authored', 'bash-3.2', 'p
125
139
  `'bats'` and `'zsh'`.
126
140
  - `Argv0Resolution` — the type `ShellSourceCode.resolveInvokedCommand` returns, re-exported from
127
141
  `sh-ast/analyze`.
128
- - `resolveWord` — adapts a parsed lint Word to `sh-ast`'s word-resolution helper, so a project that installs only
129
- this package can write custom rules that resolve static shell words (e.g. the `no-rm-rf` example
130
- in the [custom-rule tutorial](./docs/custom-rule-tutorial.md)) without a separate `sh-ast`
131
- install. Fabricated nodes are rejected because they have no original syntax association.
132
142
  - `DeclarationConvention` / `DeclaredCommand` / `commentPragma` — the extension point for how a
133
143
  project declares the external commands its scripts use, bound with
134
144
  `settings: { sh: { declarationConvention } }`. `commentPragma` is the reference convention,
@@ -140,8 +150,9 @@ ShellLanguage }, rules, configs: { recommended, 'agent-authored', 'bash-3.2', 'p
140
150
  snapshot), and the
141
151
  linted file a convention is told about so it can find a per-script sidecar. See [declarations
142
152
  outside the script](./docs/declaration-conventions.md#declarations-outside-the-script).
143
- - `readDeclaredCommands` / `DeclarationReadContext` — reads the bound convention's declarations
144
- for the file a rule is linting, checked exactly as the built-in rules check them. See [reading
153
+ - `getDeclaredCommands` — reads validated declarations for the linted file using
154
+ `settings.sh.declarationConvention` only. Returns `null` when no convention is bound and `[]`
155
+ when a bound convention declares nothing. See [reading
145
156
  the convention in your own
146
157
  rule](./docs/declaration-conventions.md#reading-the-convention-in-your-own-rule).
147
158
  - `defineShellRule` / `ShellRule` / `ShellRuleSpec` / `ShellRuleMeta` / `ShellRuleContext` /
@@ -153,10 +164,10 @@ ShellLanguage }, rules, configs: { recommended, 'agent-authored', 'bash-3.2', 'p
153
164
  it too. `ShellListener<Node>` is the type of one listener, so a helper that builds listeners for
154
165
  several rules can name its return type; `ShellTypedListeners` is the node-type-keyed half of
155
166
  `ShellRuleVisitor`. See [Writing your own rules](#writing-your-own-rules).
156
- - `resolveDeclarationConvention` / `DeclarationConventionBindingContext` — resolves the declaration
157
- convention bound for a rule (its `declarationConvention` option, else
158
- `settings: { sh: { declarationConvention: commentPragma } }`, else `null`) with the validation and error messages the
159
- built-in consumers use, so a rule of your own can read the convention. See [declaration
167
+ - `resolveDeclarationConvention` — resolves the convention in
168
+ `settings.sh.declarationConvention`, or `null` when absent. It accepts `{ settings }` and checks
169
+ the convention's hooks for rules that need `removeDeclaration` or another hook. Read
170
+ declarations through `getDeclaredCommands` so their output is validated too. See [declaration
160
171
  conventions](./docs/declaration-conventions.md#reading-the-convention-in-your-own-rule).
161
172
  - `resolveTransparentWrappers` / `TransparentWrappersBindingContext` — resolves project wrapper
162
173
  facts from `settings: { sh: { transparentWrappers: ['with-retry'] } }` and the shared network
@@ -180,8 +191,8 @@ the record types (`Node`, `Word`, `CallExpr`, `SyntaxPos`, `GoString`, …), and
180
191
  helpers. Import them from this package's subpaths, not from `sh-ast`:
181
192
 
182
193
  ```js
183
- // View-aware helpers: take lint-view nodes.
184
- import { resolveWord } from '@eslint-sh/eslint-plugin';
194
+ // Rules use context.sourceCode.resolveWord(word) for lint-view Word nodes.
195
+ import { defineShellRule } from '@eslint-sh/eslint-plugin';
185
196
  // sh-ast's root entry point: parse, walk, and print faithful records.
186
197
  import { parseSync, printSync, walk } from '@eslint-sh/eslint-plugin/syntax';
187
198
  // sh-ast/analyze: command enumeration and wrapper resolution over faithful records.
@@ -196,11 +207,11 @@ import {
196
207
  `sh-ast` this plugin depends on, the same module instance its language parses with, so you
197
208
  don't install `sh-ast` yourself and records always match the parser that made them. Nothing is
198
209
  wrapped or renamed; each name keeps `sh-ast`'s own contract and docs.
199
- - The two `resolveWord`s differ. The root's takes a lint-view `ShellNode`; the one in
200
- `…/syntax/analyze` takes a faithful `Node`. That is why the subpaths are kept apart from the
201
- root.
210
+ - `context.sourceCode.resolveWord(word)` accepts a lint-view Word and uses grammar-position
211
+ defaults. `resolveWord` in `…/syntax/analyze` accepts a faithful syntax record and retains
212
+ upstream defaults. The root exports no free word-resolution function.
202
213
  - **Versioning.** `…/syntax` and `…/syntax/analyze` track this plugin's pinned `sh-ast` minor
203
- (currently `sh-ast@^0.9.0`). Under 0.x, a different `sh-ast` minor is a breaking change, so
214
+ (currently `sh-ast@^0.10.0`). Under 0.x, a different `sh-ast` minor is a breaking change, so
204
215
  an `sh-ast` 0.x minor bump is a minor bump of this plugin, and the changelog entry names the
205
216
  new `sh-ast` version.
206
217
 
@@ -216,22 +227,16 @@ the file again, or in a fresh process, usually gets past a runtime failure.
216
227
 
217
228
  ### Known parser gaps
218
229
 
219
- The parser is the upstream `sh-ast`, and it rejects three valid Bash forms. Each is reported as a
230
+ The parser is the upstream `sh-ast`, and it rejects two valid Bash forms. Each is reported as a
220
231
  syntax error. Until the parser accepts them, write the workaround in the right-hand column.
221
232
 
222
233
  | Valid Bash form | Workaround |
223
234
  | ------------------------ | -------------------------- |
224
235
  | `x=$((a \|\| b) \| c)` | `x=$( (a \|\| b) \| c )` |
225
- | `x=(${y}[0])` | `word=${y}[0]; x=($word)` |
226
236
  | `declare -A m=([a.b]=1)` | `declare -A m=(["a.b"]=1)` |
227
237
 
228
238
  - `x=$((a || b) | c)`: the parser reads `$((` as the start of arithmetic. A space after `$(` makes
229
239
  it a command substitution containing a subshell.
230
- - `x=(${y}[0])`: inside an array literal, any element word that ends in an unquoted `[…]` is read
231
- as an indexed element (`[0]=value`) and must be followed by `=`. It is not limited to a word
232
- that starts with an expansion: `x=(a[0])`, `x=(a${y}[0])` and `x=("${y}"[0])` are rejected too
233
- (a `[0]` that is meant as a glob character class). Building the word in its own statement keeps
234
- the same splitting and globbing.
235
240
  - An associative-array subscript containing a `.` that is not quoted: the parser reads the
236
241
  subscript as arithmetic and rejects the dot ("floating point arithmetic is a zsh feature"). The
237
242
  gap is wider than the `declare` form above. It also rejects `m[a.b]=1` and `${m[a.b]}`, which
@@ -279,3 +284,5 @@ fixes (and when a printed fix reflows code, so a position-based text edit is the
279
284
  and a TypeScript rule. `docs/` ships with this package (see `package.json#files`), so the
280
285
  tutorial and its example files (`docs/examples/`) are available in every installed copy — not just
281
286
  on GitHub.
287
+
288
+ See the [error-code reference](docs/error-codes.md) for stable failure identities and dependency boundaries.
package/dist/api.d.ts ADDED
@@ -0,0 +1,92 @@
1
+ /**
2
+ * The plugin boundary: registered runtime language, instance types and rule helpers.
3
+ * Language/source classes are type-only here; ESLint owns their construction and
4
+ * supplies validated source instances to rules through the registered language.
5
+ */
6
+ import type { ConfigObject } from '@eslint/core';
7
+ import { ShellLanguage } from './language/shell-language.js';
8
+ export type { ShellLanguage } from './language/shell-language.js';
9
+ export type { ShellSourceCode } from './language/shell-source-code.js';
10
+ export type { NormalizedShellLanguageOptions, ShellLanguageOptions, ShellLanguageOptionsJSON, } from './language/shell-language-options.js';
11
+ export type { Argv0Resolution } from 'sh-ast/analyze';
12
+ export type { CommandContext, ResolveArgv0Options, WordResolution } from 'sh-ast/analyze';
13
+ export type { ShellCommandSite } from './language/command-resolution.js';
14
+ export type { ShellComment, ShellFile, ShellNode, ShellNodeField, ShellNodeMap, ShellNodeOf, ShellNodeType, ShellPosition, } from './language/syntax-view.js';
15
+ export type { ShellDialect } from './language/shell-language-options.js';
16
+ export type { DeclarationConvention, DeclaredCommand, FileDeclaredCommand, LintedFile, DescribedDeclaredCommand, ScriptDeclaredCommand, } from './declaration-conventions/declaration-convention.js';
17
+ export { getDeclaredCommands } from './declaration-conventions/get-declared-commands.js';
18
+ export { commentPragma } from './declaration-conventions/comment-pragma.js';
19
+ export { resolveTransparentWrappers } from './rules/resolve-transparent-wrappers.js';
20
+ export type { TransparentWrappersBindingContext } from './rules/resolve-transparent-wrappers.js';
21
+ export { resolveDeclarationConvention } from './rules/resolve-declaration-convention.js';
22
+ export { defineShellRule } from './rules/define-shell-rule.js';
23
+ export type { ShellListener, ShellRule, ShellRuleContext, ShellRuleMeta, ShellRuleSpec, ShellRuleVisitor, ShellTypedListeners, } from './rules/define-shell-rule.js';
24
+ export { createWrapperRule } from './rules/create-wrapper-rule.js';
25
+ export type { WrapperRuleConfig, WrapperRuleLocalInvocation, WrapperRuleLocalOperand, WrapperRuleSubcommands, } from './rules/create-wrapper-rule.js';
26
+ export type { WrapperSpec } from 'sh-ast/analyze';
27
+ /**
28
+ * The `@eslint-sh/eslint-plugin` plugin object, this package's default
29
+ * export (`import sh from '@eslint-sh/eslint-plugin'`). `languages.sh` provides
30
+ * `ShellLanguage` / `ShellSourceCode`, selected in a flat config via
31
+ * `language: "sh/sh"` (`namespace: "sh"`). `meta.version` is read from this
32
+ * package's own `package.json` at runtime — never hardcoded (see
33
+ * `version.ts`).
34
+ *
35
+ * `rules` holds the plugin's built-in rules, each built with
36
+ * `defineShellRule` (so each declares `meta.languages: ["sh/sh"]`) and
37
+ * linking its docs page from `meta.docs.url`. Consumers can also supply their
38
+ * own rules, built with the same `defineShellRule`, while sharing parsing,
39
+ * selectors, source coordinates, and directive semantics.
40
+ *
41
+ * `configs.recommended` is the `sh/recommended` preset: a single config
42
+ * object carrying `plugins: { sh }` and `language: "sh/sh"` but no `files`,
43
+ * so it inherits the globs of the entry that extends it:
44
+ * `{ files: [...], extends: [sh.configs.recommended] }`. It enables the
45
+ * convention-free policy rules. The rules index at
46
+ * https://github.com/mike-north/eslint-sh/blob/main/packages/eslint-plugin/docs/rules/README.md
47
+ * describes preset membership and available autofixes.
48
+ * `configs['agent-authored']` is the strict `sh/agent-authored` preset for
49
+ * machine-written, human-reviewed scripts:
50
+ * every `recommended` rule plus the contract rules, all at `error`.
51
+ * `configs['bash-3.2']` and `configs['posix-portable']` are deployment-target
52
+ * packs: the first sets `languageOptions.minBashVersion: "3.2"` and enables
53
+ * the version-gated `compat/*` rules except the opt-in
54
+ * `compat/no-nounset-empty-array`; the second sets
55
+ * `languageOptions.dialect: "posix"`, enables the effective `compat/*` rules and the
56
+ * shebang/dialect check; it reports named constructs
57
+ * that fail on dash, not every non-POSIX construct, and leaves `local`,
58
+ * `echo -e` and the rest of shellcheck's SC3xxx catalog to shellcheck. Every
59
+ * shipped config is a
60
+ * single object with no `files`; the two packs also carry `languageOptions`,
61
+ * because the target they name is their meaning. Beyond plugin registration
62
+ * and naming, target packs contain only language, languageOptions, and rules
63
+ * whose findings depend on that target. The shebang check belongs because it
64
+ * compares the declared interpreter with the target dialect. Error-handling
65
+ * policy comes from composing recommended, whose POSIX default is `set -eu`.
66
+ * The Bash-only empty-array nounset check cannot report under POSIX and is
67
+ * omitted from posix-portable.
68
+ *
69
+ * Re-exported, unchanged, as the default export of the `eslint-sh` umbrella
70
+ * package: importing this package or the umbrella package both yield the
71
+ * same plugin object.
72
+ *
73
+ * @public
74
+ */
75
+ declare const plugin: {
76
+ meta: {
77
+ name: string;
78
+ version: string;
79
+ namespace: string;
80
+ };
81
+ languages: {
82
+ sh: ShellLanguage;
83
+ };
84
+ rules: Record<string, import("@eslint/core").RuleDefinition<import("@eslint/core").RuleDefinitionTypeOptions>>;
85
+ configs: {
86
+ recommended: ConfigObject;
87
+ "agent-authored": ConfigObject;
88
+ "bash-3.2": ConfigObject;
89
+ "posix-portable": ConfigObject;
90
+ };
91
+ };
92
+ export default plugin;
package/dist/api.js ADDED
@@ -0,0 +1,83 @@
1
+ import { createAgentAuthoredConfig } from './configs/agent-authored.js';
2
+ import { createBash32Config } from './configs/bash-3-2.js';
3
+ import { createPosixPortableConfig } from './configs/posix-portable.js';
4
+ import { createRecommendedConfig } from './configs/recommended.js';
5
+ import { ShellLanguage } from './language/shell-language.js';
6
+ import { rules } from './rules/index.js';
7
+ import { readPackageVersion } from './version.js';
8
+ export { getDeclaredCommands } from './declaration-conventions/get-declared-commands.js';
9
+ export { commentPragma } from './declaration-conventions/comment-pragma.js';
10
+ export { resolveTransparentWrappers } from './rules/resolve-transparent-wrappers.js';
11
+ export { resolveDeclarationConvention } from './rules/resolve-declaration-convention.js';
12
+ export { defineShellRule } from './rules/define-shell-rule.js';
13
+ export { createWrapperRule } from './rules/create-wrapper-rule.js';
14
+ /* eslint-disable @api-extractor-tools/public-on-non-exported -- `plugin` is exported below as
15
+ `export default plugin`; the rule only recognizes inline exports. */
16
+ /**
17
+ * The `@eslint-sh/eslint-plugin` plugin object, this package's default
18
+ * export (`import sh from '@eslint-sh/eslint-plugin'`). `languages.sh` provides
19
+ * `ShellLanguage` / `ShellSourceCode`, selected in a flat config via
20
+ * `language: "sh/sh"` (`namespace: "sh"`). `meta.version` is read from this
21
+ * package's own `package.json` at runtime — never hardcoded (see
22
+ * `version.ts`).
23
+ *
24
+ * `rules` holds the plugin's built-in rules, each built with
25
+ * `defineShellRule` (so each declares `meta.languages: ["sh/sh"]`) and
26
+ * linking its docs page from `meta.docs.url`. Consumers can also supply their
27
+ * own rules, built with the same `defineShellRule`, while sharing parsing,
28
+ * selectors, source coordinates, and directive semantics.
29
+ *
30
+ * `configs.recommended` is the `sh/recommended` preset: a single config
31
+ * object carrying `plugins: { sh }` and `language: "sh/sh"` but no `files`,
32
+ * so it inherits the globs of the entry that extends it:
33
+ * `{ files: [...], extends: [sh.configs.recommended] }`. It enables the
34
+ * convention-free policy rules. The rules index at
35
+ * https://github.com/mike-north/eslint-sh/blob/main/packages/eslint-plugin/docs/rules/README.md
36
+ * describes preset membership and available autofixes.
37
+ * `configs['agent-authored']` is the strict `sh/agent-authored` preset for
38
+ * machine-written, human-reviewed scripts:
39
+ * every `recommended` rule plus the contract rules, all at `error`.
40
+ * `configs['bash-3.2']` and `configs['posix-portable']` are deployment-target
41
+ * packs: the first sets `languageOptions.minBashVersion: "3.2"` and enables
42
+ * the version-gated `compat/*` rules except the opt-in
43
+ * `compat/no-nounset-empty-array`; the second sets
44
+ * `languageOptions.dialect: "posix"`, enables the effective `compat/*` rules and the
45
+ * shebang/dialect check; it reports named constructs
46
+ * that fail on dash, not every non-POSIX construct, and leaves `local`,
47
+ * `echo -e` and the rest of shellcheck's SC3xxx catalog to shellcheck. Every
48
+ * shipped config is a
49
+ * single object with no `files`; the two packs also carry `languageOptions`,
50
+ * because the target they name is their meaning. Beyond plugin registration
51
+ * and naming, target packs contain only language, languageOptions, and rules
52
+ * whose findings depend on that target. The shebang check belongs because it
53
+ * compares the declared interpreter with the target dialect. Error-handling
54
+ * policy comes from composing recommended, whose POSIX default is `set -eu`.
55
+ * The Bash-only empty-array nounset check cannot report under POSIX and is
56
+ * omitted from posix-portable.
57
+ *
58
+ * Re-exported, unchanged, as the default export of the `eslint-sh` umbrella
59
+ * package: importing this package or the umbrella package both yield the
60
+ * same plugin object.
61
+ *
62
+ * @public
63
+ */
64
+ const plugin = {
65
+ meta: {
66
+ name: '@eslint-sh/eslint-plugin',
67
+ version: readPackageVersion(),
68
+ namespace: 'sh',
69
+ },
70
+ languages: {
71
+ sh: new ShellLanguage(),
72
+ },
73
+ rules,
74
+ // Filled in below: each shipped config registers this very object under
75
+ // `plugins.sh`, so the plugin must exist before its configs can.
76
+ configs: {},
77
+ };
78
+ /* eslint-enable @api-extractor-tools/public-on-non-exported */
79
+ plugin.configs.recommended = createRecommendedConfig(plugin);
80
+ plugin.configs['agent-authored'] = createAgentAuthoredConfig(plugin);
81
+ plugin.configs['bash-3.2'] = createBash32Config(plugin);
82
+ plugin.configs['posix-portable'] = createPosixPortableConfig(plugin);
83
+ export default plugin;
@@ -56,10 +56,10 @@ export declare function definedFunctionNames(sourceCode: ShellSourceCode): Reado
56
56
  * - builtins of the file's dialect (`sourceCode.dialect`);
57
57
  * - functions the file defines anywhere.
58
58
  *
59
- * @throws `ShAnalyzeMaxDepthError` (from `sh-ast/analyze`) when the file's
59
+ * @throws `ShAnalyzeMaxDepthError` (`SH_AST_ANALYZE_MAX_DEPTH`, from `sh-ast/analyze`) when the file's
60
60
  * nesting exceeds analysis's recursion guard: a partial harvest would miss
61
61
  * real commands.
62
- * @throws `ShAnalyzeInvalidWrapperSpecError` (from `sh-ast/analyze`) when
62
+ * @throws `ShAnalyzeInvalidWrapperSpecError` (`SH_AST_ANALYZE_INVALID_WRAPPER_SPEC`, from `sh-ast/analyze`) when
63
63
  * `options.transparentWrappers` contains a malformed entry.
64
64
  * @internal
65
65
  */
@@ -70,10 +70,10 @@ function wordNodeAt(site, index) {
70
70
  * - builtins of the file's dialect (`sourceCode.dialect`);
71
71
  * - functions the file defines anywhere.
72
72
  *
73
- * @throws `ShAnalyzeMaxDepthError` (from `sh-ast/analyze`) when the file's
73
+ * @throws `ShAnalyzeMaxDepthError` (`SH_AST_ANALYZE_MAX_DEPTH`, from `sh-ast/analyze`) when the file's
74
74
  * nesting exceeds analysis's recursion guard: a partial harvest would miss
75
75
  * real commands.
76
- * @throws `ShAnalyzeInvalidWrapperSpecError` (from `sh-ast/analyze`) when
76
+ * @throws `ShAnalyzeInvalidWrapperSpecError` (`SH_AST_ANALYZE_INVALID_WRAPPER_SPEC`, from `sh-ast/analyze`) when
77
77
  * `options.transparentWrappers` contains a malformed entry.
78
78
  * @internal
79
79
  */
@@ -14,18 +14,14 @@
14
14
  * shellcheck, which is why it does not promise "any POSIX `sh`". The scope and
15
15
  * the shell behaviour behind it are stated for users in `docs/compat.md`.
16
16
  *
17
- * `require-strict-mode` is configured, not merely enabled. Its default under
18
- * `posix` is already `set -eu`, but the preset states `mode: 'set -eu'`
19
- * explicitly so that neither a later edit to that default nor an inherited
20
- * `recommended` severity-only entry can make the pack demand `pipefail`,
21
- * which POSIX gained only in POSIX.1-2024 and which dash and older BusyBox
22
- * ash reject. A pack that asked for `pipefail` would make its own autofix
23
- * break a working `#!/bin/sh` script.
17
+ * Target presets carry only target facts and findings that depend on them.
18
+ * The shebang check belongs here because it verifies the declared interpreter
19
+ * against the configured target. Error-handling policy belongs to recommended:
20
+ * `extends: [sh.configs.recommended, sh.configs['posix-portable']]` requires
21
+ * the dialect-aware POSIX default, `set -eu`.
24
22
  *
25
23
  * It follows the shape contract in `./recommended.ts`, extended with
26
- * `languageOptions` for the reason given in `./bash-3-2.ts`. It composes
27
- * through `extends: [sh.configs.recommended, sh.configs['posix-portable']]`;
28
- * the later entry's `require-strict-mode` options win.
24
+ * `languageOptions` because the target is the preset's meaning.
29
25
  *
30
26
  * Rule membership follows the **Configs** column of the rule catalog (rows
31
27
  * marked `P`), which a test checks against this module.
@@ -1,14 +1,13 @@
1
1
  /**
2
2
  * The rules `sh/posix-portable` enables, all at `error`, keyed as a user
3
- * writes them (`sh/<rule-name>`): every `compat/*` rule (each reports under
4
- * `dialect: 'posix'` whatever the version), including the POSIX-only gates
5
- * `bash-3.2` has no use for, the shebang/dialect check, and strict mode in its
6
- * POSIX spelling.
3
+ * writes them (`sh/<rule-name>`): the effective `compat/*` rules under
4
+ * `dialect: 'posix'`, including the POSIX-only gates
5
+ * `bash-3.2` has no use for, and the shebang/dialect check. The empty-array
6
+ * nounset rule is omitted because it checks Bash targets and cannot report under POSIX.
7
7
  *
8
8
  * @internal
9
9
  */
10
10
  const posixPortableRules = {
11
- 'sh/require-strict-mode': ['error', { mode: 'set -eu' }],
12
11
  'sh/shebang-matches-dialect': 'error',
13
12
  'sh/compat/no-append-all-redirect': 'error',
14
13
  'sh/compat/no-associative-arrays': 'error',
@@ -27,7 +26,6 @@ const posixPortableRules = {
27
26
  'sh/compat/no-mapfile': 'error',
28
27
  'sh/compat/no-nameref': 'error',
29
28
  'sh/compat/no-negative-array-index': 'error',
30
- 'sh/compat/no-nounset-empty-array': 'error',
31
29
  'sh/compat/no-parameter-transformation': 'error',
32
30
  'sh/compat/no-pipe-all': 'error',
33
31
  'sh/compat/no-printf-time-format': 'error',
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Human diagnostics for project-owned configuration boundaries. Codes belong to
3
+ * the rejecting boundary; this module only keeps paths and received values
4
+ * consistent, without constructing errors or coupling runtime packages.
5
+ */
6
+ /**
7
+ * Render a rejected value using the project's short JSON-first convention.
8
+ * Non-JSON values must not replace the intended configuration failure with a
9
+ * serialization exception. The rendering is diagnostic text, never stored state.
10
+ * @internal
11
+ */
12
+ export declare function describeConfigurationValue(value: unknown): string;
13
+ /** The user's path identifies the binding; prose may evolve independently of the error code. @internal */
14
+ export declare function configurationMessage(where: string, detail: string, value: unknown): string;
@@ -0,0 +1,33 @@
1
+ // Generated from shared/configuration-message.ts; edit the canonical source.
2
+ /**
3
+ * Human diagnostics for project-owned configuration boundaries. Codes belong to
4
+ * the rejecting boundary; this module only keeps paths and received values
5
+ * consistent, without constructing errors or coupling runtime packages.
6
+ */
7
+ /**
8
+ * Render a rejected value using the project's short JSON-first convention.
9
+ * Non-JSON values must not replace the intended configuration failure with a
10
+ * serialization exception. The rendering is diagnostic text, never stored state.
11
+ * @internal
12
+ */
13
+ export function describeConfigurationValue(value) {
14
+ // JSON turns nonfinite numbers into null, which would identify a different rejected value.
15
+ if (typeof value === 'number' && !Number.isFinite(value)) {
16
+ return String(value);
17
+ }
18
+ if (typeof value === 'bigint') {
19
+ return `${String(value)}n`;
20
+ }
21
+ try {
22
+ // JSON.stringify also returns undefined for functions, symbols and undefined.
23
+ const rendered = JSON.stringify(value);
24
+ return rendered ?? String(value);
25
+ }
26
+ catch {
27
+ return String(value);
28
+ }
29
+ }
30
+ /** The user's path identifies the binding; prose may evolve independently of the error code. @internal */
31
+ export function configurationMessage(where, detail, value) {
32
+ return `${where}: ${detail}; received ${describeConfigurationValue(value)}.`;
33
+ }
@@ -0,0 +1,41 @@
1
+ import type { SettingsConfig } from '@eslint/core';
2
+ import type { ShellSourceCode } from '../language/shell-source-code.js';
3
+ import type { DeclaredCommand, LintedFile } from './declaration-convention.js';
4
+ /**
5
+ * The only view of the linted file a convention's hooks receive: `filename`,
6
+ * `physicalFilename` and `cwd`, frozen. A rule's `context` must never reach a
7
+ * hook, because it would let a convention report, read options or reach other
8
+ * rule state.
9
+ *
10
+ * @internal
11
+ */
12
+ export declare function toLintedFile(context: LintedFile): LintedFile;
13
+ /**
14
+ * Returns validated declarations for the linted file, or `null` when the project
15
+ * has no bound convention. An empty array means a convention is bound and the
16
+ * file declares no commands; consumers must preserve that distinction.
17
+ *
18
+ * The convention receives only a frozen {@link LintedFile}, never rule state.
19
+ * Invalid bindings, output arrays, declaration kinds, provenance or UTF-16 ranges
20
+ * throw an error naming the shared setting before a rule can trust the result.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * File() {
25
+ * const declarations = getDeclaredCommands(context);
26
+ * if (declarations === null) return;
27
+ * for (const declared of declarations) {
28
+ * // Each declaration has been checked against its kind's contract.
29
+ * }
30
+ * }
31
+ * ```
32
+ *
33
+ * @throws Error with `ESLINT_SH_DECLARATION_CONVENTION` for invalid shared bindings, or
34
+ * `ESLINT_SH_DECLARATION_OUTPUT` for invalid hook results.
35
+ * Exceptions thrown by the convention itself pass through unchanged, including their causes.
36
+ * @public
37
+ */
38
+ export declare function getDeclaredCommands(context: LintedFile & {
39
+ readonly sourceCode: ShellSourceCode;
40
+ readonly settings: SettingsConfig;
41
+ }): DeclaredCommand[] | null;