@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
|
@@ -7,17 +7,19 @@
|
|
|
7
7
|
* nothing is printed. The message suggests the portable alternative for the
|
|
8
8
|
* current time: `date '+fmt'`, which takes the same `strftime` format.
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* a
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* word.
|
|
10
|
+
* The rule reads the first format field of a `printf` invocation, after
|
|
11
|
+
* optional `-v name` and `--` operands. A destination Bash rejects (such as
|
|
12
|
+
* `a[1][2]`) stops `printf` before it reads the format, so nothing is
|
|
13
|
+
* reported. A destination that is not known, separate (`-v "$name"`) or
|
|
14
|
+
* attached to a literal `-v` (`"-v$name"`), is assumed to be valid. It scans exact text and known
|
|
15
|
+
* literal edges for directives, honoring `%%`, flags, width and precision.
|
|
16
|
+
* The report points to the format word.
|
|
17
17
|
*/
|
|
18
|
+
import { describeWordAtPosition, isSingleField } from '../../language/word-resolution.js';
|
|
18
19
|
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
19
20
|
import { resolveTransparentWrappers } from '../resolve-transparent-wrappers.js';
|
|
20
21
|
import { BUILTIN_WRAPPER } from '../transparent-wrappers.js';
|
|
22
|
+
import { canStartBashBuiltinDestination, isBashBuiltinDestination, } from './bash-builtin-destination.js';
|
|
21
23
|
import { createCompatRule } from './create-compat-rule.js';
|
|
22
24
|
/**
|
|
23
25
|
* The wrappers followed to find the `printf` command: `sh-analysis`'s
|
|
@@ -80,21 +82,58 @@ export const noPrintfTimeFormat = createCompatRule({
|
|
|
80
82
|
if (invoked?.effective.static !== true || invoked.effective.text !== 'printf')
|
|
81
83
|
return;
|
|
82
84
|
// `chainIndices` index `node.args` directly, so slice before filtering.
|
|
83
|
-
const words = node.args
|
|
85
|
+
const words = node.args
|
|
86
|
+
.slice((invoked.chainIndices.at(-1) ?? 0) + 1)
|
|
87
|
+
.map((word) => ({ word, facts: describeWordAtPosition(sourceCode, word) }))
|
|
88
|
+
.filter(({ facts }) => facts.fields.max !== 0);
|
|
84
89
|
let index = 0;
|
|
85
|
-
const first = words.at(0);
|
|
86
|
-
if (first
|
|
90
|
+
const first = words.at(0)?.facts.resolution;
|
|
91
|
+
if (first?.static && first.text === '-v') {
|
|
92
|
+
const destination = words.at(1)?.facts;
|
|
93
|
+
if (destination === undefined || !isSingleField(destination))
|
|
94
|
+
return;
|
|
95
|
+
if (destination.resolution.static &&
|
|
96
|
+
!isBashBuiltinDestination(destination.resolution.text))
|
|
97
|
+
return;
|
|
87
98
|
index = 2;
|
|
88
|
-
|
|
99
|
+
}
|
|
100
|
+
else if (first?.static && first.text.startsWith('-v')) {
|
|
101
|
+
if (!isBashBuiltinDestination(first.text.slice(2)))
|
|
102
|
+
return;
|
|
103
|
+
index = 1;
|
|
104
|
+
}
|
|
105
|
+
else if (first !== undefined && !first.static) {
|
|
106
|
+
// An expanded word with a literal `-v` prefix is an attached
|
|
107
|
+
// destination: Bash takes the rest of the field as the variable
|
|
108
|
+
// name. Expect the format next only when the known edges can
|
|
109
|
+
// belong to a variable name or an array member.
|
|
110
|
+
const facts = words[0].facts;
|
|
111
|
+
const name = facts.knownPrefix.slice(2);
|
|
112
|
+
if (isSingleField(facts) &&
|
|
113
|
+
facts.knownPrefix.startsWith('-v') &&
|
|
114
|
+
((/^(?:[A-Za-z_][A-Za-z0-9_]*)?$/u.test(name) &&
|
|
115
|
+
/^[A-Za-z0-9_]*$/u.test(facts.knownSuffix)) ||
|
|
116
|
+
(canStartBashBuiltinDestination(name) && facts.knownSuffix.endsWith(']'))))
|
|
117
|
+
index = 1;
|
|
118
|
+
}
|
|
119
|
+
const option = words.at(index)?.facts.resolution;
|
|
120
|
+
if (option?.static && option.text === '--')
|
|
89
121
|
index++;
|
|
90
|
-
const
|
|
91
|
-
if (
|
|
122
|
+
const entry = words.at(index);
|
|
123
|
+
if (entry === undefined || !isSingleField(entry.facts))
|
|
92
124
|
return;
|
|
93
|
-
const
|
|
94
|
-
const
|
|
125
|
+
const { word, facts } = entry;
|
|
126
|
+
const text = facts.resolution.static ? facts.resolution.text : facts.knownPrefix;
|
|
127
|
+
let format = timeFormatIn(text);
|
|
128
|
+
if (format === undefined &&
|
|
129
|
+
facts.features.every((feature) => feature === 'locale' || feature === 'tilde')) {
|
|
130
|
+
// The exact format can depend on encoding or a tilde directory.
|
|
131
|
+
// Recognize the directive in the literal suffix of that format.
|
|
132
|
+
format = timeFormatIn(facts.knownSuffix);
|
|
133
|
+
}
|
|
95
134
|
if (format === undefined)
|
|
96
135
|
return;
|
|
97
|
-
context.report({ node:
|
|
136
|
+
context.report({ node: word, messageId: 'printfTime', data: { ...gate, format } });
|
|
98
137
|
},
|
|
99
138
|
};
|
|
100
139
|
},
|
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
* its suggestion.
|
|
4
|
-
*
|
|
5
|
-
* @internal
|
|
6
|
-
*/
|
|
7
|
-
export type NoSourceMessageId = 'source' | 'useDot';
|
|
1
|
+
/** The message id this rule reports with. @internal */
|
|
2
|
+
export type NoSourceMessageId = 'source';
|
|
8
3
|
/**
|
|
9
4
|
* The `sh/compat/no-source` rule module.
|
|
10
5
|
*
|
|
11
6
|
* @internal
|
|
12
7
|
*/
|
|
13
|
-
export declare const noSource: import("../define-shell-rule.js").ShellRule<[],
|
|
8
|
+
export declare const noSource: import("../define-shell-rule.js").ShellRule<[], "source">;
|
|
@@ -19,46 +19,14 @@
|
|
|
19
19
|
* calls still fail on dash. A call through a wrapper is never exempt, because
|
|
20
20
|
* wrappers such as `command`, `env` and `exec` do not run functions.
|
|
21
21
|
*
|
|
22
|
-
* The
|
|
23
|
-
* file
|
|
24
|
-
* ends the script on a POSIX shell, so a `|| fallback` never runs, and the
|
|
25
|
-
* suggestion's text says so. The conditions are:
|
|
26
|
-
*
|
|
27
|
-
* - The command is written directly, with no wrapper.
|
|
28
|
-
* - It has no leading `VAR=value`. Such an assignment persists after `.` on a
|
|
29
|
-
* POSIX shell, while Bash outside POSIX mode scopes it to the `source`
|
|
30
|
-
* command.
|
|
31
|
-
* - It has exactly one operand. `.` takes one in POSIX, and dash ignores any
|
|
32
|
-
* others where `source` would set the positional parameters.
|
|
33
|
-
* - The operand has a literal `/`. A name without one makes `.` search `PATH`.
|
|
22
|
+
* The diagnostic gives the portable spelling. The edit requires a readable
|
|
23
|
+
* shell file at runtime, which lint cannot establish.
|
|
34
24
|
*/
|
|
35
25
|
import { definedFunctionNames } from '../../command-harvest/harvest-commands.js';
|
|
36
|
-
import { childNodes } from '../../language/syntax-view.js';
|
|
37
26
|
import { createCompatRule } from './create-compat-rule.js';
|
|
38
27
|
import { reachableCommandWord } from './posix-absent-command.js';
|
|
39
28
|
/** The builtin this rule gates. */
|
|
40
29
|
const SOURCE = 'source';
|
|
41
|
-
/**
|
|
42
|
-
* Whether `word` holds a `/` that no expansion can remove: in unquoted or
|
|
43
|
-
* quoted literal text, whatever its other parts expand to. A `/` inside an
|
|
44
|
-
* expansion (`${x%/*}`, `$(pwd)`) does not count, so the walk stops at anything
|
|
45
|
-
* that is not literal text or a double-quoted group.
|
|
46
|
-
*/
|
|
47
|
-
function hasLiteralSlash(word) {
|
|
48
|
-
return childNodes(word).some((part) => {
|
|
49
|
-
if (part.type === 'Lit' || part.type === 'SglQuoted')
|
|
50
|
-
return part.value.includes('/');
|
|
51
|
-
return part.type === 'DblQuoted' && hasLiteralSlash(part);
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
/** Whether `.` is an exact replacement for this `source` command (see the module comment). */
|
|
55
|
-
function dotIsEquivalent(sourceCode, node) {
|
|
56
|
-
if (sourceCode.getCommandName(node) !== SOURCE)
|
|
57
|
-
return false;
|
|
58
|
-
if (node.assigns.length > 0 || node.args.length !== 2)
|
|
59
|
-
return false;
|
|
60
|
-
return hasLiteralSlash(node.args[1]);
|
|
61
|
-
}
|
|
62
30
|
/**
|
|
63
31
|
* The `sh/compat/no-source` rule module.
|
|
64
32
|
*
|
|
@@ -69,13 +37,11 @@ export const noSource = createCompatRule({
|
|
|
69
37
|
feature: 'sourceCommand',
|
|
70
38
|
meta: {
|
|
71
39
|
type: 'problem',
|
|
72
|
-
hasSuggestions: true,
|
|
73
40
|
docs: {
|
|
74
41
|
description: 'Disallow the `source` builtin when the target shell is POSIX sh',
|
|
75
42
|
},
|
|
76
43
|
messages: {
|
|
77
|
-
source: '`source` is not part of {{target}}. dash fails with `source: not found` and the file is never read.
|
|
78
|
-
useDot: 'Replace `source` with `.`, which ends the script if the file is missing.',
|
|
44
|
+
source: '`source` is not part of {{target}}. dash fails with `source: not found` and the file is never read. To execute a readable shell file, use the `.` special built-in with a path that contains a slash, for example `. ./lib.sh`. An unreadable file can end the script.',
|
|
79
45
|
},
|
|
80
46
|
},
|
|
81
47
|
create(context, gate) {
|
|
@@ -94,9 +60,6 @@ export const noSource = createCompatRule({
|
|
|
94
60
|
node: word,
|
|
95
61
|
messageId: 'source',
|
|
96
62
|
data: { target: gate.target },
|
|
97
|
-
suggest: dotIsEquivalent(sourceCode, node)
|
|
98
|
-
? [{ messageId: 'useDot', fix: (fixer) => fixer.replaceText(word, '.') }]
|
|
99
|
-
: [],
|
|
100
63
|
});
|
|
101
64
|
},
|
|
102
65
|
};
|
|
@@ -19,12 +19,13 @@
|
|
|
19
19
|
* `! …` and `( … )` peel off; anything longer goes through `test`'s own
|
|
20
20
|
* precedence parser, where `-v` is the operator only where a primary is
|
|
21
21
|
* expected and another argument follows it. So `[ "$x" = -v ]` and
|
|
22
|
-
* `[ -n -v ]` do not report. A
|
|
23
|
-
*
|
|
24
|
-
*
|
|
22
|
+
* `[ -n -v ]` do not report. The analyzer supplies field counts. A single field can still be an operator.
|
|
23
|
+
* The rule checks literal -v words whose role is known across the possible
|
|
24
|
+
* argument texts.
|
|
25
25
|
*
|
|
26
26
|
* The report is on the `[[` test, or on the `-v` word.
|
|
27
27
|
*/
|
|
28
|
+
import { describeWordAtPosition, isSingleField, resolveWordTextAtPosition, } from '../../language/word-resolution.js';
|
|
28
29
|
import { TsVarSet } from 'sh-ast';
|
|
29
30
|
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
30
31
|
import { resolveTransparentWrappers } from '../resolve-transparent-wrappers.js';
|
|
@@ -87,91 +88,285 @@ const UNARY_OPERATORS = new Set([
|
|
|
87
88
|
'-R',
|
|
88
89
|
]);
|
|
89
90
|
/**
|
|
90
|
-
*
|
|
91
|
-
* arguments: an operand that is never an operator. A NUL cannot occur in a
|
|
92
|
-
* shell word, so no real argument equals it.
|
|
91
|
+
* Representative text for every non-operator. A NUL cannot occur in argv.
|
|
93
92
|
*/
|
|
94
|
-
const
|
|
93
|
+
const STRING_OPERAND = '\0operand';
|
|
94
|
+
/** Every spelling whose role is different from an ordinary string operand. */
|
|
95
|
+
const TEST_OPERATORS = new Set([...UNARY_OPERATORS, ...BINARY_OPERATORS, '!', '(', ')']);
|
|
96
|
+
/** An exact quoted expansion of the positional argument list. */
|
|
97
|
+
function quotedPositionals(word) {
|
|
98
|
+
const [quote] = word.parts;
|
|
99
|
+
if (word.parts.length !== 1 || quote.type !== 'DblQuoted' || quote.dollar)
|
|
100
|
+
return false;
|
|
101
|
+
const [part] = quote.parts;
|
|
102
|
+
return (quote.parts.length === 1 &&
|
|
103
|
+
part.type === 'ParamExp' &&
|
|
104
|
+
part.param?.value === '@' &&
|
|
105
|
+
!part.length &&
|
|
106
|
+
!part.excl &&
|
|
107
|
+
part.exp === undefined &&
|
|
108
|
+
part.index === undefined &&
|
|
109
|
+
part.slice === undefined &&
|
|
110
|
+
part.repl === undefined);
|
|
111
|
+
}
|
|
112
|
+
const SCALAR_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/u;
|
|
113
|
+
const STRING_PRINTF_FORMAT = /^(?:[^%]|%%|%[sbqc])*$/u;
|
|
114
|
+
/** Word syntax that cannot execute code while it expands. */
|
|
115
|
+
function inertWordPart(node) {
|
|
116
|
+
switch (node.type) {
|
|
117
|
+
case 'Word':
|
|
118
|
+
return node.parts.every(inertWordPart);
|
|
119
|
+
case 'DblQuoted':
|
|
120
|
+
return !node.dollar && node.parts.every(inertWordPart);
|
|
121
|
+
case 'Lit':
|
|
122
|
+
case 'SglQuoted':
|
|
123
|
+
return true;
|
|
124
|
+
case 'ParamExp':
|
|
125
|
+
return (!node.excl &&
|
|
126
|
+
node.index === undefined &&
|
|
127
|
+
node.exp === undefined &&
|
|
128
|
+
node.slice === undefined &&
|
|
129
|
+
node.repl === undefined);
|
|
130
|
+
default:
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** Some builtins evaluate strings again after expansion. Keep only inert forms. */
|
|
135
|
+
function preservesPositionals(source, command, args) {
|
|
136
|
+
switch (command) {
|
|
137
|
+
case ':':
|
|
138
|
+
return true;
|
|
139
|
+
case 'unset':
|
|
140
|
+
return args.every((arg) => SCALAR_NAME.test(resolveWordTextAtPosition(source, arg) ?? ''));
|
|
141
|
+
case 'printf': {
|
|
142
|
+
const first = args.at(0);
|
|
143
|
+
if (first === undefined || !isSingleField(describeWordAtPosition(source, first)))
|
|
144
|
+
return false;
|
|
145
|
+
const optionsEnded = resolveWordTextAtPosition(source, first) === '--';
|
|
146
|
+
const formatWord = args.at(optionsEnded ? 1 : 0);
|
|
147
|
+
if (formatWord === undefined || !isSingleField(describeWordAtPosition(source, formatWord)))
|
|
148
|
+
return false;
|
|
149
|
+
const format = resolveWordTextAtPosition(source, formatWord);
|
|
150
|
+
return (format !== null &&
|
|
151
|
+
(optionsEnded || !format.startsWith('-')) &&
|
|
152
|
+
STRING_PRINTF_FORMAT.test(format));
|
|
153
|
+
}
|
|
154
|
+
case 'test':
|
|
155
|
+
case '[': {
|
|
156
|
+
if (!args.every((arg) => isSingleField(describeWordAtPosition(source, arg))))
|
|
157
|
+
return false;
|
|
158
|
+
const words = args.map((arg) => resolveWordTextAtPosition(source, arg));
|
|
159
|
+
if (command === '[' && words.pop() !== ']')
|
|
160
|
+
return false;
|
|
161
|
+
return (words.length <= 1 ||
|
|
162
|
+
(words.length === 2 && words[0] === '-v' && SCALAR_NAME.test(words[1] ?? '')));
|
|
163
|
+
}
|
|
164
|
+
default:
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/** Commands reached after a plain top-level `set --` and before any opaque statement. */
|
|
169
|
+
function callsWithEmptyPositionals(source) {
|
|
170
|
+
const calls = new WeakSet();
|
|
171
|
+
let empty = false;
|
|
172
|
+
for (const stmt of source.ast.stmts) {
|
|
173
|
+
const { cmd } = stmt;
|
|
174
|
+
if (cmd?.type !== 'CallExpr' ||
|
|
175
|
+
stmt.background ||
|
|
176
|
+
stmt.coprocess ||
|
|
177
|
+
stmt.disown ||
|
|
178
|
+
stmt.negated ||
|
|
179
|
+
stmt.redirs.length !== 0 ||
|
|
180
|
+
!cmd.args.every(inertWordPart))
|
|
181
|
+
break;
|
|
182
|
+
if (cmd.args.length === 0) {
|
|
183
|
+
// An integer attribute can cause an otherwise literal assignment to
|
|
184
|
+
// evaluate the right-hand side as arithmetic.
|
|
185
|
+
if (!cmd.assigns.every((assign) => assign.index === undefined &&
|
|
186
|
+
assign.array === undefined &&
|
|
187
|
+
(assign.value === undefined ||
|
|
188
|
+
/^\d*$/u.test(resolveWordTextAtPosition(source, assign.value) ?? '-'))))
|
|
189
|
+
break;
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (cmd.assigns.length > 0)
|
|
193
|
+
break;
|
|
194
|
+
if (empty)
|
|
195
|
+
calls.add(cmd);
|
|
196
|
+
const command = source.getCommandName(cmd);
|
|
197
|
+
if (command === 'set') {
|
|
198
|
+
if (cmd.args.length !== 2 || resolveWordTextAtPosition(source, cmd.args[1]) !== '--')
|
|
199
|
+
break;
|
|
200
|
+
empty = true;
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
const args = cmd.args.slice(1).filter((arg) => !empty || !quotedPositionals(arg));
|
|
204
|
+
if (!preservesPositionals(source, command, args))
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return calls;
|
|
209
|
+
}
|
|
210
|
+
/** Text alternatives relevant to test's grammar for a proven single field. */
|
|
211
|
+
function testArgumentChoices(word, facts) {
|
|
212
|
+
if (facts.resolution.static)
|
|
213
|
+
return [facts.resolution.text];
|
|
214
|
+
const [part] = word.parts;
|
|
215
|
+
// An isolated locale-sensitive Unicode escape in ANSI-C quotes cannot
|
|
216
|
+
// spell any of test's ASCII operators, even if the shell emits an escape.
|
|
217
|
+
if (word.parts.length === 1 &&
|
|
218
|
+
part.type === 'SglQuoted' &&
|
|
219
|
+
part.dollar &&
|
|
220
|
+
facts.features.length === 1 &&
|
|
221
|
+
facts.features[0] === 'locale' &&
|
|
222
|
+
/^\\(?:u[0-9a-f]{1,4}|U[0-9a-f]{1,8})$/iu.test(part.value)) {
|
|
223
|
+
return [STRING_OPERAND];
|
|
224
|
+
}
|
|
225
|
+
return [
|
|
226
|
+
STRING_OPERAND,
|
|
227
|
+
...[...TEST_OPERATORS].filter((operator) => operator.length >= facts.knownPrefix.length + facts.knownSuffix.length &&
|
|
228
|
+
operator.startsWith(facts.knownPrefix) &&
|
|
229
|
+
operator.endsWith(facts.knownSuffix)),
|
|
230
|
+
];
|
|
231
|
+
}
|
|
232
|
+
/** Reports only literal -v words that are operators across all compatible argument shapes. */
|
|
233
|
+
function definiteVariableTests(words) {
|
|
234
|
+
if (!words.every(({ facts }) => isSingleField(facts)))
|
|
235
|
+
return [];
|
|
236
|
+
const choices = words.map(({ word, facts }) => testArgumentChoices(word, facts));
|
|
237
|
+
const argv = choices.map(([first]) => first);
|
|
238
|
+
let definite = variableTestIndices(argv);
|
|
239
|
+
if (definite.length === 0)
|
|
240
|
+
return definite;
|
|
241
|
+
// The short arity rules inspect the whole list. In a longer expression,
|
|
242
|
+
// a unary operator's role is settled once its operand has been read.
|
|
243
|
+
let end = argv.length <= 4 ? argv.length : (definite.at(-1) ?? 0) + 2;
|
|
244
|
+
let combinations = 1;
|
|
245
|
+
for (let index = 0; index < end; index++) {
|
|
246
|
+
combinations *= choices[index].length;
|
|
247
|
+
if (combinations <= 256)
|
|
248
|
+
continue;
|
|
249
|
+
if (argv.length <= 4)
|
|
250
|
+
return [];
|
|
251
|
+
definite = definite.filter((candidate) => candidate + 1 < index);
|
|
252
|
+
if (definite.length === 0)
|
|
253
|
+
return definite;
|
|
254
|
+
end = (definite.at(-1) ?? 0) + 2;
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
const uncertain = choices
|
|
258
|
+
.slice(0, end)
|
|
259
|
+
.flatMap((options, index) => (options.length > 1 ? [{ options, index }] : []));
|
|
260
|
+
function consider(position) {
|
|
261
|
+
const entry = uncertain.at(position);
|
|
262
|
+
if (entry === undefined) {
|
|
263
|
+
const indices = variableTestIndices(argv);
|
|
264
|
+
definite = definite.filter((index) => indices.includes(index));
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
for (const option of entry.options) {
|
|
268
|
+
if (definite.length === 0)
|
|
269
|
+
return;
|
|
270
|
+
argv[entry.index] = option;
|
|
271
|
+
consider(position + 1);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
consider(0);
|
|
275
|
+
return definite;
|
|
276
|
+
}
|
|
95
277
|
/** A shell variable name, optionally with an array subscript. */
|
|
96
278
|
const VARIABLE_NAME = /^[A-Za-z_][A-Za-z0-9_]*(?:\[[^\]]*\])?$/u;
|
|
279
|
+
const INTEGER_TESTS = new Set(['-eq', '-ne', '-lt', '-le', '-gt', '-ge']);
|
|
280
|
+
/** A literal decimal that Bash accepts without evaluating an operand. */
|
|
281
|
+
function isTestInteger(text) {
|
|
282
|
+
if (!/^[+-]?\d{1,19}$/u.test(text))
|
|
283
|
+
return false;
|
|
284
|
+
const value = BigInt(text);
|
|
285
|
+
return value >= -(2n ** 63n) && value < 2n ** 63n;
|
|
286
|
+
}
|
|
97
287
|
/**
|
|
98
288
|
* The indices of the arguments in `args` that Bash's `test` reads as the
|
|
99
289
|
* `-v` operator. `args` are the static texts after the command name (and
|
|
100
|
-
* without a closing `]`)
|
|
290
|
+
* without a closing `]`).
|
|
101
291
|
*/
|
|
102
|
-
function variableTestIndices(
|
|
103
|
-
const end = args.indexOf(null);
|
|
104
|
-
const argv = (end === -1 ? args : args.slice(0, end));
|
|
292
|
+
function variableTestIndices(argv) {
|
|
105
293
|
const found = [];
|
|
106
294
|
/** `test`'s two-argument rule, at offset `at`. */
|
|
107
295
|
function twoArguments(at) {
|
|
108
296
|
if (argv[at] === '-v')
|
|
109
297
|
found.push(at);
|
|
110
298
|
}
|
|
111
|
-
/**
|
|
299
|
+
/**
|
|
300
|
+
* `test`'s three-argument rule, at offset `at`. Only `! x y` can hold a
|
|
301
|
+
* `-v` operator; every other fitting form is a binary test or `( x )`, and
|
|
302
|
+
* arguments that fit no form are a syntax error.
|
|
303
|
+
*/
|
|
112
304
|
function threeArguments(at) {
|
|
113
305
|
const middle = argv[at + 1];
|
|
114
306
|
if (BINARY_OPERATORS.has(middle) || middle === '-a' || middle === '-o')
|
|
115
|
-
return
|
|
116
|
-
if (argv[at] === '!')
|
|
307
|
+
return;
|
|
308
|
+
if (argv[at] === '!')
|
|
117
309
|
twoArguments(at + 1);
|
|
118
|
-
return true;
|
|
119
|
-
}
|
|
120
|
-
return argv[at] === '(' && argv[at + 2] === ')';
|
|
121
310
|
}
|
|
122
311
|
/** `test`'s precedence parser over every argument (`expr` in Bash's test.c). */
|
|
123
312
|
function expression() {
|
|
124
313
|
let pos = 0;
|
|
125
314
|
const term = () => {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const word = argv
|
|
129
|
-
if (word ===
|
|
130
|
-
|
|
131
|
-
while (argv[pos] === '!')
|
|
132
|
-
pos++;
|
|
133
|
-
term();
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
315
|
+
while (argv[pos] === '!')
|
|
316
|
+
pos++;
|
|
317
|
+
const word = argv.at(pos);
|
|
318
|
+
if (word === undefined)
|
|
319
|
+
return false;
|
|
136
320
|
if (word === '(') {
|
|
137
321
|
pos++;
|
|
138
|
-
or()
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return;
|
|
322
|
+
if (!or() || argv[pos] !== ')')
|
|
323
|
+
return false;
|
|
324
|
+
pos++;
|
|
325
|
+
return true;
|
|
142
326
|
}
|
|
143
|
-
|
|
327
|
+
const operator = argv.at(pos + 1);
|
|
328
|
+
const right = argv.at(pos + 2);
|
|
329
|
+
if (operator !== undefined && right !== undefined && BINARY_OPERATORS.has(operator)) {
|
|
144
330
|
pos += 3;
|
|
145
|
-
return;
|
|
331
|
+
return !INTEGER_TESTS.has(operator) || (isTestInteger(word) && isTestInteger(right));
|
|
146
332
|
}
|
|
147
|
-
if (UNARY_OPERATORS.has(word)
|
|
333
|
+
if (operator !== undefined && UNARY_OPERATORS.has(word)) {
|
|
148
334
|
if (word === '-v')
|
|
149
335
|
found.push(pos);
|
|
150
336
|
pos += 2;
|
|
151
|
-
|
|
337
|
+
if (word === '-v' || word === '-R')
|
|
338
|
+
return SCALAR_NAME.test(operator);
|
|
339
|
+
if (word === '-t')
|
|
340
|
+
return isTestInteger(operator);
|
|
341
|
+
return true;
|
|
152
342
|
}
|
|
153
343
|
pos++;
|
|
344
|
+
return true;
|
|
154
345
|
};
|
|
155
346
|
const and = () => {
|
|
156
|
-
term()
|
|
347
|
+
if (!term())
|
|
348
|
+
return false;
|
|
157
349
|
while (argv[pos] === '-a') {
|
|
158
350
|
pos++;
|
|
159
|
-
term()
|
|
351
|
+
if (!term())
|
|
352
|
+
return false;
|
|
160
353
|
}
|
|
354
|
+
return true;
|
|
161
355
|
};
|
|
162
356
|
const or = () => {
|
|
163
|
-
and()
|
|
357
|
+
if (!and())
|
|
358
|
+
return false;
|
|
164
359
|
while (argv[pos] === '-o') {
|
|
165
360
|
pos++;
|
|
166
|
-
and()
|
|
361
|
+
if (!and())
|
|
362
|
+
return false;
|
|
167
363
|
}
|
|
364
|
+
return true;
|
|
168
365
|
};
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
pos++;
|
|
174
|
-
}
|
|
366
|
+
// Arguments left over after a complete expression are Bash's "too many
|
|
367
|
+
// arguments" error, raised only after the expression has been evaluated,
|
|
368
|
+
// so a `-v` in it has already run.
|
|
369
|
+
or();
|
|
175
370
|
}
|
|
176
371
|
switch (argv.length) {
|
|
177
372
|
case 0:
|
|
@@ -181,14 +376,14 @@ function variableTestIndices(args) {
|
|
|
181
376
|
if (argv[0] !== '!')
|
|
182
377
|
twoArguments(0);
|
|
183
378
|
break;
|
|
379
|
+
// Three arguments that fit no three-argument form are Bash's "binary
|
|
380
|
+
// operator expected" error; Bash does not fall back to the expression parser.
|
|
184
381
|
case 3:
|
|
185
|
-
|
|
186
|
-
expression();
|
|
382
|
+
threeArguments(0);
|
|
187
383
|
break;
|
|
188
384
|
case 4:
|
|
189
385
|
if (argv[0] === '!') {
|
|
190
|
-
|
|
191
|
-
expression();
|
|
386
|
+
threeArguments(1);
|
|
192
387
|
}
|
|
193
388
|
else if (argv[0] === '(' && argv[3] === ')') {
|
|
194
389
|
if (argv[1] !== '!')
|
|
@@ -223,26 +418,12 @@ export const noTestV = createCompatRule({
|
|
|
223
418
|
create(context, gate) {
|
|
224
419
|
const { sourceCode } = context;
|
|
225
420
|
const transparentWrappers = resolveTransparentWrappers(context, TRANSPARENT_WRAPPERS);
|
|
421
|
+
let emptyPositionals;
|
|
226
422
|
/** The variable name shown in the message for `operand`. */
|
|
227
423
|
function nameOf(operand) {
|
|
228
|
-
const text = operand?.type === 'Word' ? sourceCode
|
|
424
|
+
const text = operand?.type === 'Word' ? resolveWordTextAtPosition(sourceCode, operand) : null;
|
|
229
425
|
return text !== null && VARIABLE_NAME.test(text) ? text : 'name';
|
|
230
426
|
}
|
|
231
|
-
/**
|
|
232
|
-
* The argument `word` gives `test`: its static text; a stand-in operand
|
|
233
|
-
* for a word that is one double-quoted string, which always makes exactly
|
|
234
|
-
* one argument whatever it expands to; or `null` for any other word, which
|
|
235
|
-
* may split into several arguments or none.
|
|
236
|
-
*/
|
|
237
|
-
function argumentText(word) {
|
|
238
|
-
const text = sourceCode.resolveWordText(word);
|
|
239
|
-
if (text !== null)
|
|
240
|
-
return text;
|
|
241
|
-
if (word.parts.length === 1 && word.parts[0].type === 'DblQuoted') {
|
|
242
|
-
return QUOTED_OPERAND;
|
|
243
|
-
}
|
|
244
|
-
return null;
|
|
245
|
-
}
|
|
246
427
|
return {
|
|
247
428
|
UnaryTest(node) {
|
|
248
429
|
if (node.op !== TsVarSet)
|
|
@@ -263,19 +444,24 @@ export const noTestV = createCompatRule({
|
|
|
263
444
|
if (!TEST_COMMANDS.has(command))
|
|
264
445
|
return;
|
|
265
446
|
// `chainIndices` index `node.args` directly, so slice before filtering.
|
|
266
|
-
|
|
447
|
+
const args = node.args.slice((invoked.chainIndices.at(-1) ?? 0) + 1);
|
|
448
|
+
const omitPositionals = args.some(quotedPositionals) &&
|
|
449
|
+
(emptyPositionals ??= callsWithEmptyPositionals(sourceCode)).has(node);
|
|
450
|
+
let words = args
|
|
451
|
+
.filter((word) => !omitPositionals || !quotedPositionals(word))
|
|
452
|
+
.map((word) => ({ word, facts: describeWordAtPosition(sourceCode, word) }))
|
|
453
|
+
.filter(({ facts }) => facts.fields.max !== 0);
|
|
267
454
|
if (command === '[') {
|
|
268
455
|
const last = words.at(-1);
|
|
269
|
-
if (last
|
|
456
|
+
if (last?.facts.resolution.static !== true || last.facts.resolution.text !== ']')
|
|
270
457
|
return;
|
|
271
458
|
words = words.slice(0, -1);
|
|
272
459
|
}
|
|
273
|
-
const
|
|
274
|
-
for (const index of variableTestIndices(texts)) {
|
|
460
|
+
for (const index of definiteVariableTests(words)) {
|
|
275
461
|
context.report({
|
|
276
|
-
node: words[index],
|
|
462
|
+
node: words[index].word,
|
|
277
463
|
messageId: 'testV',
|
|
278
|
-
data: { ...gate, name: nameOf(words[index + 1]) },
|
|
464
|
+
data: { ...gate, name: nameOf(words[index + 1]?.word) },
|
|
279
465
|
});
|
|
280
466
|
}
|
|
281
467
|
},
|