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.

no-unused-vars option to allow unused vars in object destructuring with rest property

See original GitHub issue

I’d like an option for the no-unused-vars rule that would allow unused variables that are created as part of an object destructuring pattern that also includes an experimental rest property.

Use case: experimental object rest properties offer a convenient way to shallow clone an object while omitting certain properties. For example,

let foo = { a: 1, b: 2, extra: { other: 'stuff' }};

// I want a shallow clone of `foo` that omits `extra`
let { extra, ...bar } = foo; // error-- `extra` is unused

return bar; // { a: 1, b: 2 }

This can be done now by naming the unused variables with a pattern (instead of using the shorthand) and configuring no-unused-vars with varsIgnorePattern. It’s less convenient and it adds noise, though.

let { extra: ignored, ...bar } = foo;

Rest/spread properties are still a stage 2 proposal, so it might be premature to add an option to a core rule. I figured I’d open for discussion anyways 😃

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:46
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

12reactions
bryanrsmithcommented, Sep 21, 2016

I’ve written a (hopefully temporary) plugin for this at https://github.com/bryanrsmith/eslint-plugin-no-unused-vars-rest. It basically just monkey patches the option into the existing core rule.

I’d be happy to submit a PR to eslint-plugin-babel if everyone would prefer that. It’s a little odd, though, because it doesn’t require babel-eslint. Espree’s experimentalObjectRestSpread option covers it. What do you folks think?

7reactions
not-an-aardvarkcommented, Dec 5, 2016

With array destructuring, you can use this as a workaround:

const [ , ...tail] = someArray;

This isn’t possible with object destructuring.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Omit property variable when using object destructuring
Using a Rest Property it is possible to “omit” properties from an object, but by default the sibling properties are marked as “unused”....
Read more >
no-unused-vars - ESLint - Pluggable JavaScript Linter
This rule is aimed at eliminating unused variables, functions, and function parameters. A variable foo is considered to be used if any of...
Read more >
The 3 Best ESLint No-Unused-Vars Option Settings (Make it ...
My three favorite options are below: Allowing unused sibling values when using the Rest property; Allowing unused values when destructuring ...
Read more >
eslint-plugin-no-unused-vars-rest - npm
Enabling this option will allow unused variables appearing in destructuring assignments that also contain experimental rest properties. This is ...
Read more >
no-unused-vars and destructuring - Google Groups
function clean(items) { // return a copy of an array of objects, with two properties, clientID and color, removed return items.map(({ clientID, color,...
Read more >

github_iconTop Related Medium Post

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