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

Add for-of fallback for arrays in browsers without symbol support #11263

Merged
merged 4 commits into from Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/babel-helpers/src/helpers.js
Expand Up @@ -1043,8 +1043,24 @@ helpers.createForOfIteratorHelper = helper("7.9.0")`
// f: finish (always called at the end)

export default function _createForOfIteratorHelper(o) {
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null)
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
// Fallback for engines without symbol support
if (Array.isArray(o)) {
var i = 0;
var F = function(){};
return {
s: F,
n: function() {
if (i >= o.length) return { done: true };
return { done: false, value: o[i++] };
},
e: F,
Copy link
Member

Choose a reason for hiding this comment

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

This e method needs to throw the passed in error.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, good catch!

f: F,
};
}

throw new TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}

var it, normalCompletion = true, didErr = false, err;

Expand Down Expand Up @@ -1077,6 +1093,7 @@ helpers.createForOfIteratorHelperLoose = helper("7.9.0")`
var i = 0;

if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
// Fallback for engines without symbol support
if (Array.isArray(o))
return function() {
if (i >= o.length) return { done: true };
Expand Down
Expand Up @@ -6,7 +6,7 @@ function _iterableToArrayLimit(arr, i) { if ((typeof Symbol === "undefined" || !

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _createForOfIteratorHelper(o) { if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); var it, normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e) { didErr = true; err = _e; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _createForOfIteratorHelper(o) { if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o)) { var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: F, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var it, normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e) { didErr = true; err = _e; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }

// https://github.com/babel/babel/issues/7557
var _iterator = _createForOfIteratorHelper(c),
Expand Down