Skip to content

Commit

Permalink
Extract for-of iterator handling to a helper (#11262)
Browse files Browse the repository at this point in the history
* Extract for-of iterator handling to a helper

Dis greatly recudes the code size when for-of is used multiple times across the
codebase. Also, makes it easier to read what's happening in a compiled loop.

* Unify spec and loose code

* Legacy implementation fallback

* Update tmp var name

* Updates from review and after rebase
  • Loading branch information
nicolo-ribaudo authored Mar 16, 2020
1 parent a11855e commit 5997668
Show file tree
Hide file tree
Showing 51 changed files with 621 additions and 573 deletions.
55 changes: 55 additions & 0 deletions packages/babel-helpers/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,61 @@ helpers.nonIterableRest = helper("7.0.0-beta.0")`
}
`;

helpers.createForOfIteratorHelper = helper("7.9.0")`
// s: start (create the iterator)
// n: next
// e: error (called whenever something throws)
// f: finish (always called at the end)
export default 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() {
it = o[Symbol.iterator]();
},
n() {
var step = it.next();
normalCompletion = step.done;
return step;
},
e(e) {
didErr = true;
err = e;
},
f() {
try {
if (!normalCompletion && it.return != null) it.return();
} finally {
if (didErr) throw err;
}
}
};
}
`;

helpers.createForOfIteratorHelperLoose = helper("7.9.0")`
export default function _createForOfIteratorHelperLoose(o) {
var i = 0;
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
if (Array.isArray(o))
return function() {
if (i >= o.length) return { done: true };
return { done: false, value: o[i++] };
}
throw new TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
i = o[Symbol.iterator]();
return i.next.bind(i);
}
`;

helpers.skipFirstGeneratorNext = helper("7.0.0-beta.0")`
export default function _skipFirstGeneratorNext(fn) {
return function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"syntax-jsx",
"transform-react-jsx",
"transform-block-scoped-functions",
"transform-for-of"
"transform-for-of",
["external-helpers", { "helperVersion": "7.100.0" }]
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,19 @@ var _loop = function (i) {
});
};

var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
var _iterator = babelHelpers.createForOfIteratorHelper(nums),
_step;

try {
for (var _iterator = nums[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var i = _step.value;
var x;
var f;

_loop(i);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
_iterator.e(err);
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
_iterator.f();
}
Loading

0 comments on commit 5997668

Please sign in to comment.