Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unused vars in object destructuring with rest property #556

Closed
khankuan opened this issue Jun 29, 2016 · 14 comments

Comments

@khankuan
Copy link

commented Jun 29, 2016

Hi, I've a question for the following syntax:

const { a, b, ...others } = obj;

This is useful to filter out certain keys in an obj. However, it does create 2 unused variables. Wanted to know the alternative that others might use.

Cheers :)

@LinusU

This comment has been minimized.

Copy link
Member

commented Jun 29, 2016

Which is the two you don't want? You can just do like this and it will only extract those variables.

const { a, b } = obj
@khankuan

This comment has been minimized.

Copy link
Author

commented Jun 29, 2016

I need others but not a and b.

@jprichardson

This comment has been minimized.

Copy link
Member

commented Jun 29, 2016

@LinusU I think @khankuan is asking about about blacklisting certain object keys and keeping the rest. In many cases, you may not know exactly which key/vals you want, as the object may contain many different pairs, but you definitely know what you don't want.

Hopefully this lame contrived example demonstrates:

function getUserDataToDisplay () {
  const { secretKey, ...userData } = fetchFromDatabase()
  return userData
}

My solution would be that this is attempting to be too clever and the intent is not made clear. Rather, something like this would be better:

function getUserDataToDisplay () {
  // magic function blackList is defined somewhere...
  return blackList(fetchFromDatabase(), 'secretKey')
}

as this clearly communicates the intent to future readers (even yourself) of the code.

@dcousens

This comment has been minimized.

Copy link
Member

commented Jun 30, 2016

@jprichardson

// magic function blackList is defined somewhere...

https://github.com/dcousens/blacklist, may or may not fit the syntax you want

@dcousens dcousens added the question label Jun 30, 2016

@rszewczyk

This comment has been minimized.

@rstacruz

This comment has been minimized.

Copy link
Member

commented Jun 30, 2016

How about:

const { secret: _, ...data } = foo

That's how it'd be done on elixir, don't know if standard allows it though!

On Thu, Jun 30, 2016, 11:59 PM Rob Szewczyk notifications@github.com
wrote:

Also:

https://lodash.com/docs#omit
http://ramdajs.com/0.21.0/docs/#omit


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#556 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAEikcJXvNHaKzlMznx7jkxSNBh2xMYvks5qQ-fxgaJpZM4JBT8o
.

@rszewczyk

This comment has been minimized.

Copy link

commented Jun 30, 2016

@rstacruz

I tried that at first (almost posted that exact example), but that actually binds the value of secret to a variable named _

@simonkberg

This comment has been minimized.

Copy link

commented Jul 4, 2016

Relevant discussion with usage examples: Hacker0x01/react-datepicker#517 (comment)
An option that allow this pattern has been discussed but postponed by ESLint until the es proposal has progressed further: eslint/eslint#4880 (comment)

@feross

This comment has been minimized.

Copy link
Member

commented Jul 13, 2016

What if we update varsIgnorePattern so that _ is ignored? Then this would work:

const { secret: _, ...data } = foo
@feross

This comment has been minimized.

Copy link
Member

commented Jul 13, 2016

Scratch that -- that wouldn't work for ignoring two keys:

const { secret: _, secret2: _, ...data } = foo

Let's just wait for ESLint to add first-class support for this. This is an experimental language feature anyway.

Blocked on: eslint/eslint#4880

@feross feross changed the title Unused Variables for filtering keys Allow unused vars in object destructuring with rest property Jul 13, 2016

@feross feross added blocked enhancement and removed question labels Jul 13, 2016

@qzb

This comment has been minimized.

Copy link

commented Jul 19, 2016

@feross Why experimental? It's part of current standard and it's supported by current versions of node.js, chrome, firefox and even egde 😛

@simonkberg

This comment has been minimized.

Copy link

commented Jul 19, 2016

@QZD we're talking about the "Rest/Spread Properties" proposal which is currently in stage 2 https://github.com/tc39/proposals

@qzb

This comment has been minimized.

Copy link

commented Jul 19, 2016

@simonkberg @feross Sorry, my bad.

@feross

This comment has been minimized.

Copy link
Member

commented Aug 19, 2016

Trying to clean up the open issues on this repo. This issue should be reevaluated when Rest/Spread Properties becomes part of the official language standard and ESLint adds a rule to support this usage. I would keep an eye on eslint/eslint#4880 to see if there's a new issue/PR posted that implements this functionality.

@feross feross closed this Aug 19, 2016

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
9 participants
You can’t perform that action at this time.