@eslint-sh/eslint-plugin 0.7.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 (182) hide show
  1. package/README.md +53 -40
  2. package/dist/api.d.ts +92 -0
  3. package/dist/api.js +83 -0
  4. package/dist/command-harvest/builtins/bash.d.ts +1 -1
  5. package/dist/command-harvest/builtins/bash.js +1 -1
  6. package/dist/command-harvest/builtins/bats.d.ts +2 -0
  7. package/dist/command-harvest/builtins/bats.js +3 -0
  8. package/dist/command-harvest/harvest-commands.d.ts +2 -2
  9. package/dist/command-harvest/harvest-commands.js +2 -2
  10. package/dist/command-harvest/wrapper-table.d.ts +3 -19
  11. package/dist/command-harvest/wrapper-table.js +8 -38
  12. package/dist/configs/bash-3-2.d.ts +7 -6
  13. package/dist/configs/bash-3-2.js +4 -3
  14. package/dist/configs/posix-portable.d.ts +7 -11
  15. package/dist/configs/posix-portable.js +4 -6
  16. package/dist/configuration-message.d.ts +14 -0
  17. package/dist/configuration-message.js +33 -0
  18. package/dist/declaration-conventions/get-declared-commands.d.ts +41 -0
  19. package/dist/declaration-conventions/{read-declared-commands.js → get-declared-commands.js} +43 -43
  20. package/dist/eslint-plugin-api.d.ts +1506 -0
  21. package/dist/eslint-plugin-syntax-analyze.d.ts +3 -3
  22. package/dist/eslint-plugin-syntax.d.ts +7 -4
  23. package/dist/eslint-plugin.d.ts +16 -1514
  24. package/dist/index.d.ts +14 -78
  25. package/dist/index.js +22 -74
  26. package/dist/language/command-resolution.d.ts +5 -5
  27. package/dist/language/command-resolution.js +11 -7
  28. package/dist/language/selector-classes.d.ts +7 -3
  29. package/dist/language/selector-classes.js +13 -4
  30. package/dist/language/shell-language-options.d.ts +21 -18
  31. package/dist/language/shell-language-options.js +41 -39
  32. package/dist/language/shell-language.d.ts +13 -10
  33. package/dist/language/shell-language.js +23 -45
  34. package/dist/language/shell-source-code.d.ts +74 -49
  35. package/dist/language/shell-source-code.js +135 -68
  36. package/dist/language/source-coordinates.d.ts +2 -0
  37. package/dist/language/source-coordinates.js +8 -4
  38. package/dist/language/syntax-view.js +19 -7
  39. package/dist/language/word-resolution.d.ts +2 -1
  40. package/dist/language/word-resolution.js +8 -3
  41. package/dist/rules/bash-variable-semantics.d.ts +22 -0
  42. package/dist/rules/bash-variable-semantics.js +41 -0
  43. package/dist/rules/compat/create-compat-rule.js +1 -1
  44. package/dist/rules/compat/declaration-options.d.ts +2 -2
  45. package/dist/rules/compat/declaration-options.js +5 -2
  46. package/dist/rules/compat/no-append-all-redirect.js +1 -1
  47. package/dist/rules/compat/no-associative-arrays.js +2 -2
  48. package/dist/rules/compat/no-brace-sequence-padding-step.js +4 -4
  49. package/dist/rules/compat/no-case-attributes.js +3 -3
  50. package/dist/rules/compat/no-case-fallthrough.js +2 -2
  51. package/dist/rules/compat/no-case-modification.js +2 -2
  52. package/dist/rules/compat/no-coproc.js +1 -1
  53. package/dist/rules/compat/no-declare-g.js +3 -3
  54. package/dist/rules/compat/no-double-bracket.js +2 -2
  55. package/dist/rules/compat/no-epoch-variables.js +2 -2
  56. package/dist/rules/compat/no-fd-variable.js +1 -1
  57. package/dist/rules/compat/no-globstar.js +1 -1
  58. package/dist/rules/compat/no-lastpipe-semantics.js +2 -2
  59. package/dist/rules/compat/no-let.js +2 -1
  60. package/dist/rules/compat/no-mapfile.js +4 -2
  61. package/dist/rules/compat/no-nameref.js +3 -3
  62. package/dist/rules/compat/no-negative-array-index.js +39 -9
  63. package/dist/rules/compat/no-nounset-empty-array.js +15 -4
  64. package/dist/rules/compat/no-parameter-transformation.js +2 -2
  65. package/dist/rules/compat/no-pipe-all.js +1 -1
  66. package/dist/rules/compat/no-printf-time-format.js +4 -2
  67. package/dist/rules/compat/no-source.js +2 -1
  68. package/dist/rules/compat/no-test-v.js +4 -2
  69. package/dist/rules/compat/no-unicode-escape.js +2 -2
  70. package/dist/rules/compat/no-wait-n.js +4 -2
  71. package/dist/rules/compat/nounset-array-proof.d.ts +8 -0
  72. package/dist/rules/compat/nounset-array-proof.js +563 -0
  73. package/dist/rules/compat/posix-absent-command.d.ts +7 -8
  74. package/dist/rules/compat/posix-absent-command.js +17 -15
  75. package/dist/rules/compat/shopt-options.js +3 -1
  76. package/dist/rules/compat/version-gates.d.ts +9 -9
  77. package/dist/rules/compat/version-gates.js +13 -13
  78. package/dist/rules/create-wrapper-rule.d.ts +1 -1
  79. package/dist/rules/create-wrapper-rule.js +23 -16
  80. package/dist/rules/define-shell-rule.js +1 -1
  81. package/dist/rules/no-dynamic-invocation.d.ts +0 -8
  82. package/dist/rules/no-dynamic-invocation.js +7 -15
  83. package/dist/rules/no-eval.js +3 -1
  84. package/dist/rules/no-masked-exit-status.d.ts +4 -3
  85. package/dist/rules/no-masked-exit-status.js +18 -10
  86. package/dist/rules/no-secret-env-reads.d.ts +6 -3
  87. package/dist/rules/no-secret-env-reads.js +49 -15
  88. package/dist/rules/no-secret-flags.d.ts +4 -1
  89. package/dist/rules/no-secret-flags.js +21 -11
  90. package/dist/rules/no-unchecked-cd.js +6 -3
  91. package/dist/rules/no-undeclared-commands.d.ts +1 -15
  92. package/dist/rules/no-undeclared-commands.js +11 -18
  93. package/dist/rules/no-unused-declarations.d.ts +1 -18
  94. package/dist/rules/no-unused-declarations.js +15 -36
  95. package/dist/rules/prefer-local.d.ts +3 -2
  96. package/dist/rules/prefer-local.js +7 -4
  97. package/dist/rules/quote-expansions.js +3 -28
  98. package/dist/rules/require-network-wrapper.d.ts +3 -18
  99. package/dist/rules/require-network-wrapper.js +43 -10
  100. package/dist/rules/require-strict-mode.d.ts +5 -46
  101. package/dist/rules/require-strict-mode.js +17 -107
  102. package/dist/rules/require-tty-guard.d.ts +2 -0
  103. package/dist/rules/require-tty-guard.js +8 -1
  104. package/dist/rules/resolve-declaration-convention.d.ts +9 -89
  105. package/dist/rules/resolve-declaration-convention.js +19 -72
  106. package/dist/rules/resolve-network-wrapper.d.ts +20 -2
  107. package/dist/rules/resolve-network-wrapper.js +71 -36
  108. package/dist/rules/resolve-transparent-wrappers.d.ts +38 -0
  109. package/dist/rules/resolve-transparent-wrappers.js +108 -0
  110. package/dist/rules/set-options.d.ts +3 -2
  111. package/dist/rules/set-options.js +18 -6
  112. package/dist/rules/shebang-interpreter.js +4 -2
  113. package/dist/rules/strict-mode-prologue.d.ts +43 -0
  114. package/dist/rules/strict-mode-prologue.js +91 -0
  115. package/dist/rules/transparent-wrappers.d.ts +4 -3
  116. package/dist/rules/transparent-wrappers.js +5 -4
  117. package/dist/rules/user-pattern.d.ts +9 -6
  118. package/dist/rules/user-pattern.js +16 -12
  119. package/dist/shell-dialect.d.ts +8 -0
  120. package/dist/shell-dialect.js +22 -0
  121. package/dist/syntax/analyze.d.ts +3 -3
  122. package/dist/syntax/analyze.js +3 -3
  123. package/dist/syntax/index.d.ts +7 -4
  124. package/dist/syntax/index.js +7 -4
  125. package/dist/version.js +2 -1
  126. package/docs/compat.md +81 -33
  127. package/docs/custom-rule-tutorial.md +31 -21
  128. package/docs/declaration-conventions.md +20 -23
  129. package/docs/error-codes.md +93 -0
  130. package/docs/examples/no-listed-commands-rule.ts +2 -2
  131. package/docs/examples/no-rm-rf-rule.mjs +8 -10
  132. package/docs/examples/sorted-declarations-rule.mjs +6 -9
  133. package/docs/examples/tsconfig.json +10 -0
  134. package/docs/rules/README.md +46 -6
  135. package/docs/rules/_template.md +2 -2
  136. package/docs/rules/compat/no-append-all-redirect.md +12 -12
  137. package/docs/rules/compat/no-associative-arrays.md +14 -14
  138. package/docs/rules/compat/no-brace-sequence-padding-step.md +12 -12
  139. package/docs/rules/compat/no-case-attributes.md +13 -13
  140. package/docs/rules/compat/no-case-fallthrough.md +12 -12
  141. package/docs/rules/compat/no-case-modification.md +11 -11
  142. package/docs/rules/compat/no-coproc.md +13 -13
  143. package/docs/rules/compat/no-declare-g.md +13 -13
  144. package/docs/rules/compat/no-double-bracket.md +2 -2
  145. package/docs/rules/compat/no-epoch-variables.md +13 -13
  146. package/docs/rules/compat/no-fd-variable.md +12 -12
  147. package/docs/rules/compat/no-globstar.md +13 -13
  148. package/docs/rules/compat/no-lastpipe-semantics.md +13 -13
  149. package/docs/rules/compat/no-let.md +2 -2
  150. package/docs/rules/compat/no-mapfile.md +16 -16
  151. package/docs/rules/compat/no-nameref.md +13 -13
  152. package/docs/rules/compat/no-negative-array-index.md +14 -14
  153. package/docs/rules/compat/no-nounset-empty-array.md +72 -17
  154. package/docs/rules/compat/no-parameter-transformation.md +19 -19
  155. package/docs/rules/compat/no-pipe-all.md +13 -13
  156. package/docs/rules/compat/no-printf-time-format.md +13 -13
  157. package/docs/rules/compat/no-source.md +2 -2
  158. package/docs/rules/compat/no-test-v.md +12 -12
  159. package/docs/rules/compat/no-unicode-escape.md +13 -13
  160. package/docs/rules/compat/no-wait-n.md +13 -13
  161. package/docs/rules/no-dynamic-invocation.md +17 -12
  162. package/docs/rules/no-eval.md +6 -1
  163. package/docs/rules/no-masked-exit-status.md +6 -2
  164. package/docs/rules/no-secret-env-reads.md +46 -5
  165. package/docs/rules/no-secret-flags.md +32 -6
  166. package/docs/rules/no-unchecked-cd.md +22 -2
  167. package/docs/rules/no-undeclared-commands.md +16 -15
  168. package/docs/rules/no-unused-declarations.md +17 -20
  169. package/docs/rules/prefer-local.md +20 -2
  170. package/docs/rules/quote-expansions.md +8 -5
  171. package/docs/rules/require-network-wrapper.md +26 -21
  172. package/docs/rules/require-shebang.md +10 -4
  173. package/docs/rules/require-strict-mode.md +31 -23
  174. package/docs/rules/require-tty-guard.md +12 -3
  175. package/docs/selectors.md +7 -2
  176. package/docs/shellcheck.md +9 -5
  177. package/docs/transparent-wrappers.md +97 -0
  178. package/docs/wrapper-rules.md +11 -0
  179. package/package.json +3 -3
  180. package/dist/declaration-conventions/read-declared-commands.d.ts +0 -70
  181. package/dist/language/end-of-input-marker.d.ts +0 -29
  182. package/dist/language/end-of-input-marker.js +0 -88
@@ -55,14 +55,11 @@ const DEFAULT_PATTERNS = [
55
55
  '^(?!.*-(?:stdin|file)$)--?(?:access-|auth-?)?(?:password|passwd|token|secret|api-?key|auth(?:key|orization)?)(?![a-z])',
56
56
  ];
57
57
  /**
58
- * The flags every pattern is compiled with. Case-insensitive, matching the
59
- * default pattern's `/i`. No `u` flag: config validation checks each source
60
- * with ESLint's JSON-schema `regex` format, which compiles without flags, and
61
- * a few sources valid there are syntax errors under `u`. Adding `i` never
62
- * turns a valid source into an invalid one, so a pattern that passed
63
- * validation always compiles here.
58
+ * Unicode matching that ignores case for both default and custom sources.
59
+ * Keep the original flag name and pattern intact: lower-casing the subject
60
+ * would make a custom pattern containing capitals silently stop matching.
64
61
  */
65
- const PATTERN_FLAGS = 'i';
62
+ const PATTERN_FLAGS = 'iu';
66
63
  /**
67
64
  * Splits a static argument word into the flag name the patterns are tested
68
65
  * against and the value written in the same word, if any. `--token=abc` is
@@ -98,10 +95,17 @@ export const noSecretFlags = createRule({
98
95
  {
99
96
  type: 'object',
100
97
  properties: {
98
+ additionalPatterns: {
99
+ type: 'array',
100
+ // Validate with the rule's actual Unicode/case flags in create(),
101
+ // so syntax and safety errors identify the original option and source.
102
+ items: { type: 'string' },
103
+ },
101
104
  patterns: {
102
105
  type: 'array',
103
- items: { type: 'string', format: 'regex' },
104
- minItems: 1,
106
+ // Syntax is checked with the actual flags by compileUserPattern,
107
+ // which also names the option index and source in errors.
108
+ items: { type: 'string' },
105
109
  },
106
110
  },
107
111
  additionalProperties: false,
@@ -112,8 +116,14 @@ export const noSecretFlags = createRule({
112
116
  },
113
117
  },
114
118
  create(context) {
115
- const sources = context.options[0]?.patterns ?? DEFAULT_PATTERNS;
116
- const patterns = sources.map((source, index) => compileUserPattern('sh/no-secret-flags', `patterns[${String(index)}]`, source, PATTERN_FLAGS));
119
+ const options = context.options[0];
120
+ // An explicit replacement, including [], owns the base policy. Additions
121
+ // extend that base per file; compile separately to retain each option's identity.
122
+ // Diagnostic rule paths follow the configured ID, including plugin registration aliases.
123
+ const patterns = [
124
+ ...(options?.patterns ?? DEFAULT_PATTERNS).map((source, index) => compileUserPattern(context.id, `patterns[${String(index)}]`, source, PATTERN_FLAGS)),
125
+ ...(options?.additionalPatterns ?? []).map((source, index) => compileUserPattern(context.id, `additionalPatterns[${String(index)}]`, source, PATTERN_FLAGS)),
126
+ ];
117
127
  const isSecretFlag = (name) => patterns.some((pattern) => pattern.test(name));
118
128
  const { sourceCode } = context;
119
129
  /** Finds every secret-shaped flag with a literal value among `args` (argv, command word first). */
@@ -48,8 +48,9 @@
48
48
  * statement is the document body, not shell code).
49
49
  */
50
50
  import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-ast/analyze';
51
+ import { resolveTransparentWrappers } from './resolve-transparent-wrappers.js';
51
52
  import { createRule } from './create-rule.js';
52
- import { prologueEnabledOptions, setStatementChanges } from './require-strict-mode.js';
53
+ import { prologueEnabledOptions, resolveStrictModePrologue, setStatementChanges, } from './strict-mode-prologue.js';
53
54
  import { BUILTIN_WRAPPER } from './transparent-wrappers.js';
54
55
  /** The commands that change the working directory and can fail doing so. */
55
56
  const DIRECTORY_COMMANDS = new Set(['cd', 'pushd', 'popd']);
@@ -202,18 +203,20 @@ export const noUncheckedCd = createRule({
202
203
  create(context) {
203
204
  const [options] = context.options;
204
205
  const { sourceCode } = context;
206
+ const prologue = resolveStrictModePrologue(context.settings);
207
+ const transparentWrappers = resolveTransparentWrappers(context, TRANSPARENT_WRAPPERS);
205
208
  let errexitInForce = false;
206
209
  return {
207
210
  File(node) {
208
211
  errexitInForce =
209
212
  options.ignoreWhenErrexit !== false &&
210
- prologueEnabledOptions(node, sourceCode).has('errexit');
213
+ prologueEnabledOptions(node, sourceCode, prologue).has('errexit');
211
214
  },
212
215
  CallExpr(node) {
213
216
  if (errexitInForce)
214
217
  return;
215
218
  const invoked = sourceCode.resolveInvokedCommand(node, {
216
- transparentWrappers: TRANSPARENT_WRAPPERS,
219
+ transparentWrappers,
217
220
  });
218
221
  if (invoked?.effective.static !== true || !DIRECTORY_COMMANDS.has(invoked.effective.text)) {
219
222
  return;
@@ -1,20 +1,6 @@
1
- /**
2
- * The rule's single options object.
3
- *
4
- * @internal
5
- */
6
- export interface NoUndeclaredCommandsOptions {
7
- /**
8
- * Names of project-specific transparent wrappers (`with-retry`), added to
9
- * the default wrapper table (see `harvestWrapperTable`). The word after a listed
10
- * wrapper is the command it runs, so `with-retry curl` uses `curl` as well
11
- * as `with-retry`.
12
- */
13
- readonly transparentWrappers?: readonly string[];
14
- }
15
1
  /**
16
2
  * The `sh/no-undeclared-commands` rule module.
17
3
  *
18
4
  * @internal
19
5
  */
20
- export declare const noUndeclaredCommands: import("./define-shell-rule.js").ShellRule<[NoUndeclaredCommandsOptions?], "undeclaredCommand">;
6
+ export declare const noUndeclaredCommands: import("./define-shell-rule.js").ShellRule<[], "undeclaredCommand">;
@@ -28,12 +28,13 @@
28
28
  * convention's own words when it has a `declarationHint` hook, since only the
29
29
  * convention knows its syntax.
30
30
  */
31
+ import { configurationMessage } from '../configuration-message.js';
31
32
  import { harvestCommands } from '../command-harvest/harvest-commands.js';
32
33
  import { harvestWrapperTable } from '../command-harvest/wrapper-table.js';
33
34
  import { createRule } from './create-rule.js';
34
- import { readDeclaredCommands, toLintedFile, } from '../declaration-conventions/read-declared-commands.js';
35
+ import { getDeclaredCommands, toLintedFile, } from '../declaration-conventions/get-declared-commands.js';
35
36
  import { resolveDeclarationConvention } from './resolve-declaration-convention.js';
36
- import { networkWrapperSpec } from './resolve-network-wrapper.js';
37
+ import { resolveTransparentWrappers } from './resolve-transparent-wrappers.js';
37
38
  /** The registry name of this rule (`sh/no-undeclared-commands` in a user's config). */
38
39
  const RULE_NAME = 'no-undeclared-commands';
39
40
  /** The report's second sentence when the convention offers no hint for a command. */
@@ -51,7 +52,7 @@ function howToDeclare(convention, command, sourceCode, file) {
51
52
  if (hint === null)
52
53
  return GENERIC_HINT;
53
54
  if (typeof hint !== 'string' || hint.trim() === '' || /[\r\n\u2028\u2029]/u.test(hint)) {
54
- throw new Error(`Invalid settings.sh.declarationConvention: \`declarationHint\` must return null or a non-empty single-line string, but returned ${typeof hint === 'string' ? JSON.stringify(hint) : typeof hint} for ${JSON.stringify(command)}.`);
55
+ throw Object.assign(new Error(configurationMessage('settings.sh.declarationConvention.declarationHint', `must return null or a non-empty single-line string for ${JSON.stringify(command)}`, hint)), { code: 'ESLINT_SH_DECLARATION_OUTPUT' });
55
56
  }
56
57
  return hint;
57
58
  }
@@ -67,19 +68,7 @@ export const noUndeclaredCommands = createRule({
67
68
  docs: {
68
69
  description: 'Require every external command a script uses to be declared by the configured declaration convention',
69
70
  },
70
- schema: [
71
- {
72
- type: 'object',
73
- properties: {
74
- transparentWrappers: {
75
- type: 'array',
76
- items: { type: 'string', minLength: 1 },
77
- uniqueItems: true,
78
- },
79
- },
80
- additionalProperties: false,
81
- },
82
- ],
71
+ schema: [],
83
72
  messages: {
84
73
  undeclaredCommand: "'{{command}}' is used but not declared. {{howToDeclare}}",
85
74
  },
@@ -90,11 +79,15 @@ export const noUndeclaredCommands = createRule({
90
79
  return {};
91
80
  }
92
81
  const { sourceCode } = context;
93
- const transparentWrappers = harvestWrapperTable(sourceCode.dialect, context.options[0]?.transparentWrappers ?? [], networkWrapperSpec(context.settings));
82
+ const transparentWrappers = resolveTransparentWrappers(context, harvestWrapperTable(sourceCode.dialect));
94
83
  return {
95
84
  File() {
96
85
  // Only names matter here, so every kind of declaration counts the same.
97
- const declared = new Set(readDeclaredCommands(convention, context).map(({ name }) => name));
86
+ const declarations = getDeclaredCommands(context);
87
+ if (declarations === null) {
88
+ return;
89
+ }
90
+ const declared = new Set(declarations.map(({ name }) => name));
98
91
  for (const { name, node } of harvestCommands(sourceCode, {
99
92
  transparentWrappers,
100
93
  }).values()) {
@@ -1,23 +1,6 @@
1
- /**
2
- * The rule's single options object. It mirrors `no-undeclared-commands`'
3
- * options, so a project configures both rules with the same wrappers and
4
- * they compute the same usage.
5
- *
6
- * @internal
7
- */
8
- export interface NoUnusedDeclarationsOptions {
9
- /**
10
- * Names of project-specific transparent wrappers (`with-retry`), added to
11
- * the default wrapper table (see `harvestWrapperTable`). The word after a
12
- * listed wrapper is the command it runs, so `with-retry curl` uses `curl`
13
- * as well as `with-retry`, and `with-retry "$tool"` runs a command the
14
- * file cannot name.
15
- */
16
- readonly transparentWrappers?: readonly string[];
17
- }
18
1
  /**
19
2
  * The `sh/no-unused-declarations` rule module.
20
3
  *
21
4
  * @internal
22
5
  */
23
- export declare const noUnusedDeclarations: import("./define-shell-rule.js").ShellRule<[NoUnusedDeclarationsOptions?], "unusedDeclaration" | "unusedFileDeclaration" | "unusedDescribedDeclaration">;
6
+ export declare const noUnusedDeclarations: import("./define-shell-rule.js").ShellRule<[], "unusedDeclaration" | "unusedFileDeclaration" | "unusedDescribedDeclaration">;
@@ -1,10 +1,9 @@
1
- import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-ast/analyze';
1
+ import { resolveTransparentWrappers } from './resolve-transparent-wrappers.js';
2
2
  import { harvestCommands } from '../command-harvest/harvest-commands.js';
3
3
  import { harvestWrapperTable } from '../command-harvest/wrapper-table.js';
4
4
  import { createRule } from './create-rule.js';
5
- import { readDeclaredCommands } from '../declaration-conventions/read-declared-commands.js';
5
+ import { getDeclaredCommands } from '../declaration-conventions/get-declared-commands.js';
6
6
  import { resolveDeclarationConvention } from './resolve-declaration-convention.js';
7
- import { networkWrapperSpec } from './resolve-network-wrapper.js';
8
7
  import { SHELL_WRAPPER } from './transparent-wrappers.js';
9
8
  /** The registry name of this rule (`sh/no-unused-declarations` in a user's config). */
10
9
  const RULE_NAME = 'no-unused-declarations';
@@ -54,29 +53,17 @@ function trapSetsCodeString(operands) {
54
53
  const SHELL_NAMES = new Set(SHELL_WRAPPER.names);
55
54
  /** The builtins that run another file's code in this shell: POSIX `.` and Bash's `source`. */
56
55
  const SOURCE_COMMANDS = new Set(['.', 'source']);
57
- /**
58
- * The wrappers whose path forms (`/bin/bash`, `/usr/bin/env`) the completeness
59
- * check also follows, by basename: shells invoked as commands and `env`,
60
- * the wrappers that can run an embedded command string. sh-ast matches
61
- * wrapper names exactly, so without this `/bin/bash -c '…'` would resolve to
62
- * the static command `/bin/bash`.
63
- */
64
- const PATH_FOLLOWED_WRAPPERS = [
65
- SHELL_WRAPPER,
66
- ...DEFAULT_TRANSPARENT_WRAPPERS.filter((spec) => spec.names.includes('env')),
67
- ];
68
56
  /** The basename of `text` when it is a path (contains a slash), else `null`. */
69
57
  function pathBasename(text) {
70
58
  const slash = text.lastIndexOf('/');
71
59
  return slash === -1 ? null : text.slice(slash + 1);
72
60
  }
73
61
  /**
74
- * The effective command of `site` under `table`, also following a shell or
75
- * `env` named by path: while the effective command is a path whose basename
76
- * names one of {@link PATH_FOLLOWED_WRAPPERS}, that path is added as a
77
- * wrapper with the same spec and the site is resolved again. This mirrors how
78
- * `createWrapperRule` follows path-named wrappers. Each round adds a new
79
- * path, and a site has finitely many words, so the loop ends.
62
+ * The effective command of `site` under the resolved completeness `table`.
63
+ * Relative utility paths use only specs already in that table, including its
64
+ * shell-interpreter entries. Recreating a built-in spec here would undo a
65
+ * project's exact-name override and make completeness disagree with usage.
66
+ * Each round adds a new path from the finite argv, so the loop ends.
80
67
  */
81
68
  function resolveEffective(site, table) {
82
69
  let current = table;
@@ -89,7 +76,7 @@ function resolveEffective(site, table) {
89
76
  const base = pathBasename(path);
90
77
  if (base === null || current.some((spec) => spec.names.includes(path)))
91
78
  return resolution;
92
- const spec = PATH_FOLLOWED_WRAPPERS.find((entry) => entry.names.includes(base));
79
+ const spec = current.find((entry) => entry.shellBuiltin !== true && entry.names.includes(base));
93
80
  if (spec === undefined)
94
81
  return resolution;
95
82
  current = [{ ...spec, names: [path] }, ...current];
@@ -177,19 +164,7 @@ export const noUnusedDeclarations = createRule({
177
164
  description: 'Disallow declaring external commands that a script never uses, per the configured declaration convention',
178
165
  },
179
166
  fixable: 'code',
180
- schema: [
181
- {
182
- type: 'object',
183
- properties: {
184
- transparentWrappers: {
185
- type: 'array',
186
- items: { type: 'string', minLength: 1 },
187
- uniqueItems: true,
188
- },
189
- },
190
- additionalProperties: false,
191
- },
192
- ],
167
+ schema: [],
193
168
  messages: {
194
169
  unusedDeclaration: "'{{command}}' is declared but never used. Remove the declaration so the script's stated dependencies match what it runs.",
195
170
  unusedFileDeclaration: "'{{command}}' is declared in {{source}} but this script never uses it. Remove it there so the script's stated dependencies match what it runs.",
@@ -202,7 +177,7 @@ export const noUnusedDeclarations = createRule({
202
177
  return {};
203
178
  }
204
179
  const { sourceCode } = context;
205
- const transparentWrappers = harvestWrapperTable(sourceCode.dialect, context.options[0]?.transparentWrappers ?? [], networkWrapperSpec(context.settings));
180
+ const transparentWrappers = resolveTransparentWrappers(context, harvestWrapperTable(sourceCode.dialect));
206
181
  // The completeness check follows the harvest's table plus shells
207
182
  // invoked as commands, so `bash -c '…'` and `sh -c '…'` resolve to an
208
183
  // embedded command (as in `no-dynamic-invocation`) instead of stopping
@@ -229,7 +204,11 @@ export const noUnusedDeclarations = createRule({
229
204
  used.add(each);
230
205
  }
231
206
  }
232
- for (const declared of readDeclaredCommands(convention, context)) {
207
+ const declarations = getDeclaredCommands(context);
208
+ if (declarations === null) {
209
+ return;
210
+ }
211
+ for (const declared of declarations) {
233
212
  if (used.has(declared.name))
234
213
  continue;
235
214
  if (declared.kind === 'script') {
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * @internal
5
5
  */
6
- export interface PreferLocalOptions {
6
+ interface PreferLocalOptions {
7
7
  /** Variable names that may be assigned in a function without `local`. */
8
8
  readonly allow?: readonly string[];
9
9
  /**
@@ -13,4 +13,5 @@ export interface PreferLocalOptions {
13
13
  readonly allowPattern?: string;
14
14
  }
15
15
  /** Built-in rule `sh/prefer-local`. @internal */
16
- export declare const preferLocal: import("./define-shell-rule.js").ShellRule<[PreferLocalOptions], "preferLocal">;
16
+ declare const preferLocal: import("./define-shell-rule.js").ShellRule<[PreferLocalOptions], "preferLocal">;
17
+ export { preferLocal };
@@ -140,7 +140,7 @@ function scopeBindings(root, sourceCode, lastpipe) {
140
140
  return found.sort((a, b) => a.node.range[0] - b.node.range[0]);
141
141
  }
142
142
  /** Built-in rule `sh/prefer-local`. @internal */
143
- export const preferLocal = createRule({
143
+ const preferLocal = createRule({
144
144
  name: 'prefer-local',
145
145
  meta: {
146
146
  type: 'suggestion',
@@ -165,12 +165,14 @@ export const preferLocal = createRule({
165
165
  },
166
166
  create(context) {
167
167
  const { sourceCode } = context;
168
+ const [options] = context.options;
169
+ const allowed = new Set(options.allow ?? []);
170
+ // The configured ID owns option paths, independently of the rule's registry name.
171
+ const allowPattern = compileUserPattern(context.id, 'allowPattern', options.allowPattern ?? DEFAULT_ALLOW_PATTERN, 'u');
172
+ // Validate configuration in every dialect, even where the rule is inert.
168
173
  // `local` is not POSIX: under `posix` there is nothing to declare with.
169
174
  if (sourceCode.dialect === 'posix')
170
175
  return {};
171
- const [options] = context.options;
172
- const allowed = new Set(options.allow ?? []);
173
- const allowPattern = compileUserPattern('sh/prefer-local', 'allowPattern', options.allowPattern ?? DEFAULT_ALLOW_PATTERN, 'u');
174
176
  const text = sourceCode.text;
175
177
  const lastpipe = enablesLastpipe(sourceCode.ast, sourceCode);
176
178
  // Names assigned or declared outside every function, with where each
@@ -247,3 +249,4 @@ export const preferLocal = createRule({
247
249
  };
248
250
  },
249
251
  });
252
+ export { preferLocal };
@@ -40,6 +40,7 @@
40
40
  */
41
41
  import { DashHdoc, Hdoc } from 'sh-ast/analyze';
42
42
  import { createRule } from './create-rule.js';
43
+ import { isBashIntegerVariable } from './bash-variable-semantics.js';
43
44
  import { messageExcerpt } from '../language/line-breaks.js';
44
45
  /** The registry name of this rule (`sh/quote-expansions` in a user's config). */
45
46
  const RULE_NAME = 'quote-expansions';
@@ -50,33 +51,7 @@ const RULE_NAME = 'quote-expansions';
50
51
  * their values are arbitrary text.
51
52
  */
52
53
  const SINGLE_WORD_SPECIALS = new Set(['#', '?', '$', '!', '-']);
53
- /**
54
- * Bash variables that always expand to a decimal integer, so an unquoted
55
- * reference is one word with no glob characters, like the specials above.
56
- * Bash keeps them integers: assigning text to `RANDOM`, `SECONDS` or `OPTIND`
57
- * evaluates it arithmetically, `LINENO` and `BASH_SUBSHELL` ignore
58
- * assignments, and `PPID` is read-only; a hostile value in the environment is
59
- * ignored for each of them. Each exists in every Bash from 3.2, the oldest
60
- * target eslint-sh supports.
61
- *
62
- * Left out on purpose: `UID` and `EUID`, which Bash imports verbatim from the
63
- * environment (`env UID='a *' bash -c 'echo "[$UID]"'` prints `[a *]`), so
64
- * they can hold text that splits and globs; `BASHPID` (Bash 4.0), `EPOCHSECONDS` (5.0) and
65
- * `SRANDOM` (5.1), which an older Bash leaves unset, so an unquoted reference
66
- * there expands to no word at all; `EPOCHREALTIME`, whose decimal separator
67
- * follows the locale; and variables a script or the environment may set to
68
- * any text (`COLUMNS`, `SHLVL`). The list does not apply outside Bash: in a
69
- * POSIX shell such as dash these are ordinary variables.
70
- */
71
- const BASH_INTEGER_VARIABLES = new Set([
72
- 'RANDOM',
73
- 'SECONDS',
74
- 'LINENO',
75
- 'PPID',
76
- 'OPTIND',
77
- 'BASH_SUBSHELL',
78
- ]);
79
- /** The dialects whose scripts run under Bash, where {@link BASH_INTEGER_VARIABLES} holds. */
54
+ /** The dialects whose scripts run under Bash, where {@link isBashIntegerVariable} holds. */
80
55
  const BASH_DIALECTS = new Set(['bash', 'bats']);
81
56
  /** Redirection operators that introduce a here-document (`<<`, `<<-`). */
82
57
  const HEREDOC_OPERATORS = new Set([Hdoc, DashHdoc]);
@@ -94,7 +69,7 @@ function isSingleWordParamExp(node, bashIntegers) {
94
69
  }
95
70
  const name = node.param?.value;
96
71
  return (name !== undefined &&
97
- (SINGLE_WORD_SPECIALS.has(name) || (bashIntegers && BASH_INTEGER_VARIABLES.has(name))) &&
72
+ (SINGLE_WORD_SPECIALS.has(name) || (bashIntegers && isBashIntegerVariable(name))) &&
98
73
  !node.excl &&
99
74
  node.exp === undefined &&
100
75
  node.repl === undefined &&
@@ -1,19 +1,3 @@
1
- /**
2
- * `sh/require-network-wrapper`: every network command must be run through the
3
- * project's configured wrapper (a retry, audit, or proxy helper).
4
- *
5
- * This is the built-in instance of `createWrapperRule`: the commands
6
- * default to common network clients, and the wrapper comes from the user's
7
- * rule's `wrapper` option, or else from the shared `settings.sh.networkWrapper`
8
- * (which the declaration rules also read). The wrapper has **no default**.
9
- * Which wrapper guards network calls is a project's policy, and a generic
10
- * linter must not invent it, so with neither set the rule is inert: it
11
- * registers no listeners, reports nothing, and never analyzes the file.
12
- *
13
- * `git` counts only for the subcommands that talk to a remote (`fetch`,
14
- * `clone`, `pull`, `push`), found after git's own global options
15
- * (`git -C dir fetch`).
16
- */
17
1
  import type { WrapperSpec } from 'sh-ast/analyze';
18
2
  /**
19
3
  * The options of `sh/require-network-wrapper`.
@@ -26,11 +10,12 @@ export interface RequireNetworkWrapperOptions {
26
10
  /**
27
11
  * The wrapper network commands must be run through: a name, or a
28
12
  * `WrapperSpec` declaring the wrapper's option shape. Unset falls back to
29
- * `settings.sh.networkWrapper`; with neither, the rule is inert.
13
+ * `settings: { sh: { networkWrapper: 'with-retry' } }`; conflicting values
14
+ * throw instead of overriding the shared policy. With neither, the rule is inert.
30
15
  */
31
16
  readonly wrapper?: string | WrapperSpec;
32
17
  /** Commands checked in addition to `commands` (or its default). */
33
- readonly alsoTreatAsNetwork?: readonly string[];
18
+ readonly additionalCommands?: readonly string[];
34
19
  }
35
20
  /**
36
21
  * The `sh/require-network-wrapper` rule module.
@@ -1,6 +1,24 @@
1
+ /**
2
+ * `sh/require-network-wrapper`: every network command must be run through the
3
+ * project's configured wrapper (a retry, audit, or proxy helper).
4
+ *
5
+ * This is the built-in instance of `createWrapperRule`: the commands
6
+ * default to common network clients, and the wrapper comes from the user's
7
+ * shared `settings: { sh: { networkWrapper: 'with-retry' } }` or the local
8
+ * `wrapper` option. Conflicting values throw, so policies cannot silently disagree. The wrapper has **no default**.
9
+ * Which wrapper guards network calls is a project's policy, and a generic
10
+ * linter must not invent it, so with neither set the rule is inert: it
11
+ * registers no listeners, reports nothing, and never analyzes the file.
12
+ *
13
+ * `git` counts only for the subcommands that talk to a remote (`fetch`,
14
+ * `clone`, `pull`, `push`), found after git's own global options
15
+ * (`git -C dir fetch`).
16
+ */
17
+ import { configurationMessage } from '../configuration-message.js';
18
+ import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-ast/analyze';
1
19
  import { createRule } from './create-rule.js';
2
20
  import { WRAPPER_RULE_MESSAGES, buildWrapperRule } from './create-wrapper-rule.js';
3
- import { readNetworkWrapperSetting } from './resolve-network-wrapper.js';
21
+ import { networkWrapperSpec, readNetworkWrapperSetting, sameWrapperSpec, } from './resolve-network-wrapper.js';
4
22
  /** The registry name of this rule (`sh/require-network-wrapper` in a user's config). */
5
23
  const RULE_NAME = 'require-network-wrapper';
6
24
  /**
@@ -71,7 +89,7 @@ const GIT_NETWORK_SUBCOMMANDS = {
71
89
  /**
72
90
  * The subcommand restrictions this rule always applies: `git` counts only for
73
91
  * its network subcommands, whether it comes from the default `commands`, a
74
- * configured `commands`, or `alsoTreatAsNetwork`.
92
+ * configured `commands`, or `additionalCommands`.
75
93
  */
76
94
  const NETWORK_SUBCOMMANDS = {
77
95
  git: GIT_NETWORK_SUBCOMMANDS,
@@ -182,24 +200,39 @@ const WRAPPER_SPEC_SCHEMA = {
182
200
  stopsChainFlags: STRING_LIST_SCHEMA,
183
201
  positionalOperandsBeforeCommand: { type: 'integer', minimum: 0 },
184
202
  assignmentOperands: { enum: ['shell-identifier', 'env', 'sudo'] },
203
+ shellBuiltin: { type: 'boolean' },
185
204
  },
186
205
  required: ['names'],
187
206
  additionalProperties: false,
188
207
  };
189
208
  /**
190
209
  * Resolves the factory config this rule enforces for one set of options, or
191
- * `null` when no wrapper is configured.
210
+ * `null` when no wrapper is configured. The configured rule ID names local
211
+ * options; shared project facts always retain the canonical settings.sh path.
192
212
  */
193
- function wrapperRuleConfig(options, settings) {
194
- // The rule option wins; otherwise the shared setting, which the
195
- // declaration rules read too (see `./resolve-network-wrapper.ts`).
196
- const wrapper = options?.wrapper ?? readNetworkWrapperSetting(settings);
213
+ function wrapperRuleConfig(options, settings, ruleId) {
214
+ const shared = readNetworkWrapperSetting(settings);
215
+ if (options?.wrapper !== undefined && shared !== undefined) {
216
+ const configured = options.wrapper;
217
+ const option = typeof configured === 'string'
218
+ ? (DEFAULT_TRANSPARENT_WRAPPERS.find((spec) => spec.names.includes(configured)) ?? {
219
+ names: [configured],
220
+ })
221
+ : configured;
222
+ const network = networkWrapperSpec(settings);
223
+ if (network !== null && !sameWrapperSpec(option, network)) {
224
+ throw Object.assign(new Error(configurationMessage(`rules[${JSON.stringify(ruleId)}].wrapper`, 'conflicts with settings.sh.networkWrapper; configure one shared network policy', configured)), { code: 'ESLINT_SH_WRAPPER_CONFLICT' });
225
+ }
226
+ }
227
+ const wrapper = shared ?? options?.wrapper;
197
228
  if (wrapper === undefined)
198
229
  return null;
199
230
  return {
231
+ // An explicit replacement (even []) selects the base; additions extend it.
232
+ // The factory normalizes names to a set, so overlap never multiplies reports.
200
233
  commands: [
201
234
  ...(options?.commands ?? DEFAULT_NETWORK_COMMANDS),
202
- ...(options?.alsoTreatAsNetwork ?? []),
235
+ ...(options?.additionalCommands ?? []),
203
236
  ],
204
237
  wrapper,
205
238
  subcommands: NETWORK_SUBCOMMANDS,
@@ -224,7 +257,7 @@ export const requireNetworkWrapper = createRule({
224
257
  properties: {
225
258
  commands: STRING_LIST_SCHEMA,
226
259
  wrapper: { oneOf: [{ type: 'string', minLength: 1 }, WRAPPER_SPEC_SCHEMA] },
227
- alsoTreatAsNetwork: STRING_LIST_SCHEMA,
260
+ additionalCommands: STRING_LIST_SCHEMA,
228
261
  },
229
262
  additionalProperties: false,
230
263
  },
@@ -232,7 +265,7 @@ export const requireNetworkWrapper = createRule({
232
265
  messages: WRAPPER_RULE_MESSAGES,
233
266
  },
234
267
  create(context) {
235
- const config = wrapperRuleConfig(context.options[0], context.settings);
268
+ const config = wrapperRuleConfig(context.options[0], context.settings, context.id);
236
269
  if (config === null)
237
270
  return {};
238
271
  // The check itself is the rule `createWrapperRule` makes for this config
@@ -1,52 +1,11 @@
1
- import type { ShellFile, ShellNodeOf } from '../language/syntax-view.js';
2
- import type { ShellSourceCode } from '../language/shell-source-code.js';
3
- import type { SetOptionChange } from './set-options.js';
4
- /**
5
- * The rule's single options object.
6
- *
7
- * @internal
8
- */
1
+ /** The policy's required options; prologue recognition is a shared project fact. @internal */
9
2
  export interface RequireStrictModeOptions {
10
3
  /**
11
- * The required prologue: either a `set` command (`"set -euo pipefail"`),
12
- * whose enabled options become the requirement and whose text is what the
13
- * autofix inserts, or the list of required long option names
14
- * (`["errexit", "nounset"]`), for which the autofix inserts a `set` command
15
- * spelled from that list. When omitted, it is `"set -eu"` for a file
16
- * whose dialect is `posix` and `"set -euo pipefail"` for every other
17
- * dialect.
4
+ * Nonempty, unique long option names from the existing `set -o` domain.
5
+ * When absent, POSIX requires errexit/nounset and other dialects also require
6
+ * pipefail. The formatter, rather than consumer text, owns autofix spelling.
18
7
  */
19
- readonly mode?: string | readonly string[];
20
- /**
21
- * When `true`, options passed to the shell on the shebang line
22
- * (`#!/bin/bash -e`, `#!/usr/bin/env -S bash -euo pipefail`) count as
23
- * enabled, so a shebang alone can satisfy the rule. Off by default, because
24
- * those options are ignored when the script is run as `bash script.sh`.
25
- */
26
- readonly allowShebangOnly?: boolean;
27
- /** How many top-level statements, from the top, the prologue may take to appear. */
28
- readonly withinStatements?: number;
8
+ readonly requiredOptions?: readonly string[];
29
9
  }
30
- /**
31
- * Whether `stmt` is a plain, foreground, top-level `set` command, and if so
32
- * the option changes its arguments make (read through `sourceCode`, so a
33
- * non-static argument ends the reading).
34
- *
35
- * @internal
36
- */
37
- export declare function setStatementChanges(stmt: ShellNodeOf<'Stmt'>, sourceCode: ShellSourceCode): readonly SetOptionChange[];
38
- /**
39
- * The options a file's prologue leaves enabled: the state after the first
40
- * `withinStatements` top-level statements of `file`, plus the shebang's
41
- * options when `allowShebangOnly` is set. This is the single definition of
42
- * "what the prologue enables", shared with other rules (such as
43
- * `no-unchecked-cd`) that need to know whether strict mode is in force.
44
- *
45
- * @internal
46
- */
47
- export declare function prologueEnabledOptions(file: ShellFile, sourceCode: ShellSourceCode, config?: {
48
- readonly allowShebangOnly?: boolean;
49
- readonly withinStatements?: number;
50
- }): ReadonlySet<string>;
51
10
  /** Built-in rule `sh/require-strict-mode`. @internal */
52
11
  export declare const requireStrictMode: import("./define-shell-rule.js").ShellRule<[RequireStrictModeOptions], "missingStrictMode">;