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 maxIndex option

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 to add to second options with enforceForRenamedProperties, something like maxIndex.

Please provide some example code that this change will affect: When accessing indexes on arrays that are relatively large (>10), there is a point of diminishing return where destructuring the array becomes more and more impractical.

This idea is mentioned in the docs here on the extreme side where the index is 100. But there is no recourse for this scenario other than to ignore or disable the rule. But with this change, people will be able to address this scenario to their liking.

const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const previous = data[7];
const current = data[8];
const next = data[9];

which fixing this would look something like…

const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const [,,,,,, previous, current, next] = data;

This to me is nice because of the chained index values but the preceding commas required to access large indices can get unruly and negate readability, on the order of 20 or 50 let alone 100.

What does the rule currently do for this code?

const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const previous = data[7]; // <-- errors on prefer-destructuring
const current = data[8]; // <-- errors on prefer-destructuring
const next = data[9]; // <-- errors on prefer-destructuring

What will the rule do after it’s changed? For a example rule configuration like this

{
  "rules": {
    "prefer-destructuring": ["error", {
      "array": true,
      "object": true
    }, {
      "maxIndex": 8
    }]
  }
}
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const previous = data[7]; // <-- error becasue 7 <= 8
const current = data[8]; // <-- error becasue 8 <= 8
const next = data[9]; // <-- no error because 9 > 8

Are you willing to submit a pull request to implement this change? Yes

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
anikethsahacommented, Jun 4, 2020

ahh, I see.

yea I will make an issue soon.

1reaction
kaicataldocommented, Jun 4, 2020

We also have the ECMAScript 6 category, and so this could fit in either (it looks like we have other ES2015 syntax-specific rules in the stylistic section). For consistency and ease of understanding, I do think it would be better to get rid of the ECMAScript 6 category and move all those rules into the other, more descriptive categories. If you’d like to make an issue for that, feel free!

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 >
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 >
prefer-destructuring recommended even for high indexes
Tell us about your environment OS: MacOS 10.13.4 Editor: VS Code 2.9.1 ESLint Version: 4.19.1 Node Version: 8.11.1 npm Version: 6.1.0 What ...
Read more >
prefer-destructuring - Rules - ESLint - GitHub Pages
This rule enforces usage of destructuring instead of accessing a property through a member expression.
Read more >
Destructuring assignment - The Modern JavaScript Tutorial
Destructuring assignment is a special syntax that allows us to “unpack” arrays or objects into a bunch of variables, as sometimes that's ...
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