Skip to content

Commit 4db5d96

Browse files
committed
fix(javascript): parenthesize a yield, await, private in or chain tag
terser's list of tags it parenthesizes misses these four, so `(yield o)`t``, `(await o)`t``, `(#x in o)`t`` and `(a?.b)`t`` printed bare and read the template as part of the operand or stopped parsing. Adds a for-await-of `let` case.
1 parent 76156f0 commit 4db5d96

3 files changed

Lines changed: 61 additions & 18 deletions

File tree

‎.changeset/102-printer-parentheses.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-
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.
5+
Parenthesize the right side of a private `in`, a `yield`, `await`, private `in` or optional chain tagging a template, 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: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14607,6 +14607,7 @@ const installCorrect = (modules) => {
1460714607
const estreeNeedsParens = /** @type {EstreeParensTests} */ (
1460814608
modules.estreeNeedsParens
1460914609
);
14610+
const kindOf = /** @type {PrintKinds} */ (modules.kindOf);
1461014611

1461114612
/**
1461214613
* @param {PrintNode} node a node
@@ -14652,8 +14653,30 @@ const installCorrect = (modules) => {
1465214653
printArrow(node, output);
1465314654
output.print(")");
1465414655
};
14656+
// terser's list of tags it parenthesizes misses these, and each printed
14657+
// bare reads the template as part of its operand, or stops parsing.
14658+
const printTagged =
14659+
/** @type {NonNullable<EstreeGenerators["TaggedTemplateExpression"]>} */ (
14660+
estreeCodegen.TaggedTemplateExpression
14661+
);
14662+
estreeCodegen.TaggedTemplateExpression = (node, output) => {
14663+
const { tag } = node;
14664+
if (
14665+
!corrections.enabled ||
14666+
!(
14667+
tag.type === "YieldExpression" ||
14668+
tag.type === "AwaitExpression" ||
14669+
tag.type === "ChainExpression" ||
14670+
kindOf.PrivateIn(tag)
14671+
)
14672+
) {
14673+
printTagged(node, output);
14674+
return;
14675+
}
14676+
modules.printEstree(tag, output, true);
14677+
modules.printEstree(node.quasi, output);
14678+
};
1465514679

14656-
const kindOf = /** @type {PrintKinds} */ (modules.kindOf);
1465714680
/**
1465814681
* @param {PrintParent} parent a node
1465914682
* @param {PrintParent} child one of its children

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

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,22 @@ const CORRECTED_CASES = [
598598
"an arrow right of a private `in`",
599599
"class C { #x; static f() { return #x in (() => 1); } } console.log(C.f());"
600600
],
601+
[
602+
"a `yield` tagging a template",
603+
"function* g() { return (yield 1)`t`; } var i = g(); i.next(); console.log(i.next((s) => s[0]).value);"
604+
],
605+
[
606+
"an `await` tagging a template",
607+
'var o = function () { console.log("tagged"); }; (async function () { try { (await o)`t`; } catch (e) {} })(); console.log("end");'
608+
],
609+
[
610+
"a private `in` tagging a template",
611+
"class C { #x; static f(o) { try { return (#x in o)`t`; } catch (e) { return e.name; } } } console.log(C.f(() => ({})));"
612+
],
613+
[
614+
"an optional chain tagging a template",
615+
"var a = { b: (s) => s[0] }; console.log((a?.b)`t`);"
616+
],
601617
[
602618
"a parenthesized `async` heading a for-of",
603619
"var async; for ((async) of [7]); console.log(async);"
@@ -620,22 +636,26 @@ const CORRECTED_CASES = [
620636
// is measured against the input rather than against terser's.
621637
/** @type {[string, string][]} */
622638
const LET_HEAD_CASES = [
623-
[
624-
"a `let` member heading a for-of",
625-
"var let = {}; for ((let).a of [5]); console.log(let.a);"
626-
],
627-
[
628-
"a `let [` heading a for-in",
629-
'var let = []; for ((let)[0] in { k: 1 }); console.log(let[0]);'
630-
],
631-
[
632-
"a `let [` heading a for loop",
633-
"var let = [0]; for ((let)[0] = 3; ; ) break; console.log(let[0]);"
634-
],
635-
[
636-
"a `let [` starting a statement",
637-
"var let = [1]; (let)[0] = 2; console.log(let[0]);"
638-
]
639+
[
640+
"a `let` heading a for-await-of",
641+
"var let; (async function () { for await ((let) of [1]); })(); console.log(typeof let);"
642+
],
643+
[
644+
"a `let` member heading a for-of",
645+
"var let = {}; for ((let).a of [5]); console.log(let.a);"
646+
],
647+
[
648+
"a `let [` heading a for-in",
649+
"var let = []; for ((let)[0] in { k: 1 }); console.log(let[0]);"
650+
],
651+
[
652+
"a `let [` heading a for loop",
653+
"var let = [0]; for ((let)[0] = 3; ; ) break; console.log(let[0]);"
654+
],
655+
[
656+
"a `let [` starting a statement",
657+
"var let = [1]; (let)[0] = 2; console.log(let[0]);"
658+
]
639659
];
640660

641661
// Each writes a name where terser writes it as a keyword, through the `improve`

0 commit comments

Comments
 (0)