Skip to content

feat(transform-object-rest-spread): add polyfill=false option to avoid extends helper#3327

Closed
dariocravero wants to merge 2 commits intobabel:masterfrom
UXtemple:transform-object-rest-spread-polyfill-false
Closed

feat(transform-object-rest-spread): add polyfill=false option to avoid extends helper#3327
dariocravero wants to merge 2 commits intobabel:masterfrom
UXtemple:transform-object-rest-spread-polyfill-false

Conversation

@dariocravero
Copy link
Copy Markdown
Contributor

@dariocravero dariocravero commented Feb 6, 2016

This PR follows the same spirit of transform-runtime and allows a user to use Object.assign directly instead of the extends helper.

EDIT by @hzoo: referenced issue is #3934

@codecov-io
Copy link
Copy Markdown

Current coverage is 85.11%

Merging #3327 into master will decrease coverage by -0.27% as of 44b2edc

@@            master   #3327   diff @@
======================================
  Files          215     215       
  Stmts        15768   15769     +1
  Branches      3385    3386     +1
  Methods          0       0       
======================================
- Hit          13463   13422    -41
- Partial        684     726    +42
  Missed        1621    1621       

Review entire Coverage Diff as of 44b2edc

Powered by Codecov. Updated on successful CI builds.

@hzoo hzoo added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Feb 6, 2016
}
```

This plugin will polyfill `Object.assign` by default. If you want to prevent that use config this
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a config like this instead or something else like that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @hzoo, thanks for your comment, I'm not exactly following though, how would you like it to read? Thanks :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was just saying maybe If you want to prevent that use config this instead:
could be something like If you want to prevent that use a config like this instead

@hzoo
Copy link
Copy Markdown
Member

hzoo commented Feb 7, 2016

Are there other transforms that could take this option as well?

cc @amasad thoughts? Sounds fine to me

@amasad
Copy link
Copy Markdown
Member

amasad commented Feb 8, 2016

I think the use of polyfill here is confusing. Polyfill usually means that we are extending builtins which is not the case here, we are just using a babel helper. How about changing the wording and the option to useObjectAssign (which defaults to false).

@hzoo
Copy link
Copy Markdown
Member

hzoo commented Feb 8, 2016

Ok, and do we want to be more general like useNative or useBuiltIn - so we can have a convention for other plugins or specific to useObjectAssign?

@jmm
Copy link
Copy Markdown
Member

jmm commented Feb 8, 2016

I'm not crazy about polyfill either. If this is going to be a generic thing (e.g. @hzoo's naming suggestion) maybe it'd be a good idea to also make the value an object, since there could be more than one native or built-in in play in a plugin right? E.g.: useNative: {objectAssign: true}.

const helper = file.opts.useNative && file.opts.useNative.objectAssign ?
  t.memberExpression(t.identifier('Object'), t.identifier('assign')) :
  file.addHelper("extends");

@amasad
Copy link
Copy Markdown
Member

amasad commented Feb 8, 2016

+1 for useBuiltin.
useNative: {objectAssign: true} goes against my YAGNI instinct 😛

@dariocravero
Copy link
Copy Markdown
Contributor Author

I'm ok with any of those. Being more specific feels definitely much better
:). I'm wondering if it would be a good thing to have these sort of options
somewhat global though. After all, if I'm choosing to directly use
Object.assign somewhere it's vey likely that I'll want to do it everywhere
else too. What do you think about that?
On Mon 8 Feb 2016 at 22:43 Amjad Masad notifications@github.com wrote:

+1 for useBuiltin.
useNative: {objectAssign: true} goes against my YAGNI instinct [image:
😛]


Reply to this email directly or view it on GitHub
#3327 (comment).

@hzoo
Copy link
Copy Markdown
Member

hzoo commented Feb 9, 2016

That would be #3331 - we would still do the option per plugin though

@dariocravero
Copy link
Copy Markdown
Contributor Author

Good stuff! Want me to update the PR accordingly then?

@hzoo
Copy link
Copy Markdown
Member

hzoo commented Feb 9, 2016

Maybe we can get some more opinions @loganfsmyth?

Sounds like either useBuiltIn: true or useBuiltIn: { objectAssign: true } for future proof although YAGNI.

Although we can always override it later so you can do both? I say just useBuiltIn: true is fine for now and we can always add in support for an object later if we actually need it?

@jmm
Copy link
Copy Markdown
Member

jmm commented Feb 9, 2016

@amasad @hzoo

useNative: {objectAssign: true} goes against my YAGNI instinct 😛

I hear you 😃 I thought we should consider what might be the case in other plugins before deciding what to do here, since the point of the name change is to generalize it for all plugins. Personally it wouldn't bother me to be more explicit + future proof, but if you all don't think that's likely to be an issue, that's cool. And as you noted @hzoo, yep, there's an escape hatch if it turns out to be necessary:

Although we can always override it later so you can do both? I say just useBuiltIn: true is fine for now and we can always add in support for an object later if we actually need it?


path.replaceWith(t.callExpression(file.addHelper("extends"), args));
const helper = file.opts.polyfill === false ?
t.memberExpression(t.identifier('Object'), t.identifier('assign')) :
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failure is you need double quotes

@loganfsmyth
Copy link
Copy Markdown
Member

I'd be in favor of useBuiltIn: true or maybe better useBuiltIns: true (with an s) since in the general case many builtins may be in use. We should as explicit as possible about the type so we can overload it in the future if we want.

 const {useBuiltin = false} = file.opts;
 if (typeof useBuiltin !== "boolean") throw new Error("Option 'useBuiltin' for transform-object-rest-spread must be a boolean.");

@hzoo
Copy link
Copy Markdown
Member

hzoo commented Feb 28, 2016

ping @dariocravero - if you're busy I can finish up the changes

@hzoo hzoo modified the milestones: 6.x, 6.6 Feb 28, 2016
@hzoo
Copy link
Copy Markdown
Member

hzoo commented Feb 28, 2016

Although now that I look at the helper.. it already checks for Object.assign?

helpers.extends = template(`
Object.assign || (function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
})
`);

Or do you just not want to include the helper, since this option doesn't seem necessary then?

@hzoo hzoo modified the milestones: 6.x, 6.6 Mar 1, 2016
@hzoo hzoo modified the milestones: 6.x, Minor Jun 13, 2016
@hzoo hzoo added this to the Next Minor milestone Sep 9, 2016
@hzoo hzoo removed this from the Minor milestone Sep 9, 2016
@hzoo hzoo self-assigned this Sep 9, 2016
}

path.replaceWith(t.callExpression(file.addHelper("extends"), args));
const helper = file.opts.polyfill === false ?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this throw if it is passed the wrong type? E.g.

if (polyfill === undefined) polyfill = true;
if (typeof polyfill !== 'boolean') throw new Error

@hzoo
Copy link
Copy Markdown
Member

hzoo commented Sep 9, 2016

Moving to #4491 since I can't modify this PR

@hzoo hzoo closed this Sep 9, 2016
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 7, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants