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

fix: false positive with assignment in no-extra-parens #16872

Merged
merged 3 commits into from Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 32 additions & 1 deletion lib/rules/no-extra-parens.js
Expand Up @@ -766,6 +766,36 @@ module.exports = {
return false;
}

/**
* Checks if the left-hand side of an assignment is an identifier and the right-hand side is
* an anonymous class or function.
*
* As per https://tc39.es/ecma262/#sec-assignment-operators-runtime-semantics-evaluation, an
* assignment where the right-hand side is an anonymous class or function and the left-hand
* side is an *unparenthesized* identifier has different semantics than other assignments.
* Specifically, when an expression like `foo = function () {}` is evaluated, `foo.name`
* will be set to the string "foo", i.e. the identifier name. The same thing does not happen
* when evaluating `(foo) = function () {}`.
* Since the parenthesizing of the identifier in the left-hand side is significant in this
* special case, the parentheses, if present, should not be flagged as unnecessary.
* @param {ASTNode} node an AssignmentExpression node.
* @returns {boolean} `true` if the left-hand side of the assignment is an identifier and
* the right-hand side is an anonymous class or function; otherwise, `false`.
*/
function isAnonymousFunctionAssignmentException({ left, right }) {
if (left.type === "Identifier") {
const rhsType = right.type;

if (rhsType === "ArrowFunctionExpression") {
return true;
}
if ((rhsType === "FunctionExpression" || rhsType === "ClassExpression") && !right.id) {
return true;
}
}
return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

Should we also check the operator? Per the spec, this behavior seems to apply only if the operator is one of =, &&=, ||=, or ??=, not e.g. +=.

Copy link
Member Author

@fasttime fasttime Feb 8, 2023

Choose a reason for hiding this comment

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

@mdjermanovic I think so. I got confused because Babel doesn't seem to care about the operator, and decides to keep the parentheses even when optimizing (a) += function () {}. This made me think that I could be missing some edge case, but actually the spec is clear enough in defining the semantics of each operator (group) in a separate section. I'm going to update the PR like you suggested. Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated as per discussion.


return {
ArrayExpression(node) {
node.elements
Expand Down Expand Up @@ -804,7 +834,8 @@ module.exports = {
},

AssignmentExpression(node) {
if (canBeAssignmentTarget(node.left) && hasExcessParens(node.left)) {
if (canBeAssignmentTarget(node.left) && hasExcessParens(node.left) &&
(!isAnonymousFunctionAssignmentException(node) || isParenthesisedTwice(node.left))) {
report(node.left);
}

Expand Down
29 changes: 28 additions & 1 deletion tests/lib/rules/no-extra-parens.js
Expand Up @@ -771,6 +771,18 @@ ruleTester.run("no-extra-parens", rule, {
{
code: "const net = ipaddr.parseCIDR(/** @type {string} */ (cidr));",
options: ["all", { allowParensAfterCommentPattern: "@type" }]
},

// https://github.com/eslint/eslint/issues/16850
"(a) = function () {};",
"(a) = () => {};",
"(a) = class {};",
"(a) ??= function () {};",
"(a) &&= class extends SuperClass {};",
"(a) ||= async () => {}",
{
code: "((a)) = function () {};",
options: ["functions"]
}
],

Expand Down Expand Up @@ -3410,6 +3422,21 @@ ruleTester.run("no-extra-parens", rule, {
options: ["all"],
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "unexpected" }]
}
},

// https://github.com/eslint/eslint/issues/16850
invalid("(a) = function foo() {};", "a = function foo() {};", "Identifier"),
invalid("(a) = class Bar {};", "a = class Bar {};", "Identifier"),
invalid("(a.b) = function () {};", "a.b = function () {};", "MemberExpression"),
{
code: "(newClass) = [(one)] = class { static * [Symbol.iterator]() { yield 1; } };",
output: "newClass = [one] = class { static * [Symbol.iterator]() { yield 1; } };",
errors: [
{ messageId: "unexpected", type: "Identifier" },
{ messageId: "unexpected", type: "Identifier" }
]
},
invalid("((a)) = () => {};", "(a) = () => {};", "Identifier"),
invalid("(a) = (function () {})();", "a = (function () {})();", "Identifier")
]
});