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.

Add option to no-unused-vars when using object spreads to omit properties

See original GitHub issue

What version of ESLint are you using? 2.13.1

What rule do you want to change? I’d like to propose a new option to the no-unused-vars rule that does not flag variables used in situations when using the Object Spread Properties when omitting properties while copying objects.

The ES proposal for the Stage 2 Object Rest/Spread Properties enhancement can be found here: https://github.com/sebmarkbage/ecmascript-rest-spread

What code should be flagged as correct with this change? The following code has an object data that will eventually reach a point where I’d like to copy the object and omit specific properties. In this case I’m copying the properties into copiedData and omitting property one.

let data = {one: 1, two: 2, three: 3};
//...

let {one, ...copiedData} = data;

What happens when the rule is applied to this code now? It will specify one as an unused variable. While this is true, having an option to disregard unused variables when used with an omit pattern would be great to have. One area that I’ve used this pattern quite often is in React when passing properties from one component to the next and omitting properties along the way.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
basicdayscommented, Jul 8, 2016

Just wanted to also give a heads up that apparently this pattern is getting recommended in the React documentation as well: https://facebook.github.io/react/warnings/unknown-prop.html

1reaction
platinumazurecommented, Jan 24, 2017

Awesome, thanks for editing your post. Seems reasonable to me as long as it’s been behind an option. Let’s see what the team thinks.

To clarify: I’m 👍 for this contingent on it being behind an option.

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 >
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
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
no-unused-vars - TypeScript ESLint
Disallow unused variables. ... Examples​. This rule extends the base eslint/no-unused-vars rule. It adds support for TypeScript features, such as types.
Read more >
eslint-plugin-no-unused-vars-rest - npm package
This plugin provides a single rule which functions the same as the core rule no-unused-vars , except it adds the ignoreDestructuredVarsWithRest option. Enabling ......
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