@@ -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
0 commit comments