@eslint-sh/eslint-plugin 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -12
- package/dist/api.d.ts +11 -1
- package/dist/api.js +8 -0
- 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 +65 -12
- package/dist/language/command-resolution.d.ts +24 -6
- package/dist/language/command-resolution.js +44 -17
- package/dist/language/shell-language.js +2 -0
- package/dist/language/shell-source-code.d.ts +29 -11
- package/dist/language/shell-source-code.js +35 -26
- package/dist/language/word-resolution.d.ts +18 -12
- package/dist/language/word-resolution.js +133 -3
- package/dist/processors/disable-justification.d.ts +20 -0
- package/dist/processors/disable-justification.js +124 -0
- package/dist/rules/compat/bash-builtin-destination.d.ts +19 -0
- package/dist/rules/compat/bash-builtin-destination.js +83 -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 +52 -20
- 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 +55 -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 +257 -71
- 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 +65 -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 +211 -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 +148 -155
- package/docs/disable-justification.md +71 -0
- package/docs/error-codes.md +4 -0
- package/docs/examples/eslint.config.js +11 -0
- package/docs/examples/no-curl-in-functions-rule.mjs +20 -23
- package/docs/examples/no-listed-commands-rule.ts +19 -19
- package/docs/examples/no-rm-rf-rule.mjs +33 -62
- package/docs/examples/no-sample-command-rule.mjs +33 -0
- package/docs/reading-nodes.md +26 -1
- package/docs/rules/README.md +4 -4
- package/docs/rules/compat/no-mapfile.md +14 -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 +8 -2
- 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 +3 -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/rules-about-commands.md +78 -0
- package/docs/selectors.md +8 -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
|
|
@@ -158,6 +161,8 @@ ShellLanguage }, rules, configs: { recommended, 'agent-authored', 'bash-3.2', 'p
|
|
|
158
161
|
when a bound convention declares nothing. See [reading
|
|
159
162
|
the convention in your own
|
|
160
163
|
rule](./docs/declaration-conventions.md#reading-the-convention-in-your-own-rule).
|
|
164
|
+
- `createDisableJustificationProcessor` — configures the opt-in processor that requires reasons on ESLint
|
|
165
|
+
disable directives. See [disable justification](./docs/disable-justification.md).
|
|
161
166
|
- `defineShellRule` / `ShellRule` / `ShellRuleSpec` / `ShellRuleMeta` / `ShellRuleContext` /
|
|
162
167
|
`ShellRuleVisitor` / `ShellListener` / `ShellTypedListeners` — the rule-authoring helper and its types. `defineShellRule({ meta, create })`
|
|
163
168
|
returns an ordinary ESLint rule with `meta.languages: ['sh/sh']` set, so ESLint refuses to run it
|
|
@@ -198,7 +203,7 @@ import { parseSync, printSync, walk } from 'sh-ast';
|
|
|
198
203
|
import { DEFAULT_TRANSPARENT_WRAPPERS, enumerateCommands } from 'sh-analysis';
|
|
199
204
|
```
|
|
200
205
|
|
|
201
|
-
The supported dependency ranges are `sh-ast@^0.
|
|
206
|
+
The supported dependency ranges are `sh-ast@^0.13.0` and `sh-analysis@^0.4.0`.
|
|
202
207
|
The plugin has no syntax forwarding subpaths. Raw `resolveWord` from `sh-analysis` accepts
|
|
203
208
|
faithful syntax records; `context.sourceCode.resolveWord` maps an ESLint Word view to its
|
|
204
209
|
original syntax and applies grammar-position defaults.
|
|
@@ -238,23 +243,39 @@ syntax error. Until the parser accepts them, write the workaround in the right-h
|
|
|
238
243
|
|
|
239
244
|
## Writing your own rules
|
|
240
245
|
|
|
241
|
-
|
|
242
|
-
|
|
246
|
+
Start with [Rules about commands](./docs/rules-about-commands.md) for a runnable example
|
|
247
|
+
that recognizes quoted, escaped, path-qualified, and wrapped invocations. Use the
|
|
248
|
+
[selector cookbook](./docs/selectors.md) for rules about syntax.
|
|
243
249
|
|
|
244
250
|
```js
|
|
245
251
|
import { defineShellRule } from '@eslint-sh/eslint-plugin';
|
|
246
252
|
|
|
247
253
|
export default defineShellRule({
|
|
248
254
|
meta: {
|
|
249
|
-
type: '
|
|
255
|
+
type: 'suggestion',
|
|
250
256
|
schema: [],
|
|
251
|
-
messages: {
|
|
257
|
+
messages: { match: 'This script invokes sample-tool.' },
|
|
252
258
|
},
|
|
253
259
|
create(context) {
|
|
254
260
|
return {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
261
|
+
File() {
|
|
262
|
+
const { commands } = context.sourceCode.getCommandInventory();
|
|
263
|
+
for (const site of commands) {
|
|
264
|
+
const { effective, chainIndices } = site.resolveInvokedCommand();
|
|
265
|
+
const index = chainIndices.at(-1);
|
|
266
|
+
if (index === undefined || (!effective.static && chainIndices.length > 1)) continue;
|
|
267
|
+
|
|
268
|
+
const facts = site.argvFacts.at(index);
|
|
269
|
+
if (facts?.resolution !== effective) continue;
|
|
270
|
+
|
|
271
|
+
const text = effective.static ? effective.text : undefined;
|
|
272
|
+
const name = facts.basename.known
|
|
273
|
+
? facts.basename.text
|
|
274
|
+
: text?.slice(text.lastIndexOf('/') + 1);
|
|
275
|
+
const word = site.node.args.at(index);
|
|
276
|
+
if (name === 'sample-tool' && word) {
|
|
277
|
+
context.report({ node: word, messageId: 'match' });
|
|
278
|
+
}
|
|
258
279
|
}
|
|
259
280
|
},
|
|
260
281
|
};
|
|
@@ -273,9 +294,8 @@ accepts only the message ids in `meta.messages`, with no `@eslint/core` import o
|
|
|
273
294
|
receive: narrow a `ShellNode` on `type` and read its fields, typed, with one worked rule.
|
|
274
295
|
|
|
275
296
|
The [custom-rule tutorial](./docs/custom-rule-tutorial.md) is a complete, CI-tested walkthrough: a
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
and a TypeScript rule. `docs/` ships with this package (see `package.json#files`), so the
|
|
297
|
+
minimal flat config for one local rule, a full rule with `context.report`, fixes, and a
|
|
298
|
+
TypeScript rule. `docs/` ships with this package (see `package.json#files`), so the
|
|
279
299
|
tutorial and its example files (`docs/examples/`) are available in every installed copy — not just
|
|
280
300
|
on GitHub.
|
|
281
301
|
|
package/dist/api.d.ts
CHANGED
|
@@ -5,11 +5,12 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { ConfigObject } from '@eslint/core';
|
|
7
7
|
import { ShellLanguage } from './language/shell-language.js';
|
|
8
|
+
import { createDisableJustificationProcessor } from './processors/disable-justification.js';
|
|
8
9
|
export type { ShellLanguage } from './language/shell-language.js';
|
|
9
10
|
export type { ShellSourceCode } from './language/shell-source-code.js';
|
|
10
11
|
export type { NormalizedShellLanguageOptions, ShellLanguageOptions, ShellLanguageOptionsJSON, } from './language/shell-language-options.js';
|
|
11
12
|
export type { Argv0Resolution } from 'sh-analysis';
|
|
12
|
-
export type { CommandContext, ResolveArgv0Options, WordResolution } from 'sh-analysis';
|
|
13
|
+
export type { CommandContext, ResolveArgv0Options, WordFacts, WordResolution } from 'sh-analysis';
|
|
13
14
|
export type { ShellCommandSite, ShellCommandInventory, ShellCommandUncertainty, } from './language/command-resolution.js';
|
|
14
15
|
export type { ShellComment, ShellFile, ShellNode, ShellNodeField, ShellNodeMap, ShellNodeOf, ShellNodeType, ShellPosition, } from './language/syntax-view.js';
|
|
15
16
|
export type { ShellDialect } from './language/shell-language-options.js';
|
|
@@ -21,6 +22,7 @@ export type { TransparentWrappersBindingContext } from './rules/resolve-transpar
|
|
|
21
22
|
export { resolveDeclarationConvention } from './rules/resolve-declaration-convention.js';
|
|
22
23
|
export { defineShellRule } from './rules/define-shell-rule.js';
|
|
23
24
|
export type { ShellListener, ShellRule, ShellRuleContext, ShellRuleMeta, ShellRuleSpec, ShellRuleVisitor, ShellTypedListeners, } from './rules/define-shell-rule.js';
|
|
25
|
+
export { createDisableJustificationProcessor };
|
|
24
26
|
export { createWrapperRule } from './rules/create-wrapper-rule.js';
|
|
25
27
|
export type { WrapperRuleConfig, WrapperRuleLocalInvocation, WrapperRuleLocalOperand, WrapperRuleSubcommands, } from './rules/create-wrapper-rule.js';
|
|
26
28
|
export type { WrapperSpec } from 'sh-analysis';
|
|
@@ -38,6 +40,11 @@ export type { WrapperSpec } from 'sh-analysis';
|
|
|
38
40
|
* own rules, built with the same `defineShellRule`, while sharing parsing,
|
|
39
41
|
* selectors, source coordinates, and directive semantics.
|
|
40
42
|
*
|
|
43
|
+
* Select `processors['require-disable-justification']` as
|
|
44
|
+
* `processor: 'sh/require-disable-justification'` to require reasons on
|
|
45
|
+
* shell disable directives. Use {@link createDisableJustificationProcessor}
|
|
46
|
+
* to select which rule IDs the policy covers.
|
|
47
|
+
*
|
|
41
48
|
* `configs.recommended` is the `sh/recommended` preset: a single config
|
|
42
49
|
* object carrying `plugins: { sh }` and `language: "sh/sh"` but no `files`,
|
|
43
50
|
* so it inherits the globs of the entry that extends it:
|
|
@@ -82,6 +89,9 @@ declare const plugin: {
|
|
|
82
89
|
sh: ShellLanguage;
|
|
83
90
|
};
|
|
84
91
|
rules: Record<string, import("@eslint/core").RuleDefinition<import("@eslint/core").RuleDefinitionTypeOptions>>;
|
|
92
|
+
processors: {
|
|
93
|
+
'require-disable-justification': import("@eslint/core").Processor<string | import("@eslint/core").ProcessorFile>;
|
|
94
|
+
};
|
|
85
95
|
configs: {
|
|
86
96
|
recommended: ConfigObject;
|
|
87
97
|
"agent-authored": ConfigObject;
|
package/dist/api.js
CHANGED
|
@@ -3,6 +3,7 @@ import { createBash32Config } from './configs/bash-3-2.js';
|
|
|
3
3
|
import { createPosixPortableConfig } from './configs/posix-portable.js';
|
|
4
4
|
import { createRecommendedConfig } from './configs/recommended.js';
|
|
5
5
|
import { ShellLanguage } from './language/shell-language.js';
|
|
6
|
+
import { createDisableJustificationProcessor } from './processors/disable-justification.js';
|
|
6
7
|
import { rules } from './rules/index.js';
|
|
7
8
|
import { readPackageVersion } from './version.js';
|
|
8
9
|
export { getDeclaredCommands } from './declaration-conventions/get-declared-commands.js';
|
|
@@ -10,6 +11,7 @@ export { commentPragma } from './declaration-conventions/comment-pragma.js';
|
|
|
10
11
|
export { resolveTransparentWrappers } from './rules/resolve-transparent-wrappers.js';
|
|
11
12
|
export { resolveDeclarationConvention } from './rules/resolve-declaration-convention.js';
|
|
12
13
|
export { defineShellRule } from './rules/define-shell-rule.js';
|
|
14
|
+
export { createDisableJustificationProcessor };
|
|
13
15
|
export { createWrapperRule } from './rules/create-wrapper-rule.js';
|
|
14
16
|
/* eslint-disable @api-extractor-tools/public-on-non-exported -- `plugin` is exported below as
|
|
15
17
|
`export default plugin`; the rule only recognizes inline exports. */
|
|
@@ -27,6 +29,11 @@ export { createWrapperRule } from './rules/create-wrapper-rule.js';
|
|
|
27
29
|
* own rules, built with the same `defineShellRule`, while sharing parsing,
|
|
28
30
|
* selectors, source coordinates, and directive semantics.
|
|
29
31
|
*
|
|
32
|
+
* Select `processors['require-disable-justification']` as
|
|
33
|
+
* `processor: 'sh/require-disable-justification'` to require reasons on
|
|
34
|
+
* shell disable directives. Use {@link createDisableJustificationProcessor}
|
|
35
|
+
* to select which rule IDs the policy covers.
|
|
36
|
+
*
|
|
30
37
|
* `configs.recommended` is the `sh/recommended` preset: a single config
|
|
31
38
|
* object carrying `plugins: { sh }` and `language: "sh/sh"` but no `files`,
|
|
32
39
|
* so it inherits the globs of the entry that extends it:
|
|
@@ -71,6 +78,7 @@ const plugin = {
|
|
|
71
78
|
sh: new ShellLanguage(),
|
|
72
79
|
},
|
|
73
80
|
rules,
|
|
81
|
+
processors: { 'require-disable-justification': createDisableJustificationProcessor() },
|
|
74
82
|
// Filled in below: each shipped config registers this very object under
|
|
75
83
|
// `plugins.sh`, so the plugin must exist before its configs can.
|
|
76
84
|
configs: {},
|
|
@@ -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
|
}
|
|
@@ -11,6 +11,8 @@ import type { Node } from 'sh-ast';
|
|
|
11
11
|
import type { OkParseResult } from '@eslint/core';
|
|
12
12
|
import type { ParseResult } from '@eslint/core';
|
|
13
13
|
import type { PrintOptions } from 'sh-ast';
|
|
14
|
+
import { Processor } from '@eslint/core';
|
|
15
|
+
import { ProcessorFile } from '@eslint/core';
|
|
14
16
|
import type { Replace } from 'sh-ast';
|
|
15
17
|
import { ResolveArgv0Options } from 'sh-analysis';
|
|
16
18
|
import type { ResolveWordOptions } from 'sh-analysis';
|
|
@@ -27,6 +29,7 @@ import type { SourceLocation } from '@eslint/core';
|
|
|
27
29
|
import type { SyntaxPos } from 'sh-ast';
|
|
28
30
|
import { TextSourceCodeBase } from '@eslint/plugin-kit';
|
|
29
31
|
import type { TraversalStep } from '@eslint/core';
|
|
32
|
+
import { WordFacts } from 'sh-analysis';
|
|
30
33
|
import { WordResolution } from 'sh-analysis';
|
|
31
34
|
import { WrapperSpec } from 'sh-analysis';
|
|
32
35
|
|
|
@@ -67,6 +70,17 @@ export { CommandContext }
|
|
|
67
70
|
*/
|
|
68
71
|
export declare const commentPragma: Readonly<Required<DeclarationConvention>>;
|
|
69
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Creates a shell processor that requires reasons on inline disable directives.
|
|
75
|
+
* Use with `language: 'sh/sh'`. Omit `rules` to check all directives, or
|
|
76
|
+
* supply exact ESLint rule IDs or literal prefixes ending in `*`.
|
|
77
|
+
*
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
export declare function createDisableJustificationProcessor(options?: {
|
|
81
|
+
readonly rules?: readonly string[];
|
|
82
|
+
}): Processor;
|
|
83
|
+
|
|
70
84
|
/**
|
|
71
85
|
* Creates a rule that reports every listed command that is not run through
|
|
72
86
|
* `config.wrapper`, for a rule pack's "must be wrapped by X" policies:
|
|
@@ -355,6 +369,11 @@ export declare interface NormalizedShellLanguageOptions extends Record<string, u
|
|
|
355
369
|
* own rules, built with the same `defineShellRule`, while sharing parsing,
|
|
356
370
|
* selectors, source coordinates, and directive semantics.
|
|
357
371
|
*
|
|
372
|
+
* Select `processors['require-disable-justification']` as
|
|
373
|
+
* `processor: 'sh/require-disable-justification'` to require reasons on
|
|
374
|
+
* shell disable directives. Use {@link createDisableJustificationProcessor}
|
|
375
|
+
* to select which rule IDs the policy covers.
|
|
376
|
+
*
|
|
358
377
|
* `configs.recommended` is the `sh/recommended` preset: a single config
|
|
359
378
|
* object carrying `plugins: { sh }` and `language: "sh/sh"` but no `files`,
|
|
360
379
|
* so it inherits the globs of the entry that extends it:
|
|
@@ -399,6 +418,9 @@ declare const plugin: {
|
|
|
399
418
|
sh: ShellLanguage;
|
|
400
419
|
};
|
|
401
420
|
rules: Record<string, RuleDefinition<RuleDefinitionTypeOptions>>;
|
|
421
|
+
processors: {
|
|
422
|
+
'require-disable-justification': Processor<string | ProcessorFile>;
|
|
423
|
+
};
|
|
402
424
|
configs: {
|
|
403
425
|
recommended: ConfigObject;
|
|
404
426
|
"agent-authored": ConfigObject;
|
|
@@ -520,7 +542,8 @@ export declare interface ShellCommandSite {
|
|
|
520
542
|
* The path from the file root to this command, outermost frame first,
|
|
521
543
|
* exactly as `sh-analysis` reports it. For example, `date` in
|
|
522
544
|
* `f() { echo "$(date)"; }` has
|
|
523
|
-
* `[{ kind: 'function',
|
|
545
|
+
* `[{ kind: 'function', names: ['f'] }, { kind: 'cmdSubst' }]`.
|
|
546
|
+
* Function frames list names in declaration order. Anonymous functions have an empty list.
|
|
524
547
|
*/
|
|
525
548
|
readonly context: readonly CommandContext[];
|
|
526
549
|
/** The command word (`argv[0]`) resolved with `{ context: 'command-argument' }`. */
|
|
@@ -531,6 +554,8 @@ export declare interface ShellCommandSite {
|
|
|
531
554
|
* assignments are not part of argv.
|
|
532
555
|
*/
|
|
533
556
|
readonly argv: readonly WordResolution[];
|
|
557
|
+
/** Word facts for the same original argv positions, using the parsed dialect. */
|
|
558
|
+
readonly argvFacts: readonly WordFacts[];
|
|
534
559
|
/**
|
|
535
560
|
* Follows this command's argv0 through transparent wrappers (`env`, `sudo`,
|
|
536
561
|
* `nohup`, `exec`, …) and returns `sh-analysis`'s `resolveArgv0`
|
|
@@ -538,7 +563,15 @@ export declare interface ShellCommandSite {
|
|
|
538
563
|
* `ShellSourceCode.resolveInvokedCommand(node, options)`; both read the
|
|
539
564
|
* same per-file index.
|
|
540
565
|
*
|
|
541
|
-
*
|
|
566
|
+
* The final `chainIndices` entry indexes the word associated with
|
|
567
|
+
* `effective` in `node.args`. If `effective` is the same object as that
|
|
568
|
+
* position's `argvFacts[index].resolution`, it describes the word itself.
|
|
569
|
+
* A non-static word reached through a wrapper may still be an operand
|
|
570
|
+
* whose role is unresolved. The `basename` fact can be known even when
|
|
571
|
+
* the directory is dynamic. If the basename fact is unknown and `effective`
|
|
572
|
+
* is static, its `text` supplies the whole path.
|
|
573
|
+
*
|
|
574
|
+
* @param options - Wrapper and interpreter settings. Resolution uses the parsed dialect.
|
|
542
575
|
* `transparentWrappers` replaces `sh-analysis`'s
|
|
543
576
|
* `DEFAULT_TRANSPARENT_WRAPPERS`; spread the defaults into the array to
|
|
544
577
|
* extend them instead.
|
|
@@ -1047,10 +1080,15 @@ export declare interface ShellRuleVisitor extends ShellTypedListeners {
|
|
|
1047
1080
|
* `sh-analysis` owns static-word interpretation and dynamic reasons; this
|
|
1048
1081
|
* method supplies the lint tree's position-based policy, without executing shell.
|
|
1049
1082
|
*
|
|
1050
|
-
* Without explicit options, `CallExpr.args` use command-argument context
|
|
1051
|
-
* `
|
|
1052
|
-
*
|
|
1053
|
-
*
|
|
1083
|
+
* Without explicit options, `CallExpr.args` use command-argument context.
|
|
1084
|
+
* An ordinary argument such as `a:~/bin` is literal in that context, while
|
|
1085
|
+
* `PATH=a:~/bin` is dynamic because Bash can expand the tilde in an
|
|
1086
|
+
* assignment-shaped argument. Other positions use the analyzer's
|
|
1087
|
+
* conservative default, which also considers assignment-value expansion.
|
|
1088
|
+
* Explicit options override the context defaults. Resolution uses the parsed dialect.
|
|
1089
|
+
* Since sh-analysis 0.3, a top-level escaped literal can return `unsupported`
|
|
1090
|
+
* when the analyzer receives no context. For the assignment value in
|
|
1091
|
+
* `label=a\ b`, pass `{ context: 'assignment-value' }` to resolve `a b`.
|
|
1054
1092
|
*
|
|
1055
1093
|
* @param word - A parsed Word from the lint view.
|
|
1056
1094
|
* @param options - Explicit analysis policy overriding grammar defaults.
|
|
@@ -1099,10 +1137,11 @@ export declare interface ShellRuleVisitor extends ShellTypedListeners {
|
|
|
1099
1137
|
* assignment-only statement such as `FOO=1`).
|
|
1100
1138
|
*
|
|
1101
1139
|
* Delegates to `resolveArgv0` with the `CommandSite` that `enumerateCommands`
|
|
1102
|
-
* reports for this `CallExpr` in this file
|
|
1103
|
-
*
|
|
1104
|
-
*
|
|
1105
|
-
* `
|
|
1140
|
+
* reports for this `CallExpr` in this file. A wrapper that accepts assignments
|
|
1141
|
+
* can skip a fixed scalar assignment prefix when the operand is known to
|
|
1142
|
+
* occupy one field, even if the value is dynamic. For example,
|
|
1143
|
+
* `env PATH=a:~/bin printf hi` resolves to `printf`. Read the effective
|
|
1144
|
+
* command's name as `res.effective.static ? res.effective.text : null`.
|
|
1106
1145
|
* `sudo -u x "$prog"` and `exec "$@"` have a non-static `effective` with
|
|
1107
1146
|
* reason `expansion`; `env -S '…'` and `bash -c '…'` have reason
|
|
1108
1147
|
* `embedded-command`; `chain`
|
|
@@ -1111,7 +1150,14 @@ export declare interface ShellRuleVisitor extends ShellTypedListeners {
|
|
|
1111
1150
|
* {@link ShellSourceCode.getCommandInventory} reports it). When `effective` is
|
|
1112
1151
|
* static, the effective command's arguments are
|
|
1113
1152
|
* `argv.slice(chainIndices.at(-1)! + 1)`: every entry after the effective
|
|
1114
|
-
* command's own index. For
|
|
1153
|
+
* command's own index. For basename matching, check that `effective` is
|
|
1154
|
+
* the same object as `argvFacts[index].resolution` on the inventory site.
|
|
1155
|
+
* A non-static word reached through a wrapper may still be an operand
|
|
1156
|
+
* whose role is unresolved. A known `basename` supplies its `text` even
|
|
1157
|
+
* with a dynamic directory.
|
|
1158
|
+
* If the basename fact is unknown and `effective` is static, its `text`
|
|
1159
|
+
* supplies the whole path.
|
|
1160
|
+
* For an interpreter command string the final chain
|
|
1115
1161
|
* index instead locates the selector flag; the interpreter is in `chain.at(-2)`.
|
|
1116
1162
|
* `embeddedCommandInterpreters: []` retains the interpreter as the static
|
|
1117
1163
|
* effective command when a policy needs program identity. Utility wrappers
|
|
@@ -1125,7 +1171,7 @@ export declare interface ShellRuleVisitor extends ShellTypedListeners {
|
|
|
1125
1171
|
* index would silently omit real commands.
|
|
1126
1172
|
*
|
|
1127
1173
|
* @param callExpr - A parsed `CallExpr` node from this file's lint tree.
|
|
1128
|
-
* @param options -
|
|
1174
|
+
* @param options - Wrapper and interpreter settings. Resolution uses the parsed dialect.
|
|
1129
1175
|
* `transparentWrappers` replaces `sh-analysis`'s
|
|
1130
1176
|
* `DEFAULT_TRANSPARENT_WRAPPERS`; spread the defaults into the array to
|
|
1131
1177
|
* extend them instead.
|
|
@@ -1319,6 +1365,8 @@ export declare interface ShellRuleVisitor extends ShellTypedListeners {
|
|
|
1319
1365
|
line: number;
|
|
1320
1366
|
column: number;
|
|
1321
1367
|
};
|
|
1368
|
+
/** Returns inventory facts for parsed command arguments. @internal */
|
|
1369
|
+
getCommandArgumentFacts(callExpr: ShellNodeOf<'CallExpr'>): readonly WordFacts[] | undefined;
|
|
1322
1370
|
/**
|
|
1323
1371
|
* Returns the parent of `node`, or `undefined` for the root `File` node
|
|
1324
1372
|
* (or any node visited before `traverse()` has run once). ESLint always
|
|
@@ -1352,6 +1400,9 @@ export declare interface ShellRuleVisitor extends ShellTypedListeners {
|
|
|
1352
1400
|
* source as the named ecosystem precedent), generalized to a configurable
|
|
1353
1401
|
* prefix instead of the hardcoded `"eslint"` label.
|
|
1354
1402
|
*
|
|
1403
|
+
* Diagnostics about a directive should use the node's location. Its range may extend beyond the
|
|
1404
|
+
* comment so ESLint can remove an unused directive cleanly.
|
|
1405
|
+
*
|
|
1355
1406
|
* Unlike `JSONSourceCode` (whose block comments can span multiple
|
|
1356
1407
|
* physical lines, making `<prefix>-disable-line` spanning lines a real,
|
|
1357
1408
|
* reportable problem), mvdan/sh's `#` line comments cannot contain an
|
|
@@ -1409,6 +1460,8 @@ export declare interface TransparentWrappersBindingContext {
|
|
|
1409
1460
|
readonly sourceCode: ShellSourceCode;
|
|
1410
1461
|
}
|
|
1411
1462
|
|
|
1463
|
+
export { WordFacts }
|
|
1464
|
+
|
|
1412
1465
|
export { WordResolution }
|
|
1413
1466
|
|
|
1414
1467
|
/**
|
|
@@ -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,15 @@ export interface ShellCommandSite {
|
|
|
66
72
|
* `ShellSourceCode.resolveInvokedCommand(node, options)`; both read the
|
|
67
73
|
* same per-file index.
|
|
68
74
|
*
|
|
69
|
-
*
|
|
75
|
+
* The final `chainIndices` entry indexes the word associated with
|
|
76
|
+
* `effective` in `node.args`. If `effective` is the same object as that
|
|
77
|
+
* position's `argvFacts[index].resolution`, it describes the word itself.
|
|
78
|
+
* A non-static word reached through a wrapper may still be an operand
|
|
79
|
+
* whose role is unresolved. The `basename` fact can be known even when
|
|
80
|
+
* the directory is dynamic. If the basename fact is unknown and `effective`
|
|
81
|
+
* is static, its `text` supplies the whole path.
|
|
82
|
+
*
|
|
83
|
+
* @param options - Wrapper and interpreter settings. Resolution uses the parsed dialect.
|
|
70
84
|
* `transparentWrappers` replaces `sh-analysis`'s
|
|
71
85
|
* `DEFAULT_TRANSPARENT_WRAPPERS`; spread the defaults into the array to
|
|
72
86
|
* extend them instead.
|
|
@@ -78,6 +92,8 @@ export interface ShellCommandSite {
|
|
|
78
92
|
*/
|
|
79
93
|
resolveInvokedCommand(options?: ResolveArgv0Options): Argv0Resolution;
|
|
80
94
|
}
|
|
95
|
+
/** The known basename of a real chain word at its original argv position. @internal */
|
|
96
|
+
export declare function commandBasename(site: ShellCommandSite, word: Argv0ChainWord, index: number): string | null;
|
|
81
97
|
/**
|
|
82
98
|
* An opaque executable construct projected onto the original ESLint view.
|
|
83
99
|
* Its presence limits inventory completeness; it does not assert a hidden command.
|
|
@@ -124,7 +140,9 @@ export interface ShellCommandInventory {
|
|
|
124
140
|
* partial index would silently omit real commands, so callers fail closed.
|
|
125
141
|
* @internal
|
|
126
142
|
*/
|
|
127
|
-
export declare function buildCommandSiteIndex(file: ShellFile): CommandSiteIndex;
|
|
143
|
+
export declare function buildCommandSiteIndex(file: ShellFile, dialect: ShellDialect): CommandSiteIndex;
|
|
144
|
+
/** Resolves wrappers using the same dialect and facts as command enumeration. @internal */
|
|
145
|
+
export declare function resolveSiteArgv0(site: DialectCommandSite, options?: ResolveArgv0Options): Argv0Resolution;
|
|
128
146
|
/**
|
|
129
147
|
* Re-expresses every site in `index` in lint-view terms, in analysis's order.
|
|
130
148
|
* Each `node` comes from the syntax → view association recorded when `file`
|