@eslint-sh/eslint-plugin 0.10.0 → 0.11.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/dist/eslint-plugin-api.d.ts +6 -5
- package/dist/language/shell-source-code.d.ts +6 -5
- package/dist/language/shell-source-code.js +20 -18
- package/dist/language/syntax-clone.d.ts +29 -0
- package/dist/language/syntax-clone.js +76 -0
- package/dist/language/syntax-view.d.ts +22 -1
- package/dist/language/syntax-view.js +166 -33
- package/dist/rules/compat/no-test-v.js +3 -1
- package/dist/rules/compat/nounset-array-proof.js +16 -9
- package/dist/rules/no-masked-exit-status.d.ts +12 -0
- package/dist/rules/no-masked-exit-status.js +26 -16
- package/dist/rules/no-unchecked-cd.js +27 -20
- package/dist/rules/prefer-local.js +10 -8
- package/package.json +6 -6
|
@@ -1200,11 +1200,12 @@ export declare interface ShellRuleVisitor extends ShellTypedListeners {
|
|
|
1200
1200
|
* was parsed from: the first half of a printed fix. Edit the copy, then pass
|
|
1201
1201
|
* it to {@link ShellSourceCode.getPrintedReplacement}.
|
|
1202
1202
|
*
|
|
1203
|
-
* The copy is `structuredClone` of the record
|
|
1204
|
-
* {@link ShellSourceCode.getSyntax} returns
|
|
1205
|
-
*
|
|
1206
|
-
*
|
|
1207
|
-
*
|
|
1203
|
+
* The copy is what `structuredClone` of the record
|
|
1204
|
+
* {@link ShellSourceCode.getSyntax} returns would be, which sh-ast documents
|
|
1205
|
+
* and tests as the supported way to make an independent editable copy of a
|
|
1206
|
+
* syntax subtree (sh-ast 0.7.0 README, "Editing and printing"), so no second
|
|
1207
|
+
* parse is involved. It is made without recursion per tree level, so a
|
|
1208
|
+
* subtree of any depth can be copied. The copy owns its records,
|
|
1208
1209
|
* arrays, positions and byte buffers. Editing it leaves this file's original
|
|
1209
1210
|
* syntax, its lint view, its cached traversal and the answers of view-aware
|
|
1210
1211
|
* helpers such as {@link ShellSourceCode.resolveWord} and
|
|
@@ -279,11 +279,12 @@ export declare class ShellSourceCode extends TextSourceCodeBase<{
|
|
|
279
279
|
* was parsed from: the first half of a printed fix. Edit the copy, then pass
|
|
280
280
|
* it to {@link ShellSourceCode.getPrintedReplacement}.
|
|
281
281
|
*
|
|
282
|
-
* The copy is `structuredClone` of the record
|
|
283
|
-
* {@link ShellSourceCode.getSyntax} returns
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
*
|
|
282
|
+
* The copy is what `structuredClone` of the record
|
|
283
|
+
* {@link ShellSourceCode.getSyntax} returns would be, which sh-ast documents
|
|
284
|
+
* and tests as the supported way to make an independent editable copy of a
|
|
285
|
+
* syntax subtree (sh-ast 0.7.0 README, "Editing and printing"), so no second
|
|
286
|
+
* parse is involved. It is made without recursion per tree level, so a
|
|
287
|
+
* subtree of any depth can be copied. The copy owns its records,
|
|
287
288
|
* arrays, positions and byte buffers. Editing it leaves this file's original
|
|
288
289
|
* syntax, its lint view, its cached traversal and the answers of view-aware
|
|
289
290
|
* helpers such as {@link ShellSourceCode.resolveWord} and
|
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* their classes, codes and causes.
|
|
8
8
|
*/
|
|
9
9
|
import { ConfigCommentParser, Directive, TextSourceCodeBase, VisitNodeStep, } from '@eslint/plugin-kit';
|
|
10
|
-
import {
|
|
10
|
+
import { isShellComment, projectedComments, projectedCoordinates, syntaxForNode, traverseShell, viewForSyntax, walkShell, } from './syntax-view.js';
|
|
11
|
+
import { cloneSyntax } from './syntax-clone.js';
|
|
11
12
|
import { SourceCoordinates } from './source-coordinates.js';
|
|
12
13
|
import { SYNTAX_INTERFACES, printSync } from 'sh-ast';
|
|
13
14
|
import { resolveArgv0 } from 'sh-analysis';
|
|
@@ -687,11 +688,12 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
687
688
|
* was parsed from: the first half of a printed fix. Edit the copy, then pass
|
|
688
689
|
* it to {@link ShellSourceCode.getPrintedReplacement}.
|
|
689
690
|
*
|
|
690
|
-
* The copy is `structuredClone` of the record
|
|
691
|
-
* {@link ShellSourceCode.getSyntax} returns
|
|
692
|
-
*
|
|
693
|
-
*
|
|
694
|
-
*
|
|
691
|
+
* The copy is what `structuredClone` of the record
|
|
692
|
+
* {@link ShellSourceCode.getSyntax} returns would be, which sh-ast documents
|
|
693
|
+
* and tests as the supported way to make an independent editable copy of a
|
|
694
|
+
* syntax subtree (sh-ast 0.7.0 README, "Editing and printing"), so no second
|
|
695
|
+
* parse is involved. It is made without recursion per tree level, so a
|
|
696
|
+
* subtree of any depth can be copied. The copy owns its records,
|
|
695
697
|
* arrays, positions and byte buffers. Editing it leaves this file's original
|
|
696
698
|
* syntax, its lint view, its cached traversal and the answers of view-aware
|
|
697
699
|
* helpers such as {@link ShellSourceCode.resolveWord} and
|
|
@@ -711,7 +713,7 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
711
713
|
* file, or an object that did not come from the parser.
|
|
712
714
|
*/
|
|
713
715
|
getEditableSyntax(node) {
|
|
714
|
-
return
|
|
716
|
+
return cloneSyntax(this.#originalSyntaxFor(node));
|
|
715
717
|
}
|
|
716
718
|
/**
|
|
717
719
|
* Prints `edited` with sh-ast's `printSync` and pairs the text with the
|
|
@@ -879,17 +881,17 @@ export class ShellSourceCode extends TextSourceCodeBase {
|
|
|
879
881
|
}
|
|
880
882
|
const steps = [];
|
|
881
883
|
this.#steps = steps;
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
884
|
+
traverseShell(this.ast, {
|
|
885
|
+
enter: (node, parent) => {
|
|
886
|
+
if (parent) {
|
|
887
|
+
this.#parents.set(node, parent);
|
|
888
|
+
}
|
|
889
|
+
steps.push(new VisitNodeStep({ target: node, phase: 1, args: [node, parent] }));
|
|
890
|
+
},
|
|
891
|
+
exit: (node, parent) => {
|
|
892
|
+
steps.push(new VisitNodeStep({ target: node, phase: 2, args: [node, parent] }));
|
|
893
|
+
},
|
|
894
|
+
});
|
|
893
895
|
return steps.values();
|
|
894
896
|
}
|
|
895
897
|
/**
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deep copy of sh-ast syntax records that does not recurse once per tree level.
|
|
3
|
+
*
|
|
4
|
+
* `structuredClone` recurses natively, so V8 overflows its stack on a record
|
|
5
|
+
* graph nested a thousand or so levels deep, such as the left-nested records of
|
|
6
|
+
* a long `a && b && …` list. {@link cloneSyntax} gives the same result for the
|
|
7
|
+
* values a syntax tree is made of, walking the graph with an explicit work list
|
|
8
|
+
* so depth costs heap, not call stack.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Returns an independent deep copy of `value` with `structuredClone`'s
|
|
12
|
+
* semantics for syntax records: plain objects and arrays are copied entry by
|
|
13
|
+
* entry (own enumerable string keys, array holes and length preserved),
|
|
14
|
+
* `Uint8Array` byte strings are copied into fresh memory (a `Buffer` or an
|
|
15
|
+
* offset view does not share storage with the copy), a record reachable twice
|
|
16
|
+
* is copied once so shared and cyclic references keep their shape, and
|
|
17
|
+
* prototypes are not carried over. A function or symbol value fails with
|
|
18
|
+
* `structuredClone`'s `DataCloneError`.
|
|
19
|
+
*
|
|
20
|
+
* Supported input is what sh-ast records contain: plain objects, arrays,
|
|
21
|
+
* primitives and `Uint8Array`s. Any other object is handed to `structuredClone`
|
|
22
|
+
* as a whole, so a `Map` or `Set` is copied by it (records shared with the
|
|
23
|
+
* surrounding graph are duplicated) and a `Proxy` is copied as a plain object.
|
|
24
|
+
*
|
|
25
|
+
* No depth limit applies other than available memory.
|
|
26
|
+
*
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export declare function cloneSyntax<T>(value: T): T;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deep copy of sh-ast syntax records that does not recurse once per tree level.
|
|
3
|
+
*
|
|
4
|
+
* `structuredClone` recurses natively, so V8 overflows its stack on a record
|
|
5
|
+
* graph nested a thousand or so levels deep, such as the left-nested records of
|
|
6
|
+
* a long `a && b && …` list. {@link cloneSyntax} gives the same result for the
|
|
7
|
+
* values a syntax tree is made of, walking the graph with an explicit work list
|
|
8
|
+
* so depth costs heap, not call stack.
|
|
9
|
+
*/
|
|
10
|
+
/** Whether `value` is an ordinary object or array, the containers a syntax tree is built from. */
|
|
11
|
+
function isContainer(value) {
|
|
12
|
+
return Array.isArray(value) || Object.prototype.toString.call(value) === '[object Object]';
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Returns an independent deep copy of `value` with `structuredClone`'s
|
|
16
|
+
* semantics for syntax records: plain objects and arrays are copied entry by
|
|
17
|
+
* entry (own enumerable string keys, array holes and length preserved),
|
|
18
|
+
* `Uint8Array` byte strings are copied into fresh memory (a `Buffer` or an
|
|
19
|
+
* offset view does not share storage with the copy), a record reachable twice
|
|
20
|
+
* is copied once so shared and cyclic references keep their shape, and
|
|
21
|
+
* prototypes are not carried over. A function or symbol value fails with
|
|
22
|
+
* `structuredClone`'s `DataCloneError`.
|
|
23
|
+
*
|
|
24
|
+
* Supported input is what sh-ast records contain: plain objects, arrays,
|
|
25
|
+
* primitives and `Uint8Array`s. Any other object is handed to `structuredClone`
|
|
26
|
+
* as a whole, so a `Map` or `Set` is copied by it (records shared with the
|
|
27
|
+
* surrounding graph are duplicated) and a `Proxy` is copied as a plain object.
|
|
28
|
+
*
|
|
29
|
+
* No depth limit applies other than available memory.
|
|
30
|
+
*
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
export function cloneSyntax(value) {
|
|
34
|
+
const copies = new Map();
|
|
35
|
+
// eslint-disable-next-line @api-extractor-tools/forgotten-export -- The work-list entry type is private to this function.
|
|
36
|
+
const pending = [];
|
|
37
|
+
const copyOf = (item) => {
|
|
38
|
+
if (typeof item === 'symbol') {
|
|
39
|
+
// Same refusal, and error type, as structuredClone.
|
|
40
|
+
return structuredClone(item);
|
|
41
|
+
}
|
|
42
|
+
if (item === null || (typeof item !== 'object' && typeof item !== 'function')) {
|
|
43
|
+
return item;
|
|
44
|
+
}
|
|
45
|
+
const known = copies.get(item);
|
|
46
|
+
if (known !== undefined) {
|
|
47
|
+
return known;
|
|
48
|
+
}
|
|
49
|
+
let copy;
|
|
50
|
+
if (typeof item === 'function' || !isContainer(item)) {
|
|
51
|
+
copy = item instanceof Uint8Array ? new Uint8Array(item) : structuredClone(item);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
const container = Array.isArray(item)
|
|
55
|
+
? new Array(item.length)
|
|
56
|
+
: {};
|
|
57
|
+
pending.push({ source: item, copy: container });
|
|
58
|
+
copy = container;
|
|
59
|
+
}
|
|
60
|
+
copies.set(item, copy);
|
|
61
|
+
return copy;
|
|
62
|
+
};
|
|
63
|
+
const root = copyOf(value);
|
|
64
|
+
for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
|
|
65
|
+
for (const key of Object.keys(next.source)) {
|
|
66
|
+
// defineProperty keeps a key named `__proto__` an ordinary own property.
|
|
67
|
+
Object.defineProperty(next.copy, key, {
|
|
68
|
+
value: copyOf(next.source[key]),
|
|
69
|
+
writable: true,
|
|
70
|
+
enumerable: true,
|
|
71
|
+
configurable: true,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return root;
|
|
76
|
+
}
|
|
@@ -77,5 +77,26 @@ export declare function adaptSyntaxFile(file: File, source: string, positions: P
|
|
|
77
77
|
* @internal
|
|
78
78
|
*/
|
|
79
79
|
export declare function childNodes(node: ShellNode): ShellNode[];
|
|
80
|
-
/**
|
|
80
|
+
/**
|
|
81
|
+
* Visits `root` and every projected descendant depth-first in visitor-key order
|
|
82
|
+
* with an explicit stack, so a left-nested chain of any length costs heap, not
|
|
83
|
+
* call stack. `enter` runs before a node's children and `exit` after the last of
|
|
84
|
+
* them, each with the node and its parent (`undefined` for `root`). Every
|
|
85
|
+
* consumer traversal of the lint view goes through here; it makes no claim to
|
|
86
|
+
* upstream Walk control semantics.
|
|
87
|
+
* @internal
|
|
88
|
+
*/
|
|
89
|
+
export declare function traverseShell(root: ShellNode, handlers: {
|
|
90
|
+
readonly enter?: (node: ShellNode, parent: ShellNode | undefined) => void;
|
|
91
|
+
readonly exit?: (node: ShellNode, parent: ShellNode | undefined) => void;
|
|
92
|
+
}): void;
|
|
93
|
+
/** Visits `root` and every projected descendant in pre-order, including auxiliary views. @internal */
|
|
81
94
|
export declare function walkShell(root: ShellNode, visit: (node: ShellNode) => void): void;
|
|
95
|
+
/**
|
|
96
|
+
* Whether `test` holds for `root` or any projected descendant, searching
|
|
97
|
+
* depth-first in pre-order with an explicit stack and stopping at the first
|
|
98
|
+
* `true`. Returning `'skip'` leaves that node's descendants unsearched without
|
|
99
|
+
* ending the search. `test` must not depend on visiting order beyond that.
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
102
|
+
export declare function someShellNode(root: ShellNode, test: (node: ShellNode) => boolean | 'skip'): boolean;
|
|
@@ -62,6 +62,8 @@ export const shellVisitorKeys = Object.freeze(Object.fromEntries(Object.entries(
|
|
|
62
62
|
.filter(([, field]) => field.kind === 'struct' || field.kind === 'interface')
|
|
63
63
|
.map(([fieldName]) => fieldName.toLowerCase())),
|
|
64
64
|
])));
|
|
65
|
+
/** Marks that a projection frame has no further child records to project. */
|
|
66
|
+
const FIELDS_DONE = Symbol('fieldsDone');
|
|
65
67
|
/** Checks record identity without pretending unknown transport values already have syntax types. */
|
|
66
68
|
function isRecord(value) {
|
|
67
69
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
@@ -192,8 +194,9 @@ export function adaptSyntaxFile(file, source, positions) {
|
|
|
192
194
|
const position = (raw, fallback) => raw.IsValid && !raw.IsRecovered ? coordinates.fromByteOffset(raw.Offset) : fallback;
|
|
193
195
|
/** Comment views in projection order; registered against the root once projection succeeds. */
|
|
194
196
|
const comments = [];
|
|
195
|
-
/**
|
|
196
|
-
|
|
197
|
+
/** Validates `raw`, observes its span and opens a frame for projecting its fields. */
|
|
198
|
+
// eslint-disable-next-line @api-extractor-tools/forgotten-export -- The frame type belongs only to the private projection closure, not the exported signature.
|
|
199
|
+
const openFrame = (raw, fallback) => {
|
|
197
200
|
if (!isRecord(raw) || typeof raw.Type !== 'string') {
|
|
198
201
|
throw Object.assign(new TypeError('shell view expected a known upstream syntax record'), {
|
|
199
202
|
code: 'ESLINT_SH_SYNTAX_VIEW',
|
|
@@ -206,7 +209,6 @@ export function adaptSyntaxFile(file, source, positions) {
|
|
|
206
209
|
});
|
|
207
210
|
}
|
|
208
211
|
const realNode = isSyntaxNode(raw);
|
|
209
|
-
// eslint-disable-next-line @api-extractor-tools/forgotten-export -- This span belongs only to the private projection closure, not the exported signature.
|
|
210
212
|
let span = fallback;
|
|
211
213
|
// Whether this node's span lost a CRLF carriage return (see `endsBeforeCrlfNewline`).
|
|
212
214
|
let trimmedCarriageReturn = false;
|
|
@@ -225,50 +227,90 @@ export function adaptSyntaxFile(file, source, positions) {
|
|
|
225
227
|
crlfTrimmedViews.add(raw);
|
|
226
228
|
}
|
|
227
229
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
230
|
+
return {
|
|
231
|
+
raw,
|
|
232
|
+
plan,
|
|
233
|
+
realNode,
|
|
234
|
+
span,
|
|
235
|
+
trimmedCarriageReturn,
|
|
236
|
+
// Schema fields first, in schema order, then `type`, `range` and `loc`.
|
|
237
|
+
view: {},
|
|
238
|
+
childStart: Number.POSITIVE_INFINITY,
|
|
239
|
+
childEnd: Number.NEGATIVE_INFINITY,
|
|
240
|
+
fieldIndex: 0,
|
|
241
|
+
sliceItems: undefined,
|
|
242
|
+
sliceIndex: 0,
|
|
243
|
+
sliceOut: [],
|
|
244
|
+
awaitingKey: undefined,
|
|
241
245
|
};
|
|
242
|
-
|
|
246
|
+
};
|
|
247
|
+
/** Records a projected child's extent; auxiliary records take their span from it. */
|
|
248
|
+
const include = (frame, child) => {
|
|
249
|
+
if (child.range[0] < frame.childStart) {
|
|
250
|
+
frame.childStart = child.range[0];
|
|
251
|
+
}
|
|
252
|
+
if (child.range[1] > frame.childEnd) {
|
|
253
|
+
frame.childEnd = child.range[1];
|
|
254
|
+
}
|
|
255
|
+
return child;
|
|
256
|
+
};
|
|
257
|
+
/**
|
|
258
|
+
* Advances `frame` through its plan until it needs a child record projected,
|
|
259
|
+
* and returns that record; `FIELDS_DONE` means every field is done (a missing
|
|
260
|
+
* child record is `undefined` and must still reach `openFrame` to be refused). Scalar fields
|
|
261
|
+
* are filled in as they are passed.
|
|
262
|
+
*/
|
|
263
|
+
const nextChild = (frame) => {
|
|
264
|
+
const { raw, plan, view } = frame;
|
|
265
|
+
while (frame.fieldIndex < plan.length) {
|
|
266
|
+
const field = plan[frame.fieldIndex];
|
|
243
267
|
const value = raw[field.name];
|
|
244
268
|
if (field.kind === 'child') {
|
|
245
269
|
if (field.slice) {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
270
|
+
if (frame.sliceItems === undefined) {
|
|
271
|
+
const items = value === null ? [] : value;
|
|
272
|
+
if (!Array.isArray(items)) {
|
|
273
|
+
throw Object.assign(new TypeError(`shell view expected ${raw.Type}.${field.name} to be a slice`), { code: 'ESLINT_SH_SYNTAX_VIEW' });
|
|
274
|
+
}
|
|
275
|
+
frame.sliceItems = items;
|
|
276
|
+
frame.sliceIndex = 0;
|
|
277
|
+
frame.sliceOut = [];
|
|
249
278
|
}
|
|
250
|
-
|
|
251
|
-
|
|
279
|
+
while (frame.sliceIndex < frame.sliceItems.length) {
|
|
280
|
+
const item = frame.sliceItems[frame.sliceIndex++];
|
|
252
281
|
if (item !== null) {
|
|
253
|
-
|
|
282
|
+
return item;
|
|
254
283
|
}
|
|
255
284
|
}
|
|
256
|
-
view[field.key] =
|
|
285
|
+
view[field.key] = frame.sliceOut;
|
|
286
|
+
frame.sliceItems = undefined;
|
|
257
287
|
}
|
|
258
288
|
else if (value !== null) {
|
|
259
|
-
|
|
289
|
+
frame.awaitingKey = field.key;
|
|
290
|
+
frame.fieldIndex++;
|
|
291
|
+
return value;
|
|
260
292
|
}
|
|
261
293
|
}
|
|
262
294
|
else {
|
|
263
295
|
const text = field.kind === 'text' ? sourceText(value) : value;
|
|
264
296
|
view[field.key] =
|
|
265
|
-
trimmedCarriageReturn && typeof text === 'string' && text.endsWith('\r')
|
|
297
|
+
frame.trimmedCarriageReturn && typeof text === 'string' && text.endsWith('\r')
|
|
266
298
|
? text.slice(0, -1)
|
|
267
299
|
: text;
|
|
268
300
|
}
|
|
301
|
+
frame.fieldIndex++;
|
|
269
302
|
}
|
|
270
|
-
|
|
271
|
-
|
|
303
|
+
return FIELDS_DONE;
|
|
304
|
+
};
|
|
305
|
+
/** Completes `frame`'s view once every field is projected. */
|
|
306
|
+
const closeFrame = (frame) => {
|
|
307
|
+
const { raw, view } = frame;
|
|
308
|
+
let { span } = frame;
|
|
309
|
+
if (!frame.realNode && frame.childStart !== Number.POSITIVE_INFINITY) {
|
|
310
|
+
span = {
|
|
311
|
+
start: coordinates.atIndex(frame.childStart),
|
|
312
|
+
end: coordinates.atIndex(frame.childEnd),
|
|
313
|
+
};
|
|
272
314
|
}
|
|
273
315
|
view.type = raw.Type;
|
|
274
316
|
view.range = [span.start.index, span.end.index];
|
|
@@ -280,12 +322,54 @@ export function adaptSyntaxFile(file, source, positions) {
|
|
|
280
322
|
if (isShellComment(node)) {
|
|
281
323
|
comments.push(node);
|
|
282
324
|
}
|
|
283
|
-
if (realNode) {
|
|
325
|
+
if (frame.realNode) {
|
|
284
326
|
originalSyntax.set(node, raw);
|
|
285
327
|
views.set(raw, node);
|
|
286
328
|
}
|
|
287
329
|
return node;
|
|
288
330
|
};
|
|
331
|
+
/**
|
|
332
|
+
* Projects `rootRecord` and everything under it with an explicit frame stack,
|
|
333
|
+
* visiting records in the same depth-first, schema-field order a recursive
|
|
334
|
+
* projection would, so errors, comment order and view key order are unchanged.
|
|
335
|
+
* A record that contains itself, directly or through descendants, would never
|
|
336
|
+
* finish projecting, so it is refused; a record reachable twice by different
|
|
337
|
+
* paths is projected once per path, as before.
|
|
338
|
+
*/
|
|
339
|
+
const project = (rootRecord, rootFallback) => {
|
|
340
|
+
const stack = [openFrame(rootRecord, rootFallback)];
|
|
341
|
+
// Records on the path from the root to the frame being projected.
|
|
342
|
+
const active = new Set([stack[0].raw]);
|
|
343
|
+
for (;;) {
|
|
344
|
+
const frame = stack[stack.length - 1];
|
|
345
|
+
const child = nextChild(frame);
|
|
346
|
+
if (child !== FIELDS_DONE) {
|
|
347
|
+
if (typeof child === 'object' && child !== null && active.has(child)) {
|
|
348
|
+
throw Object.assign(new TypeError(`shell view found a ${child.Type} record that contains itself`), { code: 'ESLINT_SH_SYNTAX_VIEW' });
|
|
349
|
+
}
|
|
350
|
+
const opened = openFrame(child, frame.span);
|
|
351
|
+
active.add(opened.raw);
|
|
352
|
+
stack.push(opened);
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
const done = closeFrame(frame);
|
|
356
|
+
stack.pop();
|
|
357
|
+
active.delete(frame.raw);
|
|
358
|
+
const parent = stack.length === 0 ? undefined : stack[stack.length - 1];
|
|
359
|
+
if (parent === undefined) {
|
|
360
|
+
return done;
|
|
361
|
+
}
|
|
362
|
+
include(parent, done);
|
|
363
|
+
if (parent.sliceItems !== undefined) {
|
|
364
|
+
parent.sliceOut.push(done);
|
|
365
|
+
}
|
|
366
|
+
else if (parent.awaitingKey !== undefined) {
|
|
367
|
+
// A single child: the parent is waiting on exactly one record for this key.
|
|
368
|
+
parent.view[parent.awaitingKey] = done;
|
|
369
|
+
parent.awaitingKey = undefined;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
};
|
|
289
373
|
const root = project(file, wholeSource);
|
|
290
374
|
if (!isShellFile(root)) {
|
|
291
375
|
throw Object.assign(new TypeError('shell view expected a File with statements'), {
|
|
@@ -322,10 +406,59 @@ export function childNodes(node) {
|
|
|
322
406
|
}
|
|
323
407
|
return children;
|
|
324
408
|
}
|
|
325
|
-
/**
|
|
409
|
+
/**
|
|
410
|
+
* Visits `root` and every projected descendant depth-first in visitor-key order
|
|
411
|
+
* with an explicit stack, so a left-nested chain of any length costs heap, not
|
|
412
|
+
* call stack. `enter` runs before a node's children and `exit` after the last of
|
|
413
|
+
* them, each with the node and its parent (`undefined` for `root`). Every
|
|
414
|
+
* consumer traversal of the lint view goes through here; it makes no claim to
|
|
415
|
+
* upstream Walk control semantics.
|
|
416
|
+
* @internal
|
|
417
|
+
*/
|
|
418
|
+
export function traverseShell(root, handlers) {
|
|
419
|
+
// eslint-disable-next-line @api-extractor-tools/forgotten-export -- The open-node type is private to this function.
|
|
420
|
+
const open = [];
|
|
421
|
+
const enter = (node, parent) => {
|
|
422
|
+
handlers.enter?.(node, parent);
|
|
423
|
+
open.push({ node, parent, children: childNodes(node), next: 0 });
|
|
424
|
+
};
|
|
425
|
+
enter(root, undefined);
|
|
426
|
+
while (open.length > 0) {
|
|
427
|
+
const current = open[open.length - 1];
|
|
428
|
+
if (current.next < current.children.length) {
|
|
429
|
+
enter(current.children[current.next++], current.node);
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
open.pop();
|
|
433
|
+
handlers.exit?.(current.node, current.parent);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
/** Visits `root` and every projected descendant in pre-order, including auxiliary views. @internal */
|
|
326
438
|
export function walkShell(root, visit) {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
439
|
+
traverseShell(root, { enter: visit });
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Whether `test` holds for `root` or any projected descendant, searching
|
|
443
|
+
* depth-first in pre-order with an explicit stack and stopping at the first
|
|
444
|
+
* `true`. Returning `'skip'` leaves that node's descendants unsearched without
|
|
445
|
+
* ending the search. `test` must not depend on visiting order beyond that.
|
|
446
|
+
* @internal
|
|
447
|
+
*/
|
|
448
|
+
export function someShellNode(root, test) {
|
|
449
|
+
const pending = [root];
|
|
450
|
+
for (let node = pending.pop(); node !== undefined; node = pending.pop()) {
|
|
451
|
+
const verdict = test(node);
|
|
452
|
+
if (verdict === true) {
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
if (verdict === 'skip') {
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
const children = childNodes(node);
|
|
459
|
+
for (let index = children.length - 1; index >= 0; index--) {
|
|
460
|
+
pending.push(children[index]);
|
|
461
|
+
}
|
|
330
462
|
}
|
|
463
|
+
return false;
|
|
331
464
|
}
|
|
@@ -127,7 +127,9 @@ function variableTestIndices(args) {
|
|
|
127
127
|
return;
|
|
128
128
|
const word = argv[pos];
|
|
129
129
|
if (word === '!') {
|
|
130
|
-
|
|
130
|
+
// A run of negations applies to the one term after it; consume them in a loop.
|
|
131
|
+
while (argv[pos] === '!')
|
|
132
|
+
pos++;
|
|
131
133
|
term();
|
|
132
134
|
return;
|
|
133
135
|
}
|
|
@@ -504,16 +504,23 @@ export function createArraySafetyProof(source) {
|
|
|
504
504
|
}
|
|
505
505
|
}
|
|
506
506
|
/** Entry facts never cross functions or loops; an if condition can prove its then arm. */
|
|
507
|
-
function entry(
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
507
|
+
function entry(first) {
|
|
508
|
+
// The left side of &&/|| runs first and no evidence flows to their right
|
|
509
|
+
// side, so a statement on the left spine of a list has the list's entry facts.
|
|
510
|
+
let stmt = first;
|
|
511
|
+
let parent;
|
|
512
|
+
for (;;) {
|
|
513
|
+
const cached = before.get(stmt);
|
|
514
|
+
if (cached !== undefined) {
|
|
515
|
+
return cached;
|
|
516
|
+
}
|
|
517
|
+
parent = source.getParent(stmt);
|
|
518
|
+
if (parent?.type !== 'BinaryCmd' || parent.x !== stmt)
|
|
519
|
+
break;
|
|
515
520
|
const outer = source.getParent(parent);
|
|
516
|
-
|
|
521
|
+
if (outer?.type !== 'Stmt')
|
|
522
|
+
return UNKNOWN;
|
|
523
|
+
stmt = outer;
|
|
517
524
|
}
|
|
518
525
|
let facts = UNKNOWN;
|
|
519
526
|
if (!variableAttributes &&
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ShellNode } from '../language/syntax-view.js';
|
|
1
2
|
/**
|
|
2
3
|
* The rule's single options object.
|
|
3
4
|
*
|
|
@@ -17,6 +18,17 @@ interface NoMaskedExitStatusOptions {
|
|
|
17
18
|
}
|
|
18
19
|
/** Message ids this rule reports with. @internal */
|
|
19
20
|
type NoMaskedExitStatusMessageId = 'maskedExitStatus' | 'declareThenAssign' | 'assignThenExport' | 'assignThenReadonly';
|
|
21
|
+
/**
|
|
22
|
+
* Whether `node` contains a command substitution whose status the declaration
|
|
23
|
+
* would replace. A substitution counts wherever it sits in the word, including
|
|
24
|
+
* inside an arithmetic expansion, a substring offset or an array subscript:
|
|
25
|
+
* in a plain assignment a failing substitution there fails the assignment
|
|
26
|
+
* (and so trips `set -e`) in Bash, dash, zsh and ksh93. A process
|
|
27
|
+
* substitution is not entered: it expands to a path, so there is no status.
|
|
28
|
+
*
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
export declare function containsCommandSubstitution(root: ShellNode): boolean;
|
|
20
32
|
/** Built-in rule `sh/no-masked-exit-status`. @internal */
|
|
21
33
|
declare const noMaskedExitStatus: import("./define-shell-rule.js").ShellRule<[NoMaskedExitStatusOptions], NoMaskedExitStatusMessageId>;
|
|
22
34
|
export { noMaskedExitStatus };
|
|
@@ -86,7 +86,7 @@
|
|
|
86
86
|
*/
|
|
87
87
|
import { DEFAULT_TRANSPARENT_WRAPPERS } from 'sh-analysis';
|
|
88
88
|
import { resolveTransparentWrappers } from './resolve-transparent-wrappers.js';
|
|
89
|
-
import {
|
|
89
|
+
import { someShellNode, walkShell } from '../language/syntax-view.js';
|
|
90
90
|
import { lineBreakOf } from '../language/line-breaks.js';
|
|
91
91
|
import { createRule } from './create-rule.js';
|
|
92
92
|
import { batsErrexitIsOn, isStatusChecked } from './no-unchecked-cd.js';
|
|
@@ -147,13 +147,15 @@ const TRANSPARENT_WRAPPERS = [
|
|
|
147
147
|
* in a plain assignment a failing substitution there fails the assignment
|
|
148
148
|
* (and so trips `set -e`) in Bash, dash, zsh and ksh93. A process
|
|
149
149
|
* substitution is not entered: it expands to a path, so there is no status.
|
|
150
|
+
*
|
|
151
|
+
* @internal
|
|
150
152
|
*/
|
|
151
|
-
function containsCommandSubstitution(
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
return false;
|
|
156
|
-
|
|
153
|
+
export function containsCommandSubstitution(root) {
|
|
154
|
+
return someShellNode(root, (node) => {
|
|
155
|
+
if (node.type === 'CmdSubst')
|
|
156
|
+
return true;
|
|
157
|
+
return node.type === 'ProcSubst' ? 'skip' : false;
|
|
158
|
+
});
|
|
157
159
|
}
|
|
158
160
|
/** Whether `text` mentions `name` as a whole identifier. */
|
|
159
161
|
function mentionsIdentifier(text, name) {
|
|
@@ -167,9 +169,13 @@ function mentionsIdentifier(text, name) {
|
|
|
167
169
|
* `$(echo "$x")` counts. Reads made by code the substitution runs are
|
|
168
170
|
* {@link runsUnseenCode}'s concern.
|
|
169
171
|
*/
|
|
170
|
-
function readsAnyName(
|
|
172
|
+
function readsAnyName(root, names, sourceCode) {
|
|
171
173
|
if (names.length === 0)
|
|
172
174
|
return false;
|
|
175
|
+
return someShellNode(root, (node) => readsNameHere(node, names, sourceCode));
|
|
176
|
+
}
|
|
177
|
+
/** Whether `node` itself, not its descendants, reads any of `names`. */
|
|
178
|
+
function readsNameHere(node, names, sourceCode) {
|
|
173
179
|
if (node.type === 'ParamExp') {
|
|
174
180
|
if (node.excl)
|
|
175
181
|
return true;
|
|
@@ -187,7 +193,7 @@ function readsAnyName(node, names, sourceCode) {
|
|
|
187
193
|
if (names.some((name) => mentionsIdentifier(arithmetic, name)))
|
|
188
194
|
return true;
|
|
189
195
|
}
|
|
190
|
-
return
|
|
196
|
+
return false;
|
|
191
197
|
}
|
|
192
198
|
/**
|
|
193
199
|
* Whether `node` sits inside a function body: a function definition or a bats
|
|
@@ -206,11 +212,11 @@ function insideFunction(node, sourceCode) {
|
|
|
206
212
|
*/
|
|
207
213
|
const CODE_RUNNING_BUILTINS = new Set(['eval', 'source', '.']);
|
|
208
214
|
/** The names of the functions defined anywhere in the file, at any depth. */
|
|
209
|
-
function collectFunctionNames(
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
215
|
+
function collectFunctionNames(root, into) {
|
|
216
|
+
walkShell(root, (node) => {
|
|
217
|
+
if (node.type === 'FuncDecl' && node.name !== undefined)
|
|
218
|
+
into.add(node.name.value);
|
|
219
|
+
});
|
|
214
220
|
return into;
|
|
215
221
|
}
|
|
216
222
|
/**
|
|
@@ -220,7 +226,11 @@ function collectFunctionNames(node, into) {
|
|
|
220
226
|
* earlier, so such code can see a different variable afterwards. A function
|
|
221
227
|
* defined in another file cannot be seen and is not detected.
|
|
222
228
|
*/
|
|
223
|
-
function runsUnseenCode(
|
|
229
|
+
function runsUnseenCode(root, functionNames, sourceCode, transparentWrappers) {
|
|
230
|
+
return someShellNode(root, (node) => runsUnseenCodeHere(node, functionNames, sourceCode, transparentWrappers));
|
|
231
|
+
}
|
|
232
|
+
/** Whether `node` itself, not its descendants, runs code the rule cannot read. */
|
|
233
|
+
function runsUnseenCodeHere(node, functionNames, sourceCode, transparentWrappers) {
|
|
224
234
|
if (node.type === 'CallExpr' && node.args.length > 0) {
|
|
225
235
|
// This check names the launched program; opaque child-interpreter text
|
|
226
236
|
// does not turn its program identity into dynamic lookup.
|
|
@@ -234,7 +244,7 @@ function runsUnseenCode(node, functionNames, sourceCode, transparentWrappers) {
|
|
|
234
244
|
if (CODE_RUNNING_BUILTINS.has(name) || functionNames.has(name))
|
|
235
245
|
return true;
|
|
236
246
|
}
|
|
237
|
-
return
|
|
247
|
+
return false;
|
|
238
248
|
}
|
|
239
249
|
/**
|
|
240
250
|
* The state of `errexit` before each top-level statement of `file`, from the
|
|
@@ -83,28 +83,35 @@ const BATS_ERREXIT_HOOKS = new Set([
|
|
|
83
83
|
* @internal
|
|
84
84
|
*/
|
|
85
85
|
export function isStatusChecked(stmt, parentOf) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
86
|
+
// Each construct that passes the status along moves the question to the enclosing statement.
|
|
87
|
+
for (let current = stmt;;) {
|
|
88
|
+
if (current.background)
|
|
89
|
+
return false;
|
|
90
|
+
const parent = parentOf(current);
|
|
91
|
+
if (parent === undefined)
|
|
92
|
+
return false;
|
|
93
|
+
let enclosing;
|
|
94
|
+
if (parent.type === 'BinaryCmd') {
|
|
95
|
+
if (parent.x === current)
|
|
96
|
+
return parent.op === AND_OP || parent.op === OR_OP;
|
|
97
|
+
// The right side's status is the whole list's status.
|
|
98
|
+
enclosing = parentOf(parent);
|
|
99
|
+
}
|
|
100
|
+
else if (parent.type === 'IfClause' || parent.type === 'WhileClause') {
|
|
101
|
+
return parent.cond.at(-1) === current;
|
|
102
|
+
}
|
|
103
|
+
else if (parent.type === 'Block' || parent.type === 'Subshell') {
|
|
104
|
+
if (parent.stmts.at(-1) !== current)
|
|
105
|
+
return false;
|
|
106
|
+
enclosing = parentOf(parent);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
if (enclosing?.type !== 'Stmt')
|
|
103
112
|
return false;
|
|
104
|
-
|
|
105
|
-
return groupStmt?.type === 'Stmt' && isStatusChecked(groupStmt, parentOf);
|
|
113
|
+
current = enclosing;
|
|
106
114
|
}
|
|
107
|
-
return false;
|
|
108
115
|
}
|
|
109
116
|
/**
|
|
110
117
|
* Whether bats' `errexit` is still in force when `stmt` runs, for a statement
|
|
@@ -124,19 +124,21 @@ function enablesLastpipe(root, sourceCode) {
|
|
|
124
124
|
*/
|
|
125
125
|
function scopeBindings(root, sourceCode, lastpipe) {
|
|
126
126
|
const found = [];
|
|
127
|
-
|
|
127
|
+
// Pre-order with an explicit stack: children are pushed in reverse so they pop in source order.
|
|
128
|
+
const pending = [root];
|
|
129
|
+
for (let node = pending.pop(); node !== undefined; node = pending.pop()) {
|
|
128
130
|
if (node.type === 'FuncDecl' || ISOLATED_SCOPES.has(node.type))
|
|
129
|
-
|
|
131
|
+
continue;
|
|
130
132
|
if (node.type === 'BinaryCmd' && PIPE_OPS.has(node.op)) {
|
|
131
133
|
if (lastpipe && node.y !== undefined)
|
|
132
|
-
|
|
133
|
-
|
|
134
|
+
pending.push(node.y);
|
|
135
|
+
continue;
|
|
134
136
|
}
|
|
135
137
|
found.push(...bindingsOf(node, sourceCode));
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
138
|
+
const children = childNodes(node);
|
|
139
|
+
for (let index = children.length - 1; index >= 0; index--)
|
|
140
|
+
pending.push(children[index]);
|
|
141
|
+
}
|
|
140
142
|
return found.sort((a, b) => a.node.range[0] - b.node.range[0]);
|
|
141
143
|
}
|
|
142
144
|
/** Built-in rule `sh/prefer-local`. @internal */
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-sh/eslint-plugin",
|
|
3
3
|
"description": "Shell script linting for ESLint, with built-in policy rules, shared presets, and custom rules over a typed shell AST",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.11.0",
|
|
5
5
|
"author": "Mike North <michael.l.north@gmail.com>",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@eslint/core": "^1.2.1",
|
|
8
8
|
"@eslint/plugin-kit": "^0.7.2",
|
|
9
|
-
"sh-ast": "^0.
|
|
10
|
-
"sh-analysis": "^0.
|
|
9
|
+
"sh-ast": "^0.12.0",
|
|
10
|
+
"sh-analysis": "^0.2.0"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"eslint": "^10.
|
|
13
|
+
"eslint": "^10.14.0"
|
|
14
14
|
},
|
|
15
15
|
"peerDependencies": {
|
|
16
16
|
"eslint": "^10.2.0"
|
|
@@ -69,11 +69,11 @@
|
|
|
69
69
|
"build": "node ../../scripts/materialize-shared-source.mjs && tsc -p tsconfig.json && node ../../scripts/rollup-declarations.mjs api-extractor.json && cp types/eslint-plugin.d.ts dist/eslint-plugin.d.ts",
|
|
70
70
|
"check": "concurrently -n lint,types,api-report \"pnpm run check:lint-ts\" \"pnpm run check:typecheck\" \"pnpm run check:api-report\"",
|
|
71
71
|
"check:api-report": "api-extractor run",
|
|
72
|
-
"check:lint-ts": "eslint .",
|
|
72
|
+
"check:lint-ts": "cd ../.. && eslint --config eslint.config.js packages/eslint-plugin",
|
|
73
73
|
"check:typecheck": "tsc --noEmit",
|
|
74
74
|
"clean": "rimraf --glob dist \"temp/*\" \"*.tsbuildinfo\"",
|
|
75
75
|
"fix:api-report": "api-extractor run --local",
|
|
76
|
-
"fix:lint-ts": "eslint . --fix",
|
|
76
|
+
"fix:lint-ts": "cd ../.. && eslint --config eslint.config.js packages/eslint-plugin --fix",
|
|
77
77
|
"test": "vitest run",
|
|
78
78
|
"uplevel-api-reports": "mkdir -p ../../api-report-combined ../../api-docs-temp && cp api-report/eslint-plugin.api.md ../../api-report-combined/ && cp temp/eslint-plugin.api.json ../../api-docs-temp/",
|
|
79
79
|
"test:candidate": "node --experimental-strip-types test/candidate/run.mjs"
|