Skip to content

Commit

Permalink
fix: perserve parentheses of lhs id with rhs unamed fn (#14524)
Browse files Browse the repository at this point in the history
* fix: perserve () of lhs id with rhs unamed fn

* review comments
  • Loading branch information
JLHwung committed May 6, 2022
1 parent 0984a2a commit 9c1774a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/babel-generator/src/node/parentheses.ts
Expand Up @@ -18,6 +18,7 @@ import {
isForInStatement,
isForOfStatement,
isForStatement,
isFunctionExpression,
isIfStatement,
isIndexedAccessType,
isIntersectionTypeAnnotation,
Expand Down Expand Up @@ -345,6 +346,16 @@ export function Identifier(
parent: t.Node,
printStack: Array<t.Node>,
): 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") {
Expand Down
@@ -0,0 +1,7 @@
var f, g, h;
(f) = function () {};
(f) = class {};
g = function () {};
g = class {};
(h) = function noParen() {};
(h) = class noParen {}
@@ -0,0 +1,13 @@
var f, g, h;

(f) = function () {};

(f) = class {};

g = function () {};

g = class {};

h = function noParen() {};

h = class noParen {};

0 comments on commit 9c1774a

Please sign in to comment.