@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
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { ConfigCommentParser } from '@eslint/plugin-kit';
|
|
3
|
+
import { readPackageVersion } from '../version.js';
|
|
4
|
+
const name = 'require-disable-justification';
|
|
5
|
+
const ruleId = `sh/${name}`;
|
|
6
|
+
const version = readPackageVersion();
|
|
7
|
+
const commentParser = new ConfigCommentParser();
|
|
8
|
+
const hasReasonText = /[^\s\p{White_Space}]/u;
|
|
9
|
+
/**
|
|
10
|
+
* ESLint calls preprocess, constructs the source code for the unchanged block,
|
|
11
|
+
* and calls postprocess synchronously. The per-file stack lets nested lint
|
|
12
|
+
* calls collect their own findings. Pending checks retain diagnostics only.
|
|
13
|
+
*/
|
|
14
|
+
const pending = new Map();
|
|
15
|
+
function removePending(filename, check) {
|
|
16
|
+
const checks = pending.get(filename);
|
|
17
|
+
if (!checks)
|
|
18
|
+
return;
|
|
19
|
+
const index = checks.lastIndexOf(check);
|
|
20
|
+
if (index !== -1)
|
|
21
|
+
checks.splice(index, 1);
|
|
22
|
+
if (checks.length === 0)
|
|
23
|
+
pending.delete(filename);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* An empty directive list disables every selected rule. Named directives use
|
|
27
|
+
* the same comma and quote handling as ESLint's suppression pipeline.
|
|
28
|
+
*/
|
|
29
|
+
function matchesDirective(value, rules) {
|
|
30
|
+
if (rules === undefined)
|
|
31
|
+
return true;
|
|
32
|
+
const ids = Object.keys(commentParser.parseListConfig(value));
|
|
33
|
+
return (ids.length === 0 ||
|
|
34
|
+
rules.some((rule) => rule.endsWith('*') ? ids.some((id) => id.startsWith(rule.slice(0, -1))) : ids.includes(rule)));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Uses the actual linted source and its language options. Findings are held
|
|
38
|
+
* outside ESLint's rule pass and appended after suppression and unused-directive
|
|
39
|
+
* accounting. The comment location is independent of the unused fix range.
|
|
40
|
+
*
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
export function collectDisableJustificationFindings(filename, sourceCode) {
|
|
44
|
+
const check = pending.get(filename)?.at(-1);
|
|
45
|
+
if (!check || check.findings)
|
|
46
|
+
return;
|
|
47
|
+
const findings = [];
|
|
48
|
+
if (check.rules?.length !== 0) {
|
|
49
|
+
for (const directive of sourceCode.getDisableDirectives().directives) {
|
|
50
|
+
if (directive.type === 'enable' || hasReasonText.test(directive.justification ?? ''))
|
|
51
|
+
continue;
|
|
52
|
+
if (!matchesDirective(directive.value, check.rules))
|
|
53
|
+
continue;
|
|
54
|
+
// ShellSourceCode builds each directive from a projected shell comment.
|
|
55
|
+
const { start, end } = sourceCode.getLoc(directive.node);
|
|
56
|
+
findings.push({
|
|
57
|
+
ruleId,
|
|
58
|
+
message: 'Explain this disable directive with a justification after "--".',
|
|
59
|
+
severity: 2,
|
|
60
|
+
line: start.line,
|
|
61
|
+
column: start.column,
|
|
62
|
+
endLine: end.line,
|
|
63
|
+
endColumn: end.column,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
check.findings = findings;
|
|
68
|
+
}
|
|
69
|
+
/** Ensures a malformed factory option cannot silently skip the directive check. */
|
|
70
|
+
function isRuleSelection(value) {
|
|
71
|
+
return (Array.isArray(value) &&
|
|
72
|
+
Array.from(value).every((rule) => typeof rule === 'string' && rule.trim() !== ''));
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Creates a shell processor that requires reasons on inline disable directives.
|
|
76
|
+
* Use with `language: 'sh/sh'`. Omit `rules` to check all directives, or
|
|
77
|
+
* supply exact ESLint rule IDs or literal prefixes ending in `*`.
|
|
78
|
+
*
|
|
79
|
+
* @public
|
|
80
|
+
*/
|
|
81
|
+
export function createDisableJustificationProcessor(options = {}) {
|
|
82
|
+
const { rules: configured } = options;
|
|
83
|
+
if (configured !== undefined && !isRuleSelection(configured)) {
|
|
84
|
+
throw new TypeError('createDisableJustificationProcessor: rules must be an array of nonempty strings.');
|
|
85
|
+
}
|
|
86
|
+
const rules = configured === undefined ? undefined : [...configured];
|
|
87
|
+
// ESLint's config cache serializes processor metadata rather than closures.
|
|
88
|
+
const fingerprint = rules === undefined
|
|
89
|
+
? ''
|
|
90
|
+
: '/' + createHash('sha256').update(JSON.stringify(rules)).digest('hex');
|
|
91
|
+
const processor = {
|
|
92
|
+
meta: { name: `eslint-sh/${name}${fingerprint}`, version },
|
|
93
|
+
supportsAutofix: true,
|
|
94
|
+
preprocess(text, filename) {
|
|
95
|
+
const check = { processor, rules };
|
|
96
|
+
const checks = pending.get(filename) ?? [];
|
|
97
|
+
checks.push(check);
|
|
98
|
+
pending.set(filename, checks);
|
|
99
|
+
// A thrown lint aborts before postprocess. Release the orphaned frame
|
|
100
|
+
// once the synchronous lint stack unwinds.
|
|
101
|
+
queueMicrotask(() => {
|
|
102
|
+
removePending(filename, check);
|
|
103
|
+
});
|
|
104
|
+
return [text];
|
|
105
|
+
},
|
|
106
|
+
postprocess(groups, filename) {
|
|
107
|
+
const check = pending
|
|
108
|
+
.get(filename)
|
|
109
|
+
?.slice()
|
|
110
|
+
.reverse()
|
|
111
|
+
.find((entry) => entry.processor === processor);
|
|
112
|
+
if (check)
|
|
113
|
+
removePending(filename, check);
|
|
114
|
+
const messages = groups.flat();
|
|
115
|
+
if (!check?.findings && messages.every((message) => !message.fatal)) {
|
|
116
|
+
throw new TypeError(`${ruleId} requires the eslint-sh shell language.`);
|
|
117
|
+
}
|
|
118
|
+
if (!check?.findings?.length)
|
|
119
|
+
return messages;
|
|
120
|
+
return [...messages, ...check.findings].sort((a, b) => a.line - b.line || a.column - b.column);
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
return processor;
|
|
124
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a resolved destination can name a Bash variable or array member:
|
|
3
|
+
* a name, or a name followed by a non-empty subscript whose closing bracket
|
|
4
|
+
* ends the text. Bash rejects anything else, such as `a[1][2]` or `a["1]`, as
|
|
5
|
+
* "not a valid identifier" before the builtin does anything else. A
|
|
6
|
+
* subscript holding a command substitution or `${` is assumed to be valid
|
|
7
|
+
* when the text ends with `]`.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export declare function isBashBuiltinDestination(text: string): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Whether `prefix`, the known start of an expanded destination, can begin a
|
|
14
|
+
* name or an array member that the rest of the text completes: a name, or a
|
|
15
|
+
* name with a subscript that the prefix does not already close.
|
|
16
|
+
*
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export declare function canStartBashBuiltinDestination(prefix: string): boolean;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the subscript opened at `open` closes, following Bash's
|
|
3
|
+
* `skip_matched_pair` as `valid_array_reference` uses it: nested brackets
|
|
4
|
+
* count, a backslash escapes the next character, and single- and
|
|
5
|
+
* double-quoted strings are skipped. `null` means the subscript never
|
|
6
|
+
* closes. `undefined` means it holds a backquote, `$(` or `${`, whose extent
|
|
7
|
+
* Bash finds by parsing a command or expansion, which is not modeled here.
|
|
8
|
+
*/
|
|
9
|
+
function subscriptClose(text, open) {
|
|
10
|
+
let depth = 0;
|
|
11
|
+
for (let index = open; index < text.length; index++) {
|
|
12
|
+
const char = text[index];
|
|
13
|
+
const next = text[index + 1];
|
|
14
|
+
if (char === '`' || (char === '$' && (next === '(' || next === '{')))
|
|
15
|
+
return undefined;
|
|
16
|
+
if (char === '\\') {
|
|
17
|
+
index++;
|
|
18
|
+
}
|
|
19
|
+
else if (char === '[') {
|
|
20
|
+
depth++;
|
|
21
|
+
}
|
|
22
|
+
else if (char === ']') {
|
|
23
|
+
depth--;
|
|
24
|
+
if (depth === 0)
|
|
25
|
+
return index;
|
|
26
|
+
}
|
|
27
|
+
else if (char === "'") {
|
|
28
|
+
index = text.indexOf("'", index + 1);
|
|
29
|
+
if (index === -1)
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
else if (char === '"') {
|
|
33
|
+
for (index++; index < text.length && text[index] !== '"'; index++) {
|
|
34
|
+
const quoted = text[index];
|
|
35
|
+
if (quoted === '`' || (quoted === '$' && /[({]/u.test(text[index + 1] ?? ''))) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
if (quoted === '\\')
|
|
39
|
+
index++;
|
|
40
|
+
}
|
|
41
|
+
if (index >= text.length)
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Whether a resolved destination can name a Bash variable or array member:
|
|
49
|
+
* a name, or a name followed by a non-empty subscript whose closing bracket
|
|
50
|
+
* ends the text. Bash rejects anything else, such as `a[1][2]` or `a["1]`, as
|
|
51
|
+
* "not a valid identifier" before the builtin does anything else. A
|
|
52
|
+
* subscript holding a command substitution or `${` is assumed to be valid
|
|
53
|
+
* when the text ends with `]`.
|
|
54
|
+
*
|
|
55
|
+
* @internal
|
|
56
|
+
*/
|
|
57
|
+
export function isBashBuiltinDestination(text) {
|
|
58
|
+
const name = /^[A-Za-z_][A-Za-z0-9_]*/u.exec(text)?.[0];
|
|
59
|
+
if (name === undefined)
|
|
60
|
+
return false;
|
|
61
|
+
if (name.length === text.length)
|
|
62
|
+
return true;
|
|
63
|
+
if (text[name.length] !== '[' || text[name.length + 1] === ']' || !text.endsWith(']')) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
const close = subscriptClose(text, name.length);
|
|
67
|
+
return close === undefined || close === text.length - 1;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Whether `prefix`, the known start of an expanded destination, can begin a
|
|
71
|
+
* name or an array member that the rest of the text completes: a name, or a
|
|
72
|
+
* name with a subscript that the prefix does not already close.
|
|
73
|
+
*
|
|
74
|
+
* @internal
|
|
75
|
+
*/
|
|
76
|
+
export function canStartBashBuiltinDestination(prefix) {
|
|
77
|
+
const name = /^[A-Za-z_][A-Za-z0-9_]*/u.exec(prefix)?.[0];
|
|
78
|
+
if (name === undefined)
|
|
79
|
+
return prefix === '';
|
|
80
|
+
if (name.length === prefix.length)
|
|
81
|
+
return true;
|
|
82
|
+
return prefix[name.length] === '[' && typeof subscriptClose(prefix, name.length) !== 'number';
|
|
83
|
+
}
|
|
@@ -10,10 +10,10 @@ import type { ShellRuleVisitor } from '../define-shell-rule.js';
|
|
|
10
10
|
*/
|
|
11
11
|
export declare const DECLARATION_AND_LOCAL_COMMANDS: ReadonlySet<string>;
|
|
12
12
|
/**
|
|
13
|
-
* Builds the listeners that call `report(
|
|
13
|
+
* Builds the listeners that call `report(node, command, options, text)` for each
|
|
14
14
|
* `declare` / `typeset` (or each of `commands`) whose option cluster contains
|
|
15
15
|
* `flag`: one letter, or a character class such as `[lu]` for either of two.
|
|
16
16
|
*
|
|
17
17
|
* @internal
|
|
18
18
|
*/
|
|
19
|
-
export declare function declarationFlagVisitor(context: TransparentWrappersBindingContext, flag: string, report: (
|
|
19
|
+
export declare function declarationFlagVisitor(context: TransparentWrappersBindingContext, flag: string, report: (node: ShellNode, command: string, options: readonly string[], text: string) => void, commands?: ReadonlySet<string>, reportEveryOption?: boolean): ShellRuleVisitor;
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
* gating an option `local` shares (`-n`, `-l`, `-u`) adds `local` with
|
|
14
14
|
* {@link DECLARATION_AND_LOCAL_COMMANDS}.
|
|
15
15
|
*/
|
|
16
|
+
import { resolveWordTextAtPosition } from '../../language/word-resolution.js';
|
|
16
17
|
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
17
18
|
import { resolveTransparentWrappers } from '../resolve-transparent-wrappers.js';
|
|
18
19
|
import { BUILTIN_WRAPPER } from '../transparent-wrappers.js';
|
|
@@ -40,26 +41,25 @@ const TRANSPARENT_WRAPPERS = [
|
|
|
40
41
|
BUILTIN_WRAPPER,
|
|
41
42
|
];
|
|
42
43
|
/**
|
|
43
|
-
* Finds
|
|
44
|
-
* arguments
|
|
45
|
-
* command's option words, or `undefined` when no option word carries it.
|
|
44
|
+
* Finds every word carrying the flag among a declaration command's
|
|
45
|
+
* arguments. Each match shares the list of the command's option words.
|
|
46
46
|
* Option processing stops at the first operand, at `--`, and at a word that
|
|
47
47
|
* is not static, because what follows it may not be an option.
|
|
48
48
|
*/
|
|
49
|
-
function
|
|
50
|
-
|
|
49
|
+
function findFlagOptions(sourceCode, flagPattern, args) {
|
|
50
|
+
const matches = [];
|
|
51
51
|
const options = [];
|
|
52
52
|
for (const { report: node, word } of args) {
|
|
53
53
|
if (word === undefined)
|
|
54
54
|
break;
|
|
55
|
-
const text = sourceCode
|
|
55
|
+
const text = resolveWordTextAtPosition(sourceCode, word);
|
|
56
56
|
if (text === null || text === '--' || !/^[-+]./u.test(text))
|
|
57
57
|
break;
|
|
58
58
|
options.push(text);
|
|
59
|
-
if (
|
|
60
|
-
report
|
|
59
|
+
if (flagPattern.test(text))
|
|
60
|
+
matches.push({ report: node, text });
|
|
61
61
|
}
|
|
62
|
-
return
|
|
62
|
+
return matches.map((match) => ({ ...match, options }));
|
|
63
63
|
}
|
|
64
64
|
/**
|
|
65
65
|
* The arguments of a `DeclClause` as candidates. An option such as `-A` is an
|
|
@@ -72,25 +72,33 @@ function declClauseArgs(node) {
|
|
|
72
72
|
return { report: word ?? assign, word };
|
|
73
73
|
});
|
|
74
74
|
}
|
|
75
|
+
/** Reports the selected matching option words of one command. */
|
|
76
|
+
function optionReporter(sourceCode, flagPattern, report, reportEveryOption) {
|
|
77
|
+
return (command, args) => {
|
|
78
|
+
const matches = findFlagOptions(sourceCode, flagPattern, args);
|
|
79
|
+
for (const option of reportEveryOption ? matches : matches.slice(0, 1)) {
|
|
80
|
+
report(option.report, command, option.options, option.text);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
75
84
|
/**
|
|
76
|
-
* Builds the listeners that call `report(
|
|
85
|
+
* Builds the listeners that call `report(node, command, options, text)` for each
|
|
77
86
|
* `declare` / `typeset` (or each of `commands`) whose option cluster contains
|
|
78
87
|
* `flag`: one letter, or a character class such as `[lu]` for either of two.
|
|
79
88
|
*
|
|
80
89
|
* @internal
|
|
81
90
|
*/
|
|
82
|
-
export function declarationFlagVisitor(context, flag, report, commands = DECLARATION_COMMANDS) {
|
|
91
|
+
export function declarationFlagVisitor(context, flag, report, commands = DECLARATION_COMMANDS, reportEveryOption = false) {
|
|
83
92
|
const { sourceCode } = context;
|
|
84
93
|
const transparentWrappers = resolveTransparentWrappers(context, TRANSPARENT_WRAPPERS);
|
|
85
94
|
const flagPattern = new RegExp(`^-[a-zA-Z]*${flag}[a-zA-Z]*$`, 'u');
|
|
95
|
+
const reportOptions = optionReporter(sourceCode, flagPattern, report, reportEveryOption);
|
|
86
96
|
return {
|
|
87
97
|
DeclClause(node) {
|
|
88
98
|
const command = node.variant?.value;
|
|
89
99
|
if (command === undefined || !commands.has(command))
|
|
90
100
|
return;
|
|
91
|
-
|
|
92
|
-
if (option !== undefined)
|
|
93
|
-
report(option.report, command, option.options);
|
|
101
|
+
reportOptions(command, declClauseArgs(node));
|
|
94
102
|
},
|
|
95
103
|
CallExpr(node) {
|
|
96
104
|
const invoked = sourceCode.resolveInvokedCommand(node, {
|
|
@@ -105,9 +113,7 @@ export function declarationFlagVisitor(context, flag, report, commands = DECLARA
|
|
|
105
113
|
const operands = node.args
|
|
106
114
|
.slice((invoked.chainIndices.at(-1) ?? 0) + 1)
|
|
107
115
|
.map((word) => ({ report: word, word }));
|
|
108
|
-
|
|
109
|
-
if (option !== undefined)
|
|
110
|
-
report(option.report, command, option.options);
|
|
116
|
+
reportOptions(command, operands);
|
|
111
117
|
},
|
|
112
118
|
};
|
|
113
119
|
}
|
|
@@ -41,9 +41,7 @@ export const noCaseAttributes = createCompatRule({
|
|
|
41
41
|
},
|
|
42
42
|
},
|
|
43
43
|
create(context, gate) {
|
|
44
|
-
|
|
45
|
-
return declarationFlagVisitor(context, '[lu]', (node, command) => {
|
|
46
|
-
const option = (node.type === 'Word' ? sourceCode.resolveWordText(node) : null) ?? '';
|
|
44
|
+
return declarationFlagVisitor(context, '[lu]', (node, command, _options, option) => {
|
|
47
45
|
if (option.includes('l') && option.includes('u')) {
|
|
48
46
|
context.report({
|
|
49
47
|
node,
|
|
@@ -58,6 +56,6 @@ export const noCaseAttributes = createCompatRule({
|
|
|
58
56
|
messageId: 'caseAttribute',
|
|
59
57
|
data: { ...gate, command, flag, tr: TR_FOR[flag] },
|
|
60
58
|
});
|
|
61
|
-
}, DECLARATION_AND_LOCAL_COMMANDS);
|
|
59
|
+
}, DECLARATION_AND_LOCAL_COMMANDS, true);
|
|
62
60
|
},
|
|
63
61
|
});
|
|
@@ -9,32 +9,36 @@
|
|
|
9
9
|
* transparent wrappers (`command mapfile …`, `builtin readarray …`), through
|
|
10
10
|
* `ShellSourceCode.resolveInvokedCommand`.
|
|
11
11
|
*
|
|
12
|
-
* The suggestion
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* variable, so this is the narrowest side effect a read loop can have; the
|
|
22
|
-
* alternatives (a named loop variable, or saving and restoring `REPLY` in
|
|
23
|
-
* another variable) overwrite a name the script is likelier to use. The
|
|
24
|
-
* suggestion message states the tradeoff. The brace group keeps the command a single
|
|
25
|
-
* unit, so a redirection (`< file`) or pipeline (`cmd | mapfile -t a`) still
|
|
26
|
-
* feeds the loop. Other options (`-d`, `-n`, `-O`, `-s`, `-u`, `-C`, or no
|
|
27
|
-
* `-t`, which keeps each newline) and POSIX files, which have no arrays at
|
|
28
|
-
* all, get the report without a suggestion.
|
|
12
|
+
* The suggestion replaces a direct `mapfile -t` with a read loop when a
|
|
13
|
+
* trailing here-string supplies stdin. Here-strings provide shell text, while
|
|
14
|
+
* files, pipes and inherited stdin may contain NUL bytes that mapfile and read
|
|
15
|
+
* handle differently. The loop intentionally sets `REPLY` to the empty string.
|
|
16
|
+
* It is withheld when the file may give `REPLY` attributes: a `declare`,
|
|
17
|
+
* `typeset`, `local` or `readonly` naming `REPLY`, or one with an operand that
|
|
18
|
+
* is not static. An integer `REPLY` reads as `0` after the last line, so the
|
|
19
|
+
* loop never ends, and other attributes (case conversion, `readonly`, a
|
|
20
|
+
* nameref) change what the loop stores.
|
|
29
21
|
*/
|
|
22
|
+
import { resolveWordTextAtPosition } from '../../language/word-resolution.js';
|
|
23
|
+
import { WordHdoc } from 'sh-ast';
|
|
30
24
|
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
31
25
|
import { resolveTransparentWrappers } from '../resolve-transparent-wrappers.js';
|
|
26
|
+
import { someShellNode } from '../../language/syntax-view.js';
|
|
32
27
|
import { BUILTIN_WRAPPER } from '../transparent-wrappers.js';
|
|
33
28
|
import { createCompatRule } from './create-compat-rule.js';
|
|
34
29
|
/** The builtin's two names. */
|
|
35
30
|
const MAPFILE_COMMANDS = new Set(['mapfile', 'readarray']);
|
|
36
31
|
/** The array `mapfile` fills when no name is given. */
|
|
37
32
|
const DEFAULT_ARRAY = 'MAPFILE';
|
|
33
|
+
/** The commands that can give a variable attributes. */
|
|
34
|
+
const DECLARATION_COMMANDS = new Set([
|
|
35
|
+
'declare',
|
|
36
|
+
'typeset',
|
|
37
|
+
'local',
|
|
38
|
+
'readonly',
|
|
39
|
+
]);
|
|
40
|
+
/** A declaration operand that names `REPLY`, with or without a value or subscript. */
|
|
41
|
+
const REPLY_OPERAND = /^REPLY(?:$|[=+[])/u;
|
|
38
42
|
/** A shell variable name, the only operand shape the suggestion rewrites. */
|
|
39
43
|
const IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/u;
|
|
40
44
|
/**
|
|
@@ -64,11 +68,19 @@ function rewritableArray(sourceCode, node) {
|
|
|
64
68
|
return undefined;
|
|
65
69
|
if (node.assigns.length > 0)
|
|
66
70
|
return undefined;
|
|
67
|
-
const
|
|
71
|
+
const stmt = sourceCode.getParent(node);
|
|
72
|
+
if (stmt?.type !== 'Stmt' || stmt.redirs.length !== 1)
|
|
73
|
+
return undefined;
|
|
74
|
+
const [input] = stmt.redirs;
|
|
75
|
+
if (input.op !== WordHdoc ||
|
|
76
|
+
(input.n !== undefined && !/^0+$/u.test(input.n.value)) ||
|
|
77
|
+
input.range[0] < node.range[1])
|
|
78
|
+
return undefined;
|
|
79
|
+
const words = node.args.slice(1).map((word) => resolveWordTextAtPosition(sourceCode, word));
|
|
68
80
|
if (words.length === 0 || words.length > 2 || words[0] !== '-t')
|
|
69
81
|
return undefined;
|
|
70
82
|
const name = words.length === 2 ? words[1] : DEFAULT_ARRAY;
|
|
71
|
-
return name !== null && IDENTIFIER.test(name) ? name : undefined;
|
|
83
|
+
return name !== null && name !== 'REPLY' && IDENTIFIER.test(name) ? name : undefined;
|
|
72
84
|
}
|
|
73
85
|
/**
|
|
74
86
|
* The `sh/compat/no-mapfile` rule module.
|
|
@@ -93,6 +105,26 @@ export const noMapfile = createCompatRule({
|
|
|
93
105
|
const { sourceCode } = context;
|
|
94
106
|
const transparentWrappers = resolveTransparentWrappers(context, TRANSPARENT_WRAPPERS);
|
|
95
107
|
const arraysAvailable = sourceCode.dialect !== 'posix';
|
|
108
|
+
let replyDeclared;
|
|
109
|
+
/** Whether any declaration command in the file may set attributes on `REPLY`. */
|
|
110
|
+
const mayDeclareReply = () => (replyDeclared ??=
|
|
111
|
+
someShellNode(sourceCode.ast, (part) => part.type === 'DeclClause' &&
|
|
112
|
+
DECLARATION_COMMANDS.has(part.variant?.value ?? '') &&
|
|
113
|
+
part.args.some((arg) => {
|
|
114
|
+
if (arg.name !== undefined)
|
|
115
|
+
return arg.name.value === 'REPLY';
|
|
116
|
+
if (arg.value === undefined)
|
|
117
|
+
return false;
|
|
118
|
+
const text = resolveWordTextAtPosition(sourceCode, arg.value);
|
|
119
|
+
return text === null || REPLY_OPERAND.test(text);
|
|
120
|
+
})) ||
|
|
121
|
+
sourceCode.getCommandInventory().commands.some((site) => {
|
|
122
|
+
const { effective, chainIndices } = site.resolveInvokedCommand({ transparentWrappers });
|
|
123
|
+
return ((!effective.static || DECLARATION_COMMANDS.has(effective.text)) &&
|
|
124
|
+
site.argv
|
|
125
|
+
.slice((chainIndices.at(-1) ?? 0) + 1)
|
|
126
|
+
.some((word) => !word.static || REPLY_OPERAND.test(word.text)));
|
|
127
|
+
}));
|
|
96
128
|
return {
|
|
97
129
|
CallExpr(node) {
|
|
98
130
|
const invoked = sourceCode.resolveInvokedCommand(node, {
|
|
@@ -103,7 +135,7 @@ export const noMapfile = createCompatRule({
|
|
|
103
135
|
const command = invoked.effective.text;
|
|
104
136
|
if (!MAPFILE_COMMANDS.has(command))
|
|
105
137
|
return;
|
|
106
|
-
const array = arraysAvailable ? rewritableArray(sourceCode, node) : undefined;
|
|
138
|
+
const array = arraysAvailable && !mayDeclareReply() ? rewritableArray(sourceCode, node) : undefined;
|
|
107
139
|
context.report({
|
|
108
140
|
node,
|
|
109
141
|
messageId: 'mapfile',
|
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
* `useArrayLength` for its suggestion.
|
|
4
|
-
*
|
|
5
|
-
* @internal
|
|
6
|
-
*/
|
|
7
|
-
export type NoNegativeArrayIndexMessageId = 'negativeIndex' | 'useArrayLength';
|
|
1
|
+
/** Message id for an unsupported negative subscript. @internal */
|
|
2
|
+
export type NoNegativeArrayIndexMessageId = 'negativeIndex';
|
|
8
3
|
/**
|
|
9
4
|
* The `sh/compat/no-negative-array-index` rule module.
|
|
10
5
|
*
|
|
11
6
|
* @internal
|
|
12
7
|
*/
|
|
13
|
-
export declare const noNegativeArrayIndex: import("../define-shell-rule.js").ShellRule<[],
|
|
8
|
+
export declare const noNegativeArrayIndex: import("../define-shell-rule.js").ShellRule<[], "negativeIndex">;
|
|
@@ -6,14 +6,8 @@
|
|
|
6
6
|
*
|
|
7
7
|
* Bash 3.2 evaluates `-1` as the arithmetic value minus one and fails with
|
|
8
8
|
* "bad array subscript", so the read yields nothing and the assignment is
|
|
9
|
-
* refused.
|
|
10
|
-
* array
|
|
11
|
-
*
|
|
12
|
-
* The suggestion rewrites exactly a bare `${NAME[-N]}` read to
|
|
13
|
-
* `${NAME[$((${#NAME[@]}-N))]}`. Negative subscripts count back from the
|
|
14
|
-
* highest assigned index plus one, while `${#NAME[@]}` counts set elements,
|
|
15
|
-
* so the two agree only for arrays without holes; the suggestion message says
|
|
16
|
-
* so. Assignments and other expansion forms are reported without a suggestion.
|
|
9
|
+
* refused. A length-based index is only equivalent for a dense zero-based
|
|
10
|
+
* array. The rule reports the incompatible syntax without rewriting it.
|
|
17
11
|
*
|
|
18
12
|
* What is read: an `Index` that is a unary minus applied to a static
|
|
19
13
|
* integer (decimal, octal, hexadecimal or base#digits), on a `ParamExp` or an `Assign`.
|
|
@@ -27,13 +21,12 @@
|
|
|
27
21
|
* `unset` command's operand text, since the parser keeps that operand as an
|
|
28
22
|
* ordinary word; option words (`-v`) never match an element name.
|
|
29
23
|
*/
|
|
24
|
+
import { resolveWordTextAtPosition } from '../../language/word-resolution.js';
|
|
30
25
|
import { Minus } from 'sh-ast';
|
|
31
26
|
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
32
27
|
import { resolveTransparentWrappers } from '../resolve-transparent-wrappers.js';
|
|
33
28
|
import { BUILTIN_WRAPPER } from '../transparent-wrappers.js';
|
|
34
29
|
import { createCompatRule } from './create-compat-rule.js';
|
|
35
|
-
/** A bare negative-subscript read, the only shape the suggestion rewrites. */
|
|
36
|
-
const BARE_READ = /^\$\{([A-Za-z_][A-Za-z0-9_]*)\[\s*-\s*(\d+)\s*\]\}$/u;
|
|
37
30
|
/**
|
|
38
31
|
* The wrappers followed to find an `unset` command: `sh-analysis`'s
|
|
39
32
|
* defaults (which include `command`) plus Bash's `builtin`.
|
|
@@ -80,13 +73,11 @@ export const noNegativeArrayIndex = createCompatRule({
|
|
|
80
73
|
feature: 'negativeArrayIndex',
|
|
81
74
|
meta: {
|
|
82
75
|
type: 'problem',
|
|
83
|
-
hasSuggestions: true,
|
|
84
76
|
docs: {
|
|
85
77
|
description: 'Disallow negative array subscripts (`${arr[-1]}`) when the target shell lacks them',
|
|
86
78
|
},
|
|
87
79
|
messages: {
|
|
88
|
-
negativeIndex: 'Negative array subscripts need {{required}}, but this file must support {{target}}.
|
|
89
|
-
useArrayLength: 'Replace with `${{{name}}[$((${#{{name}}[@]}-{{n}}))]}` (the two differ only for an array with unset elements).',
|
|
80
|
+
negativeIndex: 'Negative array subscripts need {{required}}, but this file must support {{target}}. Use a nonnegative index. A length-based index is equivalent only for a dense zero-based array.',
|
|
90
81
|
},
|
|
91
82
|
},
|
|
92
83
|
create(context, gate) {
|
|
@@ -101,7 +92,7 @@ export const noNegativeArrayIndex = createCompatRule({
|
|
|
101
92
|
// only a Word can be resolved, and `-0` is zero, not a negative index.
|
|
102
93
|
if (operand?.type !== 'Word')
|
|
103
94
|
return false;
|
|
104
|
-
const digits = sourceCode
|
|
95
|
+
const digits = resolveWordTextAtPosition(sourceCode, operand) ?? '';
|
|
105
96
|
return isNonzeroInteger(digits);
|
|
106
97
|
}
|
|
107
98
|
return {
|
|
@@ -115,7 +106,7 @@ export const noNegativeArrayIndex = createCompatRule({
|
|
|
115
106
|
for (const word of node.args.slice((invoked.chainIndices.at(-1) ?? 0) + 1)) {
|
|
116
107
|
// An unquoted `a[-1]` holds a glob bracket, so it is not "static"; its
|
|
117
108
|
// written text is what names the element.
|
|
118
|
-
const text = sourceCode
|
|
109
|
+
const text = resolveWordTextAtPosition(sourceCode, word) ?? sourceCode.getText(word);
|
|
119
110
|
if (text === '--')
|
|
120
111
|
continue;
|
|
121
112
|
const match = UNSET_NEGATIVE.exec(text);
|
|
@@ -127,21 +118,7 @@ export const noNegativeArrayIndex = createCompatRule({
|
|
|
127
118
|
ParamExp(node) {
|
|
128
119
|
if (!isNegativeLiteral(node.index))
|
|
129
120
|
return;
|
|
130
|
-
|
|
131
|
-
context.report({
|
|
132
|
-
node,
|
|
133
|
-
messageId: 'negativeIndex',
|
|
134
|
-
data: { ...gate },
|
|
135
|
-
suggest: match === null
|
|
136
|
-
? []
|
|
137
|
-
: [
|
|
138
|
-
{
|
|
139
|
-
messageId: 'useArrayLength',
|
|
140
|
-
data: { name: match[1], n: match[2] },
|
|
141
|
-
fix: (fixer) => fixer.replaceText(node, `\${${match[1]}[$((\${#${match[1]}[@]}-${match[2]}))]}`),
|
|
142
|
-
},
|
|
143
|
-
],
|
|
144
|
-
});
|
|
121
|
+
context.report({ node, messageId: 'negativeIndex', data: { ...gate } });
|
|
145
122
|
},
|
|
146
123
|
Assign(node) {
|
|
147
124
|
if (!isNegativeLiteral(node.index))
|
|
@@ -42,10 +42,12 @@
|
|
|
42
42
|
* `nounset-array-proof.ts` owns those boundaries. Reports remain conditional:
|
|
43
43
|
* unknown state is a reason to report, not proof that the array is empty.
|
|
44
44
|
*/
|
|
45
|
+
import { parseSetWords } from '../strict-mode-prologue.js';
|
|
46
|
+
import { resolveWordTextAtPosition } from '../../language/word-resolution.js';
|
|
45
47
|
import { AlternateUnset, AlternateUnsetOrNull } from 'sh-ast';
|
|
46
48
|
import { createCompatRule } from './create-compat-rule.js';
|
|
47
49
|
import { createArraySafetyProof } from './nounset-array-proof.js';
|
|
48
|
-
import {
|
|
50
|
+
import { shebangOptionChanges } from '../set-options.js';
|
|
49
51
|
/** A shell variable name. */
|
|
50
52
|
const IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/u;
|
|
51
53
|
/**
|
|
@@ -88,7 +90,7 @@ export const noNounsetEmptyArray = createCompatRule({
|
|
|
88
90
|
function wholeArraySubscript(index) {
|
|
89
91
|
if (index?.type !== 'Word')
|
|
90
92
|
return undefined;
|
|
91
|
-
const text = sourceCode
|
|
93
|
+
const text = resolveWordTextAtPosition(sourceCode, index) ?? sourceCode.getText(index);
|
|
92
94
|
return text === '@' || text === '*' ? text : undefined;
|
|
93
95
|
}
|
|
94
96
|
/**
|
|
@@ -121,8 +123,8 @@ export const noNounsetEmptyArray = createCompatRule({
|
|
|
121
123
|
CallExpr(node) {
|
|
122
124
|
if (sourceCode.getCommandName(node) !== 'set')
|
|
123
125
|
return;
|
|
124
|
-
const args = node.args.slice(1)
|
|
125
|
-
if (enablesNounset(
|
|
126
|
+
const args = node.args.slice(1);
|
|
127
|
+
if (enablesNounset(parseSetWords(sourceCode, args).changes))
|
|
126
128
|
nounset = true;
|
|
127
129
|
},
|
|
128
130
|
ParamExp(node) {
|
|
@@ -142,9 +144,10 @@ export const noNounsetEmptyArray = createCompatRule({
|
|
|
142
144
|
'File:exit'() {
|
|
143
145
|
if (!nounset || candidates.length === 0)
|
|
144
146
|
return;
|
|
145
|
-
const
|
|
147
|
+
const arraySafety = createArraySafetyProof(sourceCode);
|
|
146
148
|
for (const { node, name, subscript } of candidates) {
|
|
147
|
-
|
|
149
|
+
const safety = arraySafety(node, name);
|
|
150
|
+
if (safety === 'safe')
|
|
148
151
|
continue;
|
|
149
152
|
const replacement = guardedExpansion(name, subscript);
|
|
150
153
|
const quoted = wholeQuotedWord(node);
|
|
@@ -152,7 +155,7 @@ export const noNounsetEmptyArray = createCompatRule({
|
|
|
152
155
|
node,
|
|
153
156
|
messageId: 'emptyArray',
|
|
154
157
|
data: { ...gate, name, subscript, example: replacement },
|
|
155
|
-
suggest: quoted === undefined
|
|
158
|
+
suggest: quoted === undefined || safety === 'unresolved-word'
|
|
156
159
|
? []
|
|
157
160
|
: [
|
|
158
161
|
{
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
* What is read: a `ParamExp` whose expansion operator is `@` followed by one
|
|
20
20
|
* of the letters above. The report is on the whole expansion.
|
|
21
21
|
*/
|
|
22
|
+
import { resolveWordTextAtPosition } from '../../language/word-resolution.js';
|
|
22
23
|
import { OtherParamOps } from 'sh-ast';
|
|
23
24
|
import { createMultiFeatureCompatRule } from './create-compat-rule.js';
|
|
24
25
|
import { TO_LOWER, TO_UPPER, trExample } from './no-case-modification.js';
|
|
@@ -96,7 +97,7 @@ export const noParameterTransformation = createMultiFeatureCompatRule({
|
|
|
96
97
|
const { exp } = node;
|
|
97
98
|
if (exp?.op !== OtherParamOps || exp.word === undefined)
|
|
98
99
|
return;
|
|
99
|
-
const operator = sourceCode
|
|
100
|
+
const operator = resolveWordTextAtPosition(sourceCode, exp.word);
|
|
100
101
|
if (operator === null)
|
|
101
102
|
return;
|
|
102
103
|
const feature = OPERATOR_FEATURES.get(operator);
|