A Babel plugin that transforms ES2015 (ES6) arrow functions into ES5-compatible function expressions, enabling modern arrow function syntax to work in legacy environments while preserving semantic behavior including lexical this binding and non-constructible nature.
npm install --save-dev babel-plugin-transform-es2015-arrow-functions// Import the plugin factory (default export)
const arrowFunctionsPlugin = require("babel-plugin-transform-es2015-arrow-functions");For ES modules:
import arrowFunctionsPlugin from "babel-plugin-transform-es2015-arrow-functions";{
"plugins": ["transform-es2015-arrow-functions"]
}{
"plugins": [
["transform-es2015-arrow-functions", { "spec": true }]
]
}const babel = require("babel-core");
const result = babel.transform("const fn = () => {};", {
plugins: ["transform-es2015-arrow-functions"]
});Input:
const greet = (name) => `Hello ${name}!`;
const numbers = [1,2,3].map(n => n * 2);
const obj = {
name: "Bob",
greet() {
this.friends.forEach(friend =>
console.log(`${this.name} greets ${friend}`)
);
}
};Output:
const greet = function(name) {
return `Hello ${name}!`;
};
const numbers = [1,2,3].map(function(n) {
return n * 2;
});
const obj = {
name: "Bob",
greet: function greet() {
var _this = this;
this.friends.forEach(function(friend) {
return console.log(`${_this.name} greets ${friend}`);
});
}
};Input:
const fn = () => this.value;Output:
const fn = function() {
babelHelpers.newArrowCheck(this, _this);
return this.value;
}.bind(this);The main export that creates a Babel plugin instance.
/**
* Creates a Babel plugin for transforming arrow functions
* @param {Object} api - Babel API object containing types and helpers
* @param {Object} api.types - Babel types (t) for AST manipulation
* @returns {Object} Babel plugin configuration with visitor pattern
*/
function arrowFunctionsPlugin({ types: t }): BabelPlugin;Controls how arrow functions are transformed.
interface PluginOptions {
/**
* Enable spec-compliant mode using .bind(this) instead of variable shadowing
* @default false
*/
spec?: boolean;
}The returned plugin configuration object.
interface BabelPlugin {
visitor: {
/**
* Visitor method that transforms arrow function expressions
* @param path - Babel AST path object for the arrow function
* @param state - Plugin state containing options and helpers
*/
ArrowFunctionExpression(path: BabelPath, state: BabelState): void;
};
}this contextvar _this = this.bind(this) calls for this bindingnew operatorbabelHelpers.newArrowCheckthis binding semanticsinterface BabelPath {
/** The AST node being visited */
node: ArrowFunctionExpression;
/** Replaces the current node with a new node */
replaceWith(node: any): void;
/** Ensures the arrow function has a block statement body */
ensureBlock(): void;
/** Gets a child path by property name */
get(property: string): BabelPath;
/** Transforms arrow function using built-in shadowing method */
arrowFunctionToShadowed(): void;
}
interface BabelState {
/** Plugin configuration options */
opts: PluginOptions;
/** Adds a helper function and returns its identifier */
addHelper(name: string): any;
}
interface ArrowFunctionExpression {
/** AST node type */
type: "ArrowFunctionExpression" | "FunctionExpression";
/** Shadow information for this binding */
shadow?: { this: boolean };
}