Skip to content

Bug: no-extra-parens autofix changes assignment semantics #16850

@fasttime

Description

@fasttime

Environment

Node version: v18.13.0
npm version: v8.19.3
Local ESLint version: v8.33.0 (Currently used)
Global ESLint version: Not found
Operating System: darwin 22.2.0

What parser are you using?

Default (Espree)

What did you do?

Configuration
{
    "parserOptions": {
        "ecmaVersion": 2015
    },
    "rules": {
        "no-extra-parens": [
            "error"
        ]
    }
}
let foo, bar;
(foo) = function () { };
(bar) = class { };
console.log(foo.name + bar.name);

What did you expect to happen?

ESLint should not remove the parentheses in (foo) and (bar) automatically. The parentheses are required to prevent the class or function from having its name property set to the name of the variable.

What actually happened?

The parentheses around foo and bar are removed by the autofix. The resulting code prints foobar instead of an empty string like the original code.

DEMO LINK

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

My suggestion is to disable the autofix for parentheses around a single identifier on the left side of an assignment when the right side contains an anonymous class or anonymous function. This would avoid unexpected changes in program behavior in some edge cases.

I can't decide if keeping the warning in the first place would be good or not.

Metadata

Metadata

Assignees

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusionarchived due to ageThis issue has been archived; please open a new issue for any further discussionbugESLint is working incorrectlyrepro:yesIssues with a reproducible exampleruleRelates to ESLint's core rules

Type

No type

Projects

Status

Complete

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions