logical-assignment: Do not assign names to anonymous functions (#11370)

This commit is contained in:
Arun Kumar Mohan 2020-05-05 09:00:41 -05:00 committed by GitHub
parent 812f3750c8
commit a8061ae7d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 84 additions and 1 deletions

View File

@ -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),
),
);
},

View File

@ -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("");

View File

@ -0,0 +1,6 @@
{
"plugins": [
"proposal-logical-assignment-operators",
"proposal-nullish-coalescing-operator"
]
}

View File

@ -0,0 +1,4 @@
var a;
a ||= function () {};
a &&= function () {};
a ??= function () {};

View File

@ -0,0 +1,3 @@
{
"plugins": ["proposal-logical-assignment-operators"]
}

View File

@ -0,0 +1,4 @@
var a;
a || (a = (0, function () {}));
a && (a = (0, function () {}));
a ?? (a = (0, function () {}));

View File

@ -0,0 +1,9 @@
var a, b = true, c;
a ||= () => {};
b &&= () => {};
c ??= () => {};
expect(a.name).toBe("");
expect(b.name).toBe("");
expect(c.name).toBe("");

View File

@ -0,0 +1,6 @@
{
"plugins": [
"proposal-logical-assignment-operators",
"proposal-nullish-coalescing-operator"
]
}

View File

@ -0,0 +1,4 @@
var a;
a ||= () => {};
a &&= () => {};
a ??= () => {};

View File

@ -0,0 +1,3 @@
{
"plugins": ["proposal-logical-assignment-operators"]
}

View File

@ -0,0 +1,4 @@
var a;
a || (a = (0, () => {}));
a && (a = (0, () => {}));
a ?? (a = (0, () => {}));

View File

@ -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");

View File

@ -0,0 +1,6 @@
{
"plugins": [
"proposal-logical-assignment-operators",
"proposal-nullish-coalescing-operator"
]
}

View File

@ -0,0 +1,4 @@
var a;
a ||= function d() {};
a &&= function e() {};
a ??= function f() {};

View File

@ -0,0 +1,3 @@
{
"plugins": ["proposal-logical-assignment-operators"]
}

View File

@ -0,0 +1,4 @@
var a;
a || (a = function d() {});
a && (a = function e() {});
a ?? (a = function f() {});