Helper function to optimise call expression
npx @tessl/cli install tessl/npm-babel--helper-optimise-call-expression@7.27.0A Babel helper function that optimizes JavaScript call expressions by generating more efficient calling patterns. It automatically converts spread argument patterns like (...arguments) to .apply(arguments) calls and regular argument patterns to .call() method invocations, while preserving the original execution context.
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 {
identifier,
callExpression,
memberExpression,
spreadElement
} from "@babel/types";
import type { Expression, CallExpression } from "@babel/types";
// Example: Optimize a call expression with spread arguments
const callee: Expression = memberExpression(identifier("obj"), identifier("method"));
const thisNode: Expression = identifier("obj");
const args: CallExpression["arguments"] = [spreadElement(identifier("arguments"))];
const optional: boolean = false;
const optimized = optimiseCallExpression(callee, thisNode, args, optional);
// Result: obj.method.apply(obj, arguments)
// Example: Optimize a call expression with regular arguments
const regularArgs: CallExpression["arguments"] = [identifier("arg1"), identifier("arg2")];
const optimizedRegular = optimiseCallExpression(callee, thisNode, regularArgs, false);
// Result: obj.method.call(obj, arg1, arg2)Generates optimized call expressions with a specified 'this' context, automatically choosing the most appropriate calling convention based on the argument pattern.
/**
* A helper function that generates a new call expression with given thisNode.
* It will also optimize `(...arguments)` to `.apply(arguments)`
*
* @param callee - The callee of call expression
* @param thisNode - The desired this of call expression
* @param args - The arguments of call expression
* @param optional - Whether the call expression is optional
* @returns The generated new call expression
*/
export default function optimiseCallExpression(
callee: Expression,
thisNode: Expression,
args: Readonly<CallExpression["arguments"]>,
optional: boolean
): CallExpression | OptionalCallExpression;Optimization Behavior:
Spread Arguments Pattern: When args contains a single spread element with arguments identifier:
a.b(...arguments) → a.b.apply(a, arguments)a.b?.(...arguments) → a.b?.apply(a, arguments)Regular Arguments Pattern: For all other argument patterns:
a.b(arg1, arg2) → a.b.call(a, arg1, arg2)a.b?.(arg1, arg2) → a.b?.call(a, arg1, arg2)Usage Examples:
import optimiseCallExpression from "@babel/helper-optimise-call-expression";
import {
identifier,
memberExpression,
spreadElement,
callExpression
} from "@babel/types";
// Optimize spread arguments call
const spreadCall = optimiseCallExpression(
memberExpression(identifier("obj"), identifier("method")),
identifier("obj"),
[spreadElement(identifier("arguments"))],
false
);
// Returns: callExpression(memberExpression(callee, identifier("apply")), [thisNode, arguments])
// Optimize regular arguments call
const regularCall = optimiseCallExpression(
memberExpression(identifier("obj"), identifier("method")),
identifier("obj"),
[identifier("arg1"), identifier("arg2")],
false
);
// Returns: callExpression(memberExpression(callee, identifier("call")), [thisNode, arg1, arg2])
// Optimize optional call
const optionalCall = optimiseCallExpression(
memberExpression(identifier("obj"), identifier("method")),
identifier("obj"),
[identifier("arg1")],
true
);
// Returns: optionalCallExpression(optionalMemberExpression(callee, identifier("call"), false, true), [thisNode, arg1], false)// Imported from @babel/types
interface Expression {
// Base interface for all expression nodes
}
interface CallExpression extends Expression {
type: "CallExpression";
callee: Expression;
arguments: Array<Expression | SpreadElement>;
}
interface OptionalCallExpression extends Expression {
type: "OptionalCallExpression";
callee: Expression;
arguments: Array<Expression | SpreadElement>;
optional: boolean;
}
interface SpreadElement {
type: "SpreadElement";
argument: Expression;
}This package requires @babel/types as a runtime dependency for AST node creation and type checking utilities.