Skip to content

Commit 4fe2b8c

Browse files
fix(javascript): convert ESTree as terser does with corrections off
The port of terser's conversion is held to terser's with the correct phase off, as every other correction is: an arrow's directive and an anonymous default export are read only while it is on. Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4
1 parent 44d89f2 commit 4fe2b8c

3 files changed

Lines changed: 26 additions & 12 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11774,6 +11774,13 @@ const installEstree = (modules) => {
1177411774
return body;
1177511775
};
1177611776

11777+
/**
11778+
* @returns {boolean} whether the `correct` phase is on, which reads a given
11779+
* ESTree tree where terser's conversion changes or throws on it
11780+
*/
11781+
const correcting = () =>
11782+
modules.corrections !== undefined && modules.corrections.enabled;
11783+
1177711784
/**
1177811785
* A function's block, its leading string statements read as directives.
1177911786
* @param {EXPECTED_ANY} estree the block
@@ -12256,8 +12263,7 @@ const installEstree = (modules) => {
1225612263
// parser reads one, so the body turns sloppy: corrections read it too.
1225712264
const body =
1225812265
estree.body.type === "BlockStatement"
12259-
? tokenAt === null &&
12260-
!(modules.corrections !== undefined && modules.corrections.enabled)
12266+
? tokenAt === null && !correcting()
1226112267
? fromEstree(estree.body)
1226212268
: blockFromEstree(estree.body)
1226312269
: BlockStatementNode({
@@ -12647,8 +12653,11 @@ const installEstree = (modules) => {
1264712653
// its parser exports a named one as a declaration and an anonymous class
1264812654
// or function as the expression it is; `drop_unused` then reads a name
1264912655
// the value has not got. Only a tree we parsed ourselves is ours to fix,
12650-
// but an anonymous one, which terser's conversion cannot read at all.
12651-
if ((tokenAt === null && declaration.id) || (!isClass && !isFunction)) {
12656+
// and, while correcting, an anonymous one, which terser's throws on.
12657+
if (
12658+
(tokenAt === null && (declaration.id || !correcting())) ||
12659+
(!isClass && !isFunction)
12660+
) {
1265212661
return makeNode(ExportNode, undefined, {
1265312662
startToken: start,
1265412663
endToken: end,

‎test/unitCases/syntax-printer-estree.unittest.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,12 @@ describe("syntax-printer's port of terser's ESTree conversion", () => {
262262
beforeAll(async () => {
263263
modules.theirs = await loadTerserSources(importTerserSource);
264264
modules.ours = (await load()).modules;
265+
// terser's conversion is the one with corrections off.
266+
modules.ours.corrections.enabled = false;
267+
});
268+
269+
afterAll(() => {
270+
modules.ours.corrections.enabled = true;
265271
});
266272

267273
/**

‎test/unitCases/syntax-printer.unittest.js‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6263,7 +6263,7 @@ describe("syntax-printer", () => {
62636263
);
62646264
});
62656265

6266-
it("should read an anonymous default export, which terser throws on", async () => {
6266+
it("should read an anonymous default export, which terser throws on as it does with corrections off", async () => {
62676267
const { minify } = await load();
62686268
for (const code of [
62696269
"export default function (a) { return a; } console.log(1);",
@@ -6283,14 +6283,13 @@ describe("syntax-printer", () => {
62836283
});
62846284
expect(fromTree.code).toBe(fromSource.code);
62856285
}
6286+
/** @type {EXPECTED_ANY} */
6287+
const options = { module: true, parse: { spidermonkey: true } };
62866288
await expect(
6287-
terserReference().minify(
6288-
estree(code),
6289-
/** @type {EXPECTED_ANY} */ ({
6290-
module: true,
6291-
parse: { spidermonkey: true }
6292-
})
6293-
)
6289+
terserReference().minify(estree(code), options)
6290+
).rejects.toThrow("definition");
6291+
await expect(
6292+
uncorrected(() => minify(estree(code), options))
62946293
).rejects.toThrow("definition");
62956294
}
62966295
});

0 commit comments

Comments
 (0)