or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel-plugin-transform-es2015-parameters

Babel plugin that compiles ES2015 default and rest parameters to ES5-compatible code

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/babel-plugin-transform-es2015-parameters@6.24.x

To install, run

npx @tessl/cli install tessl/npm-babel-plugin-transform-es2015-parameters@6.24.0

index.mddocs/

Babel Plugin Transform ES2015 Parameters

Babel 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.

Package Information

  • Package Name: babel-plugin-transform-es2015-parameters
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install --save-dev babel-plugin-transform-es2015-parameters

Core Imports

As 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;

Basic Usage

Via .babelrc (Recommended)

{
  "plugins": ["transform-es2015-parameters"]
}

Via CLI

babel --plugins transform-es2015-parameters script.js

Via Node API

const babel = require("babel-core");

const result = babel.transform(code, {
  plugins: ["transform-es2015-parameters"]
});

Transformation Examples

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`;
}

Capabilities

Plugin Function

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]
});

Visitor Object

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;
}

Individual Transformation Modules

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;
  };
}

Parameter Transformation Types

The plugin handles three main types of ES2015 parameter transformations:

Default Parameters:

  • Converts default parameter syntax to conditional assignment
  • Uses arguments object for parameter access
  • Handles complex default expressions and parameter dependencies

Rest Parameters:

  • Transforms rest syntax (...args) to arguments-based array construction
  • Optimizes common access patterns (indexing, length)
  • Handles nested function scoping correctly

Destructuring Parameters:

  • Converts destructuring patterns to variable declarations
  • Maintains proper scoping and hoisting semantics
  • Works with both object and array destructuring

Plugin Dependencies

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:

  • babel-traverse: AST traversal, visitor pattern implementation, and visitor merging
  • babel-helper-call-delegate: Utilities for creating function call delegation
  • babel-helper-get-function-arity: Calculates the number of non-default parameters
  • babel-template: Template-based AST node generation for code insertion
  • babel-types: AST node creation, validation, and type checking utilities
  • babel-runtime: Runtime support for Babel-generated code

Configuration

The plugin accepts no configuration options and works automatically when included in the Babel plugin chain.

Plugin Order Requirements:

  • Must run before plugins that expect ES5 syntax
  • Compatible with other ES2015 transformation plugins
  • Works with the babel-preset-es2015 preset
  • Arrow function transformations happen automatically when needed

Environment Requirements:

  • For default parameters that desugar into let declarations, you may need the transform-block-scoping plugin if your target environment doesn't support block scoping
  • The plugin generates code that uses the arguments object, so it's not suitable for strict mode contexts that prohibit arguments

Transformation Behavior

Arrow Function Handling

The 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;
};

Scope and Binding Safety

The plugin ensures proper variable scoping and avoids naming conflicts:

  • Uses babel-traverse scope analysis for safe variable names
  • Handles shadowed variables and binding conflicts
  • Maintains proper this binding semantics

Optimization Features

For rest parameters, the plugin includes optimizations for common usage patterns:

  • Direct array indexing: args[0] becomes arguments[offset]
  • Length access: args.length becomes optimized length calculation
  • Spread in function calls: optimized for single argument spread\n\n### Internal Architecture Details\n\nThe plugin uses sophisticated AST analysis to determine the optimal transformation approach:\n\nVisitor Merging Strategy:\njavascript\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