question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

prefer-destructuring - Option to skip when destructurable variables count less than N

See original GitHub issue

What rule do you want to change? prefer-destructuring

Does this change cause the rule to produce more or fewer warnings? Fewer

How will the change be implemented? (New option, new default behavior, etc.)? New option (Not sure if it should have some non-zero default value)

Please provide some example code that this change will affect:

const abc = this.state.abc;
// other non-assignment stuff

What does the rule currently do for this code? Asks to convert to destructuring syntax, even if there is only one destructurable variable.

const {abc} = this.state;
// other non-assignment stuff

What will the rule do after it’s changed? Keep it unchanged, for upto N allowed destructurable variables. (makes most sense when N = 1)

Reasoning behind the suggestion:

We use lot of code like this

// Unfixed
const abc = this.state.abc;
// Fixed
const { abc } = this.state;

To me it doesn’t make much sense to use destructuring just for one variables like this. First one is much more explicit without being repetitive (which is the case for multiple sequential assignments like shown below).

The rule definitely makes sense for multiple assignments like this -

const a = this.state.a;
const b = this.state.b;
const c = this.state.c;

because the fixed code is concise and clear -

const { a, b, c } = this.state;

but for one variable, its hardly concise and looks too complex when it isn’t.

Although it makes more sense to me to skip 1 variable situation, but we should probably keep it configurable as N.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bendtherulescommented, Aug 31, 2018

Added some thoughts on already destructured variables in https://github.com/eslint/eslint/issues/10743#issuecomment-415186659

Would be nice to hear more thoughts on this and take a call on whether this should be added to core.

1reaction
bendtherulescommented, Aug 31, 2018

From what I can think of, there are two ways to count the number of possible destructurings (dest count) for a variable:

  1. Sum up dest count for a group of sequential variable declaration statements to get local dest count for each destructuree variable. Whenever there is non-declaration statement, reset count of all destructuree variable. Also gets reset when moving to new scope.

Ex :

// 1. Multiple variable declarators in same variable declaration
var a = data.a, b = 3, c = data.c; // dest count for `data` = 2
// ---------

// 2. Multiple sequential variable declarations (which can contain case 1 internally)
var d = data.d;
var e = data.e, f = data.f; // dest count for `data` = 3
// ---------

// 3. Non-sequential variable declarations
var f = data.f;  // dest count for `data` = 1
console.log(f);
var g = data.g;  // dest count for `data` = 1
// ---------

// 4. Count across scopes - Shouldn't be added
var h = data.h  // dest count for `data` = 1
{
  var i = data.i;  // dest count for `data` = 1
}

Say, with min destructuring count of 3 (config),

this would raise warning:

const a = data.a, b = data.b;
const c = data.c;

this will not raise warning:

const a = data.a, b = data.b;
console.log(a);
const c = data.c;
  1. Sum up dest count for all variable declaration statements within a scope and reset across scopes Ex :
// 1. Multiple variable declarators in same variable declaration
var a = data.a, b = 3, c = data.c; // dest count for `data` = 2
// ---------

// 2. Multiple sequential variable declarations (which can contain case 1 internally)
var d = data.d;
var e = data.e, f = data.f; // dest count for `data` = 3
// ---------

// 3. Non-sequential variable declarations
var f = data.f; 
console.log(f);
var g = data.g;  // dest count for `data` = **2**
// ---------

// 4. Count across scopes - Shouldn't be added
var h = data.h  // dest count for `data` = 1
{
  var i = data.i;  // dest count for `data` = 1
}

Say, with min destructuring count of 3 (config),

this would raise warning:

const a = data.a, b = data.b;
console.log(a);
const c = data.c;

this will not raise warning:

const a = data.a, b = data.b;
{
    const c = data.c;
}

I prefer the 2nd approach as it is more permissive (in terms of counting).

Added later:

Already destructured variables should also add to the count in a different way. For eg, with this sample:

var d = { data };
var e = data.e, f = data.f;

To-be Destructured count (earlier called dest count) for data [ToBeCount] = 2 Already Destructured count for data = 1 Total (To-be + Already) destructured count for data [TotalCount] = 3

Condition to raise error is (TotalCount > MinCount) && ToBeCount > 0 (Plugin option is MinCount)

Now, given various MinCount, this is how previous snippet will work:

A. MinCount = 1 Raise error for both assignments in line 2

B. MinCount = 2 Raise error for both assignments in line 2

C. MinCount = 3 No error

Read more comments on GitHub >

github_iconTop Results From Across the Web

prefer-destructuring - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
Skipping Values In JavaScript Destructuring - Samantha Ming
You can use blanks to skip over unwanted values in JavaScript. Perfect to avoid creating useless variable assignments for values you don't want...
Read more >
Destructuring assignment - JavaScript - MDN Web Docs
The destructuring assignment syntax is a JavaScript expression that ... is greater than N, only the first N variables are assigned values.
Read more >
Add Option to Disable prefer-destructuring When the Variable ...
I would like to propose a change to prefer-destructuring. Does this change cause the rule to produce more or fewer warnings?
Read more >
How to fix Eslint error "prefer-destructuring"? - Stack Overflow
It's an ES6 feature used to unpack variables from arrays or objects. this syntax create an array from variables: var x = [y,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found