or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel--helper-call-delegate

Helper function for Babel transformations that creates delegate call expressions preserving this context and arguments access

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/helper-call-delegate@7.12.x

To install, run

npx @tessl/cli install tessl/npm-babel--helper-call-delegate@7.12.0

index.mddocs/

@babel/helper-call-delegate

@babel/helper-call-delegate is a utility function for Babel transformations that creates delegate call expressions. It analyzes function bodies to detect usage of this and arguments, then generates appropriate function calls that preserve the original context and binding.

Package Information

  • Package Name: @babel/helper-call-delegate
  • Package Type: npm
  • Language: JavaScript with Flow types (compiles to CommonJS)
  • Installation: npm install --save-dev @babel/helper-call-delegate

Core Imports

import callDelegate from "@babel/helper-call-delegate";

For CommonJS:

const callDelegate = require("@babel/helper-call-delegate");

Basic Usage

import callDelegate from "@babel/helper-call-delegate";
import type { NodePath } from "@babel/traverse";

// In a Babel plugin visitor
const visitor = {
  Function(path: NodePath) {
    // When you need to transform a function while preserving this/arguments context
    if (shouldTransformFunction(path)) {
      const delegateReturn = callDelegate(path);
      // delegateReturn is a ReturnStatement that properly handles this/arguments
      
      // Replace function body with the delegate call
      path.node.body = t.blockStatement([delegateReturn]);
    }
  }
};

Capabilities

Call Delegate Creation

Creates a delegate call expression that wraps a function body while preserving this context and arguments access.

/**
 * Creates a delegate call expression for a function
 * @param path - Babel NodePath representing the function to transform
 * @param scope - Optional scope for variable hoisting (defaults to path.scope)
 * @param shouldHoistVariables - Whether to hoist variables (defaults to true)
 * @returns ReturnStatement containing the appropriate call expression
 */
function callDelegate(
  path: NodePath,
  scope?: Scope,
  shouldHoistVariables?: boolean
): ReturnStatement;

Parameters:

  • path (NodePath): Required. A Babel NodePath representing the function to be converted
  • scope (Scope): Optional. The scope to use for variable hoisting. Defaults to path.scope
  • shouldHoistVariables (boolean): Optional. Whether to hoist variables using @babel/helper-hoist-variables. Defaults to true

Return Value: Returns a Babel AST ReturnStatement node containing:

  • For functions without this/arguments: Direct function call return functionExpression()
  • For functions with this or arguments: Apply-based call return functionExpression.apply(this, arguments)
  • For generator functions: Wraps the call in yield* expression

Behavior:

  1. Function Wrapping: Wraps the original function body in a functionExpression
  2. Context Analysis: Traverses the function body to detect this expressions and arguments references
  3. Variable Hoisting: Optionally hoists variables from the function body to the containing scope
  4. Call Generation: Creates appropriate call based on detected context usage:
    • No context needed: return container()
    • With this only: return container.apply(this)
    • With arguments only: return container.apply(null, arguments)
    • With both this and arguments: return container.apply(this, arguments)
  5. Generator Support: For generator functions, wraps calls in yield* expressions
  6. Async Support: Preserves async function characteristics in the generated function expression

Usage Examples:

import callDelegate from "@babel/helper-call-delegate";
import * as t from "@babel/types";

// Example 1: Function without this/arguments
// Input function: function() { console.log("hello"); }
// Output: return (function() { console.log("hello"); })();

// Example 2: Function with this
// Input function: function() { return this.value; }
// Output: return (function() { return this.value; }).apply(this);

// Example 3: Function with arguments
// Input function: function() { return Array.from(arguments); }
// Output: return (function() { return Array.from(arguments); }).apply(null, arguments);

// Example 4: Generator function
// Input function: function*() { yield 1; }
// Output: return yield* ((function*() { yield 1; })());

// Example 5: Async function
// Input function: async function() { return await fetch('/api'); }
// Output: return (async function() { return await fetch('/api'); })();

Integration with Babel Pipeline:

// Typical usage in a Babel plugin
export default function myBabelPlugin() {
  return {
    visitor: {
      Function(path) {
        // Check if transformation is needed
        if (needsParameterTransformation(path)) {
          // Transform parameters first
          transformParameters(path);
          
          // Create delegate call to preserve original behavior
          const delegateCall = callDelegate(path);
          
          // Replace function body
          path.node.body = t.blockStatement([delegateCall]);
        }
      }
    }
  };
}

Types

// Flow type definitions (as used in the source)
type NodePath = import("@babel/traverse").NodePath;
type Scope = import("@babel/traverse").Scope;
type ReturnStatement = import("@babel/types").ReturnStatement;
type FunctionExpression = import("@babel/types").FunctionExpression;
type CallExpression = import("@babel/types").CallExpression;
type YieldExpression = import("@babel/types").YieldExpression;

Dependencies

The helper integrates with other Babel packages:

  • @babel/helper-hoist-variables: Used for variable hoisting when shouldHoistVariables is true
  • @babel/types: Used for creating AST nodes (functionExpression, callExpression, returnStatement, etc.)
  • @babel/traverse: Provides NodePath and Scope types for function parameters

Error Handling

The helper assumes valid Babel AST nodes and may throw errors if:

  • Invalid NodePath is provided
  • The path does not represent a function node
  • Required Babel dependencies are not available

Legacy Context

This helper was previously used by babel-plugin-transform-parameters for handling default parameters in functions that reference this or arguments. While still functional, it may be considered legacy as newer Babel versions handle these transformations more directly.