Skip to content

Commit

Permalink
fix(logical-assignment): Do not assign names to anonymous functions
Browse files Browse the repository at this point in the history
  • Loading branch information
arku committed Apr 7, 2020
1 parent 2c31587 commit fd88b0f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
Expand Up @@ -39,11 +39,18 @@ export default declare(api => {
}
}

const isRHSAnonymousFunction = t.isFunctionExpression(right, {
id: null,
});
const rightExpression = isRHSAnonymousFunction
? t.sequenceExpression([t.numericLiteral(0), right])
: right;

path.replaceWith(
t.logicalExpression(
operator.slice(0, -1),
lhs,
t.assignmentExpression("=", left, right),
t.assignmentExpression("=", left, rightExpression),
),
);
},
Expand Down
@@ -0,0 +1,8 @@
var a;
console.log((a ||= function () {}).name);

var b = 1;
console.log((b &&= function () {}).name);

var c;
console.log((c ??= function () {}).name);
@@ -0,0 +1,3 @@
{
"plugins": ["proposal-logical-assignment-operators"]
}
@@ -0,0 +1,6 @@
var a;
console.log((a || (a = (0, function () {}))).name);
var b = 1;
console.log((b && (b = (0, function () {}))).name);
var c;
console.log((c ?? (c = (0, function () {}))).name);

0 comments on commit fd88b0f

Please sign in to comment.