sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-destructured-variables`

Open

#1,230 opened on Apr 28, 2021

View on GitHub
聽(4 comments)聽(6 reactions)聽(0 assignees)JavaScript聽(5,022 stars)聽(468 forks)user submission
help wantednew rule

Description

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.


Contributor guide

Rule proposal: `prefer-destructured-variables` 路 sindresorhus/eslint-plugin-unicorn#1230 | Good First Issue