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.

Introduce "ignoreForSingleProperty" value for prefer-destructuring

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? if configured, fewer; otherwise no change

How will the change be implemented? (New option, new default behavior, etc.)? new option

Please provide some example code that this change will affect:

Web Extension here:

browser.storage.sync.get("popupIconColored").then((res) => {
    const popupIconColored = res.popupIconColored;

    // …
});

(real-life example)

What does the rule currently do for this code? When prefer-destructuring is enabled, it complains that I should use destructuring here. So I’d have to use this:

const { popupIconColored } = res;

(not tested, it also has been mentioned I may have to put brackets around that even more…)

What will the rule do after it’s changed?

It should not complain about the previous code example.

I would like a config option named “smart” or similar, so that the rule only catches cases, where destructuring “makes sense” (hereby I mean that I access multiple values of an array/object of it directly) and not, when I only access one value and want a short variable name for that.

This example e.g. makes sense and should still be catched with the smart setting:

function getFullName(user) {
  const firstName = user.firstName;
  const lastName = user.lastName;

  return `${firstName} ${lastName}`;
}

Because here, I assign/get two variables from the object. However, when I only want to get one variable, destructuring just complicates the code IMHO.

In short: Add an option, which only triggers when I get multiple values from an array/object and not only a single one.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ljharbcommented, Apr 22, 2018

No, that’s the “AssignmentExpression” option.

enforceForRenamedProperties is for controlling whether you allow const foo = obj.bar or whether you require it to be const { bar: foo } = obj;.

0reactions
eslint-deprecated[bot]commented, Dec 11, 2018

Unfortunately, it looks like there wasn’t enough interest from the team or community to implement this change. While we wish we’d be able to accommodate everyone’s requests, we do need to prioritize. We’ve found that issues failing to reach accepted status after 21 days tend to never be accepted, and as such, we close those issues. This doesn’t mean the idea isn’t interesting or useful, just that it’s not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

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 >
ESLint: prefer-destructuring - Styleguide JavaScript
Use object destructuring for multiple return values instead of array destructuring. New properties can not be added over time or the order of...
Read more >
Omit property variable when using object destructuring
The ignoreRestSiblings option is a boolean (default: false). Using a Rest Property it is possible to “omit” properties from an object, but by ......
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 >
eslint-browser/prefer-destructuring.md at master - GitHub
The second has a single property, enforceForRenamedProperties , that controls whether or not the object destructuring rules are applied in cases where 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