prefer-destructuring recommended even for high indexes
See original GitHub issueTell 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:
- Created 5 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
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.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.