@eslint-sh/eslint-plugin 0.12.0 → 0.13.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 +5 -2
- package/dist/api.d.ts +1 -1
- 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 +25 -11
- package/dist/language/command-resolution.d.ts +16 -6
- package/dist/language/command-resolution.js +44 -17
- package/dist/language/shell-source-code.d.ts +18 -10
- package/dist/language/shell-source-code.js +24 -25
- package/dist/language/word-resolution.d.ts +18 -12
- package/dist/language/word-resolution.js +133 -3
- package/dist/rules/compat/bash-builtin-destination.d.ts +2 -0
- package/dist/rules/compat/bash-builtin-destination.js +4 -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 +16 -19
- 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 +38 -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 +243 -61
- 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 +54 -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 +197 -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 +10 -7
- package/docs/error-codes.md +4 -0
- package/docs/examples/no-curl-in-functions-rule.mjs +7 -5
- package/docs/rules/README.md +4 -4
- package/docs/rules/compat/no-mapfile.md +11 -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 +7 -1
- 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 +2 -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/wrapper-rules.md +1 -1
- package/package.json +4 -4
|
@@ -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,21 +88,208 @@ 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) {
|
|
@@ -123,55 +311,58 @@ function variableTestIndices(args) {
|
|
|
123
311
|
function expression() {
|
|
124
312
|
let pos = 0;
|
|
125
313
|
const term = () => {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const word = argv
|
|
129
|
-
if (word ===
|
|
130
|
-
|
|
131
|
-
while (argv[pos] === '!')
|
|
132
|
-
pos++;
|
|
133
|
-
term();
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
314
|
+
while (argv[pos] === '!')
|
|
315
|
+
pos++;
|
|
316
|
+
const word = argv.at(pos);
|
|
317
|
+
if (word === undefined)
|
|
318
|
+
return false;
|
|
136
319
|
if (word === '(') {
|
|
137
320
|
pos++;
|
|
138
|
-
or()
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return;
|
|
321
|
+
if (!or() || argv[pos] !== ')')
|
|
322
|
+
return false;
|
|
323
|
+
pos++;
|
|
324
|
+
return true;
|
|
142
325
|
}
|
|
143
|
-
|
|
326
|
+
const operator = argv.at(pos + 1);
|
|
327
|
+
const right = argv.at(pos + 2);
|
|
328
|
+
if (operator !== undefined && right !== undefined && BINARY_OPERATORS.has(operator)) {
|
|
144
329
|
pos += 3;
|
|
145
|
-
return;
|
|
330
|
+
return !INTEGER_TESTS.has(operator) || (isTestInteger(word) && isTestInteger(right));
|
|
146
331
|
}
|
|
147
|
-
if (UNARY_OPERATORS.has(word)
|
|
332
|
+
if (operator !== undefined && UNARY_OPERATORS.has(word)) {
|
|
148
333
|
if (word === '-v')
|
|
149
334
|
found.push(pos);
|
|
150
335
|
pos += 2;
|
|
151
|
-
|
|
336
|
+
if (word === '-v' || word === '-R')
|
|
337
|
+
return SCALAR_NAME.test(operator);
|
|
338
|
+
if (word === '-t')
|
|
339
|
+
return isTestInteger(operator);
|
|
340
|
+
return true;
|
|
152
341
|
}
|
|
153
342
|
pos++;
|
|
343
|
+
return true;
|
|
154
344
|
};
|
|
155
345
|
const and = () => {
|
|
156
|
-
term()
|
|
346
|
+
if (!term())
|
|
347
|
+
return false;
|
|
157
348
|
while (argv[pos] === '-a') {
|
|
158
349
|
pos++;
|
|
159
|
-
term()
|
|
350
|
+
if (!term())
|
|
351
|
+
return false;
|
|
160
352
|
}
|
|
353
|
+
return true;
|
|
161
354
|
};
|
|
162
355
|
const or = () => {
|
|
163
|
-
and()
|
|
356
|
+
if (!and())
|
|
357
|
+
return false;
|
|
164
358
|
while (argv[pos] === '-o') {
|
|
165
359
|
pos++;
|
|
166
|
-
and()
|
|
360
|
+
if (!and())
|
|
361
|
+
return false;
|
|
167
362
|
}
|
|
363
|
+
return true;
|
|
168
364
|
};
|
|
169
|
-
|
|
170
|
-
const before = pos;
|
|
171
|
-
or();
|
|
172
|
-
if (pos === before)
|
|
173
|
-
pos++;
|
|
174
|
-
}
|
|
365
|
+
or();
|
|
175
366
|
}
|
|
176
367
|
switch (argv.length) {
|
|
177
368
|
case 0:
|
|
@@ -223,26 +414,12 @@ export const noTestV = createCompatRule({
|
|
|
223
414
|
create(context, gate) {
|
|
224
415
|
const { sourceCode } = context;
|
|
225
416
|
const transparentWrappers = resolveTransparentWrappers(context, TRANSPARENT_WRAPPERS);
|
|
417
|
+
let emptyPositionals;
|
|
226
418
|
/** The variable name shown in the message for `operand`. */
|
|
227
419
|
function nameOf(operand) {
|
|
228
|
-
const text = operand?.type === 'Word' ? sourceCode
|
|
420
|
+
const text = operand?.type === 'Word' ? resolveWordTextAtPosition(sourceCode, operand) : null;
|
|
229
421
|
return text !== null && VARIABLE_NAME.test(text) ? text : 'name';
|
|
230
422
|
}
|
|
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
423
|
return {
|
|
247
424
|
UnaryTest(node) {
|
|
248
425
|
if (node.op !== TsVarSet)
|
|
@@ -263,19 +440,24 @@ export const noTestV = createCompatRule({
|
|
|
263
440
|
if (!TEST_COMMANDS.has(command))
|
|
264
441
|
return;
|
|
265
442
|
// `chainIndices` index `node.args` directly, so slice before filtering.
|
|
266
|
-
|
|
443
|
+
const args = node.args.slice((invoked.chainIndices.at(-1) ?? 0) + 1);
|
|
444
|
+
const omitPositionals = args.some(quotedPositionals) &&
|
|
445
|
+
(emptyPositionals ??= callsWithEmptyPositionals(sourceCode)).has(node);
|
|
446
|
+
let words = args
|
|
447
|
+
.filter((word) => !omitPositionals || !quotedPositionals(word))
|
|
448
|
+
.map((word) => ({ word, facts: describeWordAtPosition(sourceCode, word) }))
|
|
449
|
+
.filter(({ facts }) => facts.fields.max !== 0);
|
|
267
450
|
if (command === '[') {
|
|
268
451
|
const last = words.at(-1);
|
|
269
|
-
if (last
|
|
452
|
+
if (last?.facts.resolution.static !== true || last.facts.resolution.text !== ']')
|
|
270
453
|
return;
|
|
271
454
|
words = words.slice(0, -1);
|
|
272
455
|
}
|
|
273
|
-
const
|
|
274
|
-
for (const index of variableTestIndices(texts)) {
|
|
456
|
+
for (const index of definiteVariableTests(words)) {
|
|
275
457
|
context.report({
|
|
276
|
-
node: words[index],
|
|
458
|
+
node: words[index].word,
|
|
277
459
|
messageId: 'testV',
|
|
278
|
-
data: { ...gate, name: nameOf(words[index + 1]) },
|
|
460
|
+
data: { ...gate, name: nameOf(words[index + 1]?.word) },
|
|
279
461
|
});
|
|
280
462
|
}
|
|
281
463
|
},
|
|
@@ -15,9 +15,11 @@
|
|
|
15
15
|
* (Bash 5.1's `wait -p var`) is its operand, not an option. The report is on
|
|
16
16
|
* the option 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 { 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 `wait` command: `sh-analysis`'s
|
|
@@ -27,8 +29,6 @@ const TRANSPARENT_WRAPPERS = [
|
|
|
27
29
|
...DEFAULT_TRANSPARENT_WRAPPERS,
|
|
28
30
|
BUILTIN_WRAPPER,
|
|
29
31
|
];
|
|
30
|
-
/** An option cluster of `wait`: letters after a single `-`. */
|
|
31
|
-
const OPTION_CLUSTER = /^-[A-Za-z]+$/u;
|
|
32
32
|
/**
|
|
33
33
|
* The `sh/compat/no-wait-n` rule module.
|
|
34
34
|
*
|
|
@@ -57,17 +57,48 @@ export const noWaitN = createCompatRule({
|
|
|
57
57
|
if (invoked?.effective.static !== true || invoked.effective.text !== 'wait')
|
|
58
58
|
return;
|
|
59
59
|
// `chainIndices` index `node.args` directly, so slice before filtering.
|
|
60
|
-
const words = node.args
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
60
|
+
const words = node.args
|
|
61
|
+
.slice((invoked.chainIndices.at(-1) ?? 0) + 1)
|
|
62
|
+
.map((word) => ({ word, facts: describeWordAtPosition(sourceCode, word) }))
|
|
63
|
+
.filter(({ facts }) => facts.fields.max !== 0);
|
|
64
|
+
let nextOption;
|
|
65
|
+
options: for (let index = 0; index < words.length; index++) {
|
|
66
|
+
const { word, facts } = words[index];
|
|
67
|
+
if (!facts.resolution.static || !isSingleField(facts))
|
|
68
|
+
break;
|
|
69
|
+
const { text } = facts.resolution;
|
|
70
|
+
if (!text.startsWith('-') || text === '-' || text === '--')
|
|
71
|
+
break;
|
|
72
|
+
for (let flag = 1; flag < text.length; flag++) {
|
|
73
|
+
if (text[flag] === 'n') {
|
|
74
|
+
nextOption ??= word;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (text[flag] === 'p') {
|
|
78
|
+
const attached = text.slice(flag + 1);
|
|
79
|
+
if (attached === '') {
|
|
80
|
+
const destination = words.at(++index)?.facts;
|
|
81
|
+
if (destination === undefined)
|
|
82
|
+
return;
|
|
83
|
+
// The next source word could supply the destination and more
|
|
84
|
+
// fields (such as --). Keep any -n already encountered.
|
|
85
|
+
if (!isSingleField(destination))
|
|
86
|
+
break options;
|
|
87
|
+
if (destination.resolution.static &&
|
|
88
|
+
!isBashBuiltinDestination(destination.resolution.text))
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
else if (!isBashBuiltinDestination(attached)) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
if (text[flag] !== 'f')
|
|
97
|
+
return;
|
|
68
98
|
}
|
|
69
|
-
|
|
70
|
-
|
|
99
|
+
}
|
|
100
|
+
if (nextOption !== undefined) {
|
|
101
|
+
context.report({ node: nextOption, messageId: 'waitNext', data: { ...gate } });
|
|
71
102
|
}
|
|
72
103
|
},
|
|
73
104
|
};
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { ShellSourceCode } from '../../language/shell-source-code.js';
|
|
2
2
|
import type { ShellNodeOf } from '../../language/syntax-view.js';
|
|
3
|
+
/** How the local proof treats an array expansion. @internal */
|
|
4
|
+
export type ArraySafety = 'safe' | 'unresolved-word' | 'unproven';
|
|
3
5
|
/**
|
|
4
6
|
* Binds a proof query to one parsed file. The rule retains responsibility for
|
|
5
7
|
* selecting expansions and version gating; this helper only answers safety.
|
|
6
8
|
* @internal
|
|
7
9
|
*/
|
|
8
|
-
export declare function createArraySafetyProof(source: ShellSourceCode): (node: ShellNodeOf<'ParamExp'>, name: string) =>
|
|
10
|
+
export declare function createArraySafetyProof(source: ShellSourceCode): (node: ShellNodeOf<'ParamExp'>, name: string) => ArraySafety;
|