diff --git a/packages/babel-generator/src/node/parentheses.ts b/packages/babel-generator/src/node/parentheses.ts index 460767d0b638..d1cf322ea269 100644 --- a/packages/babel-generator/src/node/parentheses.ts +++ b/packages/babel-generator/src/node/parentheses.ts @@ -18,6 +18,7 @@ import { isForInStatement, isForOfStatement, isForStatement, + isFunctionExpression, isIfStatement, isIndexedAccessType, isIntersectionTypeAnnotation, @@ -345,6 +346,16 @@ export function Identifier( parent: t.Node, printStack: Array, ): boolean { + // 13.15.2 AssignmentExpression RS: Evaluation + // (fn) = function () {}; + if ( + node.extra?.parenthesized && + isAssignmentExpression(parent, { left: node }) && + (isFunctionExpression(parent.right) || isClassExpression(parent.right)) && + parent.right.id == null + ) { + return true; + } // Non-strict code allows the identifier `let`, but it cannot occur as-is in // certain contexts to avoid ambiguity with contextual keyword `let`. if (node.name === "let") { diff --git a/packages/babel-generator/test/fixtures/parentheses/identifier-lhs/input.js b/packages/babel-generator/test/fixtures/parentheses/identifier-lhs/input.js new file mode 100644 index 000000000000..b1c118d215b6 --- /dev/null +++ b/packages/babel-generator/test/fixtures/parentheses/identifier-lhs/input.js @@ -0,0 +1,7 @@ +var f, g, h; +(f) = function () {}; +(f) = class {}; +g = function () {}; +g = class {}; +(h) = function noParen() {}; +(h) = class noParen {} diff --git a/packages/babel-generator/test/fixtures/parentheses/identifier-lhs/output.js b/packages/babel-generator/test/fixtures/parentheses/identifier-lhs/output.js new file mode 100644 index 000000000000..164de06f999a --- /dev/null +++ b/packages/babel-generator/test/fixtures/parentheses/identifier-lhs/output.js @@ -0,0 +1,13 @@ +var f, g, h; + +(f) = function () {}; + +(f) = class {}; + +g = function () {}; + +g = class {}; + +h = function noParen() {}; + +h = class noParen {}; \ No newline at end of file