@eslint-sh/eslint-plugin 0.8.0 → 0.9.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 +41 -34
- package/dist/api.d.ts +92 -0
- package/dist/api.js +83 -0
- package/dist/command-harvest/harvest-commands.d.ts +2 -2
- package/dist/command-harvest/harvest-commands.js +2 -2
- package/dist/configs/posix-portable.d.ts +6 -10
- package/dist/configs/posix-portable.js +4 -6
- package/dist/configuration-message.d.ts +14 -0
- package/dist/configuration-message.js +33 -0
- package/dist/declaration-conventions/get-declared-commands.d.ts +41 -0
- package/dist/declaration-conventions/{read-declared-commands.js → get-declared-commands.js} +43 -43
- package/dist/eslint-plugin-api.d.ts +1506 -0
- package/dist/eslint-plugin-syntax-analyze.d.ts +3 -3
- package/dist/eslint-plugin-syntax.d.ts +3 -4
- package/dist/eslint-plugin.d.ts +16 -1560
- package/dist/index.d.ts +14 -81
- package/dist/index.js +22 -76
- package/dist/language/command-resolution.d.ts +5 -5
- package/dist/language/command-resolution.js +11 -7
- package/dist/language/selector-classes.d.ts +7 -3
- package/dist/language/selector-classes.js +13 -4
- package/dist/language/shell-language-options.d.ts +3 -3
- package/dist/language/shell-language-options.js +12 -17
- package/dist/language/shell-language.d.ts +3 -1
- package/dist/language/shell-language.js +6 -11
- package/dist/language/shell-source-code.d.ts +59 -42
- package/dist/language/shell-source-code.js +86 -60
- package/dist/language/source-coordinates.d.ts +2 -0
- package/dist/language/source-coordinates.js +8 -4
- package/dist/language/syntax-view.js +19 -7
- package/dist/language/word-resolution.d.ts +2 -1
- package/dist/language/word-resolution.js +8 -3
- package/dist/rules/create-wrapper-rule.d.ts +1 -1
- package/dist/rules/create-wrapper-rule.js +11 -9
- package/dist/rules/define-shell-rule.js +1 -1
- package/dist/rules/no-masked-exit-status.d.ts +4 -3
- package/dist/rules/no-masked-exit-status.js +3 -2
- package/dist/rules/no-secret-env-reads.d.ts +2 -0
- package/dist/rules/no-secret-env-reads.js +13 -2
- package/dist/rules/no-secret-flags.d.ts +2 -0
- package/dist/rules/no-secret-flags.js +14 -3
- package/dist/rules/no-unchecked-cd.js +3 -2
- package/dist/rules/no-undeclared-commands.js +8 -3
- package/dist/rules/no-unused-declarations.js +6 -2
- package/dist/rules/prefer-local.d.ts +3 -2
- package/dist/rules/prefer-local.js +4 -2
- package/dist/rules/require-network-wrapper.d.ts +1 -1
- package/dist/rules/require-network-wrapper.js +11 -7
- package/dist/rules/require-strict-mode.d.ts +5 -46
- package/dist/rules/require-strict-mode.js +17 -107
- package/dist/rules/require-tty-guard.d.ts +2 -0
- package/dist/rules/require-tty-guard.js +8 -1
- package/dist/rules/resolve-declaration-convention.d.ts +9 -88
- package/dist/rules/resolve-declaration-convention.js +19 -71
- package/dist/rules/resolve-network-wrapper.d.ts +1 -2
- package/dist/rules/resolve-network-wrapper.js +18 -14
- package/dist/rules/resolve-transparent-wrappers.d.ts +2 -7
- package/dist/rules/resolve-transparent-wrappers.js +19 -9
- package/dist/rules/set-options.d.ts +1 -1
- package/dist/rules/set-options.js +1 -1
- package/dist/rules/strict-mode-prologue.d.ts +43 -0
- package/dist/rules/strict-mode-prologue.js +91 -0
- package/dist/rules/user-pattern.d.ts +4 -3
- package/dist/rules/user-pattern.js +10 -8
- package/dist/shell-dialect.d.ts +8 -0
- package/dist/shell-dialect.js +22 -0
- package/dist/syntax/analyze.d.ts +3 -3
- package/dist/syntax/analyze.js +3 -3
- package/dist/syntax/index.d.ts +3 -4
- package/dist/syntax/index.js +3 -4
- package/dist/version.js +2 -1
- package/docs/compat.md +5 -6
- package/docs/custom-rule-tutorial.md +22 -18
- package/docs/declaration-conventions.md +20 -23
- package/docs/error-codes.md +93 -0
- package/docs/examples/no-rm-rf-rule.mjs +8 -10
- package/docs/examples/sorted-declarations-rule.mjs +6 -9
- package/docs/examples/tsconfig.json +10 -0
- package/docs/rules/README.md +12 -4
- package/docs/rules/compat/no-nounset-empty-array.md +1 -1
- package/docs/rules/no-masked-exit-status.md +2 -2
- package/docs/rules/no-secret-env-reads.md +13 -3
- package/docs/rules/no-secret-flags.md +14 -5
- package/docs/rules/no-unchecked-cd.md +15 -2
- package/docs/rules/require-network-wrapper.md +5 -5
- package/docs/rules/require-strict-mode.md +25 -23
- package/docs/rules/require-tty-guard.md +12 -3
- package/docs/selectors.md +7 -2
- package/docs/wrapper-rules.md +2 -0
- package/package.json +3 -3
- package/dist/declaration-conventions/read-declared-commands.d.ts +0 -70
|
@@ -1,90 +1,13 @@
|
|
|
1
1
|
import { createRule } from './create-rule.js';
|
|
2
|
-
import { SET_OPTION_NAMES,
|
|
2
|
+
import { SET_OPTION_NAMES, formatSetCommand, shebangOptionChanges } from './set-options.js';
|
|
3
|
+
import { enabledAfter, resolveStrictModePrologue, setStatementChanges, } from './strict-mode-prologue.js';
|
|
3
4
|
import { lineBreakOf } from '../language/line-breaks.js';
|
|
4
5
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
6
|
+
* Dialect-aware policy defaults. Bats runs under Bash; POSIX omits pipefail
|
|
7
|
+
* because older dash and BusyBox ash reject it. Explicit requirements win.
|
|
7
8
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* The prologue an unconfigured `mode` falls back to under the `posix`
|
|
11
|
-
* dialect: strict mode without `pipefail`, so the autofix never inserts a
|
|
12
|
-
* line that a pre-POSIX.1-2024 `sh` rejects. `pipefail` stays available
|
|
13
|
-
* there by configuring `mode`.
|
|
14
|
-
*/
|
|
15
|
-
const POSIX_DEFAULT_PROLOGUE = 'set -eu';
|
|
16
|
-
/**
|
|
17
|
-
* The prologue used when `mode` is not configured, chosen by the file's
|
|
18
|
-
* dialect. Only `posix` differs; `bats` files run under Bash and keep the
|
|
19
|
-
* Bash default.
|
|
20
|
-
*/
|
|
21
|
-
function defaultPrologueFor(dialect) {
|
|
22
|
-
return dialect === 'posix' ? POSIX_DEFAULT_PROLOGUE : DEFAULT_PROLOGUE;
|
|
23
|
-
}
|
|
24
|
-
/** How many top-level statements `withinStatements` defaults to. */
|
|
25
|
-
const DEFAULT_WITHIN_STATEMENTS = 5;
|
|
26
|
-
/**
|
|
27
|
-
* Turns the `mode` option into a {@link StrictModeRequirement}. A string mode
|
|
28
|
-
* must be a `set` command whose arguments only enable known options (no `+`
|
|
29
|
-
* flags, no positional parameters); anything else is a configuration error,
|
|
30
|
-
* reported by throwing so ESLint surfaces it instead of linting against a
|
|
31
|
-
* requirement nobody wrote.
|
|
32
|
-
*/
|
|
33
|
-
function resolveRequirement(mode) {
|
|
34
|
-
if (typeof mode !== 'string') {
|
|
35
|
-
return { required: [...new Set(mode)], prologue: formatSetCommand(mode) };
|
|
36
|
-
}
|
|
37
|
-
const prologue = mode.trim();
|
|
38
|
-
const [command, ...args] = prologue.split(/\s+/u);
|
|
39
|
-
const parsed = parseSetArguments(args);
|
|
40
|
-
if (command !== 'set' ||
|
|
41
|
-
parsed.stoppedBy !== undefined ||
|
|
42
|
-
parsed.changes.length === 0 ||
|
|
43
|
-
parsed.changes.some((change) => !change.enable)) {
|
|
44
|
-
throw new TypeError(`sh/require-strict-mode: \`mode\` must be a \`set\` command that only enables options ` +
|
|
45
|
-
`(for example "${DEFAULT_PROLOGUE}") or a list of option names; got ${JSON.stringify(mode)}.`);
|
|
46
|
-
}
|
|
47
|
-
return { required: [...new Set(parsed.changes.map((change) => change.option))], prologue };
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Whether `stmt` is a plain, foreground, top-level `set` command, and if so
|
|
51
|
-
* the option changes its arguments make (read through `sourceCode`, so a
|
|
52
|
-
* non-static argument ends the reading).
|
|
53
|
-
*
|
|
54
|
-
* @internal
|
|
55
|
-
*/
|
|
56
|
-
export function setStatementChanges(stmt, sourceCode) {
|
|
57
|
-
if (stmt.background)
|
|
58
|
-
return [];
|
|
59
|
-
const { cmd } = stmt;
|
|
60
|
-
if (cmd?.type !== 'CallExpr' || sourceCode.getCommandName(cmd) !== 'set')
|
|
61
|
-
return [];
|
|
62
|
-
const args = cmd.args.slice(1);
|
|
63
|
-
return parseSetArguments(args.map((word) => sourceCode.resolveWordText(word))).changes;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* The options enabled after applying `seed`, then each statement's `set`
|
|
67
|
-
* changes in order.
|
|
68
|
-
*/
|
|
69
|
-
function enabledAfter(seed, statementChanges) {
|
|
70
|
-
const enabled = applySetOptionChanges(new Set(), seed);
|
|
71
|
-
for (const changes of statementChanges)
|
|
72
|
-
applySetOptionChanges(enabled, changes);
|
|
73
|
-
return enabled;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* The options a file's prologue leaves enabled: the state after the first
|
|
77
|
-
* `withinStatements` top-level statements of `file`, plus the shebang's
|
|
78
|
-
* options when `allowShebangOnly` is set. This is the single definition of
|
|
79
|
-
* "what the prologue enables", shared with other rules (such as
|
|
80
|
-
* `no-unchecked-cd`) that need to know whether strict mode is in force.
|
|
81
|
-
*
|
|
82
|
-
* @internal
|
|
83
|
-
*/
|
|
84
|
-
export function prologueEnabledOptions(file, sourceCode, config = {}) {
|
|
85
|
-
const shebangChanges = config.allowShebangOnly === true ? shebangOptionChanges(sourceCode.text) : [];
|
|
86
|
-
const statements = file.stmts.slice(0, config.withinStatements ?? DEFAULT_WITHIN_STATEMENTS);
|
|
87
|
-
return enabledAfter(shebangChanges, statements.map((stmt) => setStatementChanges(stmt, sourceCode)));
|
|
9
|
+
function defaultRequiredOptions(dialect) {
|
|
10
|
+
return dialect === 'posix' ? ['errexit', 'nounset'] : ['errexit', 'nounset', 'pipefail'];
|
|
88
11
|
}
|
|
89
12
|
/** Built-in rule `sh/require-strict-mode`. @internal */
|
|
90
13
|
export const requireStrictMode = createRule({
|
|
@@ -99,32 +22,18 @@ export const requireStrictMode = createRule({
|
|
|
99
22
|
{
|
|
100
23
|
type: 'object',
|
|
101
24
|
properties: {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
items: { enum: [...SET_OPTION_NAMES] },
|
|
108
|
-
minItems: 1,
|
|
109
|
-
uniqueItems: true,
|
|
110
|
-
},
|
|
111
|
-
],
|
|
25
|
+
requiredOptions: {
|
|
26
|
+
type: 'array',
|
|
27
|
+
items: { enum: [...SET_OPTION_NAMES] },
|
|
28
|
+
minItems: 1,
|
|
29
|
+
uniqueItems: true,
|
|
112
30
|
},
|
|
113
|
-
allowShebangOnly: { type: 'boolean' },
|
|
114
|
-
withinStatements: { type: 'integer', minimum: 1 },
|
|
115
31
|
},
|
|
116
32
|
additionalProperties: false,
|
|
117
33
|
},
|
|
118
34
|
],
|
|
119
|
-
//
|
|
120
|
-
|
|
121
|
-
// for the rule to tell "not configured" apart from a configured value.
|
|
122
|
-
defaultOptions: [
|
|
123
|
-
{
|
|
124
|
-
allowShebangOnly: false,
|
|
125
|
-
withinStatements: DEFAULT_WITHIN_STATEMENTS,
|
|
126
|
-
},
|
|
127
|
-
],
|
|
35
|
+
// Absence must survive defaulting so each file's dialect selects its policy.
|
|
36
|
+
defaultOptions: [{}],
|
|
128
37
|
messages: {
|
|
129
38
|
missingStrictMode: 'Enable strict mode with `{{prologue}}` within {{window}}; not enabled: {{missing}}.',
|
|
130
39
|
},
|
|
@@ -132,12 +41,13 @@ export const requireStrictMode = createRule({
|
|
|
132
41
|
create(context) {
|
|
133
42
|
const [options] = context.options;
|
|
134
43
|
const { sourceCode } = context;
|
|
135
|
-
const
|
|
136
|
-
const
|
|
44
|
+
const required = options.requiredOptions ?? defaultRequiredOptions(sourceCode.dialect);
|
|
45
|
+
const prologue = formatSetCommand(required);
|
|
46
|
+
const { allowShebangOnly, withinStatements } = resolveStrictModePrologue(context.settings);
|
|
137
47
|
return {
|
|
138
48
|
File(node) {
|
|
139
49
|
const text = sourceCode.text;
|
|
140
|
-
const shebangChanges =
|
|
50
|
+
const shebangChanges = allowShebangOnly ? shebangOptionChanges(text) : [];
|
|
141
51
|
const statements = node.stmts.slice(0, withinStatements);
|
|
142
52
|
const statementChanges = statements.map((stmt) => setStatementChanges(stmt, sourceCode));
|
|
143
53
|
const enabled = enabledAfter(shebangChanges, statementChanges);
|
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
export interface RequireTtyGuardOptions {
|
|
7
7
|
/** Replaces the default interactive commands (`read`, `select`). */
|
|
8
8
|
readonly commands?: readonly string[];
|
|
9
|
+
/** Commands added to the replacement list, or to `read` and `select` when it is omitted. */
|
|
10
|
+
readonly additionalCommands?: readonly string[];
|
|
9
11
|
}
|
|
10
12
|
/** Message ids of this rule. @internal */
|
|
11
13
|
export type RequireTtyGuardMessageId = 'unguardedInteractive' | 'wrapInTtyGuard';
|
|
@@ -204,6 +204,7 @@ export const requireTtyGuard = createRule({
|
|
|
204
204
|
type: 'object',
|
|
205
205
|
properties: {
|
|
206
206
|
commands: { type: 'array', items: { type: 'string', minLength: 1 } },
|
|
207
|
+
additionalCommands: { type: 'array', items: { type: 'string', minLength: 1 } },
|
|
207
208
|
},
|
|
208
209
|
additionalProperties: false,
|
|
209
210
|
},
|
|
@@ -215,7 +216,13 @@ export const requireTtyGuard = createRule({
|
|
|
215
216
|
},
|
|
216
217
|
create(context) {
|
|
217
218
|
const { sourceCode } = context;
|
|
218
|
-
const
|
|
219
|
+
const options = context.options[0];
|
|
220
|
+
// Empty replacements intentionally remove defaults; a set makes overlapping
|
|
221
|
+
// base/additional names one policy without mutating either configured list.
|
|
222
|
+
const commands = new Set([
|
|
223
|
+
...(options?.commands ?? DEFAULT_COMMANDS),
|
|
224
|
+
...(options?.additionalCommands ?? []),
|
|
225
|
+
]);
|
|
219
226
|
// Interactive commands found in the file, checked once every guard is known.
|
|
220
227
|
const candidates = [];
|
|
221
228
|
const guards = [];
|
|
@@ -1,95 +1,16 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Binding resolution for the declaration-convention extension point: the one
|
|
3
|
-
* place a rule finds out which {@link DeclarationConvention} the consumer has
|
|
4
|
-
* bound, if any.
|
|
5
|
-
*
|
|
6
|
-
* Every rule that consumes a convention calls
|
|
7
|
-
* {@link resolveDeclarationConvention} from its `create()`, so all of them
|
|
8
|
-
* agree on precedence, on what "not configured" means, and on how a
|
|
9
|
-
* misconfiguration is reported. The precedence is:
|
|
10
|
-
*
|
|
11
|
-
* 1. the rule's own `declarationConvention` option (its first options
|
|
12
|
-
* object), so one rule can be pointed at a different convention. ESLint
|
|
13
|
-
* flat config copies rule options with `structuredClone`, so a convention
|
|
14
|
-
* whose methods are own properties is rejected there by ESLint itself
|
|
15
|
-
* ("could not be cloned"), and a class instance arrives with its methods
|
|
16
|
-
* stripped and fails the check below. In practice consumers bind through
|
|
17
|
-
* settings;
|
|
18
|
-
* 2. the shared `settings.sh.declarationConvention`, bound once for every
|
|
19
|
-
* consuming rule;
|
|
20
|
-
* 3. otherwise `null`: no convention is bound and the consuming rule is
|
|
21
|
-
* inert — it reports nothing. Shipping a default would smuggle one
|
|
22
|
-
* project's declaration policy into every project.
|
|
23
|
-
*
|
|
24
|
-
* Only an absent (`undefined`) value falls through to the next source. Any
|
|
25
|
-
* other value is the consumer's explicit choice and must be a convention; if
|
|
26
|
-
* it is not, resolution throws an error that names the offending setting, so
|
|
27
|
-
* the misconfiguration surfaces as a readable configuration message rather
|
|
28
|
-
* than a `TypeError` from deep inside a rule.
|
|
29
|
-
*
|
|
30
|
-
* The resolver is public so a rule pack can write its own consumer of the
|
|
31
|
-
* bound convention and get exactly the precedence, validation and error text
|
|
32
|
-
* the built-in consumers (`sh/no-undeclared-commands`,
|
|
33
|
-
* `sh/no-unused-declarations`) use, instead of re-implementing them.
|
|
34
|
-
*/
|
|
35
1
|
import type { SettingsConfig } from '@eslint/core';
|
|
36
2
|
import type { DeclarationConvention } from '../declaration-conventions/declaration-convention.js';
|
|
37
3
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* Any rule `context` satisfies it, so a rule passes its own `context`.
|
|
4
|
+
* Returns the convention bound in `settings.sh.declarationConvention`, or `null`
|
|
5
|
+
* when absent. Present invalid values throw an error naming the shared setting.
|
|
41
6
|
*
|
|
42
|
-
* @
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
/** The rule id as configured (`pack/my-rule`), named in an option-binding error. */
|
|
46
|
-
readonly id: string;
|
|
47
|
-
/** The rule's configured options; the first may carry `declarationConvention`. */
|
|
48
|
-
readonly options: readonly unknown[];
|
|
49
|
-
/** The shared settings; `settings.sh.declarationConvention` is the usual binding. */
|
|
50
|
-
readonly settings: SettingsConfig;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Resolves the declaration convention bound for the rule running in
|
|
54
|
-
* `context`: the rule's `declarationConvention` option, else
|
|
55
|
-
* `settings: { sh: { declarationConvention: convention } }`, else `null`
|
|
56
|
-
* (the rule is inert).
|
|
57
|
-
*
|
|
58
|
-
* Throws an `Error` naming the offending setting when the configured value is
|
|
59
|
-
* present but does not implement `declaredCommands` (or has a non-function
|
|
60
|
-
* `removeDeclaration`). The message names where the value came from and what
|
|
61
|
-
* it was, including the expected convention shape and the received value.
|
|
62
|
-
* Call it from `create()`, so the error surfaces as ESLint's configuration
|
|
63
|
-
* message for the file being linted.
|
|
64
|
-
*
|
|
65
|
-
* A rule that receives `null` should register no listeners: with no
|
|
66
|
-
* convention bound, a convention consumer has nothing to check.
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
* ```ts
|
|
70
|
-
* import {
|
|
71
|
-
* defineShellRule,
|
|
72
|
-
* readDeclaredCommands,
|
|
73
|
-
* resolveDeclarationConvention,
|
|
74
|
-
* } from '@eslint-sh/eslint-plugin';
|
|
75
|
-
*
|
|
76
|
-
* export const maxDeclaredCommands = defineShellRule({
|
|
77
|
-
* meta: { schema: [], messages: { tooMany: 'Declare at most 5 commands.' } },
|
|
78
|
-
* create(context) {
|
|
79
|
-
* const convention = resolveDeclarationConvention(context);
|
|
80
|
-
* if (convention === null) return {};
|
|
81
|
-
* return {
|
|
82
|
-
* File(node) {
|
|
83
|
-
* // Validated declarations for the file `context` is linting.
|
|
84
|
-
* if (readDeclaredCommands(convention, context).length > 5) {
|
|
85
|
-
* context.report({ node, messageId: 'tooMany' });
|
|
86
|
-
* }
|
|
87
|
-
* },
|
|
88
|
-
* };
|
|
89
|
-
* },
|
|
90
|
-
* });
|
|
91
|
-
* ```
|
|
7
|
+
* Use {@link getDeclaredCommands} to read validated declarations. This narrow
|
|
8
|
+
* resolver exists for consumers that also need a convention hook, such as
|
|
9
|
+
* `removeDeclaration`; it never exposes rule-specific binding policy.
|
|
92
10
|
*
|
|
11
|
+
* @throws Error with `ESLINT_SH_DECLARATION_CONVENTION` for malformed bindings or optional hooks.
|
|
93
12
|
* @public
|
|
94
13
|
*/
|
|
95
|
-
export declare function resolveDeclarationConvention(context:
|
|
14
|
+
export declare function resolveDeclarationConvention(context: {
|
|
15
|
+
readonly settings: SettingsConfig;
|
|
16
|
+
}): DeclarationConvention | null;
|
|
@@ -1,35 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Resolves the project-wide declaration policy from shared ESLint settings.
|
|
3
|
+
* Every consuming rule must use the same convention: rule options are cloned by
|
|
4
|
+
* ESLint and cannot carry the convention's methods. No implicit convention is
|
|
5
|
+
* supplied, because declaration policy belongs to the project.
|
|
6
|
+
*
|
|
7
|
+
* ESLINT_SH_DECLARATION_CONVENTION identifies a malformed binding, distinct
|
|
8
|
+
* from a bound hook returning invalid declarations or throwing its own error.
|
|
4
9
|
*/
|
|
10
|
+
import { configurationMessage } from '../configuration-message.js';
|
|
11
|
+
/** The shared setting key that names the project's declaration policy. */
|
|
5
12
|
const DECLARATION_CONVENTION_KEY = 'declarationConvention';
|
|
6
13
|
/** `value` is an object (not `null`) whose properties can be read. */
|
|
7
14
|
function isObjectLike(value) {
|
|
8
15
|
return (typeof value === 'object' || typeof value === 'function') && value !== null;
|
|
9
16
|
}
|
|
10
|
-
/** A short description of a value that is not a convention, for an error message. */
|
|
11
|
-
function describeValue(value) {
|
|
12
|
-
if (value === null)
|
|
13
|
-
return 'null';
|
|
14
|
-
if (Array.isArray(value))
|
|
15
|
-
return 'an array';
|
|
16
|
-
if (isObjectLike(value)) {
|
|
17
|
-
return typeof value.declaredCommands === 'undefined'
|
|
18
|
-
? 'an object with no `declaredCommands` method'
|
|
19
|
-
: `an object whose \`declaredCommands\` is ${typeof value.declaredCommands}, not a function`;
|
|
20
|
-
}
|
|
21
|
-
switch (typeof value) {
|
|
22
|
-
case 'string':
|
|
23
|
-
return `string ${JSON.stringify(value)}`;
|
|
24
|
-
case 'number':
|
|
25
|
-
case 'boolean':
|
|
26
|
-
case 'bigint':
|
|
27
|
-
case 'symbol':
|
|
28
|
-
return `${typeof value} ${String(value)}`;
|
|
29
|
-
default:
|
|
30
|
-
return typeof value;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
17
|
/**
|
|
34
18
|
* Checks that `value`, configured at `setting`, implements
|
|
35
19
|
* {@link DeclarationConvention}: a `declaredCommands` function, and, when a
|
|
@@ -40,64 +24,28 @@ function describeValue(value) {
|
|
|
40
24
|
function assertDeclarationConvention(value, setting) {
|
|
41
25
|
const expected = 'expected a declaration convention: an object with a `declaredCommands(sourceCode)` method';
|
|
42
26
|
if (!isObjectLike(value) || typeof value.declaredCommands !== 'function') {
|
|
43
|
-
throw new Error(
|
|
27
|
+
throw Object.assign(new Error(configurationMessage(setting, expected, value)), {
|
|
28
|
+
code: 'ESLINT_SH_DECLARATION_CONVENTION',
|
|
29
|
+
});
|
|
44
30
|
}
|
|
45
31
|
for (const hook of ['removeDeclaration', 'declarationHint']) {
|
|
46
32
|
if (value[hook] !== undefined && typeof value[hook] !== 'function') {
|
|
47
|
-
throw new Error(
|
|
33
|
+
throw Object.assign(new Error(configurationMessage(`${setting}.${hook}`, 'optional hook must be a function when present', value[hook])), { code: 'ESLINT_SH_DECLARATION_CONVENTION' });
|
|
48
34
|
}
|
|
49
35
|
}
|
|
50
36
|
}
|
|
51
37
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* `settings: { sh: { declarationConvention: convention } }`, else `null`
|
|
55
|
-
* (the rule is inert).
|
|
38
|
+
* Returns the convention bound in `settings.sh.declarationConvention`, or `null`
|
|
39
|
+
* when absent. Present invalid values throw an error naming the shared setting.
|
|
56
40
|
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* `removeDeclaration
|
|
60
|
-
* it was, including the expected convention shape and the received value.
|
|
61
|
-
* Call it from `create()`, so the error surfaces as ESLint's configuration
|
|
62
|
-
* message for the file being linted.
|
|
63
|
-
*
|
|
64
|
-
* A rule that receives `null` should register no listeners: with no
|
|
65
|
-
* convention bound, a convention consumer has nothing to check.
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```ts
|
|
69
|
-
* import {
|
|
70
|
-
* defineShellRule,
|
|
71
|
-
* readDeclaredCommands,
|
|
72
|
-
* resolveDeclarationConvention,
|
|
73
|
-
* } from '@eslint-sh/eslint-plugin';
|
|
74
|
-
*
|
|
75
|
-
* export const maxDeclaredCommands = defineShellRule({
|
|
76
|
-
* meta: { schema: [], messages: { tooMany: 'Declare at most 5 commands.' } },
|
|
77
|
-
* create(context) {
|
|
78
|
-
* const convention = resolveDeclarationConvention(context);
|
|
79
|
-
* if (convention === null) return {};
|
|
80
|
-
* return {
|
|
81
|
-
* File(node) {
|
|
82
|
-
* // Validated declarations for the file `context` is linting.
|
|
83
|
-
* if (readDeclaredCommands(convention, context).length > 5) {
|
|
84
|
-
* context.report({ node, messageId: 'tooMany' });
|
|
85
|
-
* }
|
|
86
|
-
* },
|
|
87
|
-
* };
|
|
88
|
-
* },
|
|
89
|
-
* });
|
|
90
|
-
* ```
|
|
41
|
+
* Use {@link getDeclaredCommands} to read validated declarations. This narrow
|
|
42
|
+
* resolver exists for consumers that also need a convention hook, such as
|
|
43
|
+
* `removeDeclaration`; it never exposes rule-specific binding policy.
|
|
91
44
|
*
|
|
45
|
+
* @throws Error with `ESLINT_SH_DECLARATION_CONVENTION` for malformed bindings or optional hooks.
|
|
92
46
|
* @public
|
|
93
47
|
*/
|
|
94
48
|
export function resolveDeclarationConvention(context) {
|
|
95
|
-
const ruleOptions = context.options[0];
|
|
96
|
-
if (isObjectLike(ruleOptions) && ruleOptions[DECLARATION_CONVENTION_KEY] !== undefined) {
|
|
97
|
-
const fromOption = ruleOptions[DECLARATION_CONVENTION_KEY];
|
|
98
|
-
assertDeclarationConvention(fromOption, `option \`${DECLARATION_CONVENTION_KEY}\` of rule "${context.id}"`);
|
|
99
|
-
return fromOption;
|
|
100
|
-
}
|
|
101
49
|
const shSettings = context.settings.sh;
|
|
102
50
|
if (isObjectLike(shSettings) && shSettings[DECLARATION_CONVENTION_KEY] !== undefined) {
|
|
103
51
|
const fromSettings = shSettings[DECLARATION_CONVENTION_KEY];
|
|
@@ -7,8 +7,7 @@ import type { WrapperSpec } from 'sh-ast/analyze';
|
|
|
7
7
|
export declare function validateWrapperSetting(value: unknown, setting: string): string | WrapperSpec;
|
|
8
8
|
/**
|
|
9
9
|
* Reads `settings.sh.networkWrapper`: the configured name or `WrapperSpec`,
|
|
10
|
-
* or `undefined` when it is not set. Throws
|
|
11
|
-
* `Invalid settings.sh.networkWrapper:` when the value is malformed. Call it
|
|
10
|
+
* or `undefined` when it is not set. Throws `Error` with `ESLINT_SH_WRAPPER_SETTING` and the offending path when the value is malformed. Call it
|
|
12
11
|
* from `create()`, so the error surfaces as ESLint's configuration message.
|
|
13
12
|
*
|
|
14
13
|
* @internal
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Reads the shared network policy and validates JSON-expressible wrapper specs.
|
|
3
3
|
* A network wrapper also supplies a transparent-wrapper fact to every consumer.
|
|
4
4
|
*/
|
|
5
|
+
import { configurationMessage } from '../configuration-message.js';
|
|
5
6
|
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-ast/analyze';
|
|
6
7
|
/** The setting's dotted path, as error messages name it. */
|
|
7
8
|
const SETTING = 'settings.sh.networkWrapper';
|
|
@@ -30,48 +31,51 @@ function isStringList(value) {
|
|
|
30
31
|
*/
|
|
31
32
|
export function validateWrapperSetting(value, setting) {
|
|
32
33
|
/** Names the configuration binding instead of exposing an analysis failure. */
|
|
33
|
-
function invalid(detail) {
|
|
34
|
-
|
|
34
|
+
function invalid(detail, field) {
|
|
35
|
+
// The setting owns malformed JSON specs; analysis errors keep their own identity.
|
|
36
|
+
const where = field === undefined ? setting : `${setting}.${field}`;
|
|
37
|
+
const received = field === undefined ? value : value[field];
|
|
38
|
+
throw Object.assign(new Error(configurationMessage(where, detail, received)), {
|
|
39
|
+
code: 'ESLINT_SH_WRAPPER_SETTING',
|
|
40
|
+
});
|
|
35
41
|
}
|
|
36
42
|
if (typeof value === 'string') {
|
|
37
43
|
if (value.length === 0)
|
|
38
|
-
invalid('expected a command name or a wrapper spec
|
|
44
|
+
invalid('expected a command name or a wrapper spec');
|
|
39
45
|
return value;
|
|
40
46
|
}
|
|
41
47
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
42
|
-
|
|
43
|
-
invalid(`expected a command name or a wrapper spec ({ names: [...] }), but got ${got}.`);
|
|
48
|
+
invalid('expected a command name or a wrapper spec ({ names: [...] })');
|
|
44
49
|
}
|
|
45
50
|
const spec = value;
|
|
46
51
|
for (const key of Object.keys(spec)) {
|
|
47
52
|
if (!KNOWN_FIELDS.has(key))
|
|
48
|
-
invalid(
|
|
53
|
+
invalid('unknown wrapper spec field', key);
|
|
49
54
|
}
|
|
50
55
|
if (!isStringList(spec.names) || spec.names.length === 0) {
|
|
51
|
-
invalid('
|
|
56
|
+
invalid('must be a non-empty array of non-empty strings', 'names');
|
|
52
57
|
}
|
|
53
58
|
for (const field of STRING_LIST_FIELDS) {
|
|
54
59
|
if (spec[field] !== undefined && !isStringList(spec[field])) {
|
|
55
|
-
invalid(
|
|
60
|
+
invalid('must be an array of non-empty strings', field);
|
|
56
61
|
}
|
|
57
62
|
}
|
|
58
63
|
if (spec.shellBuiltin !== undefined && typeof spec.shellBuiltin !== 'boolean') {
|
|
59
|
-
invalid('
|
|
64
|
+
invalid('must be a boolean', 'shellBuiltin');
|
|
60
65
|
}
|
|
61
66
|
const positional = spec.positionalOperandsBeforeCommand;
|
|
62
67
|
if (positional !== undefined && !(Number.isInteger(positional) && positional >= 0)) {
|
|
63
|
-
invalid('
|
|
68
|
+
invalid('must be a non-negative integer', 'positionalOperandsBeforeCommand');
|
|
64
69
|
}
|
|
65
70
|
const assignment = spec.assignmentOperands;
|
|
66
71
|
if (assignment !== undefined && !ASSIGNMENT_OPERANDS.includes(assignment)) {
|
|
67
|
-
invalid(
|
|
72
|
+
invalid(`must be one of ${ASSIGNMENT_OPERANDS.join(', ')}`, 'assignmentOperands');
|
|
68
73
|
}
|
|
69
74
|
return spec;
|
|
70
75
|
}
|
|
71
76
|
/**
|
|
72
77
|
* Reads `settings.sh.networkWrapper`: the configured name or `WrapperSpec`,
|
|
73
|
-
* or `undefined` when it is not set. Throws
|
|
74
|
-
* `Invalid settings.sh.networkWrapper:` when the value is malformed. Call it
|
|
78
|
+
* or `undefined` when it is not set. Throws `Error` with `ESLINT_SH_WRAPPER_SETTING` and the offending path when the value is malformed. Call it
|
|
75
79
|
* from `create()`, so the error surfaces as ESLint's configuration message.
|
|
76
80
|
*
|
|
77
81
|
* @internal
|
|
@@ -102,7 +106,7 @@ export function networkWrapperSpec(settings) {
|
|
|
102
106
|
});
|
|
103
107
|
}
|
|
104
108
|
/**
|
|
105
|
-
* Canonical effective grammar from sh-ast
|
|
109
|
+
* Canonical effective grammar from sh-ast's WrapperSpec matcher, excluding
|
|
106
110
|
* the names that select it. Flags are membership sets; omitted lists are empty,
|
|
107
111
|
* shellBuiltin defaults to false (utility matching), and positional count
|
|
108
112
|
* defaults to zero. An explicit assignment grammar supersedes the legacy
|
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Resolves project wrapper facts once per consuming rule. Shared facts precede
|
|
3
|
-
* the rule's built-in table, so an explicit option shape replaces the default
|
|
4
|
-
* for that name. Rules retain their distinct built-in scopes, but never their
|
|
5
|
-
* own interpretation of a configured project wrapper.
|
|
6
|
-
*/
|
|
7
1
|
import type { SettingsConfig } from '@eslint/core';
|
|
8
2
|
import type { WrapperSpec } from 'sh-ast/analyze';
|
|
9
3
|
import type { ShellSourceCode } from '../language/shell-source-code.js';
|
|
@@ -37,7 +31,8 @@ export interface TransparentWrappersBindingContext {
|
|
|
37
31
|
*
|
|
38
32
|
* @param context - The consuming rule's context.
|
|
39
33
|
* @param builtIns - The rule's own built-in wrapper scope.
|
|
40
|
-
* @throws Error
|
|
34
|
+
* @throws Error with `ESLINT_SH_WRAPPER_SETTING` for malformed bindings, or
|
|
35
|
+
* `ESLINT_SH_WRAPPER_CONFLICT` for duplicate names or contradictory shared policies.
|
|
41
36
|
* @public
|
|
42
37
|
*/
|
|
43
38
|
export declare function resolveTransparentWrappers(context: TransparentWrappersBindingContext, builtIns?: readonly WrapperSpec[]): readonly WrapperSpec[];
|
|
@@ -1,7 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves project wrapper facts once per consuming rule. Shared facts precede
|
|
3
|
+
* the rule's built-in table, so an explicit option shape replaces the default
|
|
4
|
+
* for that name. Rules retain their distinct built-in scopes, but never their
|
|
5
|
+
* own interpretation of a configured project wrapper.
|
|
6
|
+
*
|
|
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
|
+
*/
|
|
9
|
+
import { configurationMessage } from '../configuration-message.js';
|
|
1
10
|
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-ast/analyze';
|
|
2
11
|
import { networkWrapperSpec, sameWrapperOptions, validateWrapperSetting, } from './resolve-network-wrapper.js';
|
|
3
|
-
/** The
|
|
4
|
-
const SETTING = 'settings
|
|
12
|
+
/** The user-written setting path, extended with indices and fields in configuration errors. */
|
|
13
|
+
const SETTING = 'settings.sh.transparentWrappers';
|
|
5
14
|
/** A name-only entry preserves a known utility's default flag handling. */
|
|
6
15
|
function asSpec(value) {
|
|
7
16
|
if (typeof value !== 'string') {
|
|
@@ -32,7 +41,8 @@ function asSpec(value) {
|
|
|
32
41
|
*
|
|
33
42
|
* @param context - The consuming rule's context.
|
|
34
43
|
* @param builtIns - The rule's own built-in wrapper scope.
|
|
35
|
-
* @throws Error
|
|
44
|
+
* @throws Error with `ESLINT_SH_WRAPPER_SETTING` for malformed bindings, or
|
|
45
|
+
* `ESLINT_SH_WRAPPER_CONFLICT` for duplicate names or contradictory shared policies.
|
|
36
46
|
* @public
|
|
37
47
|
*/
|
|
38
48
|
export function resolveTransparentWrappers(context, builtIns = DEFAULT_TRANSPARENT_WRAPPERS) {
|
|
@@ -41,24 +51,24 @@ export function resolveTransparentWrappers(context, builtIns = DEFAULT_TRANSPARE
|
|
|
41
51
|
? sh.transparentWrappers
|
|
42
52
|
: undefined;
|
|
43
53
|
if (value !== undefined && !Array.isArray(value)) {
|
|
44
|
-
throw new Error(
|
|
54
|
+
throw Object.assign(new Error(configurationMessage(SETTING, 'expected an array of command names or wrapper specs', value)), { code: 'ESLINT_SH_WRAPPER_SETTING' });
|
|
45
55
|
}
|
|
46
|
-
const configured = (value ?? []).map((entry) => asSpec(validateWrapperSetting(entry, SETTING)));
|
|
56
|
+
const configured = (value ?? []).map((entry, index) => asSpec(validateWrapperSetting(entry, `${SETTING}[${String(index)}]`)));
|
|
47
57
|
const names = new Set();
|
|
48
|
-
for (const spec of configured) {
|
|
58
|
+
for (const [index, spec] of configured.entries()) {
|
|
49
59
|
for (const name of spec.names) {
|
|
50
60
|
if (names.has(name)) {
|
|
51
|
-
throw new Error(
|
|
61
|
+
throw Object.assign(new Error(configurationMessage(`${SETTING}[${String(index)}]`, `duplicate wrapper name ${JSON.stringify(name)}`, value?.[index])), { code: 'ESLINT_SH_WRAPPER_CONFLICT' });
|
|
52
62
|
}
|
|
53
63
|
names.add(name);
|
|
54
64
|
}
|
|
55
65
|
}
|
|
56
66
|
const network = networkWrapperSpec(context.settings);
|
|
57
|
-
for (const spec of configured) {
|
|
67
|
+
for (const [index, spec] of configured.entries()) {
|
|
58
68
|
if (network !== null &&
|
|
59
69
|
spec.names.some((name) => network.names.includes(name)) &&
|
|
60
70
|
!sameWrapperOptions(spec, network)) {
|
|
61
|
-
throw new Error(
|
|
71
|
+
throw Object.assign(new Error(configurationMessage(`${SETTING}[${String(index)}]`, 'wrapper spec conflicts with settings.sh.networkWrapper', value?.[index])), { code: 'ESLINT_SH_WRAPPER_CONFLICT' });
|
|
62
72
|
}
|
|
63
73
|
}
|
|
64
74
|
// Precedence applies to names, including their path-matching semantics.
|
|
@@ -90,7 +90,7 @@ export declare function applySetOptionChanges(enabled: Set<string>, changes: rea
|
|
|
90
90
|
export declare function formatSetCommand(options: readonly string[]): string;
|
|
91
91
|
/**
|
|
92
92
|
* The option changes the shebang line passes to the shell (read by
|
|
93
|
-
*
|
|
93
|
+
* the shared prologue definition and by
|
|
94
94
|
* `compat/no-nounset-empty-array`). Only spellings the kernel hands to the shell as written
|
|
95
95
|
* are read:
|
|
96
96
|
*
|
|
@@ -159,7 +159,7 @@ export function formatSetCommand(options) {
|
|
|
159
159
|
}
|
|
160
160
|
/**
|
|
161
161
|
* The option changes the shebang line passes to the shell (read by
|
|
162
|
-
*
|
|
162
|
+
* the shared prologue definition and by
|
|
163
163
|
* `compat/no-nounset-empty-array`). Only spellings the kernel hands to the shell as written
|
|
164
164
|
* are read:
|
|
165
165
|
*
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { SettingsConfig } from '@eslint/core';
|
|
2
|
+
import type { ShellFile, ShellNodeOf } from '../language/syntax-view.js';
|
|
3
|
+
import type { ShellSourceCode } from '../language/shell-source-code.js';
|
|
4
|
+
import type { SetOptionChange } from './set-options.js';
|
|
5
|
+
/** The normalized project definition, independent of any required option list. @internal */
|
|
6
|
+
export interface StrictModePrologue {
|
|
7
|
+
/** Whether interpreter flags count, despite being bypassed by `bash script.sh`. */
|
|
8
|
+
readonly allowShebangOnly: boolean;
|
|
9
|
+
/** Positive number of top-level statements whose final option state counts. */
|
|
10
|
+
readonly withinStatements: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Normalizes the shared project fact at rule creation, following the other
|
|
14
|
+
* shared-settings resolvers: absence uses defaults, malformed explicit values
|
|
15
|
+
* throw a named configuration error. No cache can leak a file's configuration.
|
|
16
|
+
* @throws Error with `ESLINT_SH_STRICT_MODE_SETTING` for malformed shared project facts.
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export declare function resolveStrictModePrologue(settings: SettingsConfig): StrictModePrologue;
|
|
20
|
+
/**
|
|
21
|
+
* Whether `stmt` is a plain, foreground, top-level `set` command, and if so
|
|
22
|
+
* the option changes its arguments make (read through `sourceCode`, so a
|
|
23
|
+
* non-static argument ends the reading).
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export declare function setStatementChanges(stmt: ShellNodeOf<'Stmt'>, sourceCode: ShellSourceCode): readonly SetOptionChange[];
|
|
28
|
+
/**
|
|
29
|
+
* The options enabled after applying `seed`, then each statement's `set`
|
|
30
|
+
* changes in order.
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
export declare function enabledAfter(seed: readonly SetOptionChange[], statementChanges: readonly (readonly SetOptionChange[])[]): Set<string>;
|
|
34
|
+
/**
|
|
35
|
+
* The options a file's prologue leaves enabled: the state after the first
|
|
36
|
+
* `withinStatements` top-level statements of `file`, plus the shebang's
|
|
37
|
+
* options when `allowShebangOnly` is set. This is the single definition of
|
|
38
|
+
* "what the prologue enables", shared with other rules (such as
|
|
39
|
+
* `no-unchecked-cd`) that need to know whether strict mode is in force.
|
|
40
|
+
*
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
export declare function prologueEnabledOptions(file: ShellFile, sourceCode: ShellSourceCode, config: StrictModePrologue): ReadonlySet<string>;
|