Skip to content

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

Merged
existentialism merged 1 commit intobabel:masterfrom
chrishinrichs:master
Nov 9, 2019
Merged

Fix: Don't call Object.keys on non-objects (babel#10482)#10683
existentialism merged 1 commit intobabel:masterfrom
chrishinrichs:master

Conversation

@chrishinrichs
Copy link
Copy Markdown
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' }
};

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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