logical-assignment: Do not assign names to anonymous functions (#11370)
This commit is contained in:
parent
812f3750c8
commit
a8061ae7d8
@ -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(
|
path.replaceWith(
|
||||||
t.logicalExpression(
|
t.logicalExpression(
|
||||||
operator.slice(0, -1),
|
operator.slice(0, -1),
|
||||||
lhs,
|
lhs,
|
||||||
t.assignmentExpression("=", left, right),
|
t.assignmentExpression("=", left, rightExpression),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
@ -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() {});
|
||||||
Loading…
x
Reference in New Issue
Block a user