@eslint-sh/eslint-plugin 0.12.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 +5 -3
- package/dist/command-harvest/harvest-commands.js +34 -12
- package/dist/eslint-plugin-api.d.ts +25 -11
- package/dist/language/command-resolution.d.ts +16 -6
- package/dist/language/command-resolution.js +44 -17
- package/dist/language/shell-source-code.d.ts +18 -10
- package/dist/language/shell-source-code.js +24 -25
- 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 +29 -17
- 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 +51 -19
- package/dist/rules/prefer-local.js +136 -16
- package/dist/rules/require-strict-mode.d.ts +2 -2
- package/dist/rules/require-strict-mode.js +27 -18
- package/dist/rules/require-tty-guard.d.ts +2 -0
- package/dist/rules/require-tty-guard.js +197 -45
- package/dist/rules/set-options.d.ts +3 -1
- package/dist/rules/set-options.js +5 -1
- package/dist/rules/strict-mode-prologue.d.ts +9 -2
- package/dist/rules/strict-mode-prologue.js +45 -8
- package/docs/custom-rule-tutorial.md +10 -7
- package/docs/error-codes.md +4 -0
- package/docs/examples/no-curl-in-functions-rule.mjs +7 -5
- package/docs/rules/README.md +4 -4
- 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 +2 -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 +10 -6
- package/docs/rules/prefer-local.md +4 -0
- package/docs/rules/require-network-wrapper.md +1 -1
- package/docs/rules/require-strict-mode.md +19 -11
- package/docs/rules/require-tty-guard.md +26 -4
- package/docs/wrapper-rules.md +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -102,7 +102,7 @@ ShellLanguage }, rules, configs: { recommended, 'agent-authored', 'bash-3.2', 'p
|
|
|
102
102
|
gets the same answer: `resolveWord(word, options?)` (the full `WordResolution`) and
|
|
103
103
|
`resolveWordText(word, options?)` (exactly its static text, or `null`). Both use grammar-position
|
|
104
104
|
defaults: command-argument context for `CallExpr.args`, conservative tilde handling elsewhere;
|
|
105
|
-
explicit options override those defaults. Helpers also include `getCommandName(callExpr)` (the static command word,
|
|
105
|
+
explicit options override those context defaults. Word and command analysis use the parsed dialect. Helpers also include `getCommandName(callExpr)` (the static command word,
|
|
106
106
|
without following wrappers), and `resolveInvokedCommand(callExpr, { transparentWrappers? })` (the
|
|
107
107
|
full `Argv0Resolution` once wrappers such as `env`, `sudo`, and `exec` are followed, including the
|
|
108
108
|
wrapper `chain` and the `effective` command or the reason it is unknowable). The `dialect` and
|
|
@@ -133,6 +133,9 @@ ShellLanguage }, rules, configs: { recommended, 'agent-authored', 'bash-3.2', 'p
|
|
|
133
133
|
which follows wrappers. `ResolveArgv0Options` is that method's option bag (and
|
|
134
134
|
`ShellSourceCode.resolveInvokedCommand`'s): `transparentWrappers` replaces the wrapper table
|
|
135
135
|
followed.
|
|
136
|
+
- `WordFacts` — facts for each word in a site's `argvFacts` array, in the same order as `argv`.
|
|
137
|
+
Each entry includes the resolution, field-count bounds, expansion features, known prefix and
|
|
138
|
+
suffix, known basename, and whether the word is quoted or escaped.
|
|
136
139
|
- `WordResolution` — what `ShellSourceCode.resolveWord` and a site's `argv` words report: `{ static: true, text }`
|
|
137
140
|
when the word's value is known without running the script, or `{ static: false, reason }` when
|
|
138
141
|
it is not (`reason` is `'expansion'`, `'tilde'`, `'glob'`, `'brace'`, `'locale'` or
|
|
@@ -198,7 +201,7 @@ import { parseSync, printSync, walk } from 'sh-ast';
|
|
|
198
201
|
import { DEFAULT_TRANSPARENT_WRAPPERS, enumerateCommands } from 'sh-analysis';
|
|
199
202
|
```
|
|
200
203
|
|
|
201
|
-
The supported dependency ranges are `sh-ast@^0.
|
|
204
|
+
The supported dependency ranges are `sh-ast@^0.13.0` and `sh-analysis@^0.4.0`.
|
|
202
205
|
The plugin has no syntax forwarding subpaths. Raw `resolveWord` from `sh-analysis` accepts
|
|
203
206
|
faithful syntax records; `context.sourceCode.resolveWord` maps an ESLint Word view to its
|
|
204
207
|
original syntax and applies grammar-position defaults.
|
package/dist/api.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export type { ShellLanguage } from './language/shell-language.js';
|
|
|
9
9
|
export type { ShellSourceCode } from './language/shell-source-code.js';
|
|
10
10
|
export type { NormalizedShellLanguageOptions, ShellLanguageOptions, ShellLanguageOptionsJSON, } from './language/shell-language-options.js';
|
|
11
11
|
export type { Argv0Resolution } from 'sh-analysis';
|
|
12
|
-
export type { CommandContext, ResolveArgv0Options, WordResolution } from 'sh-analysis';
|
|
12
|
+
export type { CommandContext, ResolveArgv0Options, WordFacts, WordResolution } from 'sh-analysis';
|
|
13
13
|
export type { ShellCommandSite, ShellCommandInventory, ShellCommandUncertainty, } from './language/command-resolution.js';
|
|
14
14
|
export type { ShellComment, ShellFile, ShellNode, ShellNodeField, ShellNodeMap, ShellNodeOf, ShellNodeType, ShellPosition, } from './language/syntax-view.js';
|
|
15
15
|
export type { ShellDialect } from './language/shell-language-options.js';
|
|
@@ -48,7 +48,7 @@ export interface CommandHarvest {
|
|
|
48
48
|
readonly uncertainties: readonly ShellCommandUncertainty[];
|
|
49
49
|
}
|
|
50
50
|
/** The names introduced by one declaration, including zsh's multi-name form. @internal */
|
|
51
|
-
export declare function functionNames(node: ShellNodeOf<'FuncDecl'>): readonly string[];
|
|
51
|
+
export declare function functionNames(source: ShellSourceCode, node: ShellNodeOf<'FuncDecl'>): readonly string[];
|
|
52
52
|
/**
|
|
53
53
|
* The names of every function the file defines, anywhere in it (top level,
|
|
54
54
|
* nested in another function, inside a conditional, …). Position is not read,
|
|
@@ -60,14 +60,16 @@ export declare function functionNames(node: ShellNodeOf<'FuncDecl'>): readonly s
|
|
|
60
60
|
* @internal
|
|
61
61
|
*/
|
|
62
62
|
export declare function definedFunctionNames(sourceCode: ShellSourceCode): ReadonlySet<string>;
|
|
63
|
+
/** A variable directory can still select a function with the complete path as its name. @internal */
|
|
64
|
+
export declare function mightNamePathFunction(names: ReadonlySet<string>, site: ShellCommandSite, index: number): boolean;
|
|
63
65
|
/**
|
|
64
66
|
* Returns known represented external commands `sourceCode` runs, keyed by name, each with
|
|
65
67
|
* its first use. The map iterates in first-use (source) order.
|
|
66
68
|
*
|
|
67
|
-
* A command is every
|
|
69
|
+
* A command is every known name or basename in an invocation's wrapper chain (the
|
|
68
70
|
* wrappers and the effective command), excluding:
|
|
69
71
|
*
|
|
70
|
-
* -
|
|
72
|
+
* - words with neither known text nor a known basename;
|
|
71
73
|
* - builtins of the file's dialect (`sourceCode.dialect`);
|
|
72
74
|
* - functions the file defines anywhere, subject to `options.excludeFunction`.
|
|
73
75
|
*
|
|
@@ -27,15 +27,18 @@
|
|
|
27
27
|
* first-use report nodes. Opaque analysis boundaries travel with the harvest;
|
|
28
28
|
* known observations do not prove exhaustive dependencies.
|
|
29
29
|
*/
|
|
30
|
+
import { resolveFunctionName } from '../language/word-resolution.js';
|
|
31
|
+
import { commandBasename } from '../language/command-resolution.js';
|
|
30
32
|
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
31
33
|
import { walkShell } from '../language/syntax-view.js';
|
|
32
34
|
import { builtinsForDialect } from './builtins/index.js';
|
|
33
35
|
/** The names introduced by one declaration, including zsh's multi-name form. @internal */
|
|
34
|
-
export function functionNames(node) {
|
|
35
|
-
const names = node.names
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
export function functionNames(source, node) {
|
|
37
|
+
const names = node.name === undefined ? node.names : [node.name];
|
|
38
|
+
return names.flatMap((lit) => {
|
|
39
|
+
const name = resolveFunctionName(source, lit);
|
|
40
|
+
return name === null ? [] : [name];
|
|
41
|
+
});
|
|
39
42
|
}
|
|
40
43
|
/**
|
|
41
44
|
* The names of every function the file defines, anywhere in it (top level,
|
|
@@ -52,11 +55,25 @@ export function definedFunctionNames(sourceCode) {
|
|
|
52
55
|
walkShell(sourceCode.ast, (node) => {
|
|
53
56
|
if (node.type !== 'FuncDecl')
|
|
54
57
|
return;
|
|
55
|
-
for (const name of functionNames(node))
|
|
58
|
+
for (const name of functionNames(sourceCode, node))
|
|
56
59
|
names.add(name);
|
|
57
60
|
});
|
|
58
61
|
return names;
|
|
59
62
|
}
|
|
63
|
+
/** A variable directory can still select a function with the complete path as its name. @internal */
|
|
64
|
+
export function mightNamePathFunction(names, site, index) {
|
|
65
|
+
const facts = site.argvFacts.at(index);
|
|
66
|
+
if (facts === undefined || !facts.basename.known || facts.resolution.static)
|
|
67
|
+
return false;
|
|
68
|
+
const { knownPrefix, knownSuffix } = facts;
|
|
69
|
+
for (const name of names) {
|
|
70
|
+
if (name.length >= knownPrefix.length + knownSuffix.length &&
|
|
71
|
+
name.startsWith(knownPrefix) &&
|
|
72
|
+
name.endsWith(knownSuffix))
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
60
77
|
/**
|
|
61
78
|
* The `Word` node in `site`'s command at argv position `index`. argv and the
|
|
62
79
|
* `CallExpr`'s `args` list the same words in the same order, so a position
|
|
@@ -69,10 +86,10 @@ function wordNodeAt(site, index) {
|
|
|
69
86
|
* Returns known represented external commands `sourceCode` runs, keyed by name, each with
|
|
70
87
|
* its first use. The map iterates in first-use (source) order.
|
|
71
88
|
*
|
|
72
|
-
* A command is every
|
|
89
|
+
* A command is every known name or basename in an invocation's wrapper chain (the
|
|
73
90
|
* wrappers and the effective command), excluding:
|
|
74
91
|
*
|
|
75
|
-
* -
|
|
92
|
+
* - words with neither known text nor a known basename;
|
|
76
93
|
* - builtins of the file's dialect (`sourceCode.dialect`);
|
|
77
94
|
* - functions the file defines anywhere, subject to `options.excludeFunction`.
|
|
78
95
|
*
|
|
@@ -91,13 +108,18 @@ export function harvestCommands(sourceCode, options = {}) {
|
|
|
91
108
|
for (const site of sourceCode.getCommandInventory().commands) {
|
|
92
109
|
const { chain, chainIndices } = site.resolveInvokedCommand({ transparentWrappers });
|
|
93
110
|
for (const [position, word] of chain.entries()) {
|
|
94
|
-
|
|
111
|
+
const name = word.static ? word.text : commandBasename(site, word, chainIndices[position]);
|
|
112
|
+
if (name === null || harvested.has(name))
|
|
113
|
+
continue;
|
|
114
|
+
if (word.static && builtins.has(name))
|
|
95
115
|
continue;
|
|
96
|
-
|
|
97
|
-
|
|
116
|
+
if (position === 0 &&
|
|
117
|
+
options.excludeFunction === undefined &&
|
|
118
|
+
mightNamePathFunction(functions, site, chainIndices[position]))
|
|
98
119
|
continue;
|
|
99
|
-
if (functions.has(name) && (options.excludeFunction?.(name, site) ?? true))
|
|
120
|
+
if (word.static && functions.has(name) && (options.excludeFunction?.(name, site) ?? true)) {
|
|
100
121
|
continue;
|
|
122
|
+
}
|
|
101
123
|
harvested.set(name, { name, node: wordNodeAt(site, chainIndices[position]), site });
|
|
102
124
|
}
|
|
103
125
|
}
|
|
@@ -27,6 +27,7 @@ import type { SourceLocation } from '@eslint/core';
|
|
|
27
27
|
import type { SyntaxPos } from 'sh-ast';
|
|
28
28
|
import { TextSourceCodeBase } from '@eslint/plugin-kit';
|
|
29
29
|
import type { TraversalStep } from '@eslint/core';
|
|
30
|
+
import { WordFacts } from 'sh-analysis';
|
|
30
31
|
import { WordResolution } from 'sh-analysis';
|
|
31
32
|
import { WrapperSpec } from 'sh-analysis';
|
|
32
33
|
|
|
@@ -520,7 +521,8 @@ export declare interface ShellCommandSite {
|
|
|
520
521
|
* The path from the file root to this command, outermost frame first,
|
|
521
522
|
* exactly as `sh-analysis` reports it. For example, `date` in
|
|
522
523
|
* `f() { echo "$(date)"; }` has
|
|
523
|
-
* `[{ kind: 'function',
|
|
524
|
+
* `[{ kind: 'function', names: ['f'] }, { kind: 'cmdSubst' }]`.
|
|
525
|
+
* Function frames list names in declaration order. Anonymous functions have an empty list.
|
|
524
526
|
*/
|
|
525
527
|
readonly context: readonly CommandContext[];
|
|
526
528
|
/** The command word (`argv[0]`) resolved with `{ context: 'command-argument' }`. */
|
|
@@ -531,6 +533,8 @@ export declare interface ShellCommandSite {
|
|
|
531
533
|
* assignments are not part of argv.
|
|
532
534
|
*/
|
|
533
535
|
readonly argv: readonly WordResolution[];
|
|
536
|
+
/** Word facts for the same original argv positions, using the parsed dialect. */
|
|
537
|
+
readonly argvFacts: readonly WordFacts[];
|
|
534
538
|
/**
|
|
535
539
|
* Follows this command's argv0 through transparent wrappers (`env`, `sudo`,
|
|
536
540
|
* `nohup`, `exec`, …) and returns `sh-analysis`'s `resolveArgv0`
|
|
@@ -538,7 +542,7 @@ export declare interface ShellCommandSite {
|
|
|
538
542
|
* `ShellSourceCode.resolveInvokedCommand(node, options)`; both read the
|
|
539
543
|
* same per-file index.
|
|
540
544
|
*
|
|
541
|
-
* @param options -
|
|
545
|
+
* @param options - Wrapper and interpreter settings. Resolution uses the parsed dialect.
|
|
542
546
|
* `transparentWrappers` replaces `sh-analysis`'s
|
|
543
547
|
* `DEFAULT_TRANSPARENT_WRAPPERS`; spread the defaults into the array to
|
|
544
548
|
* extend them instead.
|
|
@@ -1047,10 +1051,15 @@ export declare interface ShellRuleVisitor extends ShellTypedListeners {
|
|
|
1047
1051
|
* `sh-analysis` owns static-word interpretation and dynamic reasons; this
|
|
1048
1052
|
* method supplies the lint tree's position-based policy, without executing shell.
|
|
1049
1053
|
*
|
|
1050
|
-
* Without explicit options, `CallExpr.args` use command-argument context
|
|
1051
|
-
* `
|
|
1052
|
-
*
|
|
1053
|
-
*
|
|
1054
|
+
* Without explicit options, `CallExpr.args` use command-argument context.
|
|
1055
|
+
* An ordinary argument such as `a:~/bin` is literal in that context, while
|
|
1056
|
+
* `PATH=a:~/bin` is dynamic because Bash can expand the tilde in an
|
|
1057
|
+
* assignment-shaped argument. Other positions use the analyzer's
|
|
1058
|
+
* conservative default, which also considers assignment-value expansion.
|
|
1059
|
+
* Explicit options override the context defaults. Resolution uses the parsed dialect.
|
|
1060
|
+
* Since sh-analysis 0.3, a top-level escaped literal can return `unsupported`
|
|
1061
|
+
* when the analyzer receives no context. For the assignment value in
|
|
1062
|
+
* `label=a\ b`, pass `{ context: 'assignment-value' }` to resolve `a b`.
|
|
1054
1063
|
*
|
|
1055
1064
|
* @param word - A parsed Word from the lint view.
|
|
1056
1065
|
* @param options - Explicit analysis policy overriding grammar defaults.
|
|
@@ -1099,10 +1108,11 @@ export declare interface ShellRuleVisitor extends ShellTypedListeners {
|
|
|
1099
1108
|
* assignment-only statement such as `FOO=1`).
|
|
1100
1109
|
*
|
|
1101
1110
|
* Delegates to `resolveArgv0` with the `CommandSite` that `enumerateCommands`
|
|
1102
|
-
* reports for this `CallExpr` in this file
|
|
1103
|
-
*
|
|
1104
|
-
*
|
|
1105
|
-
* `
|
|
1111
|
+
* reports for this `CallExpr` in this file. A wrapper that accepts assignments
|
|
1112
|
+
* can skip a fixed scalar assignment prefix when the operand is known to
|
|
1113
|
+
* occupy one field, even if the value is dynamic. For example,
|
|
1114
|
+
* `env PATH=a:~/bin printf hi` resolves to `printf`. Read the effective
|
|
1115
|
+
* command's name as `res.effective.static ? res.effective.text : null`.
|
|
1106
1116
|
* `sudo -u x "$prog"` and `exec "$@"` have a non-static `effective` with
|
|
1107
1117
|
* reason `expansion`; `env -S '…'` and `bash -c '…'` have reason
|
|
1108
1118
|
* `embedded-command`; `chain`
|
|
@@ -1125,7 +1135,7 @@ export declare interface ShellRuleVisitor extends ShellTypedListeners {
|
|
|
1125
1135
|
* index would silently omit real commands.
|
|
1126
1136
|
*
|
|
1127
1137
|
* @param callExpr - A parsed `CallExpr` node from this file's lint tree.
|
|
1128
|
-
* @param options -
|
|
1138
|
+
* @param options - Wrapper and interpreter settings. Resolution uses the parsed dialect.
|
|
1129
1139
|
* `transparentWrappers` replaces `sh-analysis`'s
|
|
1130
1140
|
* `DEFAULT_TRANSPARENT_WRAPPERS`; spread the defaults into the array to
|
|
1131
1141
|
* extend them instead.
|
|
@@ -1319,6 +1329,8 @@ export declare interface ShellRuleVisitor extends ShellTypedListeners {
|
|
|
1319
1329
|
line: number;
|
|
1320
1330
|
column: number;
|
|
1321
1331
|
};
|
|
1332
|
+
/** Returns inventory facts for parsed command arguments. @internal */
|
|
1333
|
+
getCommandArgumentFacts(callExpr: ShellNodeOf<'CallExpr'>): readonly WordFacts[] | undefined;
|
|
1322
1334
|
/**
|
|
1323
1335
|
* Returns the parent of `node`, or `undefined` for the root `File` node
|
|
1324
1336
|
* (or any node visited before `traverse()` has run once). ESLint always
|
|
@@ -1409,6 +1421,8 @@ export declare interface TransparentWrappersBindingContext {
|
|
|
1409
1421
|
readonly sourceCode: ShellSourceCode;
|
|
1410
1422
|
}
|
|
1411
1423
|
|
|
1424
|
+
export { WordFacts }
|
|
1425
|
+
|
|
1412
1426
|
export { WordResolution }
|
|
1413
1427
|
|
|
1414
1428
|
/**
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import type { Argv0Resolution, CommandUncertainty, CommandContext, CommandSite, ResolveArgv0Options, WordResolution } from 'sh-analysis';
|
|
1
|
+
import type { Argv0ChainWord, Argv0Resolution, CommandUncertainty, CommandContext, CommandSite, ResolveArgv0Options, WordFacts, WordResolution } from 'sh-analysis';
|
|
2
2
|
import type { CallExpr } from 'sh-ast';
|
|
3
|
+
import type { ShellDialect } from './shell-language-options.js';
|
|
3
4
|
import type { ShellFile, ShellNode, ShellNodeOf } from './syntax-view.js';
|
|
5
|
+
/** Inventory requested with the source dialect always includes facts. @internal */
|
|
6
|
+
export type DialectCommandSite = CommandSite & Required<Pick<CommandSite, 'dialect' | 'argvFacts'>>;
|
|
4
7
|
/**
|
|
5
8
|
* Every command site in one parsed file, from a single `enumerateCommands`
|
|
6
9
|
* call. `sites` keeps analysis's own list and order (source order); `byNode`
|
|
@@ -10,10 +13,10 @@ import type { ShellFile, ShellNode, ShellNodeOf } from './syntax-view.js';
|
|
|
10
13
|
* @internal
|
|
11
14
|
*/
|
|
12
15
|
export interface CommandSiteIndex {
|
|
13
|
-
readonly sites: readonly
|
|
16
|
+
readonly sites: readonly DialectCommandSite[];
|
|
14
17
|
/** Opaque execution boundaries retained alongside represented commands. */
|
|
15
18
|
readonly uncertainties: readonly CommandUncertainty[];
|
|
16
|
-
readonly byNode: ReadonlyMap<CallExpr,
|
|
19
|
+
readonly byNode: ReadonlyMap<CallExpr, DialectCommandSite>;
|
|
17
20
|
}
|
|
18
21
|
/**
|
|
19
22
|
* One command invocation in a linted shell file, in lint-view terms: an
|
|
@@ -48,7 +51,8 @@ export interface ShellCommandSite {
|
|
|
48
51
|
* The path from the file root to this command, outermost frame first,
|
|
49
52
|
* exactly as `sh-analysis` reports it. For example, `date` in
|
|
50
53
|
* `f() { echo "$(date)"; }` has
|
|
51
|
-
* `[{ kind: 'function',
|
|
54
|
+
* `[{ kind: 'function', names: ['f'] }, { kind: 'cmdSubst' }]`.
|
|
55
|
+
* Function frames list names in declaration order. Anonymous functions have an empty list.
|
|
52
56
|
*/
|
|
53
57
|
readonly context: readonly CommandContext[];
|
|
54
58
|
/** The command word (`argv[0]`) resolved with `{ context: 'command-argument' }`. */
|
|
@@ -59,6 +63,8 @@ export interface ShellCommandSite {
|
|
|
59
63
|
* assignments are not part of argv.
|
|
60
64
|
*/
|
|
61
65
|
readonly argv: readonly WordResolution[];
|
|
66
|
+
/** Word facts for the same original argv positions, using the parsed dialect. */
|
|
67
|
+
readonly argvFacts: readonly WordFacts[];
|
|
62
68
|
/**
|
|
63
69
|
* Follows this command's argv0 through transparent wrappers (`env`, `sudo`,
|
|
64
70
|
* `nohup`, `exec`, …) and returns `sh-analysis`'s `resolveArgv0`
|
|
@@ -66,7 +72,7 @@ export interface ShellCommandSite {
|
|
|
66
72
|
* `ShellSourceCode.resolveInvokedCommand(node, options)`; both read the
|
|
67
73
|
* same per-file index.
|
|
68
74
|
*
|
|
69
|
-
* @param options -
|
|
75
|
+
* @param options - Wrapper and interpreter settings. Resolution uses the parsed dialect.
|
|
70
76
|
* `transparentWrappers` replaces `sh-analysis`'s
|
|
71
77
|
* `DEFAULT_TRANSPARENT_WRAPPERS`; spread the defaults into the array to
|
|
72
78
|
* extend them instead.
|
|
@@ -78,6 +84,8 @@ export interface ShellCommandSite {
|
|
|
78
84
|
*/
|
|
79
85
|
resolveInvokedCommand(options?: ResolveArgv0Options): Argv0Resolution;
|
|
80
86
|
}
|
|
87
|
+
/** The known basename of a real chain word at its original argv position. @internal */
|
|
88
|
+
export declare function commandBasename(site: ShellCommandSite, word: Argv0ChainWord, index: number): string | null;
|
|
81
89
|
/**
|
|
82
90
|
* An opaque executable construct projected onto the original ESLint view.
|
|
83
91
|
* Its presence limits inventory completeness; it does not assert a hidden command.
|
|
@@ -124,7 +132,9 @@ export interface ShellCommandInventory {
|
|
|
124
132
|
* partial index would silently omit real commands, so callers fail closed.
|
|
125
133
|
* @internal
|
|
126
134
|
*/
|
|
127
|
-
export declare function buildCommandSiteIndex(file: ShellFile): CommandSiteIndex;
|
|
135
|
+
export declare function buildCommandSiteIndex(file: ShellFile, dialect: ShellDialect): CommandSiteIndex;
|
|
136
|
+
/** Resolves wrappers using the same dialect and facts as command enumeration. @internal */
|
|
137
|
+
export declare function resolveSiteArgv0(site: DialectCommandSite, options?: ResolveArgv0Options): Argv0Resolution;
|
|
128
138
|
/**
|
|
129
139
|
* Re-expresses every site in `index` in lint-view terms, in analysis's order.
|
|
130
140
|
* Each `node` comes from the syntax → view association recorded when `file`
|
|
@@ -9,7 +9,16 @@
|
|
|
9
9
|
* propagate unchanged instead of becoming partial inventories or parser errors.
|
|
10
10
|
*/
|
|
11
11
|
import { enumerateCommands, resolveArgv0 } from 'sh-analysis';
|
|
12
|
-
import { syntaxForNode, viewForSyntax
|
|
12
|
+
import { syntaxForNode, viewForSyntax } from './syntax-view.js';
|
|
13
|
+
import { analysisDialect, withLfWordFacts } from './word-resolution.js';
|
|
14
|
+
/** The known basename of a real chain word at its original argv position. @internal */
|
|
15
|
+
export function commandBasename(site, word, index) {
|
|
16
|
+
const facts = site.argvFacts.at(index);
|
|
17
|
+
// An unresolved wrapper flag or embedded program is not an observed command word.
|
|
18
|
+
if (word !== facts?.resolution || !facts.basename.known)
|
|
19
|
+
return null;
|
|
20
|
+
return facts.basename.text;
|
|
21
|
+
}
|
|
13
22
|
/**
|
|
14
23
|
* Builds the {@link CommandSiteIndex} for a whole parsed file with a single
|
|
15
24
|
* `enumerateCommands` call over the file's original syntax. Each site's
|
|
@@ -32,20 +41,17 @@ import { syntaxForNode, viewForSyntax, withoutCrlfCarriageReturn } from './synta
|
|
|
32
41
|
* partial index would silently omit real commands, so callers fail closed.
|
|
33
42
|
* @internal
|
|
34
43
|
*/
|
|
35
|
-
export function buildCommandSiteIndex(file) {
|
|
44
|
+
export function buildCommandSiteIndex(file, dialect) {
|
|
36
45
|
const syntax = syntaxForNode(file);
|
|
37
46
|
if (syntax?.Type !== 'File') {
|
|
38
47
|
throw Object.assign(new TypeError('expected a parsed shell File from this language adapter'), {
|
|
39
48
|
code: 'ESLINT_SH_FILE_NODE',
|
|
40
49
|
});
|
|
41
50
|
}
|
|
42
|
-
const inventory = enumerateCommands(syntax);
|
|
51
|
+
const inventory = enumerateCommands(syntax, { dialect: analysisDialect(dialect) });
|
|
43
52
|
const sites = Object.freeze(inventory.commands.map((site) => freezeCommandSite(withLfArgv(site))));
|
|
44
53
|
const uncertainties = Object.freeze(inventory.uncertainties.map((uncertainty) => {
|
|
45
|
-
|
|
46
|
-
Object.freeze(frame);
|
|
47
|
-
}
|
|
48
|
-
Object.freeze(uncertainty.context);
|
|
54
|
+
freezeCommandContext(uncertainty.context);
|
|
49
55
|
return Object.freeze(uncertainty);
|
|
50
56
|
}));
|
|
51
57
|
return { sites, uncertainties, byNode: new Map(sites.map((site) => [site.node, site])) };
|
|
@@ -58,11 +64,18 @@ export function buildCommandSiteIndex(file) {
|
|
|
58
64
|
* chains from `site.argv` and `getCommandName` reads `site.argv0`.
|
|
59
65
|
*/
|
|
60
66
|
function withLfArgv(site) {
|
|
67
|
+
const { dialect, argvFacts: observed } = site;
|
|
68
|
+
if (dialect === undefined || observed === undefined) {
|
|
69
|
+
throw new Error('dialect-aware command inventory must include word facts');
|
|
70
|
+
}
|
|
61
71
|
const words = site.node.Args ?? [];
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
72
|
+
const argvFacts = observed.map((facts, index) => withLfWordFacts(words[index] ?? null, facts));
|
|
73
|
+
const argv = argvFacts.map(({ resolution }) => resolution);
|
|
74
|
+
return { ...site, dialect, argvFacts, argv0: argv[0], argv };
|
|
75
|
+
}
|
|
76
|
+
/** Resolves wrappers using the same dialect and facts as command enumeration. @internal */
|
|
77
|
+
export function resolveSiteArgv0(site, options) {
|
|
78
|
+
return resolveArgv0(site, { ...options, dialect: site.dialect });
|
|
66
79
|
}
|
|
67
80
|
/**
|
|
68
81
|
* Freezes the data a rule can reach through `site`: the site, its `context`
|
|
@@ -73,19 +86,32 @@ function withLfArgv(site) {
|
|
|
73
86
|
* Freezing in place, instead of copying, keeps every value identical to what
|
|
74
87
|
* analysis returned. That is safe because `enumerateCommands` builds these
|
|
75
88
|
* objects fresh on each call, and nothing in `sh-analysis` writes to them
|
|
76
|
-
* after it returns: `resolveArgv0` only reads a site.
|
|
77
|
-
* primitives or source text, so freezing each frame shallowly is complete.
|
|
89
|
+
* after it returns: `resolveArgv0` only reads a site.
|
|
78
90
|
*/
|
|
79
91
|
function freezeCommandSite(site) {
|
|
80
|
-
|
|
81
|
-
Object.freeze(frame);
|
|
82
|
-
Object.freeze(site.context);
|
|
92
|
+
freezeCommandContext(site.context);
|
|
83
93
|
for (const word of site.argv)
|
|
84
94
|
Object.freeze(word);
|
|
95
|
+
for (const facts of site.argvFacts) {
|
|
96
|
+
Object.freeze(facts.features);
|
|
97
|
+
Object.freeze(facts.fields);
|
|
98
|
+
Object.freeze(facts.basename);
|
|
99
|
+
Object.freeze(facts);
|
|
100
|
+
}
|
|
101
|
+
Object.freeze(site.argvFacts);
|
|
85
102
|
Object.freeze(site.argv);
|
|
86
103
|
Object.freeze(site.argv0);
|
|
87
104
|
return Object.freeze(site);
|
|
88
105
|
}
|
|
106
|
+
/** Freezes the shared context frames and their function name lists. */
|
|
107
|
+
function freezeCommandContext(context) {
|
|
108
|
+
for (const frame of context) {
|
|
109
|
+
if (frame.kind === 'function')
|
|
110
|
+
Object.freeze(frame.names);
|
|
111
|
+
Object.freeze(frame);
|
|
112
|
+
}
|
|
113
|
+
Object.freeze(context);
|
|
114
|
+
}
|
|
89
115
|
/**
|
|
90
116
|
* Re-expresses every site in `index` in lint-view terms, in analysis's order.
|
|
91
117
|
* Each `node` comes from the syntax → view association recorded when `file`
|
|
@@ -112,7 +138,8 @@ export function viewCommandSites(file, index) {
|
|
|
112
138
|
context: site.context,
|
|
113
139
|
argv0: site.argv0,
|
|
114
140
|
argv: site.argv,
|
|
115
|
-
|
|
141
|
+
argvFacts: site.argvFacts,
|
|
142
|
+
resolveInvokedCommand: (options) => resolveSiteArgv0(site, options),
|
|
116
143
|
});
|
|
117
144
|
}));
|
|
118
145
|
}
|
|
@@ -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.
|
|
@@ -398,6 +404,8 @@ export declare class ShellSourceCode extends TextSourceCodeBase<{
|
|
|
398
404
|
line: number;
|
|
399
405
|
column: number;
|
|
400
406
|
};
|
|
407
|
+
/** Returns inventory facts for parsed command arguments. @internal */
|
|
408
|
+
getCommandArgumentFacts(callExpr: ShellNodeOf<'CallExpr'>): readonly WordFacts[] | undefined;
|
|
401
409
|
/**
|
|
402
410
|
* Returns the parent of `node`, or `undefined` for the root `File` node
|
|
403
411
|
* (or any node visited before `traverse()` has run once). ESLint always
|
|
@@ -11,9 +11,8 @@ import { isShellComment, projectedComments, projectedCoordinates, someShellNode,
|
|
|
11
11
|
import { cloneSyntax } from './syntax-clone.js';
|
|
12
12
|
import { SourceCoordinates } from './source-coordinates.js';
|
|
13
13
|
import { DashHdoc, Hdoc, SYNTAX_INTERFACES, printSync } from 'sh-ast';
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import { resolveWord } from './word-resolution.js';
|
|
14
|
+
import { buildCommandSiteIndex, rawCallExprFor, resolveSiteArgv0, viewCommandInventory, } from './command-resolution.js';
|
|
15
|
+
import { resolvePublicWord } from './word-resolution.js';
|
|
17
16
|
/**
|
|
18
17
|
* Subtree edits must cover the original heredoc extents. The printer emits a
|
|
19
18
|
* heredoc's body and delimiter even when the parser ends the target at its
|
|
@@ -522,10 +521,15 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
522
521
|
* `sh-analysis` owns static-word interpretation and dynamic reasons; this
|
|
523
522
|
* method supplies the lint tree's position-based policy, without executing shell.
|
|
524
523
|
*
|
|
525
|
-
* Without explicit options, `CallExpr.args` use command-argument context
|
|
526
|
-
* `
|
|
527
|
-
*
|
|
528
|
-
*
|
|
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`.
|
|
529
533
|
*
|
|
530
534
|
* @param word - A parsed Word from the lint view.
|
|
531
535
|
* @param options - Explicit analysis policy overriding grammar defaults.
|
|
@@ -534,7 +538,7 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
534
538
|
* @public
|
|
535
539
|
*/
|
|
536
540
|
resolveWord(word, options) {
|
|
537
|
-
return
|
|
541
|
+
return resolvePublicWord(this, word, options);
|
|
538
542
|
}
|
|
539
543
|
/**
|
|
540
544
|
* The text-only form of {@link ShellSourceCode.resolveWord}: returns its text
|
|
@@ -551,16 +555,6 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
551
555
|
const resolution = this.resolveWord(word, options);
|
|
552
556
|
return resolution.static ? resolution.text : null;
|
|
553
557
|
}
|
|
554
|
-
/**
|
|
555
|
-
* The `resolveWord` options implied by `word`'s grammar position when the
|
|
556
|
-
* caller supplies none: command-argument context for a `CallExpr` argument,
|
|
557
|
-
* otherwise `resolveWord`'s own conservative default.
|
|
558
|
-
*/
|
|
559
|
-
#defaultWordOptions(word) {
|
|
560
|
-
const parent = this.getParent(word);
|
|
561
|
-
const isCommandArgument = parent?.type === 'CallExpr' && Array.isArray(parent.args) && parent.args.includes(word);
|
|
562
|
-
return isCommandArgument ? { context: 'command-argument' } : undefined;
|
|
563
|
-
}
|
|
564
558
|
/**
|
|
565
559
|
* Returns the static text of `callExpr`'s command word (`argv[0]`) as
|
|
566
560
|
* written, or `null` when that word is dynamic or absent. Transparent
|
|
@@ -592,10 +586,11 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
592
586
|
* assignment-only statement such as `FOO=1`).
|
|
593
587
|
*
|
|
594
588
|
* Delegates to `resolveArgv0` with the `CommandSite` that `enumerateCommands`
|
|
595
|
-
* reports for this `CallExpr` in this file
|
|
596
|
-
*
|
|
597
|
-
*
|
|
598
|
-
* `
|
|
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`.
|
|
599
594
|
* `sudo -u x "$prog"` and `exec "$@"` have a non-static `effective` with
|
|
600
595
|
* reason `expansion`; `env -S '…'` and `bash -c '…'` have reason
|
|
601
596
|
* `embedded-command`; `chain`
|
|
@@ -618,7 +613,7 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
618
613
|
* index would silently omit real commands.
|
|
619
614
|
*
|
|
620
615
|
* @param callExpr - A parsed `CallExpr` node from this file's lint tree.
|
|
621
|
-
* @param options -
|
|
616
|
+
* @param options - Wrapper and interpreter settings. Resolution uses the parsed dialect.
|
|
622
617
|
* `transparentWrappers` replaces `sh-analysis`'s
|
|
623
618
|
* `DEFAULT_TRANSPARENT_WRAPPERS`; spread the defaults into the array to
|
|
624
619
|
* extend them instead.
|
|
@@ -631,7 +626,7 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
631
626
|
*/
|
|
632
627
|
resolveInvokedCommand(callExpr, options) {
|
|
633
628
|
const site = this.#commandSiteFor(callExpr);
|
|
634
|
-
return site ?
|
|
629
|
+
return site ? resolveSiteArgv0(site, options) : null;
|
|
635
630
|
}
|
|
636
631
|
/**
|
|
637
632
|
* Returns represented command sites and explicit opaque execution boundaries.
|
|
@@ -920,7 +915,7 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
920
915
|
}
|
|
921
916
|
/** This file's command-site index, built by one `enumerateCommands` call on first use. */
|
|
922
917
|
#getCommandSiteIndex() {
|
|
923
|
-
this.#commandSites ??= buildCommandSiteIndex(this.ast);
|
|
918
|
+
this.#commandSites ??= buildCommandSiteIndex(this.ast, this.dialect);
|
|
924
919
|
return this.#commandSites;
|
|
925
920
|
}
|
|
926
921
|
/**
|
|
@@ -932,6 +927,10 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
932
927
|
const raw = rawCallExprFor(callExpr);
|
|
933
928
|
return this.#getCommandSiteIndex().byNode.get(raw);
|
|
934
929
|
}
|
|
930
|
+
/** Returns inventory facts for parsed command arguments. @internal */
|
|
931
|
+
getCommandArgumentFacts(callExpr) {
|
|
932
|
+
return this.#commandSiteFor(callExpr)?.argvFacts;
|
|
933
|
+
}
|
|
935
934
|
/**
|
|
936
935
|
* Returns the parent of `node`, or `undefined` for the root `File` node
|
|
937
936
|
* (or any node visited before `traverse()` has run once). ESLint always
|