Skip to content

Relax rule: Only enforce const in destructuring when all variables are constant #1325

@feross

Description

@feross

When we added prefer-const, we kept the default setting which is quite strict when there's a bunch of variables being assigned at once in destructuring. For example, this is considered an error:

let { a, b } = obj
a = a + 1

This is because b is never mutated. So it wants us to rewrite the code like this:

let { a } = obj
const { b } = obj
a = a + 1

When there are many variables being assigned at once through destructuring, or when variables are being switched from mutated vs. not mutated, this causes a lot of churn. I'd rather err on the side of leniency here and just allow the whole block to be let if any of the variables are mutated.

Only when we can affirmatively say that all variables are const, should the whole thing be forced to be const. So, I'll enable the {"destructing": "all"} option. https://eslint.org/docs/rules/prefer-const#destructuring

Feedback welcome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions