no-unused-vars option to allow unused vars in object destructuring with rest property
See original GitHub issueI’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:
- Created 8 years ago
- Reactions:46
- Comments:15 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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?With array destructuring, you can use this as a workaround:
This isn’t possible with object destructuring.