Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for reviewers: the "exec" and "transform" tests are in different folders because they have different configs.

]
}
@@ -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() {});