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

Fix: Don't call Object.keys on non-objects (babel#10482) #10683

Merged
merged 1 commit into from Nov 9, 2019

Conversation

chrishinrichs
Copy link
Contributor

@chrishinrichs chrishinrichs commented Nov 8, 2019

Q                       A
Fixed Issues? Fixes #10482
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This changes the polyfill for the object spread operator to ensure that the argument to be spread is an Object before calling Object.keys. This fixes a JS error in IE11 which implements the old ES5 version of Object.keys which does not support non-object arguments.

A common pattern that will run into this issue:

const someBool = false;
const obj = {
  ...someBool && { foo: 'bar' }
};

@@ -396,7 +396,7 @@ helpers.objectSpread = helper("7.0.0-beta.0")`

export default function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = (arguments[i] != null) ? arguments[i] : {};
var source = (typeof arguments[i] === 'object' && arguments[i] != null) ? arguments[i] : {};
var ownKeys = Object.keys(source);
Copy link
Member

@ljharb ljharb Nov 8, 2019

Choose a reason for hiding this comment

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

it would be better to do this:

Suggested change
var ownKeys = Object.keys(source);
var ownKeys = Object.keys(Object(source));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review! I have made the suggested change @ljharb, can you take another look?

@nicolo-ribaudo nicolo-ribaudo added area: helpers PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Nov 8, 2019
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

My initial reaction about the issue was "you just need to load a polyfill", but given that the fix is so simple I don't see why we shouldn't include it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: helpers outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

objectSpread not working with boolean arguments
6 participants