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 proposes object destructuring for array

See original GitHub issue
  • ESLint Version: 3.13.0
  • Node Version: 7.4.0
  • npm Version: 4.0.5

What parser (default, Babel-ESLint, etc.) are you using? default

Please show your full configuration:

'prefer-destructuring': [2, { array: true, object: true }, { enforceForRenamedProperties: true }]

What did you do? Please include the actual source code causing the issue.

/******************************************************************************/
'use strict';
/******************************************************************************/
const arr = ['a', 'b', 'c'];
const last = arr[arr.length - 1];
console.log(last);

What did you expect to happen? No error report.

What actually happened? Please include the actual, raw output from ESLint.

5:7  error  Use object destructuring  prefer-destructuring

The same with this code (that seems to contradict ‘Examples of correct code for this rule’ in the doc).

/******************************************************************************/
'use strict';
/******************************************************************************/
const arr = ['a', 'b', 'c'];
const lastIndex = arr.length - 1;
const last = arr[lastIndex];
console.log(last);
6:7  error  Use object destructuring  prefer-destructuring

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
fasttimecommented, Jan 7, 2017

I believe in the first snippet, the line

const last = arr[arr.length - 1];

can be written as

const { [arr.length - 1]: last } = arr;

And in the second snippet

const last = arr[lastIndex];

is the same as

const { [lastIndex]: last } = arr;
3reactions
kaicataldocommented, Jan 9, 2017

I honestly agree that enforcing destructuring with the computed property is confusing and not very easy to read. I would support an option to relax this rule for computed properties. Maybe enforceForComputedProperties, to be in line with the current schema?

{
  "rules": {
    "prefer-destructuring": ["error", {
      "array": true,
      "object": true
    }, {
      "enforceForRenamedProperties": false,
      "enforceForComputedProperties": false
    }]
  }
}
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 do I pass eslint's "prefer-destructuring" rule if I'm ...
I wanted to reassign two variables to the two properties specifically in the [0] position of the array. I did: clientX = evt.touchEvents[0] ......
Read more >
Destructuring Arrays & Objects: JavaScript ES6 Feature Series ...
For my final installment in this series, I will tackle array and object destructuring: the most concise way to pull out values and ......
Read more >
prefer-destructuring - Rules - ESLint - GitHub Pages
Each property accepts an object that accepts two properties, array and object , which can be used to control the destructuring requirement for...
Read more >
[Pitch] Destructuring Assignment of Structs and Classes
I'd like to propose adding destructuring assignment for both structs and ... This pitch does not include index-based destructuring of Array ...
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