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

objectSpread not working with boolean arguments #10482

Closed
MengMengYan opened this issue Sep 23, 2019 · 5 comments · Fixed by #10683
Closed

objectSpread not working with boolean arguments #10482

MengMengYan opened this issue Sep 23, 2019 · 5 comments · Fixed by #10683
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@MengMengYan
Copy link

Hi,
We have a scenario where objectSpread throws an exception when the arguments list contains a boolean.

This particular piece of code

function _objectSpread(target) {
  for (var i = 1; i < arguments.length; i++) {
    var source = arguments[i] != null  ? arguments[i] : {};
    var ownKeys = Object.keys(source);

    if (typeof Object.getOwnPropertySymbols === 'function') {
      ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
        return Object.getOwnPropertyDescriptor(source, sym).enumerable;
      }));
    }

    ownKeys.forEach(function (key) {
      defineProperty(target, key, source[key]);
    });
  }

  return target;
}

gives an exception : Object.keys argument is not an object.

Shouldn't the check be like:
var source = arguments[i] != null && typeof arguments[i] === 'object' ? arguments[i] : {};

@babel-bot
Copy link
Collaborator

Hey @MengMengYan! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@nicolo-ribaudo
Copy link
Member

Object.keys should support boolean arguments. You might need to load a polyfill.

@chrishinrichs
Copy link
Contributor

@nicolo-ribaudo this fails in IE11, which has Object.keys natively - seems like this should be fixed as we're using a babel-preset-env setting that should include IE11 and this issue did not occur with babel@6

@peey
Copy link
Contributor

peey commented Nov 8, 2019

@chrishinrichs the spec for Object.keys dictates that it should work for booleans. IE11 might be deviating from the spec.

Can you check if Object.keys(new Object(boolean)) works? Your browser's supposed to be doing something similar internally.

chrishinrichs pushed a commit to chrishinrichs/babel that referenced this issue Nov 8, 2019
@chrishinrichs
Copy link
Contributor

@peey from what I can gather, IE11 implements the ES5 version of Object.keys which specified that the argument had to be an Object (http://ecma-international.org/ecma-262/5.1/#sec-15.2.3.14). The ES6 spec changed to indicate it should be coerced to an object, which is what modern browsers do (http://ecma-international.org/ecma-262/6.0/index.html#sec-object.keys).

I confirmed that Object.keys(Object(boolean)) works in IE11 and have updated my PR accordingly.

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants