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

Rule proposal: prefer-destructured-variables #1230

Open
fisker opened this issue Apr 28, 2021 · 4 comments
Open

Rule proposal: prefer-destructured-variables #1230

fisker opened this issue Apr 28, 2021 · 4 comments

Comments

@fisker
Copy link
Collaborator

fisker commented Apr 28, 2021

I understand we already have consistent-destructuring, and it already covers cases I'm going to post, but that rule is buggy, and I don't like destructuring all used properties in one, I prefer destructuring when needed, for example:

const {a} = foo;

if (a) {
  // ... do something not related to `foo.b`
}

const {b} = foo;

// ... do something with `b`

Fail

const {a} = foo;
console.log(foo.a); 

Pass

const {a} = foo;
console.log(a, foo.b); 

It's like splitting consistent-destructuring into two rules, this one and another like prefer-one-destructuring.


@brettz9
Copy link
Contributor

brettz9 commented Apr 28, 2021

This looks like a good solution for your use case and a good compromise solution for those of us who do want to require destructuring where possible and practical, but not tyrannically so (as long as it is implemented to meet my case as it sounds it would be and GuoSirius' (since that is technically a variable but just not yet available)). In any case, some false negatives are not concealing technically bad code.

@sindresorhus
Copy link
Owner

I agree, I thought consistent-restructuring would be a good idea, but in practice, it's just been annoying. I think we should also turn consistent-destructuring off by default.

@sindresorhus
Copy link
Owner

This is now accepted.

vossmalte added a commit to vossmalte/eslint-plugin-unicorn that referenced this issue Jan 17, 2024
vossmalte added a commit to vossmalte/eslint-plugin-unicorn that referenced this issue Jan 17, 2024
vossmalte added a commit to vossmalte/eslint-plugin-unicorn that referenced this issue Jan 17, 2024
vossmalte added a commit to vossmalte/eslint-plugin-unicorn that referenced this issue Jan 17, 2024
@gurgunday
Copy link
Contributor

const {a} = foo;
console.log(foo.a);
// PASS

Writing the obvious, but the rule should check if foo.a was assigned to a new value and not warn if that's the case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants