@eslint-sh/eslint-plugin 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -2
- package/dist/api.d.ts +1 -1
- package/dist/command-harvest/harvest-commands.d.ts +10 -4
- package/dist/command-harvest/harvest-commands.js +39 -10
- package/dist/eslint-plugin-api.d.ts +36 -14
- package/dist/language/command-resolution.d.ts +16 -6
- package/dist/language/command-resolution.js +44 -17
- package/dist/language/line-breaks.d.ts +10 -4
- package/dist/language/line-breaks.js +14 -5
- package/dist/language/shell-language.d.ts +2 -0
- package/dist/language/shell-language.js +10 -1
- package/dist/language/shell-source-code.d.ts +29 -13
- package/dist/language/shell-source-code.js +126 -55
- package/dist/language/word-resolution.d.ts +18 -12
- package/dist/language/word-resolution.js +133 -3
- package/dist/rules/compat/bash-builtin-destination.d.ts +2 -0
- package/dist/rules/compat/bash-builtin-destination.js +4 -0
- package/dist/rules/compat/declaration-options.d.ts +2 -2
- package/dist/rules/compat/declaration-options.js +23 -17
- package/dist/rules/compat/no-case-attributes.js +2 -4
- package/dist/rules/compat/no-mapfile.js +16 -19
- package/dist/rules/compat/no-negative-array-index.d.ts +3 -8
- package/dist/rules/compat/no-negative-array-index.js +7 -30
- package/dist/rules/compat/no-nounset-empty-array.js +10 -7
- package/dist/rules/compat/no-parameter-transformation.js +2 -1
- package/dist/rules/compat/no-printf-time-format.js +38 -16
- package/dist/rules/compat/no-source.d.ts +3 -8
- package/dist/rules/compat/no-source.js +3 -40
- package/dist/rules/compat/no-test-v.js +243 -61
- package/dist/rules/compat/no-wait-n.js +43 -12
- package/dist/rules/compat/nounset-array-proof.d.ts +3 -1
- package/dist/rules/compat/nounset-array-proof.js +84 -63
- package/dist/rules/compat/posix-absent-command.js +3 -2
- package/dist/rules/compat/shopt-options.js +3 -2
- package/dist/rules/create-wrapper-rule.js +16 -9
- package/dist/rules/no-dynamic-invocation.js +8 -9
- package/dist/rules/no-eval.js +19 -8
- package/dist/rules/no-masked-exit-status.js +34 -46
- package/dist/rules/no-secret-flags.js +4 -3
- package/dist/rules/no-unchecked-cd.d.ts +8 -3
- package/dist/rules/no-unchecked-cd.js +54 -25
- package/dist/rules/no-unused-declarations.js +127 -30
- package/dist/rules/prefer-local.js +146 -52
- package/dist/rules/require-shebang.js +23 -21
- package/dist/rules/require-strict-mode.d.ts +2 -2
- package/dist/rules/require-strict-mode.js +28 -16
- package/dist/rules/require-tty-guard.d.ts +2 -0
- package/dist/rules/require-tty-guard.js +205 -50
- package/dist/rules/set-options.d.ts +5 -16
- package/dist/rules/set-options.js +17 -45
- package/dist/rules/shebang-interpreter.d.ts +33 -10
- package/dist/rules/shebang-interpreter.js +522 -53
- package/dist/rules/shebang-matches-dialect.js +4 -4
- package/dist/rules/strict-mode-prologue.d.ts +9 -2
- package/dist/rules/strict-mode-prologue.js +45 -8
- package/dist/rules/variable-reads.d.ts +12 -0
- package/dist/rules/variable-reads.js +31 -0
- package/docs/custom-rule-tutorial.md +19 -10
- package/docs/error-codes.md +6 -1
- package/docs/examples/no-curl-in-functions-rule.mjs +7 -5
- package/docs/rules/README.md +5 -5
- package/docs/rules/compat/no-mapfile.md +11 -20
- package/docs/rules/compat/no-negative-array-index.md +9 -22
- package/docs/rules/compat/no-nounset-empty-array.md +5 -5
- package/docs/rules/compat/no-printf-time-format.md +2 -1
- package/docs/rules/compat/no-source.md +8 -17
- package/docs/rules/compat/no-test-v.md +7 -1
- package/docs/rules/compat/no-wait-n.md +3 -1
- package/docs/rules/no-dynamic-invocation.md +3 -0
- package/docs/rules/no-eval.md +22 -1
- package/docs/rules/no-masked-exit-status.md +4 -1
- package/docs/rules/no-secret-flags.md +22 -2
- package/docs/rules/no-unchecked-cd.md +2 -1
- package/docs/rules/no-undeclared-commands.md +1 -1
- package/docs/rules/no-unused-declarations.md +18 -9
- package/docs/rules/prefer-local.md +15 -33
- package/docs/rules/require-network-wrapper.md +1 -1
- package/docs/rules/require-shebang.md +46 -3
- package/docs/rules/require-strict-mode.md +40 -15
- package/docs/rules/require-tty-guard.md +27 -4
- package/docs/rules/shebang-matches-dialect.md +3 -0
- package/docs/shellcheck.md +1 -1
- package/docs/wrapper-rules.md +1 -1
- package/package.json +4 -4
|
@@ -10,7 +10,7 @@ import { TextSourceCodeBase } from '@eslint/plugin-kit';
|
|
|
10
10
|
import type { ShellFile as ShFile, ShellNode as ShNode, ShellNodeOf, ShellComment } from './syntax-view.js';
|
|
11
11
|
import type { Directive as IDirective, FileProblem, RulesConfig, SourceLocation, TraversalStep } from '@eslint/core';
|
|
12
12
|
import type { Node as SyntaxNode, PrintOptions, SyntaxPos } from 'sh-ast';
|
|
13
|
-
import type { Argv0Resolution, ResolveArgv0Options, ResolveWordOptions, WordResolution } from 'sh-analysis';
|
|
13
|
+
import type { Argv0Resolution, ResolveArgv0Options, ResolveWordOptions, WordFacts, WordResolution } from 'sh-analysis';
|
|
14
14
|
import type { ShellDialect, ShellLanguageOptions } from './shell-language-options.js';
|
|
15
15
|
import type { ShellCommandInventory } from './command-resolution.js';
|
|
16
16
|
/**
|
|
@@ -126,10 +126,15 @@ export declare class ShellSourceCode extends TextSourceCodeBase<{
|
|
|
126
126
|
* `sh-analysis` owns static-word interpretation and dynamic reasons; this
|
|
127
127
|
* method supplies the lint tree's position-based policy, without executing shell.
|
|
128
128
|
*
|
|
129
|
-
* Without explicit options, `CallExpr.args` use command-argument context
|
|
130
|
-
* `
|
|
131
|
-
*
|
|
132
|
-
*
|
|
129
|
+
* Without explicit options, `CallExpr.args` use command-argument context.
|
|
130
|
+
* An ordinary argument such as `a:~/bin` is literal in that context, while
|
|
131
|
+
* `PATH=a:~/bin` is dynamic because Bash can expand the tilde in an
|
|
132
|
+
* assignment-shaped argument. Other positions use the analyzer's
|
|
133
|
+
* conservative default, which also considers assignment-value expansion.
|
|
134
|
+
* Explicit options override the context defaults. Resolution uses the parsed dialect.
|
|
135
|
+
* Since sh-analysis 0.3, a top-level escaped literal can return `unsupported`
|
|
136
|
+
* when the analyzer receives no context. For the assignment value in
|
|
137
|
+
* `label=a\ b`, pass `{ context: 'assignment-value' }` to resolve `a b`.
|
|
133
138
|
*
|
|
134
139
|
* @param word - A parsed Word from the lint view.
|
|
135
140
|
* @param options - Explicit analysis policy overriding grammar defaults.
|
|
@@ -178,10 +183,11 @@ export declare class ShellSourceCode extends TextSourceCodeBase<{
|
|
|
178
183
|
* assignment-only statement such as `FOO=1`).
|
|
179
184
|
*
|
|
180
185
|
* Delegates to `resolveArgv0` with the `CommandSite` that `enumerateCommands`
|
|
181
|
-
* reports for this `CallExpr` in this file
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
* `
|
|
186
|
+
* reports for this `CallExpr` in this file. A wrapper that accepts assignments
|
|
187
|
+
* can skip a fixed scalar assignment prefix when the operand is known to
|
|
188
|
+
* occupy one field, even if the value is dynamic. For example,
|
|
189
|
+
* `env PATH=a:~/bin printf hi` resolves to `printf`. Read the effective
|
|
190
|
+
* command's name as `res.effective.static ? res.effective.text : null`.
|
|
185
191
|
* `sudo -u x "$prog"` and `exec "$@"` have a non-static `effective` with
|
|
186
192
|
* reason `expansion`; `env -S '…'` and `bash -c '…'` have reason
|
|
187
193
|
* `embedded-command`; `chain`
|
|
@@ -204,7 +210,7 @@ export declare class ShellSourceCode extends TextSourceCodeBase<{
|
|
|
204
210
|
* index would silently omit real commands.
|
|
205
211
|
*
|
|
206
212
|
* @param callExpr - A parsed `CallExpr` node from this file's lint tree.
|
|
207
|
-
* @param options -
|
|
213
|
+
* @param options - Wrapper and interpreter settings. Resolution uses the parsed dialect.
|
|
208
214
|
* `transparentWrappers` replaces `sh-analysis`'s
|
|
209
215
|
* `DEFAULT_TRANSPARENT_WRAPPERS`; spread the defaults into the array to
|
|
210
216
|
* extend them instead.
|
|
@@ -314,6 +320,11 @@ export declare class ShellSourceCode extends TextSourceCodeBase<{
|
|
|
314
320
|
* The range is `node.range`, except that a `File` root replaces the whole
|
|
315
321
|
* text (`[0, text.length]`) whatever `node` is, which allows whole-file
|
|
316
322
|
* regeneration. `options` is passed to `printSync` unchanged.
|
|
323
|
+
* A `File` root can cover deferred heredocs outside a subtree. If a
|
|
324
|
+
* carriage return may belong to a heredoc, printing is refused even for a
|
|
325
|
+
* `File` root because the parser can interpret it as part of a line ending.
|
|
326
|
+
* The printer must also preserve the positions of carriage returns in
|
|
327
|
+
* the replaced text, including inside quoted words.
|
|
317
328
|
*
|
|
318
329
|
* The printer formats; it does not replay the source. Its output starts at
|
|
319
330
|
* column 0 and indents nested lines with upstream's indentation (tabs by
|
|
@@ -340,9 +351,12 @@ export declare class ShellSourceCode extends TextSourceCodeBase<{
|
|
|
340
351
|
* @throws TypeError with `ESLINT_SH_FOREIGN_VIEW_NODE` when `node` has no original record in this file (see
|
|
341
352
|
* {@link ShellSourceCode.getEditableSyntax}), when `edited` is not a
|
|
342
353
|
* printable root (`ESLINT_SH_PRINT_ROOT`; the message names its type), or when the printer's output
|
|
343
|
-
* is not valid UTF-8 (`ESLINT_SH_PRINT_ENCODING`), or
|
|
344
|
-
*
|
|
345
|
-
*
|
|
354
|
+
* is not valid UTF-8 (`ESLINT_SH_PRINT_ENCODING`), or a subtree replacement cannot cover the original
|
|
355
|
+
* or edited heredocs (`ESLINT_SH_PRINT_HEREDOC_RANGE`), or the replacement could
|
|
356
|
+
* lose a carriage return in a heredoc (`ESLINT_SH_PRINT_HEREDOC_TEXT`), or the
|
|
357
|
+
* positions of carriage returns in the replacement differ from the source
|
|
358
|
+
* (`ESLINT_SH_PRINT_UNSAFE_TEXT`). Non-UTF-8 output comes back from `printSync`
|
|
359
|
+
* as a `Uint8Array` and is never decoded lossily into a fix.
|
|
346
360
|
* @throws `PrintError` (`SH_AST_SYNTAX_PRINT`), `PrintPanicError` (`SH_AST_SYNTAX_PRINT_PANIC`),
|
|
347
361
|
* or `InputError` (`SH_AST_SYNTAX_INPUT`), passed through unchanged from sh-ast, when upstream's
|
|
348
362
|
* printer fails for `edited` or `options`.
|
|
@@ -390,6 +404,8 @@ export declare class ShellSourceCode extends TextSourceCodeBase<{
|
|
|
390
404
|
line: number;
|
|
391
405
|
column: number;
|
|
392
406
|
};
|
|
407
|
+
/** Returns inventory facts for parsed command arguments. @internal */
|
|
408
|
+
getCommandArgumentFacts(callExpr: ShellNodeOf<'CallExpr'>): readonly WordFacts[] | undefined;
|
|
393
409
|
/**
|
|
394
410
|
* Returns the parent of `node`, or `undefined` for the root `File` node
|
|
395
411
|
* (or any node visited before `traverse()` has run once). ESLint always
|
|
@@ -7,44 +7,60 @@
|
|
|
7
7
|
* their classes, codes and causes.
|
|
8
8
|
*/
|
|
9
9
|
import { ConfigCommentParser, Directive, TextSourceCodeBase, VisitNodeStep, } from '@eslint/plugin-kit';
|
|
10
|
-
import { isShellComment, projectedComments, projectedCoordinates, syntaxForNode, traverseShell, viewForSyntax, walkShell, } from './syntax-view.js';
|
|
10
|
+
import { isShellComment, projectedComments, projectedCoordinates, someShellNode, syntaxForNode, traverseShell, viewForSyntax, walkShell, } from './syntax-view.js';
|
|
11
11
|
import { cloneSyntax } from './syntax-clone.js';
|
|
12
12
|
import { SourceCoordinates } from './source-coordinates.js';
|
|
13
|
-
import { SYNTAX_INTERFACES, printSync } from 'sh-ast';
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import { resolveWord } from './word-resolution.js';
|
|
13
|
+
import { DashHdoc, Hdoc, SYNTAX_INTERFACES, printSync } from 'sh-ast';
|
|
14
|
+
import { buildCommandSiteIndex, rawCallExprFor, resolveSiteArgv0, viewCommandInventory, } from './command-resolution.js';
|
|
15
|
+
import { resolvePublicWord } from './word-resolution.js';
|
|
17
16
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* Iteration tolerates shared/cyclic records; sh-ast still validates
|
|
22
|
-
* the printable syntax itself.
|
|
17
|
+
* Subtree edits must cover the original heredoc extents. The printer emits a
|
|
18
|
+
* heredoc's body and delimiter even when the parser ends the target at its
|
|
19
|
+
* header. An empty heredoc has no body position to establish its extent.
|
|
23
20
|
*/
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
21
|
+
function heredocsFitReplacement(target, edited) {
|
|
22
|
+
const [start, end] = target.range;
|
|
23
|
+
const originalOffsets = new Set();
|
|
24
|
+
const hasDeferredHeredoc = someShellNode(target, (node) => {
|
|
25
|
+
if (node.type !== 'Redirect' || (node.op !== Hdoc && node.op !== DashHdoc))
|
|
26
|
+
return false;
|
|
27
|
+
if (node.hdoc === undefined || node.range[0] < start || node.range[1] > end)
|
|
28
|
+
return true;
|
|
29
|
+
const original = syntaxForNode(node);
|
|
30
|
+
if (original?.Type !== 'Redirect')
|
|
31
|
+
return true;
|
|
32
|
+
originalOffsets.add(original.OpPos.Offset);
|
|
33
|
+
return false;
|
|
34
|
+
});
|
|
35
|
+
if (hasDeferredHeredoc)
|
|
36
|
+
return false;
|
|
37
|
+
// Edited records carry their original positions. Each printed heredoc must
|
|
38
|
+
// match a source redirect within the replacement range.
|
|
39
|
+
const pending = [edited];
|
|
27
40
|
const seen = new Set();
|
|
28
41
|
while (pending.length > 0) {
|
|
29
42
|
const current = pending.pop();
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
'the printer changes their value'), { code: 'ESLINT_SH_PRINT_UNSAFE_TEXT' });
|
|
35
|
-
}
|
|
36
|
-
// A checked byte-valued string is a leaf; its individual numbers are not syntax children.
|
|
37
|
-
if (current instanceof Uint8Array) {
|
|
43
|
+
if (current === null ||
|
|
44
|
+
typeof current !== 'object' ||
|
|
45
|
+
current instanceof Uint8Array ||
|
|
46
|
+
seen.has(current)) {
|
|
38
47
|
continue;
|
|
39
48
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
49
|
+
seen.add(current);
|
|
50
|
+
if ('Type' in current && current.Type === 'Redirect') {
|
|
51
|
+
const redirect = current;
|
|
52
|
+
if (redirect.Op === Hdoc || redirect.Op === DashHdoc) {
|
|
53
|
+
const offset = redirect.OpPos?.Offset;
|
|
54
|
+
if (offset === undefined || !originalOffsets.has(offset))
|
|
55
|
+
return false;
|
|
45
56
|
}
|
|
46
57
|
}
|
|
58
|
+
const children = Object.values(current);
|
|
59
|
+
for (const child of children) {
|
|
60
|
+
pending.push(child);
|
|
61
|
+
}
|
|
47
62
|
}
|
|
63
|
+
return true;
|
|
48
64
|
}
|
|
49
65
|
/**
|
|
50
66
|
* Shared instance — `ConfigCommentParser` is stateless (see
|
|
@@ -505,10 +521,15 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
505
521
|
* `sh-analysis` owns static-word interpretation and dynamic reasons; this
|
|
506
522
|
* method supplies the lint tree's position-based policy, without executing shell.
|
|
507
523
|
*
|
|
508
|
-
* Without explicit options, `CallExpr.args` use command-argument context
|
|
509
|
-
* `
|
|
510
|
-
*
|
|
511
|
-
*
|
|
524
|
+
* Without explicit options, `CallExpr.args` use command-argument context.
|
|
525
|
+
* An ordinary argument such as `a:~/bin` is literal in that context, while
|
|
526
|
+
* `PATH=a:~/bin` is dynamic because Bash can expand the tilde in an
|
|
527
|
+
* assignment-shaped argument. Other positions use the analyzer's
|
|
528
|
+
* conservative default, which also considers assignment-value expansion.
|
|
529
|
+
* Explicit options override the context defaults. Resolution uses the parsed dialect.
|
|
530
|
+
* Since sh-analysis 0.3, a top-level escaped literal can return `unsupported`
|
|
531
|
+
* when the analyzer receives no context. For the assignment value in
|
|
532
|
+
* `label=a\ b`, pass `{ context: 'assignment-value' }` to resolve `a b`.
|
|
512
533
|
*
|
|
513
534
|
* @param word - A parsed Word from the lint view.
|
|
514
535
|
* @param options - Explicit analysis policy overriding grammar defaults.
|
|
@@ -517,7 +538,7 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
517
538
|
* @public
|
|
518
539
|
*/
|
|
519
540
|
resolveWord(word, options) {
|
|
520
|
-
return
|
|
541
|
+
return resolvePublicWord(this, word, options);
|
|
521
542
|
}
|
|
522
543
|
/**
|
|
523
544
|
* The text-only form of {@link ShellSourceCode.resolveWord}: returns its text
|
|
@@ -534,16 +555,6 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
534
555
|
const resolution = this.resolveWord(word, options);
|
|
535
556
|
return resolution.static ? resolution.text : null;
|
|
536
557
|
}
|
|
537
|
-
/**
|
|
538
|
-
* The `resolveWord` options implied by `word`'s grammar position when the
|
|
539
|
-
* caller supplies none: command-argument context for a `CallExpr` argument,
|
|
540
|
-
* otherwise `resolveWord`'s own conservative default.
|
|
541
|
-
*/
|
|
542
|
-
#defaultWordOptions(word) {
|
|
543
|
-
const parent = this.getParent(word);
|
|
544
|
-
const isCommandArgument = parent?.type === 'CallExpr' && Array.isArray(parent.args) && parent.args.includes(word);
|
|
545
|
-
return isCommandArgument ? { context: 'command-argument' } : undefined;
|
|
546
|
-
}
|
|
547
558
|
/**
|
|
548
559
|
* Returns the static text of `callExpr`'s command word (`argv[0]`) as
|
|
549
560
|
* written, or `null` when that word is dynamic or absent. Transparent
|
|
@@ -575,10 +586,11 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
575
586
|
* assignment-only statement such as `FOO=1`).
|
|
576
587
|
*
|
|
577
588
|
* Delegates to `resolveArgv0` with the `CommandSite` that `enumerateCommands`
|
|
578
|
-
* reports for this `CallExpr` in this file
|
|
579
|
-
*
|
|
580
|
-
*
|
|
581
|
-
* `
|
|
589
|
+
* reports for this `CallExpr` in this file. A wrapper that accepts assignments
|
|
590
|
+
* can skip a fixed scalar assignment prefix when the operand is known to
|
|
591
|
+
* occupy one field, even if the value is dynamic. For example,
|
|
592
|
+
* `env PATH=a:~/bin printf hi` resolves to `printf`. Read the effective
|
|
593
|
+
* command's name as `res.effective.static ? res.effective.text : null`.
|
|
582
594
|
* `sudo -u x "$prog"` and `exec "$@"` have a non-static `effective` with
|
|
583
595
|
* reason `expansion`; `env -S '…'` and `bash -c '…'` have reason
|
|
584
596
|
* `embedded-command`; `chain`
|
|
@@ -601,7 +613,7 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
601
613
|
* index would silently omit real commands.
|
|
602
614
|
*
|
|
603
615
|
* @param callExpr - A parsed `CallExpr` node from this file's lint tree.
|
|
604
|
-
* @param options -
|
|
616
|
+
* @param options - Wrapper and interpreter settings. Resolution uses the parsed dialect.
|
|
605
617
|
* `transparentWrappers` replaces `sh-analysis`'s
|
|
606
618
|
* `DEFAULT_TRANSPARENT_WRAPPERS`; spread the defaults into the array to
|
|
607
619
|
* extend them instead.
|
|
@@ -614,7 +626,7 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
614
626
|
*/
|
|
615
627
|
resolveInvokedCommand(callExpr, options) {
|
|
616
628
|
const site = this.#commandSiteFor(callExpr);
|
|
617
|
-
return site ?
|
|
629
|
+
return site ? resolveSiteArgv0(site, options) : null;
|
|
618
630
|
}
|
|
619
631
|
/**
|
|
620
632
|
* Returns represented command sites and explicit opaque execution boundaries.
|
|
@@ -725,6 +737,11 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
725
737
|
* The range is `node.range`, except that a `File` root replaces the whole
|
|
726
738
|
* text (`[0, text.length]`) whatever `node` is, which allows whole-file
|
|
727
739
|
* regeneration. `options` is passed to `printSync` unchanged.
|
|
740
|
+
* A `File` root can cover deferred heredocs outside a subtree. If a
|
|
741
|
+
* carriage return may belong to a heredoc, printing is refused even for a
|
|
742
|
+
* `File` root because the parser can interpret it as part of a line ending.
|
|
743
|
+
* The printer must also preserve the positions of carriage returns in
|
|
744
|
+
* the replaced text, including inside quoted words.
|
|
728
745
|
*
|
|
729
746
|
* The printer formats; it does not replay the source. Its output starts at
|
|
730
747
|
* column 0 and indents nested lines with upstream's indentation (tabs by
|
|
@@ -751,9 +768,12 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
751
768
|
* @throws TypeError with `ESLINT_SH_FOREIGN_VIEW_NODE` when `node` has no original record in this file (see
|
|
752
769
|
* {@link ShellSourceCode.getEditableSyntax}), when `edited` is not a
|
|
753
770
|
* printable root (`ESLINT_SH_PRINT_ROOT`; the message names its type), or when the printer's output
|
|
754
|
-
* is not valid UTF-8 (`ESLINT_SH_PRINT_ENCODING`), or
|
|
755
|
-
*
|
|
756
|
-
*
|
|
771
|
+
* is not valid UTF-8 (`ESLINT_SH_PRINT_ENCODING`), or a subtree replacement cannot cover the original
|
|
772
|
+
* or edited heredocs (`ESLINT_SH_PRINT_HEREDOC_RANGE`), or the replacement could
|
|
773
|
+
* lose a carriage return in a heredoc (`ESLINT_SH_PRINT_HEREDOC_TEXT`), or the
|
|
774
|
+
* positions of carriage returns in the replacement differ from the source
|
|
775
|
+
* (`ESLINT_SH_PRINT_UNSAFE_TEXT`). Non-UTF-8 output comes back from `printSync`
|
|
776
|
+
* as a `Uint8Array` and is never decoded lossily into a fix.
|
|
757
777
|
* @throws `PrintError` (`SH_AST_SYNTAX_PRINT`), `PrintPanicError` (`SH_AST_SYNTAX_PRINT_PANIC`),
|
|
758
778
|
* or `InputError` (`SH_AST_SYNTAX_INPUT`), passed through unchanged from sh-ast, when upstream's
|
|
759
779
|
* printer fails for `edited` or `options`.
|
|
@@ -762,24 +782,71 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
762
782
|
const original = this.#originalSyntaxFor(node);
|
|
763
783
|
const rootType = printableRootType(edited);
|
|
764
784
|
const root = edited.Type === 'Stmt' ? { ...edited, Comments: null } : edited;
|
|
765
|
-
|
|
785
|
+
if (rootType !== 'File' &&
|
|
786
|
+
(this.#intersectsEnclosingHeredocHeader(node) || !heredocsFitReplacement(node, root))) {
|
|
787
|
+
throw Object.assign(new TypeError('cannot print a subtree fix without its complete heredocs'), { code: 'ESLINT_SH_PRINT_HEREDOC_RANGE' });
|
|
788
|
+
}
|
|
789
|
+
const target = rootType === 'File' ? this.ast : node;
|
|
790
|
+
if (this.#hasHeredocCarriageReturn(target)) {
|
|
791
|
+
throw Object.assign(new TypeError('cannot print a replacement that may lose a carriage return in a heredoc'), { code: 'ESLINT_SH_PRINT_HEREDOC_TEXT' });
|
|
792
|
+
}
|
|
766
793
|
const printed = printSync(root, options);
|
|
767
794
|
if (typeof printed !== 'string') {
|
|
768
795
|
throw Object.assign(new TypeError('printSync produced output that is not valid UTF-8; a text fix cannot hold it without ' +
|
|
769
796
|
'lossy decoding'), { code: 'ESLINT_SH_PRINT_ENCODING' });
|
|
770
797
|
}
|
|
771
|
-
|
|
772
|
-
|
|
798
|
+
const range = rootType === 'File' ? [0, this.text.length] : [node.range[0], node.range[1]];
|
|
799
|
+
const source = this.text.slice(...range);
|
|
800
|
+
let originalCr = source.indexOf('\r');
|
|
801
|
+
let printedCr = printed.indexOf('\r');
|
|
802
|
+
while (originalCr !== -1 && originalCr === printedCr) {
|
|
803
|
+
originalCr = source.indexOf('\r', originalCr + 1);
|
|
804
|
+
printedCr = printed.indexOf('\r', printedCr + 1);
|
|
805
|
+
}
|
|
806
|
+
if (originalCr !== printedCr) {
|
|
807
|
+
throw Object.assign(new TypeError('cannot print a replacement that changes the positions of carriage returns'), { code: 'ESLINT_SH_PRINT_UNSAFE_TEXT' });
|
|
773
808
|
}
|
|
774
809
|
const dropsTerminator = edited.Type === 'Stmt' &&
|
|
775
810
|
original.Type === 'Stmt' &&
|
|
776
811
|
original.Semicolon.IsValid &&
|
|
777
812
|
!printsOwnTerminator(edited);
|
|
778
813
|
return {
|
|
779
|
-
range
|
|
814
|
+
range,
|
|
780
815
|
text: dropsTerminator ? `${printed};` : printed,
|
|
781
816
|
};
|
|
782
817
|
}
|
|
818
|
+
/** Refuse when the parser may have normalized a carriage return in a heredoc. */
|
|
819
|
+
#hasHeredocCarriageReturn(target) {
|
|
820
|
+
let first;
|
|
821
|
+
return someShellNode(target, (node) => {
|
|
822
|
+
if (node.type !== 'Redirect' || (node.op !== Hdoc && node.op !== DashHdoc))
|
|
823
|
+
return false;
|
|
824
|
+
first ??= this.text.indexOf('\r', target.range[0]);
|
|
825
|
+
if (first === -1)
|
|
826
|
+
return false;
|
|
827
|
+
const carriageReturn = first >= node.range[0] ? first : this.text.indexOf('\r', node.range[0]);
|
|
828
|
+
// A CR can sit at the projected end of the delimiter, or the parser may
|
|
829
|
+
// have treated that delimiter as the start of an empty heredoc and supplied no body extent.
|
|
830
|
+
const end = node.hdoc === undefined ? this.text.length : node.range[1];
|
|
831
|
+
return carriageReturn !== -1 && carriageReturn <= end;
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
/** A statement owns its redirects even when their headers sit between command arguments. */
|
|
835
|
+
#intersectsEnclosingHeredocHeader(node) {
|
|
836
|
+
const [start, end] = node.range;
|
|
837
|
+
for (let owner = this.getParent(node); owner !== undefined; owner = this.getParent(owner)) {
|
|
838
|
+
if (owner.type !== 'Stmt')
|
|
839
|
+
continue;
|
|
840
|
+
for (const redirect of owner.redirs) {
|
|
841
|
+
if (redirect.op !== Hdoc && redirect.op !== DashHdoc)
|
|
842
|
+
continue;
|
|
843
|
+
const headerEnd = redirect.word?.range[1] ?? redirect.range[1];
|
|
844
|
+
if (start < headerEnd && redirect.range[0] < end)
|
|
845
|
+
return true;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
return false;
|
|
849
|
+
}
|
|
783
850
|
/**
|
|
784
851
|
* `node`'s original record in this file, or a `TypeError` when it has none,
|
|
785
852
|
* so the printed-fix helpers never copy or replace a range they cannot tie
|
|
@@ -848,7 +915,7 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
848
915
|
}
|
|
849
916
|
/** This file's command-site index, built by one `enumerateCommands` call on first use. */
|
|
850
917
|
#getCommandSiteIndex() {
|
|
851
|
-
this.#commandSites ??= buildCommandSiteIndex(this.ast);
|
|
918
|
+
this.#commandSites ??= buildCommandSiteIndex(this.ast, this.dialect);
|
|
852
919
|
return this.#commandSites;
|
|
853
920
|
}
|
|
854
921
|
/**
|
|
@@ -860,6 +927,10 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
860
927
|
const raw = rawCallExprFor(callExpr);
|
|
861
928
|
return this.#getCommandSiteIndex().byNode.get(raw);
|
|
862
929
|
}
|
|
930
|
+
/** Returns inventory facts for parsed command arguments. @internal */
|
|
931
|
+
getCommandArgumentFacts(callExpr) {
|
|
932
|
+
return this.#commandSiteFor(callExpr)?.argvFacts;
|
|
933
|
+
}
|
|
863
934
|
/**
|
|
864
935
|
* Returns the parent of `node`, or `undefined` for the root `File` node
|
|
865
936
|
* (or any node visited before `traverse()` has run once). ESLint always
|
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
import type { ResolveWordOptions, WordResolution } from 'sh-analysis';
|
|
1
|
+
import type { DescribeWordOptions, ResolveWordOptions, WordFacts, WordResolution } from 'sh-analysis';
|
|
2
|
+
import type { ShellDialect } from './shell-language-options.js';
|
|
3
|
+
import type { ShellSourceCode } from './shell-source-code.js';
|
|
4
|
+
import { withoutCrlfCarriageReturn } from './syntax-view.js';
|
|
2
5
|
import type { ShellNodeOf } from './syntax-view.js';
|
|
3
|
-
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
/** Bats shares Bash expansion rules. @internal */
|
|
7
|
+
export declare function analysisDialect(dialect: ShellDialect): DescribeWordOptions['dialect'];
|
|
8
|
+
/** A word whose expansion occupies exactly one argv position. @internal */
|
|
9
|
+
export declare function isSingleField({ fields }: WordFacts): boolean;
|
|
10
|
+
/** Describes a built-in rule's word at its original AST position. @internal */
|
|
11
|
+
export declare function describeWordAtPosition(source: ShellSourceCode, word: ShellNodeOf<'Word'>): WordFacts;
|
|
12
|
+
/** Quote removal for a function declaration's parser-provided literal name. @internal */
|
|
13
|
+
export declare function resolveFunctionName(source: ShellSourceCode, name: ShellNodeOf<'Lit'>): string | null;
|
|
14
|
+
/** Projects word facts into the same CRLF view as the word resolver. @internal */
|
|
15
|
+
export declare function withLfWordFacts(syntax: Parameters<typeof withoutCrlfCarriageReturn>[0], facts: WordFacts): WordFacts;
|
|
16
|
+
/** The known text of a built-in rule's word at its original AST position. @internal */
|
|
17
|
+
export declare function resolveWordTextAtPosition(source: ShellSourceCode, word: ShellNodeOf<'Word'>): string | null;
|
|
18
|
+
/** Preserves the public helper's context defaults using the source dialect. @internal */
|
|
19
|
+
export declare function resolvePublicWord(source: ShellSourceCode, word: ShellNodeOf<'Word'>, options?: ResolveWordOptions): WordResolution;
|
|
@@ -3,8 +3,134 @@
|
|
|
3
3
|
* ESLINT_SH_WORD_NODE rejects a missing source association; dynamic words
|
|
4
4
|
* remain ordinary analysis results rather than errors.
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import { DashHdoc, Hdoc, WordHdoc } from 'sh-ast';
|
|
7
|
+
import { describeWord, resolveWord as resolveSyntaxWord } from 'sh-analysis';
|
|
7
8
|
import { syntaxForNode, withoutCrlfCarriageReturn } from './syntax-view.js';
|
|
9
|
+
/** Bats shares Bash expansion rules. @internal */
|
|
10
|
+
export function analysisDialect(dialect) {
|
|
11
|
+
return dialect === 'bats' ? 'bash' : dialect;
|
|
12
|
+
}
|
|
13
|
+
/** A word whose expansion occupies exactly one argv position. @internal */
|
|
14
|
+
export function isSingleField({ fields }) {
|
|
15
|
+
return fields.min === 1 && fields.max === 1;
|
|
16
|
+
}
|
|
17
|
+
const COMMAND_WORD = { context: 'command-argument' };
|
|
18
|
+
const WORD_WITHOUT_FILENAME_GENERATION = {
|
|
19
|
+
context: 'command-argument',
|
|
20
|
+
filenameGeneration: false,
|
|
21
|
+
};
|
|
22
|
+
const ASSIGNMENT_VALUE = {
|
|
23
|
+
context: 'assignment-value',
|
|
24
|
+
filenameGeneration: false,
|
|
25
|
+
};
|
|
26
|
+
/** Options supported by the analyzer for an unquoted position established by the lint tree. */
|
|
27
|
+
function optionsAtPosition(source, word, commandArgumentsOnly = false) {
|
|
28
|
+
const parent = source.getParent(word);
|
|
29
|
+
if (parent?.type === 'CallExpr' && parent.args.includes(word))
|
|
30
|
+
return COMMAND_WORD;
|
|
31
|
+
if (parent?.type === 'Redirect' && (parent.op === Hdoc || parent.op === DashHdoc)) {
|
|
32
|
+
// An isolated word does not establish a heredoc's quoting rules.
|
|
33
|
+
return { filenameGeneration: false, braceExpansion: false };
|
|
34
|
+
}
|
|
35
|
+
if (commandArgumentsOnly)
|
|
36
|
+
return undefined;
|
|
37
|
+
switch (parent?.type) {
|
|
38
|
+
case 'Assign':
|
|
39
|
+
if (parent.value !== word)
|
|
40
|
+
return undefined;
|
|
41
|
+
if (parent.name !== undefined)
|
|
42
|
+
return ASSIGNMENT_VALUE;
|
|
43
|
+
return source.getParent(parent)?.type === 'DeclClause' ? COMMAND_WORD : undefined;
|
|
44
|
+
case 'ArrayElem':
|
|
45
|
+
return parent.value === word && parent.index === undefined ? COMMAND_WORD : undefined;
|
|
46
|
+
case 'BinaryTest':
|
|
47
|
+
case 'UnaryTest':
|
|
48
|
+
case 'TestClause':
|
|
49
|
+
case 'ParenTest':
|
|
50
|
+
case 'CaseClause':
|
|
51
|
+
case 'CaseItem':
|
|
52
|
+
return WORD_WITHOUT_FILENAME_GENERATION;
|
|
53
|
+
case 'WordIter':
|
|
54
|
+
return COMMAND_WORD;
|
|
55
|
+
case 'Redirect':
|
|
56
|
+
// POSIX non-interactive redirections and Bash here-strings suppress filename generation.
|
|
57
|
+
if (parent.word !== word)
|
|
58
|
+
return undefined;
|
|
59
|
+
return source.dialect === 'posix' ||
|
|
60
|
+
(parent.op === WordHdoc && (source.dialect === 'bash' || source.dialect === 'bats'))
|
|
61
|
+
? WORD_WITHOUT_FILENAME_GENERATION
|
|
62
|
+
: COMMAND_WORD;
|
|
63
|
+
default:
|
|
64
|
+
// In particular, arithmetic subscripts and parameter-expansion operands
|
|
65
|
+
// cannot use a stand-alone unquoted word's escape rules.
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Resolves a built-in rule's word using its original AST position. @internal */
|
|
70
|
+
function resolveWordAtPosition(source, word) {
|
|
71
|
+
return describeWordAtPosition(source, word).resolution;
|
|
72
|
+
}
|
|
73
|
+
/** Describes a built-in rule's word at its original AST position. @internal */
|
|
74
|
+
export function describeWordAtPosition(source, word) {
|
|
75
|
+
const parent = source.getParent(word);
|
|
76
|
+
if (parent?.type === 'CallExpr') {
|
|
77
|
+
const observed = source.getCommandArgumentFacts(parent)?.[parent.args.indexOf(word)];
|
|
78
|
+
if (observed !== undefined)
|
|
79
|
+
return observed;
|
|
80
|
+
}
|
|
81
|
+
const syntax = originalWord(word);
|
|
82
|
+
return withLfWordFacts(syntax, describeWord(syntax, {
|
|
83
|
+
...optionsAtPosition(source, word),
|
|
84
|
+
dialect: analysisDialect(source.dialect),
|
|
85
|
+
}));
|
|
86
|
+
}
|
|
87
|
+
/** Quote removal for a function declaration's parser-provided literal name. @internal */
|
|
88
|
+
export function resolveFunctionName(source, name) {
|
|
89
|
+
if (source.dialect !== 'zsh')
|
|
90
|
+
return name.value;
|
|
91
|
+
const part = syntaxForNode(name);
|
|
92
|
+
if (part?.Type !== 'Lit')
|
|
93
|
+
return null;
|
|
94
|
+
const result = resolveSyntaxWord({ Type: 'Word', Parts: [part] }, {
|
|
95
|
+
context: 'command-argument',
|
|
96
|
+
dialect: analysisDialect(source.dialect),
|
|
97
|
+
filenameGeneration: false,
|
|
98
|
+
braceExpansion: false,
|
|
99
|
+
});
|
|
100
|
+
return result.static ? result.text : null;
|
|
101
|
+
}
|
|
102
|
+
/** Projects word facts into the same CRLF view as the word resolver. @internal */
|
|
103
|
+
export function withLfWordFacts(syntax, facts) {
|
|
104
|
+
const suffix = withoutCrlfCarriageReturn(syntax, { static: true, text: facts.knownSuffix });
|
|
105
|
+
if (!suffix.static || suffix.text === facts.knownSuffix)
|
|
106
|
+
return facts;
|
|
107
|
+
const basename = facts.basename.known ? facts.basename.text.slice(0, -1) : '';
|
|
108
|
+
const removedOnlyLiteral = suffix.text === '' && !facts.resolution.static;
|
|
109
|
+
return {
|
|
110
|
+
...facts,
|
|
111
|
+
resolution: withoutCrlfCarriageReturn(syntax, facts.resolution),
|
|
112
|
+
fields: suffix.text === '' && !facts.quoted
|
|
113
|
+
? { min: 0, max: facts.resolution.static ? 0 : facts.fields.max }
|
|
114
|
+
: removedOnlyLiteral
|
|
115
|
+
? { ...facts.fields, min: 0 }
|
|
116
|
+
: facts.fields,
|
|
117
|
+
knownPrefix: facts.resolution.static ? suffix.text : facts.knownPrefix,
|
|
118
|
+
knownSuffix: suffix.text,
|
|
119
|
+
basename: basename === '' ? { known: false } : { known: true, text: basename },
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** The known text of a built-in rule's word at its original AST position. @internal */
|
|
123
|
+
export function resolveWordTextAtPosition(source, word) {
|
|
124
|
+
const result = resolveWordAtPosition(source, word);
|
|
125
|
+
return result.static ? result.text : null;
|
|
126
|
+
}
|
|
127
|
+
/** Preserves the public helper's context defaults using the source dialect. @internal */
|
|
128
|
+
export function resolvePublicWord(source, word, options) {
|
|
129
|
+
return resolveWord(word, {
|
|
130
|
+
...(options ?? optionsAtPosition(source, word, true)),
|
|
131
|
+
dialect: analysisDialect(source.dialect),
|
|
132
|
+
});
|
|
133
|
+
}
|
|
8
134
|
/**
|
|
9
135
|
* Resolves a parsed lint Word using the upstream-shaped syntax associated with it.
|
|
10
136
|
* The view supplies locations and selector fields; sh-analysis owns the word
|
|
@@ -15,10 +141,14 @@ import { syntaxForNode, withoutCrlfCarriageReturn } from './syntax-view.js';
|
|
|
15
141
|
* @throws TypeError with `ESLINT_SH_WORD_NODE` for a word without original syntax.
|
|
16
142
|
* @internal
|
|
17
143
|
*/
|
|
18
|
-
|
|
144
|
+
function resolveWord(word, options) {
|
|
145
|
+
const syntax = originalWord(word);
|
|
146
|
+
return withoutCrlfCarriageReturn(syntax, resolveSyntaxWord(syntax, options));
|
|
147
|
+
}
|
|
148
|
+
function originalWord(word) {
|
|
19
149
|
const syntax = syntaxForNode(word);
|
|
20
150
|
if (syntax?.Type !== 'Word') {
|
|
21
151
|
throw Object.assign(new TypeError('resolveWord expects a parsed shell Word from this language adapter'), { code: 'ESLINT_SH_WORD_NODE' });
|
|
22
152
|
}
|
|
23
|
-
return
|
|
153
|
+
return syntax;
|
|
24
154
|
}
|
|
@@ -10,10 +10,10 @@ import type { ShellRuleVisitor } from '../define-shell-rule.js';
|
|
|
10
10
|
*/
|
|
11
11
|
export declare const DECLARATION_AND_LOCAL_COMMANDS: ReadonlySet<string>;
|
|
12
12
|
/**
|
|
13
|
-
* Builds the listeners that call `report(
|
|
13
|
+
* Builds the listeners that call `report(node, command, options, text)` for each
|
|
14
14
|
* `declare` / `typeset` (or each of `commands`) whose option cluster contains
|
|
15
15
|
* `flag`: one letter, or a character class such as `[lu]` for either of two.
|
|
16
16
|
*
|
|
17
17
|
* @internal
|
|
18
18
|
*/
|
|
19
|
-
export declare function declarationFlagVisitor(context: TransparentWrappersBindingContext, flag: string, report: (
|
|
19
|
+
export declare function declarationFlagVisitor(context: TransparentWrappersBindingContext, flag: string, report: (node: ShellNode, command: string, options: readonly string[], text: string) => void, commands?: ReadonlySet<string>, reportEveryOption?: boolean): ShellRuleVisitor;
|