Skip to content

Latest commit

Β 

History

History
76 lines (57 loc) Β· 1.76 KB

consistent-destructuring.md

File metadata and controls

76 lines (57 loc) Β· 1.76 KB

consistent-destructuring

πŸ“ Use destructured variables over properties.

🚫 Disabled by default.

πŸ’‘ This rule is manually fixable by editor suggestions.

Enforces the use of already destructured variables over accessing the same direct, non-computed identifier property again. Previous destructurings are easily missed which leads to an inconsistent code style.

This rule is partly fixable. It does not suggest adding new properties to existing destructuring patterns, as that could read properties earlier than before.

There is no option to ignore specific sources or properties. Properties without a corresponding destructured variable are ignored by design.

Examples

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

// βœ…
const {a} = foo;
console.log(a);

// βœ…
console.log(foo.a, foo.b);
// βœ…
const {a} = foo;
console.log(a, foo.b);
// βœ…
const {
	a: {b},
} = foo;
console.log(foo.a.c);
// ❌
const {bar} = foo;
const {a} = foo.bar;

// βœ…
const {bar} = foo;
const {a} = bar;
// βœ…
const {a} = foo;
console.log(a, foo.b());
// βœ…
const {a} = foo.bar;
console.log(foo.bar.a);

A destructured value is a snapshot. A direct call such as update(foo) or foo.update() between the destructuring and the property access could have changed the object, so that access is not reported. This is best-effort: a call in a nested function, a method call on this, and a call later in a loop body (which runs again before the next access) are not taken into account.

// βœ…
const {a} = foo;
update(foo);
console.log(foo.a);