Babel plugin that compiles ES2015 default and rest parameters to ES5-compatible code
npx @tessl/cli install tessl/npm-babel-plugin-transform-es2015-parameters@6.24.0Babel plugin that compiles ES2015 (ES6) default and rest parameters to ES5-compatible code. This plugin transforms modern JavaScript parameter features including default parameters, rest parameters, and destructuring parameters into equivalent ES5 constructs that work in legacy environments.
npm install --save-dev babel-plugin-transform-es2015-parametersAs a Babel plugin, this package is typically not imported directly but configured through Babel's configuration system:
// Via require (for programmatic use)
const plugin = require("babel-plugin-transform-es2015-parameters");
// Default export is the plugin function
const transform = require("babel-core").transform;{
"plugins": ["transform-es2015-parameters"]
}babel --plugins transform-es2015-parameters script.jsconst babel = require("babel-core");
const result = babel.transform(code, {
plugins: ["transform-es2015-parameters"]
});Default Parameters:
// Input
function greet(name = "World", greeting = "Hello") {
return `${greeting}, ${name}!`;
}
// Output
function greet() {
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "World";
var greeting = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "Hello";
return `${greeting}, ${name}!`;
}Rest Parameters:
// Input (with complex usage)
function broken(x, ...foo) {
if (true) {
return hello(...foo)
}
}
// Output (full array construction)
function broken(x) {
if (true) {
for (var _len = arguments.length, foo = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
foo[_key - 1] = arguments[_key];
}
return hello.apply(undefined, foo);
}
}
// Input (simple access patterns)
function simple(f, ...items) {
return items[0];
}
// Output (optimized for simple access)
function simple(f) {
return arguments.length <= 1 ? undefined : arguments[1];
}Destructuring Parameters:
// Input
function process({name, age}) {
return `${name} is ${age} years old`;
}
// Output
function process(_ref) {
let {name, age} = _ref;
return `${name} is ${age} years old`;
}The main export is a Babel plugin function that returns a visitor configuration.
/**
* Main Babel plugin function
* @returns {Object} Babel plugin configuration with visitor
*/
function plugin(): BabelPluginConfig;
interface BabelPluginConfig {
visitor: BabelVisitor;
}
interface BabelVisitor {
/** Handles arrow functions that need parameter transformation */
ArrowFunctionExpression(path: NodePath): void;
/** Handles all function types for parameter transformation (from merged visitors) */
Function(path: NodePath): void;
}
// Core Babel types used in the plugin
interface NodePath {
node: Node;
scope: Scope;
get(key: string): NodePath | NodePath[];
isRestElement(): boolean;
isAssignmentPattern(): boolean;
arrowFunctionToShadowed(): void;
ensureBlock(): void;
traverse(visitor: Object, state?: any): void;
replaceWith(node: Node): void;
}
interface Node {
type: string;
params?: Node[];
body?: Node;
[key: string]: any;
}
interface Scope {
generateUidIdentifier(name?: string): Node;
hasOwnBinding(name: string): boolean;
getOwnBinding(name: string): { kind: string };
bindingIdentifierEquals(name: string, node: Node): boolean;
isPure(node: Node): boolean;
push(opts: { id: Node; kind: string }): void;
}Usage:
const plugin = require("babel-plugin-transform-es2015-parameters");
const babel = require("babel-core");
const result = babel.transform(sourceCode, {
plugins: [plugin]
});The plugin returns a visitor object that handles AST node transformations. The visitor is created by merging visitors from three transformation modules.
interface BabelVisitor {
/** Transforms arrow functions that need access to arguments */
ArrowFunctionExpression(path: NodePath): void;
/** Handles all function types for parameter transformation (merged from sub-visitors) */
Function(path: NodePath): void;
}The plugin internally uses three separate transformation modules that can be imported individually:
// Individual module imports (internal API)
const destructuring = require("babel-plugin-transform-es2015-parameters/src/destructuring");
const defaultParams = require("babel-plugin-transform-es2015-parameters/src/default");
const restParams = require("babel-plugin-transform-es2015-parameters/src/rest");
// Each module exports a visitor object
interface TransformationModule {
visitor: {
Function(path: NodePath): void;
};
}The plugin handles three main types of ES2015 parameter transformations:
Default Parameters:
arguments object for parameter accessRest Parameters:
Destructuring Parameters:
interface Dependencies {
"babel-traverse": "^6.24.1"; // AST traversal and visitor merging
"babel-helper-call-delegate": "^6.24.1"; // Function call delegation utilities
"babel-helper-get-function-arity": "^6.24.1"; // Function arity calculation
"babel-template": "^6.24.1"; // Template-based code generation
"babel-types": "^6.24.1"; // AST node type utilities
"babel-runtime": "^6.22.0"; // Babel runtime support
}These dependencies provide:
The plugin accepts no configuration options and works automatically when included in the Babel plugin chain.
Plugin Order Requirements:
Environment Requirements:
let declarations, you may need the transform-block-scoping plugin if your target environment doesn't support block scopingarguments object, so it's not suitable for strict mode contexts that prohibit argumentsThe plugin automatically converts arrow functions to regular functions when they contain parameters that require access to the arguments object:
// Input
const fn = (a = 1, ...rest) => a + rest.length;
// Output (conceptual - actual output varies)
const fn = function(a = 1, ...rest) {
// parameter transformations applied
return a + rest.length;
};The plugin ensures proper variable scoping and avoids naming conflicts:
babel-traverse scope analysis for safe variable namesthis binding semanticsFor rest parameters, the plugin includes optimizations for common usage patterns:
args[0] becomes arguments[offset]args.length becomes optimized length calculationjavascript\n// The main plugin merges three specialized visitors\nvisitors.merge([\n { ArrowFunctionExpression: /* arrow function handler */ },\n destructuring.visitor, // Object/array destructuring\n rest.visitor, // Rest parameter handling with optimizations\n def.visitor // Default parameter transformation\n])\n\n\nRest Parameter Optimization:\n- Uses memberExpressionOptimisationVisitor to analyze usage patterns\n- Tracks references to determine if full array construction is needed\n- Optimizes simple access patterns to direct arguments references\n- Handles complex cases with full array initialization\n\nScope Safety:\n- Generates unique identifiers to avoid naming conflicts\n- Tracks variable bindings and shadowing\n- Ensures proper hoisting behavior for generated variables