Fix typeof Symbol.prototype (#3686)

* formatting

* fix `typeof Symbol.prototype`

Babel uses a helper function to return the correct value for `typeof
obj` when obj is a Symbol and support for Symbol has been polyfilled.

This function assumes that `obj.constructor === Symbol` implies `typeof
obj === 'symbol'`.

This isn't true when obj is `Symbol.prototype`. In that case (REPL from
node 6, the same holds in Firefox):

```
> Symbol.prototype.constructor === Symbol
true
> typeof Symbol.prototype
'object'
>
```

AFAICS, that's the only case where the assumption doesn't hold.

The test added by this patch fails only on node 0.10, as 0.12 already
has a native implementation of Symbol and the polyfill code doesn't run.

This caused a problem in core-js when it's compiled with babel (the
issue was isolated by @skozin here:
https://github.com/zloirock/core-js/issues/189#issuecomment-209864582).
This commit is contained in:
Alberto Piai 2016-09-20 18:19:09 +02:00 committed by Henry Zhu
parent cb4b2b0744
commit 8f6d4ae5cf
3 changed files with 7 additions and 2 deletions

View File

@ -8,7 +8,11 @@ export default helpers;
helpers.typeof = template(` helpers.typeof = template(`
(typeof Symbol === "function" && typeof Symbol.iterator === "symbol") (typeof Symbol === "function" && typeof Symbol.iterator === "symbol")
? function (obj) { return typeof obj; } ? function (obj) { return typeof obj; }
: function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype
? "symbol"
: typeof obj;
};
`); `);
helpers.jsx = template(` helpers.jsx = template(`

View File

@ -1,4 +1,4 @@
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _Symbol = foo(); var _Symbol = foo();
typeof s === "undefined" ? "undefined" : _typeof(s); typeof s === "undefined" ? "undefined" : _typeof(s);

View File

@ -1,3 +1,4 @@
var s = Symbol("s"); var s = Symbol("s");
assert.equal(typeof s, "symbol"); assert.equal(typeof s, "symbol");
assert.ok(typeof s === "symbol"); assert.ok(typeof s === "symbol");
assert.ok(typeof Symbol.prototype === 'object', "`typeof Symbol.prototype` should be 'object'");