@babel/plugin-transform-for-of

    In

    Out

    1. var _iteratorNormalCompletion = true;
    2. var _didIteratorError = false;
    3. var _iteratorError = undefined;
    4. try {
    5. for (
    6. var _iterator = foo[Symbol.iterator](), _step;
    7. !(_iteratorNormalCompletion = (_step = _iterator.next()).done);
    8. _iteratorNormalCompletion = true
    9. ) {
    10. var i = _step.value;
    11. }
    12. } catch (err) {
    13. _iteratorError = err;
    14. } finally {
    15. try {
    16. if (!_iteratorNormalCompletion && _iterator.return != null) {
    17. _iterator.return();
    18. }
    19. } finally {
    20. if (_didIteratorError) {
    21. throw _iteratorError;
    22. }
    23. }
    24. }

    Without options:

    1. {
    2. "plugins": ["@babel/plugin-transform-for-of"]
    3. }

    With options:

    Via CLI

    1. babel --plugins @babel/plugin-transform-for-of script.js
    1. require("@babel/core").transformSync("code", {
    2. plugins: ["@babel/plugin-transform-for-of"],
    3. });

    loose

    boolean, defaults to false

    In loose mode, arrays are put in a fast path, thus heavily increasing performance.

    1. // babel.config.json
    2. {
    3. "assumptions": {
    4. "skipForOfIteratorClosing": true
    5. }
    6. }

    Example

    In

    Out

    1. var _iterator = foo,
    2. _isArray = Array.isArray(_iterator),
    3. _i = 0,
    4. _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();
    5. ;
    6. ) {
    7. var _ref;
    8. if (_isArray) {
    9. if (_i >= _iterator.length) break;
    10. _ref = _iterator[_i++];
    11. _i = _iterator.next();
    12. if (_i.done) break;
    13. _ref = _i.value;
    14. }
    15. var i = _ref;
    16. }

    Abrupt completions

    Under the skipForOfIteratorClosing assumption, an iterator’s return method will not be called on abrupt completions caused by thrown errors.

    Please see google/traceur-compiler#1773 and for more information.

    boolean, defaults to false

    Added in: v7.10.0

    This option allows for-of to be used with array-like objects.

    While it is not spec-compliant to iterate array-like objects as if they were arrays, there are many objects that would be iterables in modern browsers with Symbol.iterator support. Some notable examples are the DOM collections, like document.querySelectorAll("img.big"), which are the main use case for this option.

    Please note that Babel allows iterating arguments in old engines even if this option is disabled, because it’s defined as iterable in the ECMAScript specification.

    assumeArray

    boolean, defaults to false

    This will apply the optimization shown below to all for-of loops by assuming that all loops are arrays.

    Can be useful when you just want a for-of loop to represent a basic for loop over an array.

    If a basic array is used, Babel will compile the for-of loop down to a regular for loop.

    In

    1. for (let a of [1, 2, 3]) {
    2. }
    1. var _arr = [1, 2, 3];
    2. for (var _i = 0; _i < _arr.length; _i++) {
    3. var a = _arr[_i];
    4. }