@babel/plugin-transform-for-of is a Babel plugin that compiles ES2015 for...of statements into ES5-compatible code. It provides optimized transformations with various options for different scenarios, from simple array iteration to complex iterator protocol handling.
npm install --save-dev @babel/plugin-transform-for-ofThe plugin is primarily used through Babel configuration:
{
"plugins": ["@babel/plugin-transform-for-of"]
}For programmatic usage with Babel:
import * as babel from "@babel/core";
import forOfPlugin from "@babel/plugin-transform-for-of";
const result = babel.transformSync(code, {
plugins: [forOfPlugin]
});CommonJS usage:
const babel = require("@babel/core");
const forOfPlugin = require("@babel/plugin-transform-for-of").default;
const result = babel.transformSync(code, {
plugins: [forOfPlugin]
});{
"plugins": [
["@babel/plugin-transform-for-of", {
"loose": false,
"assumeArray": false,
"allowArrayLike": false
}]
]
}Input ES2015 code:
for (const item of items) {
console.log(item);
}Output ES5 code (default mode):
var _iterator = createForOfIteratorHelper(items);
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var item = _step.value;
console.log(item);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}The plugin implements different transformation strategies based on configuration and Babel assumptions:
The main plugin export that creates a Babel plugin instance.
/**
* Main plugin export - Babel plugin that transforms for-of statements
* @param options - Plugin configuration options
* @returns Babel plugin instance
*/
function transformForOfPlugin(options?: Options): BabelPlugin;
export default transformForOfPlugin;
/**
* Babel plugin interface
*/
interface BabelPlugin {
name: "transform-for-of";
visitor: {
ForOfStatement(path: any): void;
};
}Configuration interface for customizing plugin behavior.
/**
* Configuration options for the for-of transform plugin
*/
interface Options {
/** Allow array-like objects to be iterated with optimized code */
allowArrayLike?: boolean;
/** Assume the iterable is always an array for maximum optimization */
assumeArray?: boolean;
/** Use loose mode transformation (less spec-compliant but simpler output) */
loose?: boolean;
}
export { Options };Full ES2015 spec compliance with iterator protocol and proper error handling:
// Input
for (const item of items) {
console.log(item);
}
// Output
var _iterator = createForOfIteratorHelper(items);
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var item = _step.value;
console.log(item);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}When assumeArray: true, generates optimized array loops:
// Input
for (const item of items) {
console.log(item);
}
// Output
for (var _i = 0, _items = items; _i < _items.length; _i++) {
var item = _items[_i];
console.log(item);
}When loose: true, generates simplified code without full error handling:
// Input
for (const item of items) {
console.log(item);
}
// Output
for (var _iterator = createForOfIteratorHelperLoose(items), _step;
!(_step = _iterator()).done;) {
var item = _step.value;
console.log(item);
}When allowArrayLike: true, supports array-like objects with iterator helpers:
// Handles objects like NodeList, arguments, etc.
// Uses createForOfIteratorHelper with array-like supportThe plugin integrates with Babel's assumption system:
/**
* Babel assumptions that affect plugin behavior
*/
interface BabelAssumptions {
/** Equivalent to assumeArray option */
iterableIsArray?: boolean;
/** Equivalent to allowArrayLike option */
arrayLikeIsIterable?: boolean;
/** Equivalent to loose option */
skipForOfIteratorClosing?: boolean;
}Configuration with assumptions:
{
"assumptions": {
"iterableIsArray": true,
"skipForOfIteratorClosing": false
},
"plugins": ["@babel/plugin-transform-for-of"]
}The plugin validates option combinations and throws errors for invalid configurations:
/**
* Plugin validation errors for invalid option combinations
*/
type PluginError =
| "The loose and assumeArray options cannot be used together"
| "The assumeArray and allowArrayLike options cannot be used together"
| "The iterableIsArray and arrayLikeIsIterable assumptions are not compatible"
| "The allowArrayLike is only supported when using @babel/core@^7.10.0";Invalid Combinations:
loose: true with assumeArray: trueassumeArray: true with allowArrayLike: trueiterableIsArray assumption with arrayLikeIsIterable assumption/**
* Required dependencies for the plugin
*/
interface RuntimeDependencies {
"@babel/helper-plugin-utils": "workspace:^";
"@babel/helper-skip-transparent-expression-wrappers": "workspace:^";
}
/**
* Peer dependencies required for plugin operation
*/
interface PeerDependencies {
"@babel/core": "^7.0.0-0";
}{
"plugins": [
["@babel/plugin-transform-for-of", {
"assumeArray": true
}]
]
}{
"plugins": [
["@babel/plugin-transform-for-of", {
"loose": true
}]
]
}{
"plugins": [
["@babel/plugin-transform-for-of", {
"allowArrayLike": true
}]
]
}{
"assumptions": {
"iterableIsArray": false,
"arrayLikeIsIterable": true,
"skipForOfIteratorClosing": false
},
"plugins": ["@babel/plugin-transform-for-of"]
}