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

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

Closed
feross opened this issue Jul 11, 2019 · 1 comment
Closed
Labels

Comments

@feross
Copy link
Member

feross commented Jul 11, 2019

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.

feross added a commit to standard/eslint-config-standard that referenced this issue Jul 11, 2019
@feross feross changed the title Relax rule: Only error in destructuring if all variables should be const (prefer-const) Relax rule: Only enforce const in destructuring when all variables should be const Jul 11, 2019
@feross feross changed the title Relax rule: Only enforce const in destructuring when all variables should be const Relax rule: Only enforce const in destructuring when all variables are constant Jul 11, 2019
@feross
Copy link
Member Author

feross commented Jul 11, 2019

Released as 13.0.1 since I think this rule is likely to cause folks a lot of trouble.

Specifically, it wanted me to split up this component's props into a const and let because one of the props (style) is mutated:

const Loader = props => {
  let {
    center,
    class: className,
    label = 'Loading',
    style,
    ...rest
  } = props

  if (center) {
    style = {
      blah: 1
    }
  }

This is too much work. I'd rather just keep this whole destructing assignment using let.

@feross feross closed this as completed Jul 11, 2019
@feross feross added the bug label Jul 11, 2019
brettz9 pushed a commit to brettz9/eslint-config-standard that referenced this issue Dec 31, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
Archived in project
Development

No branches or pull requests

1 participant