Skip to content

Commit

Permalink
fix(spread): do not require Symbol
Browse files Browse the repository at this point in the history
fixes #9277
  • Loading branch information
clshortfuse committed Mar 31, 2019
1 parent 1f5444e commit ded2532
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
22 changes: 18 additions & 4 deletions packages/babel-helpers/src/helpers.js
Expand Up @@ -860,10 +860,24 @@ helpers.arrayWithHoles = helper("7.0.0-beta.0")`

helpers.iterableToArray = helper("7.0.0-beta.0")`
export default function _iterableToArray(iter) {
if (
Symbol.iterator in Object(iter) ||
Object.prototype.toString.call(iter) === "[object Arguments]"
) return Array.from(iter);
const prototypeWhiteList = [
'[object Arguments]',
];
let hasSymbol = false;
try {
if (Symbol.iterator in Object(iter)) {
hasSymbol = true;
}
} catch (e) {}
if (hasSymbol) {
// Return outside catch block to allow any errors to be thrown by Array.from
return Array.from(iter);
}
// Fallback to whitelist
if (prototypeWhiteList.indexOf(Object.prototype.toString.call(iter)) !== -1) {
return Array.from(iter);
}
return null;
}
`;

Expand Down
19 changes: 18 additions & 1 deletion packages/babel-runtime-corejs2/helpers/esm/iterableToArray.js
@@ -1,5 +1,22 @@
import _Array$from from "../../core-js/array/from";
import _isIterable from "../../core-js/is-iterable";
export default function _iterableToArray(iter) {
if (_isIterable(Object(iter)) || Object.prototype.toString.call(iter) === "[object Arguments]") return _Array$from(iter);
var prototypeWhiteList = ['[object Arguments]'];
var hasSymbol = false;

try {
if (_isIterable(Object(iter))) {
hasSymbol = true;
}
} catch (e) {}

if (hasSymbol) {
return _Array$from(iter);
}

if (prototypeWhiteList.indexOf(Object.prototype.toString.call(iter)) !== -1) {
return _Array$from(iter);
}

return null;
}
19 changes: 18 additions & 1 deletion packages/babel-runtime-corejs2/helpers/iterableToArray.js
Expand Up @@ -3,7 +3,24 @@ var _Array$from = require("../core-js/array/from");
var _isIterable = require("../core-js/is-iterable");

function _iterableToArray(iter) {
if (_isIterable(Object(iter)) || Object.prototype.toString.call(iter) === "[object Arguments]") return _Array$from(iter);
var prototypeWhiteList = ['[object Arguments]'];
var hasSymbol = false;

try {
if (_isIterable(Object(iter))) {
hasSymbol = true;
}
} catch (e) {}

if (hasSymbol) {
return _Array$from(iter);
}

if (prototypeWhiteList.indexOf(Object.prototype.toString.call(iter)) !== -1) {
return _Array$from(iter);
}

return null;
}

module.exports = _iterableToArray;
19 changes: 18 additions & 1 deletion packages/babel-runtime/helpers/esm/iterableToArray.js
@@ -1,3 +1,20 @@
export default function _iterableToArray(iter) {
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
var prototypeWhiteList = ['[object Arguments]'];
var hasSymbol = false;

try {
if (Symbol.iterator in Object(iter)) {
hasSymbol = true;
}
} catch (e) {}

if (hasSymbol) {
return Array.from(iter);
}

if (prototypeWhiteList.indexOf(Object.prototype.toString.call(iter)) !== -1) {
return Array.from(iter);
}

return null;
}
19 changes: 18 additions & 1 deletion packages/babel-runtime/helpers/iterableToArray.js
@@ -1,5 +1,22 @@
function _iterableToArray(iter) {
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
var prototypeWhiteList = ['[object Arguments]'];
var hasSymbol = false;

try {
if (Symbol.iterator in Object(iter)) {
hasSymbol = true;
}
} catch (e) {}

if (hasSymbol) {
return Array.from(iter);
}

if (prototypeWhiteList.indexOf(Object.prototype.toString.call(iter)) !== -1) {
return Array.from(iter);
}

return null;
}

module.exports = _iterableToArray;

0 comments on commit ded2532

Please sign in to comment.