Skip to content

Commit

Permalink
logical-assignment: Do not assign names to anonymous functions (#11370)
Browse files Browse the repository at this point in the history
  • Loading branch information
arku committed May 5, 2020
1 parent 812f375 commit a8061ae
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 1 deletion.
Expand Up @@ -39,11 +39,16 @@ export default declare(api => {
}
}

const isRHSAnonymousFunction = t.isFunction(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,9 @@
var a, b = true, c;

a ||= function () {};
b &&= function () {};
c ??= function () {};

expect(a.name).toBe("");
expect(b.name).toBe("");
expect(c.name).toBe("");
@@ -0,0 +1,6 @@
{
"plugins": [
"proposal-logical-assignment-operators",
"proposal-nullish-coalescing-operator"
]
}
@@ -0,0 +1,4 @@
var a;
a ||= function () {};
a &&= function () {};
a ??= function () {};
@@ -0,0 +1,3 @@
{
"plugins": ["proposal-logical-assignment-operators"]
}
@@ -0,0 +1,4 @@
var a;
a || (a = (0, function () {}));
a && (a = (0, function () {}));
a ?? (a = (0, function () {}));
@@ -0,0 +1,9 @@
var a, b = true, c;

a ||= () => {};
b &&= () => {};
c ??= () => {};

expect(a.name).toBe("");
expect(b.name).toBe("");
expect(c.name).toBe("");
@@ -0,0 +1,6 @@
{
"plugins": [
"proposal-logical-assignment-operators",
"proposal-nullish-coalescing-operator"
]
}
@@ -0,0 +1,4 @@
var a;
a ||= () => {};
a &&= () => {};
a ??= () => {};
@@ -0,0 +1,3 @@
{
"plugins": ["proposal-logical-assignment-operators"]
}
@@ -0,0 +1,4 @@
var a;
a || (a = (0, () => {}));
a && (a = (0, () => {}));
a ?? (a = (0, () => {}));
@@ -0,0 +1,9 @@
var a, b = true, c;

a ||= function d() {};
b &&= function e() {};
c ??= function f() {};

expect(a.name).toBe("d");
expect(b.name).toBe("e");
expect(c.name).toBe("f");
@@ -0,0 +1,6 @@
{
"plugins": [
"proposal-logical-assignment-operators",
"proposal-nullish-coalescing-operator"
]
}
@@ -0,0 +1,4 @@
var a;
a ||= function d() {};
a &&= function e() {};
a ??= function f() {};
@@ -0,0 +1,3 @@
{
"plugins": ["proposal-logical-assignment-operators"]
}
@@ -0,0 +1,4 @@
var a;
a || (a = function d() {});
a && (a = function e() {});
a ?? (a = function f() {});

0 comments on commit a8061ae

Please sign in to comment.