Helper function for Babel transformations that creates delegate call expressions preserving this context and arguments access
npx @tessl/cli install tessl/npm-babel--helper-call-delegate@7.12.0@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.
npm install --save-dev @babel/helper-call-delegateimport callDelegate from "@babel/helper-call-delegate";For CommonJS:
const callDelegate = require("@babel/helper-call-delegate");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]);
}
}
};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 convertedscope (Scope): Optional. The scope to use for variable hoisting. Defaults to path.scopeshouldHoistVariables (boolean): Optional. Whether to hoist variables using @babel/helper-hoist-variables. Defaults to trueReturn Value:
Returns a Babel AST ReturnStatement node containing:
this/arguments: Direct function call return functionExpression()this or arguments: Apply-based call return functionExpression.apply(this, arguments)yield* expressionBehavior:
functionExpressionthis expressions and arguments referencesreturn container()this only: return container.apply(this)arguments only: return container.apply(null, arguments)this and arguments: return container.apply(this, arguments)yield* expressionsUsage 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]);
}
}
}
};
}// 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;The helper integrates with other Babel packages:
shouldHoistVariables is trueThe helper assumes valid Babel AST nodes and may throw errors if:
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.