Skip to content

Commit 76156f0

Browse files
committed
fix(javascript): parenthesize what the printer wrote as another program
A yield, assignment, conditional, sequence or arrow right of `#x in` printed bare, and `async` or `let` heading a for-of, or `let [` heading a statement or loop, printed unparenthesized; each no longer parsed or read otherwise. Moves 6 test262 cases off the printer's list; printing a 1 MB bundle is within noise.
1 parent 63f2330 commit 76156f0

5 files changed

Lines changed: 231 additions & 32 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Parenthesize the right side of a private `in` and a `let` or `async` starting a statement or loop head where the minifier printed code that no longer parsed or read otherwise.

‎lib/javascript/syntax-printer.js‎

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14603,10 +14603,109 @@ const installCorrect = (modules) => {
1460314603
A.terserKey(property.key) === "__proto__" &&
1460414604
!isShorthandProto(property);
1460514605

14606+
const estreeCodegen = /** @type {EstreeGenerators} */ (modules.estreeCodegen);
14607+
const estreeNeedsParens = /** @type {EstreeParensTests} */ (
14608+
modules.estreeNeedsParens
14609+
);
14610+
14611+
/**
14612+
* @param {PrintNode} node a node
14613+
* @param {OutputStream} output the stream printed into
14614+
* @returns {boolean} whether it is printed as the right side of `#x in`
14615+
*/
14616+
const isPrivateInRight = (node, output) => {
14617+
const parent = /** @type {PrintNode | undefined} */ (output.parent());
14618+
return (
14619+
corrections.enabled &&
14620+
parent !== undefined &&
14621+
parent.type === "BinaryExpression" &&
14622+
parent.left.type === "PrivateIdentifier" &&
14623+
parent.right === node
14624+
);
14625+
};
14626+
// terser holds `#x in y` apart from binary expressions, so what a binary's
14627+
// right side wraps in parentheses printed bare after a private `in`, and
14628+
// read as another expression or stopped parsing.
14629+
const parensByType = /** @type {Record<string, EstreeParens>} */ (
14630+
estreeNeedsParens
14631+
);
14632+
for (const type of [
14633+
"YieldExpression",
14634+
"AssignmentExpression",
14635+
"ConditionalExpression",
14636+
"SequenceExpression"
14637+
]) {
14638+
const parens = parensByType[type];
14639+
parensByType[type] = (node, output) =>
14640+
isPrivateInRight(node, output) || parens(node, output);
14641+
}
14642+
const printArrow =
14643+
/** @type {NonNullable<EstreeGenerators["ArrowFunctionExpression"]>} */ (
14644+
estreeCodegen.ArrowFunctionExpression
14645+
);
14646+
estreeCodegen.ArrowFunctionExpression = (node, output) => {
14647+
if (!isPrivateInRight(node, output)) {
14648+
printArrow(node, output);
14649+
return;
14650+
}
14651+
output.print("(");
14652+
printArrow(node, output);
14653+
output.print(")");
14654+
};
14655+
14656+
const kindOf = /** @type {PrintKinds} */ (modules.kindOf);
14657+
/**
14658+
* @param {PrintParent} parent a node
14659+
* @param {PrintParent} child one of its children
14660+
* @returns {boolean} whether the child's first token is the parent's
14661+
*/
14662+
const startsParent = (parent, child) =>
14663+
(parent.type === "SequenceExpression" && parent.expressions[0] === child) ||
14664+
(parent.type === "CallExpression" && parent.callee === child) ||
14665+
(parent.type === "TaggedTemplateExpression" && parent.tag === child) ||
14666+
(parent.type === "MemberExpression" && parent.object === child) ||
14667+
(parent.type === "ChainExpression" && parent.expression === child) ||
14668+
(parent.type === "ConditionalExpression" && parent.test === child) ||
14669+
(kindOf.Binary(parent) &&
14670+
/** @type {PrintBinaryLike} */ (parent).left === child) ||
14671+
(kindOf.UnaryPostfix(parent) &&
14672+
/** @type {PrintUnary} */ (parent).argument === child);
14673+
// A statement or loop head may not start with `let [`, a for-of head with
14674+
// `let` or a bare `async`: each reads as a declaration or an arrow.
14675+
estreeNeedsParens.Identifier = (node, output) => {
14676+
if (
14677+
!corrections.enabled ||
14678+
(node.name !== "let" && node.name !== "async")
14679+
) {
14680+
return false;
14681+
}
14682+
const parent = /** @type {PrintParent} */ (output.parent(0));
14683+
/** @type {PrintParent} */
14684+
let child = node;
14685+
/** @type {PrintParent | undefined} */
14686+
let head;
14687+
for (let i = 0; (head = output.parent(i)); i++) {
14688+
if (!startsParent(head, child)) break;
14689+
child = head;
14690+
}
14691+
if (head === undefined) return false;
14692+
if (head.type === "ForOfStatement" && head.left === child) {
14693+
return node.name === "let" || (child === node && !head.await);
14694+
}
14695+
return (
14696+
node.name === "let" &&
14697+
((head.type === "ExpressionStatement" && head.expression === child) ||
14698+
(head.type === "ForStatement" && head.init === child) ||
14699+
(head.type === "ForInStatement" && head.left === child)) &&
14700+
parent.type === "MemberExpression" &&
14701+
parent.computed &&
14702+
!parent.optional
14703+
);
14704+
};
14705+
1460614706
// `{ __proto__ }` is an own property and `{ __proto__: x }` the prototype, a
1460714707
// difference terser's tree keeps only in the tokens, so print by them; an
1460814708
// import attribute is never a shorthand, so its `__proto__` prints alike.
14609-
const estreeCodegen = /** @type {EstreeGenerators} */ (modules.estreeCodegen);
1461014709
const printProperty =
1461114710
/** @type {NonNullable<EstreeGenerators["Property"]>} */ (
1461214711
estreeCodegen.Property
@@ -30377,6 +30476,7 @@ const installEstreePrint = (modules) => {
3037730476
);
3037830477
// Shared with `correct`, which wraps an entry.
3037930478
modules.estreeCodegen = codegen;
30479+
modules.estreeNeedsParens = needsParens;
3038030480

3038130481
/**
3038230482
* @param {FormatOptions=} given format options

‎test/specCases/test262.spectest.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,8 +1507,6 @@ const MINIFIED_FAILURES = [
15071507
"terser writes a program that no longer parses",
15081508
minifiers: ["terser", "printer"],
15091509
tests: [
1510-
"expressions/in/private-field-rhs-yield-present.js (sloppy)",
1511-
"expressions/in/private-field-rhs-yield-present.js (strict)",
15121510
"expressions/new.target/unary-expr.js (sloppy)",
15131511
"expressions/new.target/unary-expr.js (strict)",
15141512
"expressions/object/method-definition/async-returns-async-arrow-returns-newtarget.js (sloppy)",
@@ -1541,10 +1539,6 @@ const MINIFIED_FAILURES = [
15411539
"statements/class/elements/private-static-method-visible-to-direct-eval.js (strict)",
15421540
"statements/class/elements/private-static-setter-visible-to-direct-eval.js (sloppy)",
15431541
"statements/class/elements/private-static-setter-visible-to-direct-eval.js (strict)",
1544-
"statements/for-of/head-lhs-async-escaped.js (sloppy)",
1545-
"statements/for-of/head-lhs-async-escaped.js (strict)",
1546-
"statements/for-of/head-lhs-async-parens.js (sloppy)",
1547-
"statements/for-of/head-lhs-async-parens.js (strict)",
15481542
"statements/return/S12.9_A1_T1.js (sloppy)",
15491543
"statements/return/S12.9_A1_T1.js (strict)",
15501544
"statements/return/S12.9_A1_T10.js (sloppy)",
@@ -1613,12 +1607,18 @@ const MINIFIED_FAILURES = [
16131607
"expressions/dynamic-import/import-attributes/2nd-param-await-ident.js (strict)",
16141608
"expressions/function/static-init-await-reference.js (sloppy)",
16151609
"expressions/function/static-init-await-reference.js (strict)",
1610+
"expressions/in/private-field-rhs-yield-present.js (sloppy)",
1611+
"expressions/in/private-field-rhs-yield-present.js (strict)",
16161612
"statements/class/elements/private-field-is-visible-in-computed-properties.js (sloppy)",
16171613
"statements/class/elements/private-field-is-visible-in-computed-properties.js (strict)",
16181614
"statements/class/elements/private-field-with-initialized-id-is-visible-in-computed-properties.js (sloppy)",
16191615
"statements/class/elements/private-field-with-initialized-id-is-visible-in-computed-properties.js (strict)",
16201616
"statements/for-await-of/let-block-with-newline.js (sloppy)",
16211617
"statements/for-in/let-block-with-newline.js (sloppy)",
1618+
"statements/for-of/head-lhs-async-escaped.js (sloppy)",
1619+
"statements/for-of/head-lhs-async-escaped.js (strict)",
1620+
"statements/for-of/head-lhs-async-parens.js (sloppy)",
1621+
"statements/for-of/head-lhs-async-parens.js (strict)",
16221622
"statements/for-of/let-block-with-newline.js (sloppy)"
16231623
]
16241624
},

0 commit comments

Comments
 (0)