Skip to content

Commit 4e66509

Browse files
fix(javascript): read and write ESTree trees the minifier threw on or changed (#22626)
* fix(javascript): read and write ESTree trees the minifier threw on or changed Given as ESTree, an arrow kept its `"use strict"` as a statement, so its body ran sloppy, and an anonymous `export default` function or class threw; given back as ESTree, a template inlined into another threw. Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4 * 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 * fix(javascript): read only ESTree's directives as directives With corrections on and no source held, a parenthesized or escaped leading string given as ESTree no longer turns a body strict; the tree's own `directive` field decides. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4 * fix(javascript): write an untagged template's raw text from its value The compressor changes only a template segment's value, so ESTree output kept the source's raw text, which another printer reading `raw` emits. With corrections on, an untagged template's raw is written anew. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4 * fix(javascript): type the template escape's match Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4 * docs(changeset): name ESTree trees among the minifier fixes Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4 --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 2c3d32e commit 4e66509

4 files changed

Lines changed: 244 additions & 20 deletions

File tree

‎.changeset/025-javascript-minifier-corrections.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Fix JavaScript minifier crashes, altered behavior and unparsable output, including parameter aliases.
5+
Fix JavaScript minifier crashes, altered behavior and unparsable output, including parameter aliases and ESTree trees.

‎lib/javascript/syntax-printer.js‎

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11746,9 +11746,10 @@ const installEstree = (modules) => {
1174611746
/**
1174711747
* Turns the leading string statements of a body into directives.
1174811748
* @param {Node[]} body the statements
11749+
* @param {EXPECTED_ANY[]=} prologue the ESTree statements, whose `directive` says which are directives when no source is held
1174911750
* @returns {Node[]} the same array
1175011751
*/
11751-
const normalizeDirectives = (body) => {
11752+
const normalizeDirectives = (body, prologue) => {
1175211753
for (let i = 0; i < body.length; i++) {
1175311754
const statement = /** @type {EXPECTED_ANY} */ (body[i]);
1175411755
if (
@@ -11757,7 +11758,12 @@ const installEstree = (modules) => {
1175711758
) {
1175811759
return body;
1175911760
}
11760-
const raw = directiveSource(statement.expression);
11761+
let raw = directiveSource(statement.expression);
11762+
if (raw === null && prologue !== undefined) {
11763+
const { directive, expression } = prologue[i];
11764+
if (typeof directive !== "string") return body;
11765+
raw = expression.raw || JSON.stringify(directive);
11766+
}
1176111767
// A string the source does not open a prologue with ends it: one inside
1176211768
// parentheses is an expression, whatever it spells.
1176311769
if (raw === undefined) return body;
@@ -11779,14 +11785,29 @@ const installEstree = (modules) => {
1177911785
return body;
1178011786
};
1178111787

11788+
/**
11789+
* @returns {boolean} whether the `correct` phase is on, which reads a given
11790+
* ESTree tree where terser's conversion changes or throws on it
11791+
*/
11792+
const correcting = () =>
11793+
modules.corrections !== undefined && modules.corrections.enabled;
11794+
11795+
/**
11796+
* @param {EXPECTED_ANY[]} statements a body given as ESTree
11797+
* @returns {EXPECTED_ANY[] | undefined} them, when their `directive` decides
11798+
* the prologue: terser's conversion reads any leading string as one
11799+
*/
11800+
const prologueFromEstree = (statements) =>
11801+
tokenAt === null && correcting() ? statements : undefined;
11802+
1178211803
/**
1178311804
* A function's block, its leading string statements read as directives.
1178411805
* @param {EXPECTED_ANY} estree the block
1178511806
* @returns {Node} terser's block
1178611807
*/
1178711808
const blockFromEstree = (estree) => {
1178811809
const block = fromEstree(estree);
11789-
normalizeDirectives(block.body);
11810+
normalizeDirectives(block.body, prologueFromEstree(estree.body));
1179011811
return block;
1179111812
};
1179211813

@@ -12174,7 +12195,10 @@ const installEstree = (modules) => {
1217412195
ToplevelNode({
1217512196
startToken: startToken(estree),
1217612197
endToken: endToken(estree),
12177-
body: normalizeDirectives(estree.body.map(fromEstree))
12198+
body: normalizeDirectives(
12199+
estree.body.map(fromEstree),
12200+
prologueFromEstree(estree.body)
12201+
)
1217812202
}),
1217912203
ArrayPattern: (estree) =>
1218012204
makeNode(DestructuringNode, undefined, {
@@ -12258,10 +12282,10 @@ const installEstree = (modules) => {
1225812282
FunctionExpression: (estree) => lambdaFromEstree(estree, false),
1225912283
ArrowFunctionExpression: (estree) => {
1226012284
// terser's conversion leaves an arrow's directive a statement, where its
12261-
// parser reads one; only a tree we parsed ourselves is ours to fix.
12285+
// parser reads one, so the body turns sloppy: corrections read it too.
1226212286
const body =
1226312287
estree.body.type === "BlockStatement"
12264-
? tokenAt === null
12288+
? tokenAt === null && !correcting()
1226512289
? fromEstree(estree.body)
1226612290
: blockFromEstree(estree.body)
1226712291
: BlockStatementNode({
@@ -12650,8 +12674,12 @@ const installEstree = (modules) => {
1265012674
// WHY: terser's conversion exports every declaration as a value, where
1265112675
// its parser exports a named one as a declaration and an anonymous class
1265212676
// or function as the expression it is; `drop_unused` then reads a name
12653-
// the value has not got. Only a tree we parsed ourselves is ours to fix.
12654-
if (tokenAt === null || (!isClass && !isFunction)) {
12677+
// the value has not got. Only a tree we parsed ourselves is ours to fix,
12678+
// and, while correcting, an anonymous one, which terser's throws on.
12679+
if (
12680+
(tokenAt === null && (declaration.id || !correcting())) ||
12681+
(!isClass && !isFunction)
12682+
) {
1265512683
return makeNode(ExportNode, undefined, {
1265612684
startToken: start,
1265712685
endToken: end,
@@ -13333,29 +13361,57 @@ const installEstree = (modules) => {
1333313361
defineToEstree("PrefixedTemplateString", (node) => ({
1333413362
type: "TaggedTemplateExpression",
1333513363
tag: toEstree(node.tag),
13336-
quasi: toEstree(node.quasi)
13364+
quasi: templateToEstree(node.quasi, true)
1333713365
}));
13338-
defineToEstree("TemplateString", (node) => {
13366+
defineToEstree("TemplateString", (node) => templateToEstree(node, false));
13367+
13368+
/**
13369+
* A template as ESTree; an untagged one's raw text is written anew from its
13370+
* value while correcting, since the compressor changes only the value.
13371+
* @param {EXPECTED_ANY} node terser's template
13372+
* @param {boolean} tagged whether a tag reads its raw text
13373+
* @returns {EXPECTED_ANY} the template literal
13374+
*/
13375+
const templateToEstree = (node, tagged) => {
1333913376
/** @type {EXPECTED_ANY[]} */
1334013377
const quasis = [];
1334113378
/** @type {EXPECTED_ANY[]} */
1334213379
const expressions = [];
13343-
for (let i = 0; i < node.segments.length; i++) {
13344-
if (i % 2 !== 0) {
13345-
expressions.push(toEstree(node.segments[i]));
13380+
// Inlining a template into another leaves two text segments in a row,
13381+
// which ESTree holds as one.
13382+
let afterText = false;
13383+
for (const segment of node.segments) {
13384+
if (segment.type !== "TemplateElement") {
13385+
expressions.push(toEstree(segment));
13386+
afterText = false;
13387+
} else if (afterText) {
13388+
const { value } = quasis[quasis.length - 1];
13389+
value.raw += segment.raw;
13390+
value.cooked =
13391+
value.cooked === null || segment.value === null
13392+
? null
13393+
: value.cooked + segment.value;
1334613394
} else {
1334713395
quasis.push({
1334813396
type: "TemplateElement",
13349-
value: {
13350-
raw: node.segments[i].raw,
13351-
cooked: node.segments[i].value
13352-
},
13353-
tail: i === node.segments.length - 1
13397+
value: { raw: segment.raw, cooked: segment.value },
13398+
tail: false
1335413399
});
13400+
afterText = true;
13401+
}
13402+
}
13403+
quasis[quasis.length - 1].tail = true;
13404+
if (!tagged && correcting()) {
13405+
for (const { value } of quasis) {
13406+
value.raw = value.cooked.replace(
13407+
/\\|`|\$\{|\r/g,
13408+
(/** @type {string} */ match) =>
13409+
match === "\r" ? "\\r" : `\\${match}`
13410+
);
1335513411
}
1335613412
}
1335713413
return { type: "TemplateLiteral", quasis, expressions };
13358-
});
13414+
};
1335913415
defineToEstree("Defun", (node) => ({
1336013416
type: "FunctionDeclaration",
1336113417
id: toEstree(node.id),

‎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: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6239,6 +6239,168 @@ describe("syntax-printer", () => {
62396239
expect(ast.createWalker().webpackSkipsSqueezed).toBe(false);
62406240
});
62416241

6242+
describe("a tree given or given back as ESTree", () => {
6243+
/**
6244+
* @param {string} code a module
6245+
* @returns {EXPECTED_ANY} acorn's tree for it
6246+
*/
6247+
const estree = (code) =>
6248+
acorn.parse(code, { ecmaVersion: "latest", sourceType: "module" });
6249+
6250+
it("should keep an arrow's directive, as terser does only with corrections off", async () => {
6251+
const { minify } = await load();
6252+
const code = '(() => { "use strict"; x = 1; })();';
6253+
/** @type {EXPECTED_ANY} */
6254+
const options = { parse: { spidermonkey: true } };
6255+
expect((await minify(estree(code), options)).code).toBe(
6256+
'(()=>{"use strict";x=1})();'
6257+
);
6258+
const reference = await terserReference().minify(estree(code), options);
6259+
expect(reference.code).toBe("x=1;");
6260+
expect(
6261+
(await uncorrected(() => minify(estree(code), options))).code
6262+
).toBe(reference.code);
6263+
});
6264+
6265+
it("should read only ESTree's directives as directives, where terser reads any leading string", async () => {
6266+
const { minify } = await load();
6267+
/** @type {EXPECTED_ANY} */
6268+
const options = { parse: { spidermonkey: true } };
6269+
/**
6270+
* @param {string} code a script
6271+
* @returns {EXPECTED_ANY} acorn's tree for it
6272+
*/
6273+
const script = (code) => acorn.parse(code, { ecmaVersion: "latest" });
6274+
for (const code of [
6275+
'(() => { ("use strict"); x = 1; })();',
6276+
'(() => { "use\\x20strict"; x = 1; })();',
6277+
'(function () { ("use strict"); x = 1; })();'
6278+
]) {
6279+
expect((await minify(script(code), options)).code).toBe("x=1;");
6280+
}
6281+
const code = '("use strict"); x = 1; function f() {}';
6282+
expect((await minify(script(code), options)).code).toBe(
6283+
"function f(){}x=1;"
6284+
);
6285+
const reference = await terserReference().minify(script(code), options);
6286+
expect(reference.code).toBe('"use strict";function f(){}x=1;');
6287+
expect(
6288+
(await uncorrected(() => minify(script(code), options))).code
6289+
).toBe(reference.code);
6290+
});
6291+
6292+
it("should give back an untagged template's raw text written from its value, where terser keeps the source's", async () => {
6293+
const { minify } = await load();
6294+
const code = 'console.log(`a${"${`\\r\\\\"}${b}c`, tag`\\x${a}`);';
6295+
/** @type {EXPECTED_ANY} */
6296+
const options = { format: { spidermonkey: true } };
6297+
/**
6298+
* @param {EXPECTED_ANY} result a minify's result
6299+
* @returns {EXPECTED_ANY[]} the values of both templates' text
6300+
*/
6301+
const values = (result) =>
6302+
result.ast.body[0].expression.arguments.map(
6303+
(/** @type {EXPECTED_ANY} */ template) =>
6304+
(template.quasi || template).quasis.map(
6305+
(/** @type {EXPECTED_ANY} */ quasi) => quasi.value
6306+
)
6307+
);
6308+
expect(values(await minify(code, options))).toEqual([
6309+
[
6310+
{ raw: "a\\${\\`\\r\\\\", cooked: "a${`\r\\" },
6311+
{ raw: "c", cooked: "c" }
6312+
],
6313+
[
6314+
{ raw: "\\x", cooked: null },
6315+
{ raw: "", cooked: "" }
6316+
]
6317+
]);
6318+
const reference = values(
6319+
await terserReference().minify(code, options)
6320+
);
6321+
expect(reference[0][0].raw).toBe("a");
6322+
expect(
6323+
values(await uncorrected(() => minify(code, options)))[0]
6324+
).toEqual(reference[0]);
6325+
});
6326+
6327+
it("should give back a template inlined into another, which terser throws on", async () => {
6328+
const { minify } = await load();
6329+
const code = "console.log(`x${`y${a}`}z`, `p${`q`}r${b}`);";
6330+
/** @type {EXPECTED_ANY} */
6331+
const options = { format: { spidermonkey: true, code: true } };
6332+
const { code: printed, ast } = /** @type {EXPECTED_ANY} */ (
6333+
await minify(code, options)
6334+
);
6335+
expect(printed).toBe("console.log(`xy${a}z`,`pqr${b}`);");
6336+
const [first, second] = ast.body[0].expression.arguments;
6337+
expect(
6338+
[first, second].map((/** @type {EXPECTED_ANY} */ template) => [
6339+
template.quasis.map((/** @type {EXPECTED_ANY} */ quasi) => [
6340+
quasi.value.cooked,
6341+
quasi.tail
6342+
]),
6343+
template.expressions.length
6344+
])
6345+
).toEqual([
6346+
[
6347+
[
6348+
["xy", false],
6349+
["z", true]
6350+
],
6351+
1
6352+
],
6353+
[
6354+
[
6355+
["pqr", false],
6356+
["", true]
6357+
],
6358+
1
6359+
]
6360+
]);
6361+
const back = await minify(ast, {
6362+
compress: false,
6363+
mangle: false,
6364+
parse: { spidermonkey: true }
6365+
});
6366+
expect(back.code).toBe(printed);
6367+
await expect(terserReference().minify(code, options)).rejects.toThrow(
6368+
"to_mozilla_ast"
6369+
);
6370+
});
6371+
6372+
it("should read an anonymous default export, which terser throws on as it does with corrections off", async () => {
6373+
const { minify } = await load();
6374+
for (const code of [
6375+
"export default function (a) { return a; } console.log(1);",
6376+
"export default class { m() {} }",
6377+
"export default function* () {}",
6378+
"export default async function () {}"
6379+
]) {
6380+
for (const options of [
6381+
{ module: true },
6382+
{ module: true, compress: false, mangle: false },
6383+
{ module: true, toplevel: true, compress: { passes: 2 } }
6384+
]) {
6385+
const fromSource = await minify(code, options);
6386+
const fromTree = await minify(estree(code), {
6387+
...options,
6388+
parse: { spidermonkey: true }
6389+
});
6390+
expect(fromTree.code).toBe(fromSource.code);
6391+
}
6392+
/** @type {EXPECTED_ANY} */
6393+
const options = { module: true, parse: { spidermonkey: true } };
6394+
await expect(
6395+
terserReference().minify(estree(code), options)
6396+
).rejects.toThrow("definition");
6397+
await expect(
6398+
uncorrected(() => minify(estree(code), options))
6399+
).rejects.toThrow("definition");
6400+
}
6401+
});
6402+
});
6403+
62426404
describe("fields named as ESTree names them", () => {
62436405
// Each field the node classes take ESTree's name for, read and written
62446406
// by every phase: a loop, branch, call, generator, class, template, try.

0 commit comments

Comments
 (0)