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 to call delegate for Babel transformations with proper this and arguments handling

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

To install, run

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

index.mddocs/

babel-helper-call-delegate

babel-helper-call-delegate is a Babel helper function that creates delegate function calls by wrapping function bodies in function expressions and handling this context and arguments object properly. It analyzes function bodies to detect usage of this expressions and arguments references, then generates appropriate call expressions using Function.prototype.apply when necessary to preserve the execution context.

Package Information

  • Package Name: babel-helper-call-delegate
  • Package Type: npm
  • Language: JavaScript with Flow types
  • Installation: npm install babel-helper-call-delegate

Core Imports

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

For CommonJS:

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

Required Dependencies:

This helper requires additional Babel packages for proper usage:

// Flow type imports (for TypeScript/Flow projects)
import type { NodePath } from "babel-traverse";
import * as t from "babel-types";

// For CommonJS
const t = require("babel-types");

Basic Usage

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

// Inside a Babel plugin visitor function
export const visitor = {
  Function(path) {
    // When you need to wrap a function body while preserving this/arguments
    const delegateCall = callDelegate(path, path.scope);
    
    // Create a new function body with the delegate call
    const newBody = t.blockStatement([delegateCall]);
    path.set("body", newBody);
  }
};

Capabilities

Function Delegation

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

/**
 * Creates a delegate function call by wrapping the provided function body
 * in a function expression with proper this and arguments handling
 * @param path - A Babel traverse NodePath representing the function to delegate
 * @param scope - The scope to use for variable hoisting (optional, defaults to path.scope)
 * @returns A return statement containing the delegate call
 */
function callDelegate(path: NodePath, scope?: Scope): ReturnStatement;

Parameters:

  • path (NodePath): A Babel traverse NodePath representing the function to delegate
  • scope (Scope, optional): The scope to use for variable hoisting, defaults to path.scope

Returns:

  • ReturnStatement: A Babel AST return statement containing the delegate call

Behavior:

  1. Creates a function expression wrapper around the original function body
  2. Hoists variables from the original function scope using babel-helper-hoist-variables
  3. Traverses the function body to detect usage of this and arguments
  4. If arguments is detected, transforms the call to use .apply() method
  5. If this is also detected, passes this as the first argument to .apply()
  6. If only arguments is detected (no this), passes null as the first argument to .apply()
  7. Handles generator functions by wrapping the call in a yield* expression
  8. Handles async functions while preserving their asynchronous nature
  9. Returns a return statement containing the delegate call

Usage Example:

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

// Example: Transform a function that uses 'this' and 'arguments'
function transformFunction(path) {
  const originalBody = path.node.body;
  
  // Create delegate call
  const delegateCall = callDelegate(path);
  
  // Replace function body with delegate call
  path.set("body", t.blockStatement([delegateCall]));
}

// Input function:  function(a, b) { return this.method(arguments[0], arguments[1]); }
// Output function: function(a, b) { return function() { return this.method(arguments[0], arguments[1]); }.apply(this, arguments); }

Types

// From babel-traverse
interface NodePath {
  node: Node;
  scope: Scope;
  traverse(visitor: Visitor, state?: any): void;
  // ... other NodePath methods
}

// From babel-traverse  
interface Scope {
  push(opts: { id: Identifier }): void;
  // ... other Scope methods
}

// From babel-types
interface ReturnStatement {
  type: "ReturnStatement";
  argument: Expression | null;
}

interface Node {
  type: string;
  // ... other Node properties
}

Generated Output Patterns

The helper generates different output based on this and arguments usage detection:

No this or arguments usage

// Input: function() { console.log("hello"); }
// Output: return function() { console.log("hello"); }();

With arguments only

// Input: function() { console.log(arguments[0]); }
// Output: return function() { console.log(arguments[0]); }.apply(null, arguments);

With this and arguments

// Input: function() { return this.method(arguments[0]); }  
// Output: return function() { return this.method(arguments[0]); }.apply(this, arguments);

Generator functions

// Input: function*() { yield this.value; }
// Output: return yield* function*() { yield this.value; }.apply(this, arguments);

Async functions

// Input: async function() { return await this.getData(arguments[0]); }
// Output: return async function() { return await this.getData(arguments[0]); }.apply(this, arguments);

Common Use Cases

  1. ES2015 Parameter Transformations: Used in babel-plugin-transform-es2015-parameters when handling default parameters that require function delegation
  2. Function Wrapping: When a function body needs to be wrapped but maintain proper this binding and arguments access
  3. Scope Preservation: When transforming functions while preserving their execution context
  4. IIFE Creation: Creating immediately invoked function expressions while maintaining context