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.

Proposal: Nested Destructuring

See original GitHub issue

The purpose of this proposal is to introduce an option to warn about nested destructuring, which can be hard to read and understand. This is in line with the original proposal in https://github.com/eslint/eslint/issues/6053.

What rule do you want to change?

prefer-destructuring

Does this change cause the rule to produce more or fewer warnings?

It will produce more warnings.

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

In the current state the rule can only be configured to emit warnings if a destructuring is possible and it doesn’t care if the destructuring is nested. It is however not possible to do the opposite and warn if destructuring is used, and it is also not possible to warn only when nested destructuring is used.

I would like to propose to deprecate the true/false settings of array/object in the rule options and replace them with always/flat/never.

  • always should warn when destructuring is possible
  • flat should warn when nested destructuring is used, or flat destructuring is possible
  • never should warn when destructuring is used

I would personally prefer it if the default was changed to flat for both array and object, but that would be a breaking change, so likely not a good idea. An alternativ however is introducing this as a new destructuring rule, and deprecate the prefer-destructuring rule completely.

Please provide some example code that this change will affect:

   const { inject: { service }, Mixin, assert, isPresent } = Ember;

   let { a: [{foo: f}] } = obj

What does the rule currently do for this code?

Nothing, as the rule is currently not warning about destructuring.

What will the rule do after it’s changed?

// Do not use nested destructuring (at 1:17)
   const { inject: { service }, Mixin, assert, isPresent } = Ember;
// ----------------^

// Do not use nested destructuring (at 3:10)
   let { a: [{foo: f}] } = obj
// ---------^

no-nested-destructuring proposal

Please describe what the rule should do:

The rule should warn about the use of nested destructuring.

What category of rule is this? (place an “X” next to just one item)

[x] Enforces code style [ ] Warns about a potential error [x] Suggests an alternate way of doing something [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

// Do not use nested destructuring (at 1:17)
   const { inject: { service }, Mixin, assert, isPresent } = Ember;
// ----------------^

// Do not use nested destructuring (at 3:10)
   let { a: [{foo: f}] } = obj
// ---------^

First draft implementation is available at https://astexplorer.net/#/vTZ1hYHzaZ

Why should this rule be included in ESLint (instead of a plugin)?

Using nested destructuring can often lead to code that is harder to read and comprehend compared to basic variable assignments or using multiple non-nested destructurings in a row. I think this is a rule that should be included in ESLint itself to increase awareness of the problem and help everyone write easier to understand code. This could obviously be implemented in a plugin but ultimately I think this is something that the majority of JS/ES6 developers would use.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
joncursicommented, Feb 17, 2017

Or perhaps, a setting that allows you to specify when object destructuring should kick in, such as after this.

// OK
const something = process.env.variable;

// NOT OK
const something = this.props.something;
1reaction
joncursicommented, Feb 17, 2017

yep, I think having it configurable by number of destructuring levels makes sense

Agreed 100%. I want to use destructuring, but I also don’t want my code to turn into a soup of curly braces. Having a configurable option for depth would give some much-needed flexibility.

// Would be nice if this were allowed
const {
  one,
  two,
  three,
} = this.props;

// However ESLint wants you to destructure this further, which is hard to read IMO:
const {
  props: {
    one,
    two,
    three,
  },
} = this;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating an array destructing a nested object - Stack Overflow
Here's an example that destructures correctly. In this example, I destructure just the first object in your proposal.stakes array (i.e.: ...
Read more >
Heres how JavaScript's Nested Object Destructuring works
A quick video detailing how destructuring works with nested objects and arrays. Check out my https://ES6.io course for more!
Read more >
Destructuring Nested Objects - Medium
Here is another example that extracts the props, but assigns it to a different name. Destructuring also works for nested objects. But what ......
Read more >
Nested object and array destructuring - DEV Community ‍ ‍
Destructuring on nested object?. Destructuring is commonly used, but do you know how to do destructuring nested object and array?
Read more >
javascript destructuring nested object - You.com | The AI ...
A guide on destructuring a nested object enclosed within an array. I recently published two articles that explain destructuring for arrays and objects....
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