Helper function to optimize call expressions by choosing between apply() and call() methods for Babel transformations
npx @tessl/cli install tessl/npm-babel-helper-optimise-call-expression@6.24.0babel-helper-optimise-call-expression is a utility function for optimizing function call expressions in JavaScript AST transformations. It intelligently chooses between Function.prototype.apply() and Function.prototype.call() patterns to generate optimal call expressions for Babel plugins and transformers.
npm install babel-helper-optimise-call-expressionimport optimiseCallExpression from "babel-helper-optimise-call-expression";For CommonJS:
const optimiseCallExpression = require("babel-helper-optimise-call-expression");import optimiseCallExpression from "babel-helper-optimise-call-expression";
import * as t from "babel-types";
// Example usage in a Babel plugin
function transformMethodCall(path) {
const callee = t.identifier("methodName");
const thisNode = t.identifier("obj");
const args = [t.stringLiteral("arg1"), t.numericLiteral(42)];
// Generates optimized call expression
const optimizedCall = optimiseCallExpression(callee, thisNode, args);
// Result: methodName.call(obj, "arg1", 42)
path.replaceWith(optimizedCall);
}
// Example with spread arguments
function transformSpreadCall(path) {
const callee = t.identifier("super");
const thisNode = t.thisExpression();
const args = [t.spreadElement(t.identifier("arguments"))];
// Generates apply() call for spread arguments pattern
const optimizedCall = optimiseCallExpression(callee, thisNode, args);
// Result: super.apply(this, arguments)
path.replaceWith(optimizedCall);
}Optimizes function call expressions by choosing the most appropriate calling pattern based on the provided arguments.
/**
* Optimizes function call expressions by choosing between apply() and call() patterns
* @param {ASTNode} callee - The function/method being called (AST node)
* @param {ASTNode} thisNode - The context/this value for the call (AST node)
* @param {ASTNode[]} args - Array of argument expressions (Array of AST nodes)
* @returns {CallExpression} Optimized call expression AST node
*/
function optimiseCallExpression(callee, thisNode, args);Optimization Logic:
args contains a single spread element with the identifier arguments, generates callee.apply(thisNode, arguments)callee.call(thisNode, ...args)Parameters:
callee: The function or method being called, represented as a babel-types AST nodethisNode: The context object or this value for the function call, as a babel-types AST nodeargs: Array of argument expressions as babel-types AST nodesReturns:
A t.callExpression AST node using either the .apply() or .call() pattern depending on the arguments provided.
Usage Examples:
import optimiseCallExpression from "babel-helper-optimise-call-expression";
import * as t from "babel-types";
// Case 1: Regular arguments -> call() pattern
const regularCall = optimiseCallExpression(
t.identifier("fn"),
t.thisExpression(),
[t.stringLiteral("hello"), t.numericLiteral(42)]
);
// Generates: fn.call(this, "hello", 42)
// Case 2: Spread arguments -> apply() pattern
const spreadCall = optimiseCallExpression(
t.identifier("fn"),
t.thisExpression(),
[t.spreadElement(t.identifier("arguments"))]
);
// Generates: fn.apply(this, arguments)
// Case 3: Method call optimization
const methodCall = optimiseCallExpression(
t.memberExpression(t.identifier("obj"), t.identifier("method")),
t.identifier("context"),
[t.identifier("arg1"), t.identifier("arg2")]
);
// Generates: obj.method.call(context, arg1, arg2)
// Case 4: Real usage in babel-helper-replace-supers
class ReplaceSupers {
optimiseCall(callee, args) {
const thisNode = t.thisExpression();
return optimiseCallExpression(callee, thisNode, args);
}
}This package works with babel-types AST nodes. Key types include:
// Input parameter types (from babel-types)
interface ASTNode {
type: string;
}
interface CallExpression extends ASTNode {
type: "CallExpression";
callee: ASTNode;
arguments: ASTNode[];
}
interface MemberExpression extends ASTNode {
type: "MemberExpression";
object: ASTNode;
property: ASTNode;
}
interface SpreadElement extends ASTNode {
type: "SpreadElement";
argument: ASTNode;
}
interface Identifier extends ASTNode {
type: "Identifier";
name: string;
}This package has the following dependencies:
babel-runtime: ^6.22.0 - Babel runtime helpersbabel-types: ^6.24.1 - Utilities for AST node creation and type checkingThe function uses several babel-types utilities:
t.callExpression() - Creates function call expressionst.memberExpression() - Creates property/method access expressionst.identifier() - Creates identifier nodest.isSpreadElement() - Type checker for spread elementst.isIdentifier() - Type checker for identifiersThis helper is designed for use within:
this binding in transformed method calls