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

⚛️Add idx in babel-preset #1874

Closed
wants to merge 2 commits into from
Closed

Conversation

pierr
Copy link

@pierr pierr commented Mar 21, 2017

Idx is the existential function, it seems to me that it would be a benefit for almost all project using react and data in a graph.
I concidered using eject to add this feature but maybe this be a good add for the community.

see https://facebook.github.io/react-native/blog/2017/03/13/idx-the-existential-function.html for more informations about it.

@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks!

If you are contributing on behalf of someone else (eg your employer): the individual CLA is not sufficient - use https://developers.facebook.com/opensource/cla?type=company instead. Contact cla@fb.com if you have any questions.

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@gaearon gaearon added this to the 0.9.6 milestone Mar 21, 2017
@gaearon
Copy link
Contributor

gaearon commented Mar 21, 2017

Let's do it.

@gaearon
Copy link
Contributor

gaearon commented Mar 21, 2017

Hmm. Actually I'm concerned that Babel plugin doesn't verify idx comes from an import from idx package. This change could be dangerous if somebody has a function called idx in their code.

Can you please raise an issue about this in idx repo?

@gaearon gaearon removed this from the 0.9.6 milestone Mar 21, 2017
@pierr
Copy link
Author

pierr commented Mar 21, 2017

Ok i will do this. Thanks for your quick reply.

This will allow the use of the preset.
@pierr
Copy link
Author

pierr commented Mar 21, 2017

@gaearon If fact there is no ambiguity, as idx has to be imported and in the project direct dependencies or devDependencies. I add it to react-scripts dev dependencies.

import idx from 'idx'
const props = {user: {friends :[{friends: 1}]}}
idx(props, _ => _.user.friends[0].friends)

Generated

screen shot 2017-03-21 at 21 38 20

And i have been able to use idx in a map somewhere else

[1,2,4].map((a, idx) => console.log('idx', idx))

@gaearon
Copy link
Contributor

gaearon commented Mar 22, 2017

What happens if the user writes

function idx() {
  // my own code
}

var a = idx();

Isn't Babel plugin going to change the function meaning?

@pierr
Copy link
Author

pierr commented Mar 22, 2017

Indeed...
screen shot 2017-03-22 at 07 04 59

@ggregoire
Copy link

ggregoire commented Mar 23, 2017

Isn't it redundant for those who already use Lodash?

var props = { user: { friends: [{ friends: ['yo'] }] } }

_.has(props, 'user.friends[0].friends') // true
_.get(props, 'user.friends[0].friends') // ['yo']

var props = { user: { friends: [] } }

_.has(props, 'user.friends[0].friends') // false
_.get(props, 'user.friends[0].friends') // undefined

@viankakrisna
Copy link
Contributor

@ggregoire thanks for the lodash trick! been using it everywhere now :) I think the use case for idx babel plugin is to transform the function calls to nested ternary checks https://facebook.github.io/react-native/blog/2017/03/13/idx-the-existential-function.html not really sure why we need this though (will it make the bundle larger for deeply nested property access?)

@ericnakagawa
Copy link

ericnakagawa commented Apr 23, 2017

I'd love to have this functionality! I'm writing a very long list of ternary functions on my own and it is tedious and bug prone!

This helps greatly when dealing with asynchronously loaded data from APIs you don't control/own.

@Timer Timer added this to the 2.0.0 milestone Jan 12, 2018
@wtgtybhertgeghgtwtg
Copy link
Contributor

Correct me if I'm wrong, but shouldn't the optional chaining syntax available with babel@7 now that it's in?
babel PR: babel/babel#5813

@gaearon
Copy link
Contributor

gaearon commented Jan 13, 2018

We won't enable that proposal unless it advances to 3rd or 4th stages.

@Timer
Copy link
Contributor

Timer commented Jan 13, 2018

Maybe we shouldn't merge this given the Optional Chaining Operator exists?
I imagine it will progress quickly.

@Timer
Copy link
Contributor

Timer commented Jan 13, 2018

I'll close this because there hasn't been much support around it and there's a syntax proposal in the works.

Thanks @pierr!

@Timer Timer closed this Jan 13, 2018
@gaearon
Copy link
Contributor

gaearon commented Jan 13, 2018

I think Optional Chaining was somewhat controversial and wouldn't count on it progressing quickly.

However, this feature seems like a good fit for babel-plugin-macros support we just merged.
So I'd rather see this as a macro in userland.

@kentcdodds
Copy link
Contributor

Hey guess what! There's already a macro for it! https://github.com/dralletje/idx.macro

Woo!

@Timer
Copy link
Contributor

Timer commented Jan 14, 2018

That's seriously awesome @kentcdodds

@kentcdodds
Copy link
Contributor

kentcdodds commented Jan 14, 2018

That said, it does need to upgrade to babel-plugin-macros: dralletje/idx.macro#3 😅 should be a cinch though!

@lock lock bot locked and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants