Skip to content

Commit e6c792c

Browse files
alexander-akaitclaude
authored andcommitted
fix(javascript): keep an import or export a loop runs in a SyntaxTree
With allowImportExportEverywhere, a for or while body can be an import or export declaration, which stays a node object: _treeNode wrote it into the tree's fourth slot unconverted, and the body was lost. Claude-Session: https://claude.ai/code/session_017TkBWTk3wcyUcJYDp5ZhaP
1 parent 8569e69 commit e6c792c

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

‎lib/javascript/syntax-parser.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10421,13 +10421,13 @@ class WebpackParser extends BaseParser {
1042110421
* @param {AnyNode | number | null=} slot0 its record's first field
1042210422
* @param {AnyNode | number | null=} slot1 the second
1042310423
* @param {AnyNode | number | null=} slot2 the third
10424-
* @param {AnyNode | number | null=} slot3 the fourth, only a for statement's body: written already
10424+
* @param {AnyNode | number | null=} slot3 the fourth, only a for statement's body
1042510425
* @returns {NodeOf<T>} it, standing as its id
1042610426
*/
1042710427
_treeNode(type, start, flags, slot0, slot1, slot2, slot3) {
1042810428
// WHY: a child is mostly written already or absent, and a call costs
10429-
// more than the test, so `_treeId` is called only for an object; a
10430-
// statement never stays one, so the fourth slot needs no test.
10429+
// more than the test, so `_treeId` is called only for an object; a for
10430+
// body is one when it is an import or export allowed everywhere.
1043110431
return /** @type {NodeOf<T>} */ (
1043210432
/** @type {unknown} */ (
1043310433
/** @type {SyntaxTree} */ (this._tree).addNode(
@@ -10444,7 +10444,9 @@ class WebpackParser extends BaseParser {
1044410444
typeof slot2 !== "object" || slot2 === null
1044510445
? slot2
1044610446
: this._treeId(slot2),
10447-
/** @type {number | undefined} */ (slot3)
10447+
typeof slot3 !== "object" || slot3 === null
10448+
? slot3
10449+
: this._treeId(slot3)
1044810450
)
1044910451
)
1045010452
);

‎test/unitCases/javascript/parser.unittest.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5680,6 +5680,25 @@ describe("WebpackParser writing a SyntaxTree", () => {
56805680
);
56815681
});
56825682

5683+
it("writes an import or export a loop runs, where they are allowed everywhere", () => {
5684+
for (const source of [
5685+
'for (;;) import a from "b";',
5686+
"while (x) export const c = 1;",
5687+
"let d; for (d of e) export { d };"
5688+
]) {
5689+
const everywhere = { ...options, allowImportExportEverywhere: true };
5690+
const tree = new SyntaxTree();
5691+
const program = /** @type {number} */ (
5692+
/** @type {unknown} */ (
5693+
new WebpackParser({ ...everywhere, syntaxTree: tree }, source).parse()
5694+
)
5695+
);
5696+
expect(plain(toEstreeObject(tree, program))).toEqual(
5697+
plain(new WebpackParser(everywhere, source).parse())
5698+
);
5699+
}
5700+
});
5701+
56835702
it.each([
56845703
"({ ...{ a } } = b);",
56855704
"({ get a() {} } = b);",

0 commit comments

Comments
 (0)