@eslint-sh/eslint-plugin 0.9.0 → 0.10.1
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 +23 -29
- package/dist/api.d.ts +4 -4
- package/dist/command-harvest/builtins/bats.d.ts +1 -1
- package/dist/command-harvest/harvest-commands.d.ts +18 -7
- package/dist/command-harvest/harvest-commands.js +11 -10
- package/dist/command-harvest/wrapper-table.d.ts +1 -1
- package/dist/command-harvest/wrapper-table.js +1 -1
- package/dist/eslint-plugin-api.d.ts +71 -60
- package/dist/language/command-resolution.d.ts +44 -10
- package/dist/language/command-resolution.js +39 -13
- package/dist/language/shell-source-code.d.ts +31 -47
- package/dist/language/shell-source-code.js +47 -62
- package/dist/language/syntax-clone.d.ts +29 -0
- package/dist/language/syntax-clone.js +76 -0
- package/dist/language/syntax-view.d.ts +23 -2
- package/dist/language/syntax-view.js +166 -33
- package/dist/language/word-resolution.d.ts +2 -2
- package/dist/language/word-resolution.js +2 -2
- package/dist/rules/compat/declaration-options.js +2 -2
- package/dist/rules/compat/no-brace-sequence-padding-step.js +1 -1
- package/dist/rules/compat/no-mapfile.js +2 -2
- package/dist/rules/compat/no-negative-array-index.js +2 -2
- package/dist/rules/compat/no-printf-time-format.js +2 -2
- package/dist/rules/compat/no-test-v.js +5 -3
- package/dist/rules/compat/no-wait-n.js +2 -2
- package/dist/rules/compat/nounset-array-proof.js +16 -9
- package/dist/rules/compat/posix-absent-command.js +2 -2
- package/dist/rules/compat/shopt-options.js +2 -2
- package/dist/rules/create-wrapper-rule.d.ts +3 -3
- package/dist/rules/create-wrapper-rule.js +8 -4
- package/dist/rules/no-dynamic-invocation.d.ts +1 -1
- package/dist/rules/no-dynamic-invocation.js +12 -4
- package/dist/rules/no-eval.js +6 -2
- package/dist/rules/no-masked-exit-status.d.ts +12 -0
- package/dist/rules/no-masked-exit-status.js +27 -17
- package/dist/rules/no-secret-env-reads.js +1 -1
- package/dist/rules/no-unchecked-cd.js +28 -21
- package/dist/rules/no-undeclared-commands.js +2 -1
- package/dist/rules/no-unused-declarations.js +7 -2
- package/dist/rules/prefer-local.js +10 -8
- package/dist/rules/quote-expansions.js +1 -1
- package/dist/rules/require-network-wrapper.d.ts +1 -1
- package/dist/rules/require-network-wrapper.js +10 -4
- package/dist/rules/require-tty-guard.js +1 -1
- package/dist/rules/resolve-network-wrapper.d.ts +2 -2
- package/dist/rules/resolve-network-wrapper.js +6 -6
- package/dist/rules/resolve-transparent-wrappers.d.ts +1 -1
- package/dist/rules/resolve-transparent-wrappers.js +2 -2
- package/dist/rules/transparent-wrappers.d.ts +3 -3
- package/dist/rules/transparent-wrappers.js +1 -1
- package/docs/custom-rule-tutorial.md +35 -35
- package/docs/error-codes.md +6 -5
- package/docs/examples/no-backticks-rule.mjs +1 -1
- package/docs/examples/no-curl-in-functions-rule.mjs +2 -2
- package/docs/examples/no-listed-commands-rule.ts +1 -1
- package/docs/examples/no-rm-rf-rule.mjs +1 -1
- package/docs/rules/no-dynamic-invocation.md +5 -0
- package/docs/rules/no-undeclared-commands.md +4 -0
- package/docs/rules/no-unused-declarations.md +4 -0
- package/docs/rules/require-network-wrapper.md +1 -1
- package/docs/selectors.md +1 -1
- package/docs/wrapper-rules.md +1 -1
- package/package.json +12 -23
- package/dist/eslint-plugin-syntax-analyze.d.ts +0 -23
- package/dist/eslint-plugin-syntax.d.ts +0 -30
- package/dist/syntax/analyze.d.ts +0 -19
- package/dist/syntax/analyze.js +0 -21
- package/dist/syntax/index.d.ts +0 -26
- package/dist/syntax/index.js +0 -28
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* `sh/require-network-wrapper` is the built-in instance whose wrapper comes
|
|
10
10
|
* from the user's rule options.
|
|
11
11
|
*
|
|
12
|
-
* "Run through" is decided from `sh-
|
|
12
|
+
* "Run through" is decided from `sh-analysis`'s transparent-wrapper
|
|
13
13
|
* resolution, never by reading words locally: each command site's argv0 is
|
|
14
14
|
* followed through the default wrapper table (`sudo`, `env`, `nohup`, …), the
|
|
15
15
|
* bound wrapper, and any command with modeled subcommands, and the resulting
|
|
@@ -35,9 +35,13 @@
|
|
|
35
35
|
* listed command does not report, and neither does a subcommand that is not
|
|
36
36
|
* static. Commands inside command strings (`bash -c '…'`) are not commands to
|
|
37
37
|
* this analysis and are out of scope.
|
|
38
|
+
*
|
|
39
|
+
* This policy checks represented, statically identified commands. Opaque executable
|
|
40
|
+
* syntax is outside that named-command scope; no-dynamic-invocation reports those
|
|
41
|
+
* inventory limits when a project requires execution completeness.
|
|
38
42
|
*/
|
|
39
43
|
import { configurationMessage } from '../configuration-message.js';
|
|
40
|
-
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-
|
|
44
|
+
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
41
45
|
import { resolveTransparentWrappers } from './resolve-transparent-wrappers.js';
|
|
42
46
|
import { definedFunctionNames } from '../command-harvest/harvest-commands.js';
|
|
43
47
|
import { createRule, packageDocsUrl } from './create-rule.js';
|
|
@@ -222,7 +226,7 @@ function wrapperRuleListeners(compiled, context) {
|
|
|
222
226
|
return {
|
|
223
227
|
File() {
|
|
224
228
|
let functions;
|
|
225
|
-
for (const site of sourceCode.
|
|
229
|
+
for (const site of sourceCode.getCommandInventory().commands) {
|
|
226
230
|
const resolution = resolveChain(compiled, site);
|
|
227
231
|
const finding = findListedCommand(compiled, resolution.chain);
|
|
228
232
|
if (finding === null || finding.wrapped)
|
|
@@ -393,7 +397,7 @@ export function buildWrapperRule(config) {
|
|
|
393
397
|
* naming the command and the wrapper. Its `meta.docs.url` and
|
|
394
398
|
* `meta.docs.description` default to this package's wrapper-rules page and a
|
|
395
399
|
* sentence naming the policy; `config.docs` overrides either. A listed
|
|
396
|
-
* command is wrapped when the wrapper encloses it in `sh-
|
|
400
|
+
* command is wrapped when the wrapper encloses it in `sh-analysis`'s
|
|
397
401
|
* transparent-wrapper chain, so `audit-run sudo kubectl apply` is wrapped.
|
|
398
402
|
* Command and wrapper words written as paths match by basename
|
|
399
403
|
* (`/usr/bin/kubectl`). Commands inside `$(…)`, functions, and pipelines are
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
*
|
|
12
12
|
* The rule asks, never decides:
|
|
13
13
|
*
|
|
14
|
-
* -
|
|
14
|
+
* - Represented commands come from `ShellSourceCode.getCommandInventory().commands`,
|
|
15
15
|
* so commands nested in command substitutions, process substitutions,
|
|
16
16
|
* pipelines and function bodies are all checked, each once.
|
|
17
17
|
* - Which command each one runs comes from `resolveInvokedCommand`, which
|
|
@@ -20,7 +20,9 @@
|
|
|
20
20
|
* reports. `'embedded-command'` reports as `embeddedCommand`; every other
|
|
21
21
|
* reason (an expansion, a glob, a tilde, an unrecognized wrapper flag, …)
|
|
22
22
|
* reports as `dynamicInvocation`.
|
|
23
|
-
* -
|
|
23
|
+
* - Opaque executable syntax reports independently at the original construct.
|
|
24
|
+
* Dispatcher exemptions permit computed argv words, never embedded opaque code.
|
|
25
|
+
* - The wrappers followed are `sh-analysis`'s defaults, Bash's `builtin`,
|
|
24
26
|
* shells invoked as commands (so `bash -c` is an embedded command and
|
|
25
27
|
* `bash script.sh` runs `script.sh`), and the shared project wrapper facts.
|
|
26
28
|
*
|
|
@@ -30,7 +32,7 @@
|
|
|
30
32
|
* command word. The rule offers no fix and no suggestion; choosing the
|
|
31
33
|
* command is a human decision.
|
|
32
34
|
*/
|
|
33
|
-
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-
|
|
35
|
+
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
34
36
|
import { resolveTransparentWrappers } from './resolve-transparent-wrappers.js';
|
|
35
37
|
import { createRule } from './create-rule.js';
|
|
36
38
|
import { BUILTIN_WRAPPER, SHELL_WRAPPER } from './transparent-wrappers.js';
|
|
@@ -80,6 +82,7 @@ export const noDynamicInvocation = createRule({
|
|
|
80
82
|
],
|
|
81
83
|
defaultOptions: [{ allowInFunctions: [] }],
|
|
82
84
|
messages: {
|
|
85
|
+
opaqueExecutable: 'This syntax contains opaque executable text, so its commands cannot be statically reviewed. Write the commands directly instead.',
|
|
83
86
|
dynamicInvocation: 'The command this runs is not known until run time, so the script cannot be statically reviewed. Name the command with a literal word instead.',
|
|
84
87
|
embeddedCommand: 'This runs shell code embedded in a string, which cannot be statically reviewed. Write the commands directly instead.',
|
|
85
88
|
},
|
|
@@ -92,7 +95,12 @@ export const noDynamicInvocation = createRule({
|
|
|
92
95
|
const transparentWrappers = resolveTransparentWrappers(context, BUILT_IN_WRAPPERS);
|
|
93
96
|
return {
|
|
94
97
|
File() {
|
|
95
|
-
|
|
98
|
+
const inventory = sourceCode.getCommandInventory();
|
|
99
|
+
// Dispatcher exemptions cover computed argv words, never opaque embedded code.
|
|
100
|
+
for (const uncertainty of inventory.uncertainties) {
|
|
101
|
+
context.report({ node: uncertainty.node, messageId: 'opaqueExecutable' });
|
|
102
|
+
}
|
|
103
|
+
for (const site of inventory.commands) {
|
|
96
104
|
const { effective } = site.resolveInvokedCommand({ transparentWrappers });
|
|
97
105
|
if (effective.static)
|
|
98
106
|
continue;
|
package/dist/rules/no-eval.js
CHANGED
|
@@ -23,8 +23,12 @@
|
|
|
23
23
|
* (`$cmd args`) and embedded command strings (`bash -c "$x"`) belong to
|
|
24
24
|
* `no-dynamic-invocation`. The rule has no options and offers no fix or
|
|
25
25
|
* suggestion, because no mechanical rewrite of `eval` preserves behavior.
|
|
26
|
+
*
|
|
27
|
+
* This policy checks represented, statically identified commands. Opaque executable
|
|
28
|
+
* syntax is outside that named-command scope; no-dynamic-invocation reports those
|
|
29
|
+
* inventory limits when a project requires execution completeness.
|
|
26
30
|
*/
|
|
27
|
-
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-
|
|
31
|
+
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
28
32
|
import { resolveTransparentWrappers } from './resolve-transparent-wrappers.js';
|
|
29
33
|
import { createRule } from './create-rule.js';
|
|
30
34
|
import { BUILTIN_WRAPPER } from './transparent-wrappers.js';
|
|
@@ -32,7 +36,7 @@ import { BUILTIN_WRAPPER } from './transparent-wrappers.js';
|
|
|
32
36
|
const RULE_NAME = 'no-eval';
|
|
33
37
|
/**
|
|
34
38
|
* The wrappers followed to find the command an invocation actually runs:
|
|
35
|
-
* `sh-
|
|
39
|
+
* `sh-analysis`'s defaults (`command`, `exec`, `env`, `sudo`, …) plus
|
|
36
40
|
* Bash's `builtin`, so `builtin eval …` is recognized as `eval`.
|
|
37
41
|
*/
|
|
38
42
|
const TRANSPARENT_WRAPPERS = [
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ShellNode } from '../language/syntax-view.js';
|
|
1
2
|
/**
|
|
2
3
|
* The rule's single options object.
|
|
3
4
|
*
|
|
@@ -17,6 +18,17 @@ interface NoMaskedExitStatusOptions {
|
|
|
17
18
|
}
|
|
18
19
|
/** Message ids this rule reports with. @internal */
|
|
19
20
|
type NoMaskedExitStatusMessageId = 'maskedExitStatus' | 'declareThenAssign' | 'assignThenExport' | 'assignThenReadonly';
|
|
21
|
+
/**
|
|
22
|
+
* Whether `node` contains a command substitution whose status the declaration
|
|
23
|
+
* would replace. A substitution counts wherever it sits in the word, including
|
|
24
|
+
* inside an arithmetic expansion, a substring offset or an array subscript:
|
|
25
|
+
* in a plain assignment a failing substitution there fails the assignment
|
|
26
|
+
* (and so trips `set -e`) in Bash, dash, zsh and ksh93. A process
|
|
27
|
+
* substitution is not entered: it expands to a path, so there is no status.
|
|
28
|
+
*
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
export declare function containsCommandSubstitution(root: ShellNode): boolean;
|
|
20
32
|
/** Built-in rule `sh/no-masked-exit-status`. @internal */
|
|
21
33
|
declare const noMaskedExitStatus: import("./define-shell-rule.js").ShellRule<[NoMaskedExitStatusOptions], NoMaskedExitStatusMessageId>;
|
|
22
34
|
export { noMaskedExitStatus };
|
|
@@ -84,9 +84,9 @@
|
|
|
84
84
|
* `-r` (and `readonly` itself) forbids a later assignment, so those forms are
|
|
85
85
|
* split into the declaration, the assignment, then `readonly <names>`.
|
|
86
86
|
*/
|
|
87
|
-
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-
|
|
87
|
+
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
88
88
|
import { resolveTransparentWrappers } from './resolve-transparent-wrappers.js';
|
|
89
|
-
import {
|
|
89
|
+
import { someShellNode, walkShell } from '../language/syntax-view.js';
|
|
90
90
|
import { lineBreakOf } from '../language/line-breaks.js';
|
|
91
91
|
import { createRule } from './create-rule.js';
|
|
92
92
|
import { batsErrexitIsOn, isStatusChecked } from './no-unchecked-cd.js';
|
|
@@ -147,13 +147,15 @@ const TRANSPARENT_WRAPPERS = [
|
|
|
147
147
|
* in a plain assignment a failing substitution there fails the assignment
|
|
148
148
|
* (and so trips `set -e`) in Bash, dash, zsh and ksh93. A process
|
|
149
149
|
* substitution is not entered: it expands to a path, so there is no status.
|
|
150
|
+
*
|
|
151
|
+
* @internal
|
|
150
152
|
*/
|
|
151
|
-
function containsCommandSubstitution(
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
return false;
|
|
156
|
-
|
|
153
|
+
export function containsCommandSubstitution(root) {
|
|
154
|
+
return someShellNode(root, (node) => {
|
|
155
|
+
if (node.type === 'CmdSubst')
|
|
156
|
+
return true;
|
|
157
|
+
return node.type === 'ProcSubst' ? 'skip' : false;
|
|
158
|
+
});
|
|
157
159
|
}
|
|
158
160
|
/** Whether `text` mentions `name` as a whole identifier. */
|
|
159
161
|
function mentionsIdentifier(text, name) {
|
|
@@ -167,9 +169,13 @@ function mentionsIdentifier(text, name) {
|
|
|
167
169
|
* `$(echo "$x")` counts. Reads made by code the substitution runs are
|
|
168
170
|
* {@link runsUnseenCode}'s concern.
|
|
169
171
|
*/
|
|
170
|
-
function readsAnyName(
|
|
172
|
+
function readsAnyName(root, names, sourceCode) {
|
|
171
173
|
if (names.length === 0)
|
|
172
174
|
return false;
|
|
175
|
+
return someShellNode(root, (node) => readsNameHere(node, names, sourceCode));
|
|
176
|
+
}
|
|
177
|
+
/** Whether `node` itself, not its descendants, reads any of `names`. */
|
|
178
|
+
function readsNameHere(node, names, sourceCode) {
|
|
173
179
|
if (node.type === 'ParamExp') {
|
|
174
180
|
if (node.excl)
|
|
175
181
|
return true;
|
|
@@ -187,7 +193,7 @@ function readsAnyName(node, names, sourceCode) {
|
|
|
187
193
|
if (names.some((name) => mentionsIdentifier(arithmetic, name)))
|
|
188
194
|
return true;
|
|
189
195
|
}
|
|
190
|
-
return
|
|
196
|
+
return false;
|
|
191
197
|
}
|
|
192
198
|
/**
|
|
193
199
|
* Whether `node` sits inside a function body: a function definition or a bats
|
|
@@ -206,11 +212,11 @@ function insideFunction(node, sourceCode) {
|
|
|
206
212
|
*/
|
|
207
213
|
const CODE_RUNNING_BUILTINS = new Set(['eval', 'source', '.']);
|
|
208
214
|
/** The names of the functions defined anywhere in the file, at any depth. */
|
|
209
|
-
function collectFunctionNames(
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
215
|
+
function collectFunctionNames(root, into) {
|
|
216
|
+
walkShell(root, (node) => {
|
|
217
|
+
if (node.type === 'FuncDecl' && node.name !== undefined)
|
|
218
|
+
into.add(node.name.value);
|
|
219
|
+
});
|
|
214
220
|
return into;
|
|
215
221
|
}
|
|
216
222
|
/**
|
|
@@ -220,7 +226,11 @@ function collectFunctionNames(node, into) {
|
|
|
220
226
|
* earlier, so such code can see a different variable afterwards. A function
|
|
221
227
|
* defined in another file cannot be seen and is not detected.
|
|
222
228
|
*/
|
|
223
|
-
function runsUnseenCode(
|
|
229
|
+
function runsUnseenCode(root, functionNames, sourceCode, transparentWrappers) {
|
|
230
|
+
return someShellNode(root, (node) => runsUnseenCodeHere(node, functionNames, sourceCode, transparentWrappers));
|
|
231
|
+
}
|
|
232
|
+
/** Whether `node` itself, not its descendants, runs code the rule cannot read. */
|
|
233
|
+
function runsUnseenCodeHere(node, functionNames, sourceCode, transparentWrappers) {
|
|
224
234
|
if (node.type === 'CallExpr' && node.args.length > 0) {
|
|
225
235
|
// This check names the launched program; opaque child-interpreter text
|
|
226
236
|
// does not turn its program identity into dynamic lookup.
|
|
@@ -234,7 +244,7 @@ function runsUnseenCode(node, functionNames, sourceCode, transparentWrappers) {
|
|
|
234
244
|
if (CODE_RUNNING_BUILTINS.has(name) || functionNames.has(name))
|
|
235
245
|
return true;
|
|
236
246
|
}
|
|
237
|
-
return
|
|
247
|
+
return false;
|
|
238
248
|
}
|
|
239
249
|
/**
|
|
240
250
|
* The state of `errexit` before each top-level statement of `file`, from the
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
* command index separates wrapper operands from the sink's arguments.
|
|
29
29
|
* - Matching is a name pattern, not secret detection.
|
|
30
30
|
*/
|
|
31
|
-
import { AlternateUnset, AlternateUnsetOrNull } from 'sh-ast
|
|
31
|
+
import { AlternateUnset, AlternateUnsetOrNull } from 'sh-ast';
|
|
32
32
|
import { resolveTransparentWrappers } from './resolve-transparent-wrappers.js';
|
|
33
33
|
import { createRule } from './create-rule.js';
|
|
34
34
|
import { compileUserPattern } from './user-pattern.js';
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
* negated, in the background, or carrying a here-document (the text after the
|
|
48
48
|
* statement is the document body, not shell code).
|
|
49
49
|
*/
|
|
50
|
-
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-
|
|
50
|
+
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
51
51
|
import { resolveTransparentWrappers } from './resolve-transparent-wrappers.js';
|
|
52
52
|
import { createRule } from './create-rule.js';
|
|
53
53
|
import { prologueEnabledOptions, resolveStrictModePrologue, setStatementChanges, } from './strict-mode-prologue.js';
|
|
@@ -83,28 +83,35 @@ const BATS_ERREXIT_HOOKS = new Set([
|
|
|
83
83
|
* @internal
|
|
84
84
|
*/
|
|
85
85
|
export function isStatusChecked(stmt, parentOf) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
86
|
+
// Each construct that passes the status along moves the question to the enclosing statement.
|
|
87
|
+
for (let current = stmt;;) {
|
|
88
|
+
if (current.background)
|
|
89
|
+
return false;
|
|
90
|
+
const parent = parentOf(current);
|
|
91
|
+
if (parent === undefined)
|
|
92
|
+
return false;
|
|
93
|
+
let enclosing;
|
|
94
|
+
if (parent.type === 'BinaryCmd') {
|
|
95
|
+
if (parent.x === current)
|
|
96
|
+
return parent.op === AND_OP || parent.op === OR_OP;
|
|
97
|
+
// The right side's status is the whole list's status.
|
|
98
|
+
enclosing = parentOf(parent);
|
|
99
|
+
}
|
|
100
|
+
else if (parent.type === 'IfClause' || parent.type === 'WhileClause') {
|
|
101
|
+
return parent.cond.at(-1) === current;
|
|
102
|
+
}
|
|
103
|
+
else if (parent.type === 'Block' || parent.type === 'Subshell') {
|
|
104
|
+
if (parent.stmts.at(-1) !== current)
|
|
105
|
+
return false;
|
|
106
|
+
enclosing = parentOf(parent);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
if (enclosing?.type !== 'Stmt')
|
|
103
112
|
return false;
|
|
104
|
-
|
|
105
|
-
return groupStmt?.type === 'Stmt' && isStatusChecked(groupStmt, parentOf);
|
|
113
|
+
current = enclosing;
|
|
106
114
|
}
|
|
107
|
-
return false;
|
|
108
115
|
}
|
|
109
116
|
/**
|
|
110
117
|
* Whether bats' `errexit` is still in force when `stmt` runs, for a statement
|
|
@@ -88,9 +88,10 @@ export const noUndeclaredCommands = createRule({
|
|
|
88
88
|
return;
|
|
89
89
|
}
|
|
90
90
|
const declared = new Set(declarations.map(({ name }) => name));
|
|
91
|
+
// Report known dependencies even when opaque syntax prevents an exhaustive harvest.
|
|
91
92
|
for (const { name, node } of harvestCommands(sourceCode, {
|
|
92
93
|
transparentWrappers,
|
|
93
|
-
}).values()) {
|
|
94
|
+
}).commands.values()) {
|
|
94
95
|
if (declared.has(name))
|
|
95
96
|
continue;
|
|
96
97
|
context.report({
|
|
@@ -189,11 +189,16 @@ export const noUnusedDeclarations = createRule({
|
|
|
189
189
|
const removeDeclaration = convention.removeDeclaration?.bind(convention);
|
|
190
190
|
return {
|
|
191
191
|
File() {
|
|
192
|
-
const
|
|
192
|
+
const inventory = sourceCode.getCommandInventory();
|
|
193
|
+
// Opaque syntax may use any declaration, so absence cannot justify deletion.
|
|
194
|
+
if (inventory.uncertainties.length > 0) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const sites = inventory.commands;
|
|
193
198
|
if (sites.some((site) => runsUnknownCode(site, completenessWrappers)))
|
|
194
199
|
return;
|
|
195
200
|
const used = new Set();
|
|
196
|
-
for (const name of harvestCommands(sourceCode, { transparentWrappers }).keys()) {
|
|
201
|
+
for (const name of harvestCommands(sourceCode, { transparentWrappers }).commands.keys()) {
|
|
197
202
|
for (const each of namesFor(name))
|
|
198
203
|
used.add(each);
|
|
199
204
|
}
|
|
@@ -124,19 +124,21 @@ function enablesLastpipe(root, sourceCode) {
|
|
|
124
124
|
*/
|
|
125
125
|
function scopeBindings(root, sourceCode, lastpipe) {
|
|
126
126
|
const found = [];
|
|
127
|
-
|
|
127
|
+
// Pre-order with an explicit stack: children are pushed in reverse so they pop in source order.
|
|
128
|
+
const pending = [root];
|
|
129
|
+
for (let node = pending.pop(); node !== undefined; node = pending.pop()) {
|
|
128
130
|
if (node.type === 'FuncDecl' || ISOLATED_SCOPES.has(node.type))
|
|
129
|
-
|
|
131
|
+
continue;
|
|
130
132
|
if (node.type === 'BinaryCmd' && PIPE_OPS.has(node.op)) {
|
|
131
133
|
if (lastpipe && node.y !== undefined)
|
|
132
|
-
|
|
133
|
-
|
|
134
|
+
pending.push(node.y);
|
|
135
|
+
continue;
|
|
134
136
|
}
|
|
135
137
|
found.push(...bindingsOf(node, sourceCode));
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
138
|
+
const children = childNodes(node);
|
|
139
|
+
for (let index = children.length - 1; index >= 0; index--)
|
|
140
|
+
pending.push(children[index]);
|
|
141
|
+
}
|
|
140
142
|
return found.sort((a, b) => a.node.range[0] - b.node.range[0]);
|
|
141
143
|
}
|
|
142
144
|
/** Built-in rule `sh/prefer-local`. @internal */
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
* autofix, because a script may rely on the splitting (for example
|
|
39
39
|
* `$CFLAGS` holding several flags), and only its author can say so.
|
|
40
40
|
*/
|
|
41
|
-
import { DashHdoc, Hdoc } from 'sh-ast
|
|
41
|
+
import { DashHdoc, Hdoc } from 'sh-ast';
|
|
42
42
|
import { createRule } from './create-rule.js';
|
|
43
43
|
import { isBashIntegerVariable } from './bash-variable-semantics.js';
|
|
44
44
|
import { messageExcerpt } from '../language/line-breaks.js';
|
|
@@ -13,9 +13,13 @@
|
|
|
13
13
|
* `git` counts only for the subcommands that talk to a remote (`fetch`,
|
|
14
14
|
* `clone`, `pull`, `push`), found after git's own global options
|
|
15
15
|
* (`git -C dir fetch`).
|
|
16
|
+
*
|
|
17
|
+
* This policy checks represented, statically identified commands. Opaque executable
|
|
18
|
+
* syntax is outside that named-command scope; no-dynamic-invocation reports those
|
|
19
|
+
* inventory limits when a project requires execution completeness.
|
|
16
20
|
*/
|
|
17
21
|
import { configurationMessage } from '../configuration-message.js';
|
|
18
|
-
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-
|
|
22
|
+
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
19
23
|
import { createRule } from './create-rule.js';
|
|
20
24
|
import { WRAPPER_RULE_MESSAGES, buildWrapperRule } from './create-wrapper-rule.js';
|
|
21
25
|
import { networkWrapperSpec, readNetworkWrapperSetting, sameWrapperSpec, } from './resolve-network-wrapper.js';
|
|
@@ -186,9 +190,11 @@ const STRING_LIST_SCHEMA = {
|
|
|
186
190
|
};
|
|
187
191
|
/**
|
|
188
192
|
* The JSON-expressible form of a `WrapperSpec`, for the `wrapper` option.
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
* `
|
|
193
|
+
* Assignment-like operands are skipped only under an explicit
|
|
194
|
+
* `assignmentOperands` grammar (`shell-identifier`, `env`, or `sudo`);
|
|
195
|
+
* omission leaves them available as command candidates. `noArgFlagPattern`
|
|
196
|
+
* is a `RegExp`, so a rule pack needing a flag pattern binds its wrapper
|
|
197
|
+
* with `createWrapperRule` instead of JSON configuration.
|
|
192
198
|
*/
|
|
193
199
|
const WRAPPER_SPEC_SCHEMA = {
|
|
194
200
|
type: 'object',
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
* that here-document's terminator and would carry the other command into the
|
|
31
31
|
* guard.
|
|
32
32
|
*/
|
|
33
|
-
import { DashHdoc, Hdoc } from 'sh-ast
|
|
33
|
+
import { DashHdoc, Hdoc } from 'sh-ast';
|
|
34
34
|
import { createRule } from './create-rule.js';
|
|
35
35
|
import { lineBreakOf } from '../language/line-breaks.js';
|
|
36
36
|
/** Registry name of this rule. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { WrapperSpec } from 'sh-
|
|
1
|
+
import type { WrapperSpec } from 'sh-analysis';
|
|
2
2
|
/**
|
|
3
3
|
* Checks a configured value and returns it typed: a non-empty command name,
|
|
4
4
|
* or a `WrapperSpec` object made of the JSON-expressible fields.
|
|
@@ -16,7 +16,7 @@ export declare function readNetworkWrapperSetting(settings: Readonly<Record<stri
|
|
|
16
16
|
/**
|
|
17
17
|
* The `WrapperSpec` for the network wrapper set in `settings.sh.networkWrapper`,
|
|
18
18
|
* for a rule that looks through wrappers, or `null` when none is set. A name
|
|
19
|
-
* that is one of `sh-
|
|
19
|
+
* that is one of `sh-analysis`'s default wrappers (`timeout`) keeps that
|
|
20
20
|
* entry's known options; any other name is a wrapper with no options.
|
|
21
21
|
*
|
|
22
22
|
* @internal
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* A network wrapper also supplies a transparent-wrapper fact to every consumer.
|
|
4
4
|
*/
|
|
5
5
|
import { configurationMessage } from '../configuration-message.js';
|
|
6
|
-
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-
|
|
6
|
+
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
7
7
|
/** The setting's dotted path, as error messages name it. */
|
|
8
8
|
const SETTING = 'settings.sh.networkWrapper';
|
|
9
9
|
/**
|
|
@@ -90,7 +90,7 @@ export function readNetworkWrapperSetting(settings) {
|
|
|
90
90
|
/**
|
|
91
91
|
* The `WrapperSpec` for the network wrapper set in `settings.sh.networkWrapper`,
|
|
92
92
|
* for a rule that looks through wrappers, or `null` when none is set. A name
|
|
93
|
-
* that is one of `sh-
|
|
93
|
+
* that is one of `sh-analysis`'s default wrappers (`timeout`) keeps that
|
|
94
94
|
* entry's known options; any other name is a wrapper with no options.
|
|
95
95
|
*
|
|
96
96
|
* @internal
|
|
@@ -106,11 +106,11 @@ export function networkWrapperSpec(settings) {
|
|
|
106
106
|
});
|
|
107
107
|
}
|
|
108
108
|
/**
|
|
109
|
-
* Canonical effective grammar from sh-
|
|
109
|
+
* Canonical effective grammar from sh-analysis's WrapperSpec matcher, excluding
|
|
110
110
|
* the names that select it. Flags are membership sets; omitted lists are empty,
|
|
111
111
|
* shellBuiltin defaults to false (utility matching), and positional count
|
|
112
|
-
* defaults to zero.
|
|
113
|
-
*
|
|
112
|
+
* defaults to zero. Assignment operands require an explicit grammar; absence
|
|
113
|
+
* means every operand remains available as a command candidate.
|
|
114
114
|
*
|
|
115
115
|
* Expanded built-ins may contain a flag pattern even though JSON settings may
|
|
116
116
|
* not. Compare its source and flags structurally, including absence, rather
|
|
@@ -127,7 +127,7 @@ function canonicalWrapperGrammar(spec) {
|
|
|
127
127
|
stopsChainFlags: flags(spec.stopsChainFlags),
|
|
128
128
|
shellBuiltin: spec.shellBuiltin ?? false,
|
|
129
129
|
positionalOperandsBeforeCommand: spec.positionalOperandsBeforeCommand ?? 0,
|
|
130
|
-
assignmentOperands: spec.assignmentOperands ??
|
|
130
|
+
assignmentOperands: spec.assignmentOperands ?? null,
|
|
131
131
|
noArgFlagPattern: pattern === undefined ? null : { source: pattern.source, flags: pattern.flags },
|
|
132
132
|
});
|
|
133
133
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SettingsConfig } from '@eslint/core';
|
|
2
|
-
import type { WrapperSpec } from 'sh-
|
|
2
|
+
import type { WrapperSpec } from 'sh-analysis';
|
|
3
3
|
import type { ShellSourceCode } from '../language/shell-source-code.js';
|
|
4
4
|
/** The rule-context fields needed to resolve shared wrappers and their path spellings. @public */
|
|
5
5
|
export interface TransparentWrappersBindingContext {
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* ESLINT_SH_WRAPPER_SETTING rejects malformed bindings; ESLINT_SH_WRAPPER_CONFLICT rejects contradictory shared facts instead of choosing a policy by list order.
|
|
8
8
|
*/
|
|
9
9
|
import { configurationMessage } from '../configuration-message.js';
|
|
10
|
-
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-
|
|
10
|
+
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
11
11
|
import { networkWrapperSpec, sameWrapperOptions, validateWrapperSetting, } from './resolve-network-wrapper.js';
|
|
12
12
|
/** The user-written setting path, extended with indices and fields in configuration errors. */
|
|
13
13
|
const SETTING = 'settings.sh.transparentWrappers';
|
|
@@ -89,7 +89,7 @@ export function resolveTransparentWrappers(context, builtIns = DEFAULT_TRANSPARE
|
|
|
89
89
|
// Relative utility paths need exact aliases in sh-ast. Derive them only from
|
|
90
90
|
// static argv words in this file; no path is resolved or executed here.
|
|
91
91
|
const paths = new Map();
|
|
92
|
-
for (const site of context.sourceCode.
|
|
92
|
+
for (const site of context.sourceCode.getCommandInventory().commands) {
|
|
93
93
|
for (const word of site.argv) {
|
|
94
94
|
if (!word.static || !word.text.includes('/') || paths.has(word.text)) {
|
|
95
95
|
continue;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Wrapper data that built-in rules add to `sh-
|
|
2
|
+
* Wrapper data that built-in rules add to `sh-analysis`'s
|
|
3
3
|
* `DEFAULT_TRANSPARENT_WRAPPERS` when they ask
|
|
4
4
|
* `ShellSourceCode.resolveInvokedCommand` which command an invocation runs.
|
|
5
5
|
*
|
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
* find the word a wrapper runs. No rule logic lives here, and no entry makes
|
|
8
8
|
* a safety judgment. Each rule decides which of these entries it follows.
|
|
9
9
|
*/
|
|
10
|
-
import type { WrapperSpec } from 'sh-
|
|
10
|
+
import type { WrapperSpec } from 'sh-analysis';
|
|
11
11
|
/**
|
|
12
12
|
* Bash's `builtin [shell-builtin [args …]]`: runs the named builtin, bypassing
|
|
13
13
|
* any function of the same name. It takes no options of its own, so the word
|
|
14
14
|
* after it is the command it runs. Only the exact builtin name has these
|
|
15
15
|
* semantics; an executable path with the same basename is an ordinary program.
|
|
16
16
|
*
|
|
17
|
-
* `sh-
|
|
17
|
+
* `sh-analysis`'s default wrapper table leaves `builtin` out, so
|
|
18
18
|
* `builtin "$x"` would otherwise resolve to the static command `builtin`.
|
|
19
19
|
*
|
|
20
20
|
* @see https://www.gnu.org/software/bash/manual/bash.html#Bash-Builtins — `builtin`
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* after it is the command it runs. Only the exact builtin name has these
|
|
5
5
|
* semantics; an executable path with the same basename is an ordinary program.
|
|
6
6
|
*
|
|
7
|
-
* `sh-
|
|
7
|
+
* `sh-analysis`'s default wrapper table leaves `builtin` out, so
|
|
8
8
|
* `builtin "$x"` would otherwise resolve to the static command `builtin`.
|
|
9
9
|
*
|
|
10
10
|
* @see https://www.gnu.org/software/bash/manual/bash.html#Bash-Builtins — `builtin`
|