@eslint-sh/eslint-plugin 0.12.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/README.md +32 -12
  2. package/dist/api.d.ts +11 -1
  3. package/dist/api.js +8 -0
  4. package/dist/command-harvest/harvest-commands.d.ts +5 -3
  5. package/dist/command-harvest/harvest-commands.js +34 -12
  6. package/dist/eslint-plugin-api.d.ts +65 -12
  7. package/dist/language/command-resolution.d.ts +24 -6
  8. package/dist/language/command-resolution.js +44 -17
  9. package/dist/language/shell-language.js +2 -0
  10. package/dist/language/shell-source-code.d.ts +29 -11
  11. package/dist/language/shell-source-code.js +35 -26
  12. package/dist/language/word-resolution.d.ts +18 -12
  13. package/dist/language/word-resolution.js +133 -3
  14. package/dist/processors/disable-justification.d.ts +20 -0
  15. package/dist/processors/disable-justification.js +124 -0
  16. package/dist/rules/compat/bash-builtin-destination.d.ts +19 -0
  17. package/dist/rules/compat/bash-builtin-destination.js +83 -0
  18. package/dist/rules/compat/declaration-options.d.ts +2 -2
  19. package/dist/rules/compat/declaration-options.js +23 -17
  20. package/dist/rules/compat/no-case-attributes.js +2 -4
  21. package/dist/rules/compat/no-mapfile.js +52 -20
  22. package/dist/rules/compat/no-negative-array-index.d.ts +3 -8
  23. package/dist/rules/compat/no-negative-array-index.js +7 -30
  24. package/dist/rules/compat/no-nounset-empty-array.js +10 -7
  25. package/dist/rules/compat/no-parameter-transformation.js +2 -1
  26. package/dist/rules/compat/no-printf-time-format.js +55 -16
  27. package/dist/rules/compat/no-source.d.ts +3 -8
  28. package/dist/rules/compat/no-source.js +3 -40
  29. package/dist/rules/compat/no-test-v.js +257 -71
  30. package/dist/rules/compat/no-wait-n.js +43 -12
  31. package/dist/rules/compat/nounset-array-proof.d.ts +3 -1
  32. package/dist/rules/compat/nounset-array-proof.js +84 -63
  33. package/dist/rules/compat/posix-absent-command.js +3 -2
  34. package/dist/rules/compat/shopt-options.js +3 -2
  35. package/dist/rules/create-wrapper-rule.js +16 -9
  36. package/dist/rules/no-dynamic-invocation.js +8 -9
  37. package/dist/rules/no-eval.js +19 -8
  38. package/dist/rules/no-masked-exit-status.js +29 -17
  39. package/dist/rules/no-secret-flags.js +4 -3
  40. package/dist/rules/no-unchecked-cd.d.ts +8 -3
  41. package/dist/rules/no-unchecked-cd.js +65 -25
  42. package/dist/rules/no-unused-declarations.js +51 -19
  43. package/dist/rules/prefer-local.js +136 -16
  44. package/dist/rules/require-strict-mode.d.ts +2 -2
  45. package/dist/rules/require-strict-mode.js +27 -18
  46. package/dist/rules/require-tty-guard.d.ts +2 -0
  47. package/dist/rules/require-tty-guard.js +211 -45
  48. package/dist/rules/set-options.d.ts +3 -1
  49. package/dist/rules/set-options.js +5 -1
  50. package/dist/rules/strict-mode-prologue.d.ts +9 -2
  51. package/dist/rules/strict-mode-prologue.js +45 -8
  52. package/docs/custom-rule-tutorial.md +148 -155
  53. package/docs/disable-justification.md +71 -0
  54. package/docs/error-codes.md +4 -0
  55. package/docs/examples/eslint.config.js +11 -0
  56. package/docs/examples/no-curl-in-functions-rule.mjs +20 -23
  57. package/docs/examples/no-listed-commands-rule.ts +19 -19
  58. package/docs/examples/no-rm-rf-rule.mjs +33 -62
  59. package/docs/examples/no-sample-command-rule.mjs +33 -0
  60. package/docs/reading-nodes.md +26 -1
  61. package/docs/rules/README.md +4 -4
  62. package/docs/rules/compat/no-mapfile.md +14 -20
  63. package/docs/rules/compat/no-negative-array-index.md +9 -22
  64. package/docs/rules/compat/no-nounset-empty-array.md +5 -5
  65. package/docs/rules/compat/no-printf-time-format.md +2 -1
  66. package/docs/rules/compat/no-source.md +8 -17
  67. package/docs/rules/compat/no-test-v.md +8 -2
  68. package/docs/rules/compat/no-wait-n.md +3 -1
  69. package/docs/rules/no-dynamic-invocation.md +3 -0
  70. package/docs/rules/no-eval.md +22 -1
  71. package/docs/rules/no-masked-exit-status.md +2 -1
  72. package/docs/rules/no-secret-flags.md +22 -2
  73. package/docs/rules/no-unchecked-cd.md +3 -1
  74. package/docs/rules/no-undeclared-commands.md +1 -1
  75. package/docs/rules/no-unused-declarations.md +10 -6
  76. package/docs/rules/prefer-local.md +4 -0
  77. package/docs/rules/require-network-wrapper.md +1 -1
  78. package/docs/rules/require-strict-mode.md +19 -11
  79. package/docs/rules/require-tty-guard.md +26 -4
  80. package/docs/rules-about-commands.md +78 -0
  81. package/docs/selectors.md +8 -4
  82. package/docs/wrapper-rules.md +1 -1
  83. package/package.json +4 -4
@@ -30,7 +30,9 @@
30
30
  * that here-document's terminator and would carry the other command into the
31
31
  * guard.
32
32
  */
33
+ import { analysisDialect, describeWordAtPosition, isSingleField, resolveWordTextAtPosition, } from '../language/word-resolution.js';
33
34
  import { DashHdoc, Hdoc } from 'sh-ast';
35
+ import { someShellNode } from '../language/syntax-view.js';
34
36
  import { createRule } from './create-rule.js';
35
37
  import { lineBreakOf, shellLineBreakOf } from '../language/line-breaks.js';
36
38
  /** Registry name of this rule. */
@@ -56,14 +58,43 @@ const REDIRECT_INPUT_OPS = new Set([
56
58
  const DUP_INPUT = 67;
57
59
  /** The `-t` unary test operator (`sh-ast`'s `TsFdTerm`). */
58
60
  const TS_FD_TERM = 123;
59
- /**
60
- * Read options that consume an argument, so the next word is not a variable
61
- * name or another option cluster.
62
- */
63
- const READ_ARG_FLAGS = new Set(['a', 'd', 'i', 'n', 'N', 'p', 't', 'u']);
61
+ /** Decimal timeout accepted by Bash read, including signs and zero spellings. */
62
+ const BASH_READ_TIMEOUT = /^(?:\+?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]*)|-(?:0+(?:\.0*)?|\.0*))$/u;
63
+ const MKSH_READ_TIMEOUT = /^[0-9]+(?:\.[0-9]+)?$/u;
64
+ /** Flags whose argument is required, and switches that leave stdin selected. */
65
+ const READ_FLAGS = {
66
+ bash: { operands: 'adinNptu', switches: 'Eers' },
67
+ posix: { operands: 'p', switches: 'r' },
68
+ mksh: { operands: 'adnNt', switches: 'Ars' },
69
+ zsh: { operands: 'du', switches: 'Aers' },
70
+ };
71
+ /** Decimal spellings of fd 0 and supported alternate descriptors. */
72
+ function descriptorInput(target, dialect, redirect) {
73
+ if (target === null)
74
+ return 'unknown';
75
+ if (redirect && target === '-')
76
+ return 'explicit';
77
+ if (target === 'p' && (dialect === 'mksh' || dialect === 'zsh'))
78
+ return 'explicit';
79
+ const numeric = dialect === 'posix' || dialect === 'mksh'
80
+ ? /^[0-9]$/u
81
+ : redirect
82
+ ? dialect === 'bash'
83
+ ? /^[0-9]+-?$/u
84
+ : /^[0-9]+$/u
85
+ : /^[ \t\n\r\f\v]*[+-]?[0-9]+$/u;
86
+ if (!numeric.test(target))
87
+ return 'unknown';
88
+ const fd = Number(redirect ? target.replace(/-$/u, '') : target);
89
+ if (!Number.isSafeInteger(fd) || fd < 0 || fd > 2_147_483_647)
90
+ return 'unknown';
91
+ if (fd > 0)
92
+ return 'explicit';
93
+ return redirect && target.endsWith('-') ? 'unknown' : 'stdin';
94
+ }
64
95
  /** The static text of `node` if it is a `Word` with a known value. */
65
96
  function textOf(sourceCode, node) {
66
- return node?.type === 'Word' ? sourceCode.resolveWordText(node) : null;
97
+ return node?.type === 'Word' ? resolveWordTextAtPosition(sourceCode, node) : null;
67
98
  }
68
99
  /**
69
100
  * Whether `call` is a `[ … -t 0 … ]` or `test … -t 0 …` command. `-t 0` must
@@ -78,7 +109,9 @@ function isCommandTtyTest(call, sourceCode) {
78
109
  }
79
110
  /** Whether `test` is a `[[ -t 0 ]]` unary test. */
80
111
  function isClauseTtyTest(test, sourceCode) {
81
- return test.op === TS_FD_TERM && textOf(sourceCode, test.x) === '0';
112
+ return (test.op === TS_FD_TERM &&
113
+ test.x?.type === 'Word' &&
114
+ resolveWordTextAtPosition(sourceCode, test.x) === '0');
82
115
  }
83
116
  /** Whether `inner` lies within `outer`, by source range. */
84
117
  function within(inner, outer) {
@@ -91,46 +124,152 @@ function listRange(stmts) {
91
124
  return [stmts[0].range[0], stmts[stmts.length - 1].range[1]];
92
125
  }
93
126
  /**
94
- * Whether a redirect on a statement feeds fd 0 from an explicit source:
95
- * `< file`, `0< file`, `<<`, `<<<`, `<> file`, or `<&N` for `N` other than 0.
127
+ * Whether a redirect feeds fd 0 from an explicit source:
128
+ * `< file`, `0< file`, `<<`, `<<<`, `<> file`, a known nonzero descriptor,
129
+ * a Zsh or mksh coprocess (`<&p`), or a closed descriptor (`<&-`).
96
130
  */
97
- function redirectsStdin(stmt, sourceCode) {
98
- return stmt.redirs.some((redir) => {
131
+ function redirectedInput(stmt, sourceCode) {
132
+ const dialect = analysisDialect(sourceCode.dialect);
133
+ let input = 'stdin';
134
+ for (const redir of stmt.redirs) {
99
135
  if (!REDIRECT_INPUT_OPS.has(redir.op))
100
- return false;
136
+ continue;
101
137
  const fd = redir.n === undefined ? '0' : redir.n.value;
102
- if (fd !== '0')
103
- return false;
104
- return redir.op !== DUP_INPUT || textOf(sourceCode, redir.word) !== '0';
105
- });
138
+ if (fd !== '0' && !((dialect === 'bash' || dialect === 'zsh') && /^0+$/u.test(fd)))
139
+ continue;
140
+ if (redir.op !== DUP_INPUT)
141
+ return 'explicit';
142
+ const duplicated = descriptorInput(textOf(sourceCode, redir.word), dialect, true);
143
+ if (duplicated === 'explicit')
144
+ return 'explicit';
145
+ if (duplicated === 'unknown')
146
+ input = 'unknown';
147
+ }
148
+ return input;
106
149
  }
107
150
  /**
108
- * Whether a `read` takes input from a descriptor other than 0 (`-u 3`,
109
- * `-u3`, `-ru 3`). A non-static descriptor counts as non-zero, because the
110
- * rule cannot show it is stdin. Option parsing stops at `--` or the first
111
- * non-option word.
151
+ * The final input descriptor selected by `read`. An unresolved option or
152
+ * operand can change subsequent option parsing. Even when the final descriptor
153
+ * is zero, an earlier nonzero descriptor can fail Bash's checks before the read.
112
154
  */
113
- function readsFromOtherFd(call, sourceCode) {
114
- const words = call.args.slice(1);
155
+ function readInput(call, sourceCode) {
156
+ const dialect = analysisDialect(sourceCode.dialect);
157
+ const { operands, switches } = READ_FLAGS[dialect];
158
+ const words = call.args
159
+ .slice(1)
160
+ .map((word) => describeWordAtPosition(sourceCode, word))
161
+ .filter((facts) => facts.fields.max !== 0);
162
+ let input = 'stdin';
163
+ let checkedAlternateDescriptor = false;
164
+ // Zsh's -p flag reads the coprocess wherever it appears; `-u p` selects it
165
+ // only until a later -u selects another descriptor.
166
+ let usesCoprocess = false;
167
+ let coprocessDescriptor = false;
168
+ let readsKeys = false;
169
+ let readsAnswer = false;
170
+ let selectedDescriptor = false;
171
+ let uncertainArguments = false;
115
172
  for (let i = 0; i < words.length; i += 1) {
116
- const text = textOf(sourceCode, words[i]);
117
- if (text === null || !text.startsWith('-') || text === '-' || text === '--')
118
- return false;
173
+ const word = words[i];
174
+ if (!isSingleField(word))
175
+ return 'unknown';
176
+ if (!word.resolution.static) {
177
+ if (word.knownPrefix === '' || word.knownPrefix.startsWith('-'))
178
+ return 'unknown';
179
+ // Option processing stops here. The variable operand can still be invalid.
180
+ uncertainArguments = true;
181
+ break;
182
+ }
183
+ const text = word.resolution.text;
184
+ if (!text.startsWith('-') || text === '-' || text === '--')
185
+ break;
119
186
  for (let j = 1; j < text.length; j += 1) {
120
187
  const flag = text.charAt(j);
121
- if (!READ_ARG_FLAGS.has(flag))
122
- continue;
123
188
  const rest = text.slice(j + 1);
189
+ if (switches.includes(flag))
190
+ continue;
191
+ // Zsh's -q, like -k, reads one character from the terminal unless -u
192
+ // or -p selects another descriptor, even when stdin is redirected.
193
+ if (dialect === 'zsh' && flag === 'q') {
194
+ readsKeys = true;
195
+ readsAnswer = true;
196
+ continue;
197
+ }
198
+ if (dialect === 'zsh' && flag === 'k') {
199
+ readsKeys = true;
200
+ if (rest !== '') {
201
+ if (!/^[0-9]+$/u.test(rest))
202
+ return 'unknown';
203
+ }
204
+ else {
205
+ const next = words.at(i + 1)?.resolution;
206
+ if (next?.static && /^[0-9]+$/u.test(next.text))
207
+ i++;
208
+ }
209
+ break;
210
+ }
211
+ if (flag === 'p' && (dialect === 'mksh' || dialect === 'zsh')) {
212
+ if (dialect === 'zsh')
213
+ usesCoprocess = true;
214
+ input = 'explicit';
215
+ checkedAlternateDescriptor = true;
216
+ continue;
217
+ }
218
+ // mksh's -u takes an optional attached descriptor. -u with no digits
219
+ // selects stdin and leaves the next word to be read as a variable name.
220
+ if (flag === 'u' && dialect === 'mksh') {
221
+ input = descriptorInput(rest === '' ? '0' : rest, dialect, false);
222
+ if (input === 'unknown')
223
+ return input;
224
+ checkedAlternateDescriptor ||= input === 'explicit';
225
+ break;
226
+ }
227
+ if (!operands.includes(flag))
228
+ return 'unknown';
229
+ let operand = rest;
230
+ if (rest === '') {
231
+ const next = words.at(++i);
232
+ if (next === undefined || !isSingleField(next))
233
+ return 'unknown';
234
+ operand = next.resolution.static ? next.resolution.text : null;
235
+ // These arguments can be any one string. Keep uncertain expansions
236
+ // out of the proposed edit when read still uses stdin.
237
+ uncertainArguments ||= operand === null;
238
+ }
124
239
  if (flag === 'u') {
125
- const fd = rest === '' ? textOf(sourceCode, words[i + 1]) : rest;
126
- return fd !== '0';
240
+ selectedDescriptor = true;
241
+ coprocessDescriptor = dialect === 'zsh' && operand === 'p';
242
+ input = descriptorInput(operand, dialect, false);
243
+ if (input === 'unknown')
244
+ return input;
245
+ checkedAlternateDescriptor ||= input === 'explicit';
246
+ }
247
+ else if (flag === 't') {
248
+ const timeout = dialect === 'bash' ? BASH_READ_TIMEOUT : MKSH_READ_TIMEOUT;
249
+ if (operand === null || !timeout.test(operand))
250
+ return 'unknown';
251
+ }
252
+ else if ((flag === 'n' || flag === 'N') &&
253
+ (operand === null || !/^[0-9]+$/u.test(operand) || Number(operand) > 2_147_483_647)) {
254
+ return 'unknown';
255
+ }
256
+ else if (flag === 'a' && (operand === null || !/^[A-Za-z_][A-Za-z_0-9]*$/u.test(operand))) {
257
+ return 'unknown';
127
258
  }
128
- if (rest === '')
129
- i += 1;
130
259
  break;
131
260
  }
132
261
  }
133
- return false;
262
+ if (usesCoprocess || coprocessDescriptor)
263
+ return 'explicit';
264
+ if (readsKeys && !selectedDescriptor)
265
+ return 'terminal';
266
+ // `read -q` from a selected stdin can still fail on its operands (such as
267
+ // `-A` with two names); a guard would turn that failure into success.
268
+ if (readsAnswer && input === 'stdin')
269
+ return 'unknown';
270
+ return input === 'stdin' && (checkedAlternateDescriptor || uncertainArguments)
271
+ ? 'unknown'
272
+ : input;
134
273
  }
135
274
  /**
136
275
  * Whether the exit status of `stmt` steers control flow: it is a condition of
@@ -219,6 +358,21 @@ export const requireTtyGuard = createRule({
219
358
  },
220
359
  create(context) {
221
360
  const { sourceCode } = context;
361
+ const dialect = analysisDialect(sourceCode.dialect);
362
+ const statusNames = new Set(['?']);
363
+ if (dialect === 'bash')
364
+ statusNames.add('PIPESTATUS');
365
+ if (dialect === 'zsh') {
366
+ statusNames.add('pipestatus');
367
+ statusNames.add('status');
368
+ }
369
+ const readsStatus = (stmt) => someShellNode(stmt, (part) => {
370
+ if (part.type === 'ParamExp') {
371
+ return statusNames.has(part.param?.value ?? '') || (dialect === 'bash' && part.excl);
372
+ }
373
+ return ((part.type === 'ArithmExp' || part.type === 'ArithmCmd') &&
374
+ someShellNode(part, (term) => term.type === 'Lit' && statusNames.has(term.value)));
375
+ });
222
376
  const options = context.options[0];
223
377
  // Empty replacements intentionally remove defaults; a set makes overlapping
224
378
  // base/additional names one policy without mutating either configured list.
@@ -250,23 +404,29 @@ export const requireTtyGuard = createRule({
250
404
  return false;
251
405
  };
252
406
  /** Whether stdin is supplied to `node` by a redirect or a pipe. */
253
- const hasExplicitInput = (node) => {
407
+ const inheritedInput = (node) => {
254
408
  let child = node;
409
+ let input = 'stdin';
255
410
  for (let parent = sourceCode.getParent(child); parent; parent = sourceCode.getParent(child)) {
256
- if (parent.type === 'Stmt' && redirectsStdin(parent, sourceCode))
257
- return true;
411
+ if (parent.type === 'Stmt') {
412
+ const redirected = redirectedInput(parent, sourceCode);
413
+ if (redirected === 'explicit')
414
+ return 'explicit';
415
+ if (redirected === 'unknown')
416
+ input = 'unknown';
417
+ }
258
418
  // A coprocess body reads the coprocess's own input pipe, never the
259
419
  // terminal, so a terminal guard is meaningless there.
260
420
  if (parent.type === 'CoprocClause')
261
- return true;
421
+ return 'explicit';
262
422
  if (parent.type === 'BinaryCmd' &&
263
423
  (parent.op === PIPE || parent.op === PIPE_ALL) &&
264
424
  parent.y === child) {
265
- return true;
425
+ return 'explicit';
266
426
  }
267
427
  child = parent;
268
428
  }
269
- return false;
429
+ return input;
270
430
  };
271
431
  return {
272
432
  CallExpr(node) {
@@ -275,9 +435,10 @@ export const requireTtyGuard = createRule({
275
435
  const name = sourceCode.getCommandName(node);
276
436
  if (name === null || !commands.has(name))
277
437
  return;
278
- if (name === 'read' && readsFromOtherFd(node, sourceCode))
438
+ const input = name === 'read' ? readInput(node, sourceCode) : 'stdin';
439
+ if (input === 'explicit')
279
440
  return;
280
- candidates.push({ node, name });
441
+ candidates.push({ node, name, input });
281
442
  },
282
443
  UnaryTest(node) {
283
444
  if (isClauseTtyTest(node, sourceCode))
@@ -285,21 +446,26 @@ export const requireTtyGuard = createRule({
285
446
  },
286
447
  ForClause(node) {
287
448
  if (node.select && commands.has('select')) {
288
- candidates.push({ node, name: 'select' });
449
+ candidates.push({ node, name: 'select', input: 'stdin' });
289
450
  }
290
451
  },
291
452
  'File:exit'() {
292
- for (const { node, name } of candidates) {
293
- if (hasExplicitInput(node) || isGuarded(node))
453
+ for (const { node, name, input } of candidates) {
454
+ const inherited = inheritedInput(node);
455
+ if ((inherited === 'explicit' && input !== 'terminal') || isGuarded(node))
294
456
  continue;
295
457
  const stmt = sourceCode.getParent(node);
296
458
  // A statement that runs in the background cannot be wrapped without
297
459
  // changing what is backgrounded, so it gets no suggestion.
298
460
  // A statement sharing its here-document's opener line with another
299
- // command gets none either (see `sharesHeredocOpenerLine`).
300
- const wrappable = stmt?.type === 'Stmt' &&
461
+ // command gets none either (see `sharesHeredocOpenerLine`). The TTY
462
+ // test also changes the status visible to expansions in the statement.
463
+ const wrappable = input === 'stdin' &&
464
+ inherited === 'stdin' &&
465
+ stmt?.type === 'Stmt' &&
301
466
  !stmt.background &&
302
467
  !isControlFlowOperand(stmt, sourceCode) &&
468
+ !readsStatus(stmt) &&
303
469
  !sharesHeredocOpenerLine(stmt, sourceCode.text);
304
470
  context.report({
305
471
  node,
@@ -55,6 +55,8 @@ export interface ParsedSetArguments {
55
55
  */
56
56
  readonly stoppedBy: 'operand' | 'unknown' | 'malformed' | undefined;
57
57
  }
58
+ /** One field known to start positional arguments when an option does not consume it. @internal */
59
+ export declare const SET_POSITIONAL_OPERAND: unique symbol;
58
60
  /**
59
61
  * Reads a `set`-style argument list (the arguments after the command word, so
60
62
  * `["-euo", "pipefail"]` for `set -euo pipefail`) into the option changes it
@@ -69,7 +71,7 @@ export interface ParsedSetArguments {
69
71
  * @param args - Each argument's static text, or `null` when it is not static.
70
72
  * @internal
71
73
  */
72
- export declare function parseSetArguments(args: readonly (string | null)[]): ParsedSetArguments;
74
+ export declare function parseSetArguments(args: readonly (string | null | typeof SET_POSITIONAL_OPERAND)[]): ParsedSetArguments;
73
75
  /**
74
76
  * Applies `changes` to `enabled` in order, so a later `+e` undoes an earlier
75
77
  * `-e`. Mutates and returns `enabled`.
@@ -60,6 +60,8 @@ const SET_OPTION_FLAGS = Object.freeze({
60
60
  export const SET_OPTION_NAMES = Object.freeze(Object.keys(SET_OPTION_FLAGS));
61
61
  /** Single-letter flag → the long option name it toggles (the inverse of {@link SET_OPTION_FLAGS}). */
62
62
  const OPTION_NAME_BY_FLAG = new Map(Object.entries(SET_OPTION_FLAGS).flatMap(([name, flag]) => flag === undefined ? [] : [[flag, name]]));
63
+ /** One field known to start positional arguments when an option does not consume it. @internal */
64
+ export const SET_POSITIONAL_OPERAND = Symbol('set positional operand');
63
65
  /**
64
66
  * Reads a `set`-style argument list (the arguments after the command word, so
65
67
  * `["-euo", "pipefail"]` for `set -euo pipefail`) into the option changes it
@@ -80,6 +82,8 @@ export function parseSetArguments(args) {
80
82
  while (index < args.length) {
81
83
  const arg = args[index];
82
84
  index += 1;
85
+ if (arg === SET_POSITIONAL_OPERAND)
86
+ return { changes, stoppedBy: 'operand' };
83
87
  if (arg === null)
84
88
  return { changes, stoppedBy: 'unknown' };
85
89
  if (arg === '--')
@@ -98,7 +102,7 @@ export function parseSetArguments(args) {
98
102
  if (flag === 'o') {
99
103
  const name = index < args.length ? args[index] : undefined;
100
104
  index += 1;
101
- if (name === null)
105
+ if (name === null || name === SET_POSITIONAL_OPERAND)
102
106
  return { changes, stoppedBy: 'unknown' };
103
107
  if (name === undefined || !Object.hasOwn(SET_OPTION_FLAGS, name)) {
104
108
  return { changes, stoppedBy: 'malformed' };
@@ -1,7 +1,7 @@
1
1
  import type { SettingsConfig } from '@eslint/core';
2
2
  import type { ShellFile, ShellNodeOf } from '../language/syntax-view.js';
3
3
  import type { ShellSourceCode } from '../language/shell-source-code.js';
4
- import type { SetOptionChange } from './set-options.js';
4
+ import type { ParsedSetArguments, SetOptionChange } from './set-options.js';
5
5
  /** The normalized project definition, independent of any required option list. @internal */
6
6
  export interface StrictModePrologue {
7
7
  /** Whether interpreter flags count, despite being bypassed by `bash script.sh`. */
@@ -24,6 +24,10 @@ export declare function resolveStrictModePrologue(settings: SettingsConfig): Str
24
24
  *
25
25
  * @internal
26
26
  */
27
+ export declare function setStatementArguments(stmt: ShellNodeOf<'Stmt'>, sourceCode: ShellSourceCode): ParsedSetArguments | undefined;
28
+ /** Reads shell set options from the proven argument shape. @internal */
29
+ export declare function parseSetWords(source: ShellSourceCode, words: readonly ShellNodeOf<'Word'>[]): ParsedSetArguments;
30
+ /** The known option changes for a plain `set` statement. @internal */
27
31
  export declare function setStatementChanges(stmt: ShellNodeOf<'Stmt'>, sourceCode: ShellSourceCode): readonly SetOptionChange[];
28
32
  /**
29
33
  * The options enabled after applying `seed`, then each statement's `set`
@@ -40,4 +44,7 @@ export declare function enabledAfter(seed: readonly SetOptionChange[], statement
40
44
  *
41
45
  * @internal
42
46
  */
43
- export declare function prologueEnabledOptions(file: ShellFile, sourceCode: ShellSourceCode, config: StrictModePrologue): ReadonlySet<string>;
47
+ export declare function prologueOptionState(file: ShellFile, sourceCode: ShellSourceCode, config: StrictModePrologue): {
48
+ enabled: ReadonlySet<string>;
49
+ hasUnknownOptions: boolean;
50
+ };
@@ -5,8 +5,9 @@
5
5
  * ESLINT_SH_STRICT_MODE_SETTING identifies a malformed project prologue fact;
6
6
  * rule-option schema failures remain ESLint responsibilities.
7
7
  */
8
+ import { describeWordAtPosition, isSingleField } from '../language/word-resolution.js';
8
9
  import { configurationMessage } from '../configuration-message.js';
9
- import { applySetOptionChanges, parseSetArguments, shebangOptionChanges } from './set-options.js';
10
+ import { applySetOptionChanges, parseSetArguments, SET_POSITIONAL_OPERAND, shebangOptionChanges, } from './set-options.js';
10
11
  /** The user-written path of the shared project fact, extended with its offending field. */
11
12
  const SETTING = 'settings.sh.strictModePrologue';
12
13
  /**
@@ -52,16 +53,48 @@ export function resolveStrictModePrologue(settings) {
52
53
  *
53
54
  * @internal
54
55
  */
55
- export function setStatementChanges(stmt, sourceCode) {
56
+ export function setStatementArguments(stmt, sourceCode) {
56
57
  if (stmt.background) {
57
- return [];
58
+ return undefined;
58
59
  }
59
60
  const { cmd } = stmt;
60
61
  if (cmd?.type !== 'CallExpr' || sourceCode.getCommandName(cmd) !== 'set') {
61
- return [];
62
+ return undefined;
62
63
  }
63
- const args = cmd.args.slice(1);
64
- return parseSetArguments(args.map((word) => sourceCode.resolveWordText(word))).changes;
64
+ const parsed = parseSetWords(sourceCode, cmd.args.slice(1));
65
+ if (stmt.redirs.length === 0)
66
+ return parsed;
67
+ // The redirection may fail before set runs. A successful disable can
68
+ // invalidate an earlier proof. Even a close may need a spare descriptor
69
+ // to restore the original one after set returns.
70
+ const final = new Map(parsed.changes.map((change) => [change.option, change]));
71
+ return {
72
+ changes: [...final.values()].filter((change) => !change.enable),
73
+ stoppedBy: 'unknown',
74
+ };
75
+ }
76
+ /** Reads shell set options from the proven argument shape. @internal */
77
+ export function parseSetWords(source, words) {
78
+ const args = [];
79
+ for (const word of words) {
80
+ const facts = describeWordAtPosition(source, word);
81
+ if (facts.fields.max === 0)
82
+ continue;
83
+ if (facts.resolution.static && isSingleField(facts)) {
84
+ args.push(facts.resolution.text);
85
+ }
86
+ else {
87
+ const first = facts.knownPrefix.charAt(0);
88
+ args.push(isSingleField(facts) && first !== '' && first !== '-' && first !== '+'
89
+ ? SET_POSITIONAL_OPERAND
90
+ : null);
91
+ }
92
+ }
93
+ return parseSetArguments(args);
94
+ }
95
+ /** The known option changes for a plain `set` statement. @internal */
96
+ export function setStatementChanges(stmt, sourceCode) {
97
+ return setStatementArguments(stmt, sourceCode)?.changes ?? [];
65
98
  }
66
99
  /**
67
100
  * The options enabled after applying `seed`, then each statement's `set`
@@ -84,8 +117,12 @@ export function enabledAfter(seed, statementChanges) {
84
117
  *
85
118
  * @internal
86
119
  */
87
- export function prologueEnabledOptions(file, sourceCode, config) {
120
+ export function prologueOptionState(file, sourceCode, config) {
88
121
  const shebangChanges = config.allowShebangOnly ? shebangOptionChanges(sourceCode.text) : [];
89
122
  const statements = file.stmts.slice(0, config.withinStatements);
90
- return enabledAfter(shebangChanges, statements.map((stmt) => setStatementChanges(stmt, sourceCode)));
123
+ const argumentsByStatement = statements.map((stmt) => setStatementArguments(stmt, sourceCode));
124
+ return {
125
+ enabled: enabledAfter(shebangChanges, argumentsByStatement.map((args) => args?.changes ?? [])),
126
+ hasUnknownOptions: argumentsByStatement.some((args) => args?.stoppedBy === 'unknown'),
127
+ };
91
128
  }