sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-destructured-variables`

已關閉

#1,230 建立於 2021年4月28日

 (4 則留言) (7 個反應) (0 位負責人)JavaScript (468 個分叉)user submission
help wantednew rule

倉庫指標

星標
 (5,022 顆星)
PR 合併指標
 (平均合併 1天 16小時) (30 天內合併 399 個 PR)

描述

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.


貢獻者指南