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 should not check non-numeric indexes

See original GitHub issue

Tell us about your environment

  • ESLint Version: 4.14.0
  • Node Version: 9.3.9
  • npm Version: 5.6.0

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

Please show your full configuration:

Configuration
{
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module"
  },

  "extends": [
    "airbnb-base"
  ],

  "rules": {
    "class-methods-use-this": "off",
    "import/no-extraneous-dependencies": ["off", {"devDependencies": ["webpack-config/*.js"]}],
    "no-prototype-builtins": "off",
    "no-param-reassign": [0],
    "prefer-default-export": "off"
  }
}

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 default behavior

Please provide some example code that this change will affect:

const INTEGER_WORDS = {
  one: 1,
  two: 2,
  three: 3,
  four: 4,
 };

let num = 1;
num = INTEGER_WORDS[num]; 

What does the rule currently do for this code?

Generates a warning for num = INTEGER_WORDS[num];

What will the rule do after it’s changed?

It will ignore array indexes/keys that are not constants. I would argue this should always be the case, as it must be considered bad practice to destructure from a computed index or key.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
j-f1commented, Dec 28, 2017

You could do let { [num]: num } = INTEGER_WORDS, but I agree that that should be an exception.

0reactions
aparajitacommented, Dec 31, 2017

The fix seems to be quite easy. I haven’t thought through it deeply, but this works:

Change these lines to:

if (!rightNode.computed && ((property.type === "Literal" && leftNode.name === property.value) ||
    (property.type === "Identifier" && leftNode.name === property.name))) {

I added the check for rightNode.computed. If that’s true, a non-literal expression is being used.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use array destructuring (prefer-destructuring) error on eslint
Good advice. Sometimes eslint is just in the way. The prefer-destructuring rule basically doesn't fit for any large arrays? Where "large" is ...
Read more >
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 makes it possible to unpack values from arrays, or properties from ...
Read more >
Array Destructuring – How Does Destructuring Work in JS?
Keep in mind that you do not have to use an empty array as the destructuring parameter's default argument. You can use any...
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