or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel--helper-optimise-call-expression

Helper function to optimise call expression

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/helper-optimise-call-expression@7.27.x

To install, run

npx @tessl/cli install tessl/npm-babel--helper-optimise-call-expression@7.27.0

index.mddocs/

@babel/helper-optimise-call-expression

A 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.

Package Information

  • Package Name: @babel/helper-optimise-call-expression
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @babel/helper-optimise-call-expression

Core Imports

import optimiseCallExpression from "@babel/helper-optimise-call-expression";

For CommonJS:

const optimiseCallExpression = require("@babel/helper-optimise-call-expression");

Basic Usage

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)

Capabilities

Call Expression Optimization

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:

  1. Spread Arguments Pattern: When args contains a single spread element with arguments identifier:

    • Regular call: a.b(...arguments)a.b.apply(a, arguments)
    • Optional call: a.b?.(...arguments)a.b?.apply(a, arguments)
  2. Regular Arguments Pattern: For all other argument patterns:

    • Regular call: a.b(arg1, arg2)a.b.call(a, arg1, arg2)
    • Optional call: 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)

Types

// 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;
}

Dependencies

This package requires @babel/types as a runtime dependency for AST node creation and type checking utilities.