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 recommended even for high indexes

See original GitHub issue

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 parser (default, Babel-ESLint, etc.) are you using? babel-eslint

Please show your full configuration:

Configuration

.eslintrc.json:

{
    "parser": "babel-eslint",
    "extends": "airbnb",
    "rules": {
        "max-len": 0,
        "newline-per-chained-call": 0,
        "object-curly-newline": 0
    },
    "globals": {
      "window": true,
      "document": true,
      "sessionStorage": true,
      "localStorage": true,
      "Accept": true,
      "Blob": true
    },
    "env": {
      "es6": true,
      "node": true,
      "jest": true
    }
}

What did you do?

Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

test.js:

const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];

let b;
b = a[15];
./node_modules/.bin/eslint test.js

What did you expect to happen? I expected ESLint to ignore the prefer-destructuring rule for this many elements, as it is not feasible, efficient, or maintainable to count out that many commas.

Note that the following:

const b = a[15];

does not get flagged for the prefer-destructuring rule. Only when a variable is getting assigned (rather than initialized) does it break.

What actually happened?

Please include the actual, raw output from ESLint.

/[redacted]/test.js
  3:5   error  'b' is assigned a value but never used         no-unused-vars
  4:1   error  Use array destructuring                        prefer-destructuring
  4:1   error  'b' is never reassigned. Use 'const' instead   prefer-const
  4:11  error  Newline required at end of file but not found  eol-last

✖ 4 problems (4 errors, 0 warnings)
  1 error, 0 warnings potentially fixable with the `--fix` option.

The issue at hand:

4:1   error  Use array destructuring                        prefer-destructuring

ESLint is requiring destructuring when indexing the 15 element. In my opinion this should be capped at 3, or made to conform with what happens when initializing.

Edit

In the interest of preventing cross contamination, here is a snippet that outputs only the above error:

const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];

let b;
b = a[15];

b = 2;
return b;

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
not-an-aardvarkcommented, Jun 22, 2018

I think the initialization-vs-assignment behavior is happening because you’re extending eslint-config-airbnb, which has a different configuration for initialization versus assignment.

1reaction
platinumazurecommented, Jun 22, 2018

It seems to me that the declaration and assignment cases should be identical (unless rule options are configured which changes the behavior of one or the other), so just on that alone I think we probably have a bug.

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 >
A Better & Cleaner Way To Destructure JavaScript Arrays
All of this explains why, for a cleaner and better syntax trick, we can de-structure Arrays like Objects, but using the array indexes...
Read more >
Where and how can I use the destructuring assignment syntax ...
All you have to do is declare a variable for each value in the sequence. You can define fewer variables than there are...
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 >
Understanding the JavaScript Destructuring Syntax - Telerik
So if we need to access the first object in the array, with index 0, we can do array destructuring nested in the...
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