or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel--helper-get-function-arity

Helper function to get function arity by analyzing parameter lists for assignment patterns and rest parameters

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/helper-get-function-arity@7.16.x

To install, run

npx @tessl/cli install tessl/npm-babel--helper-get-function-arity@7.16.0

index.mddocs/

@babel/helper-get-function-arity

Helper function to get function arity by analyzing parameter lists for assignment patterns and rest parameters. This utility determines the effective arity (number of required parameters) of JavaScript functions, which is essential for Babel transformations that need to understand function signatures.

Package Information

  • Package Name: @babel/helper-get-function-arity
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @babel/helper-get-function-arity

Core Imports

import getFunctionArity from "@babel/helper-get-function-arity";

For CommonJS:

const getFunctionArity = require("@babel/helper-get-function-arity");

Basic Usage

import getFunctionArity from "@babel/helper-get-function-arity";
import * as t from "@babel/types";

// Example with a function that has no optional parameters
const regularFunction = t.functionExpression(
  null,
  [t.identifier("a"), t.identifier("b"), t.identifier("c")],
  t.blockStatement([])
);
const arity1 = getFunctionArity(regularFunction); // Returns: 3

// Example with a function that has default parameters
const functionWithDefaults = t.functionExpression(
  null,
  [
    t.identifier("a"),
    t.assignmentPattern(t.identifier("b"), t.numericLiteral(10)),
    t.identifier("c")
  ],
  t.blockStatement([])
);
const arity2 = getFunctionArity(functionWithDefaults); // Returns: 1

// Example with a function that has rest parameters
const functionWithRest = t.functionExpression(
  null,
  [
    t.identifier("a"),
    t.identifier("b"),
    t.restElement(t.identifier("rest"))
  ],
  t.blockStatement([])
);
const arity3 = getFunctionArity(functionWithRest); // Returns: 2

Capabilities

Function Arity Analysis

Determines the effective arity of JavaScript functions by finding the first optional or rest parameter.

/**
 * Get the effective arity of a function by analyzing its parameter list
 * @param node - A Babel AST node representing any function type
 * @returns The index of the first optional parameter, or total parameter count if all are required
 */
function getFunctionArity(node: t.Function): number;

The function analyzes the params array of the function node and:

  1. Iterates through each parameter
  2. Checks if the parameter is an assignment pattern (default parameter) using isAssignmentPattern()
  3. Checks if the parameter is a rest element (...args) using isRestElement()
  4. Returns the index of the first optional/rest parameter found
  5. If no optional/rest parameters are found, returns the total parameter count

Types

Function Union Type (from @babel/types)

type Function =
  | FunctionDeclaration
  | FunctionExpression
  | ObjectMethod
  | ArrowFunctionExpression
  | ClassMethod
  | ClassPrivateMethod;

All these function types have a params property of type Array<Identifier | Pattern | RestElement>.

Parameter Types

type Identifier = {
  type: "Identifier";
  name: string;
};

type AssignmentPattern = {
  type: "AssignmentPattern";
  left: Pattern;
  right: Expression;
};

type RestElement = {
  type: "RestElement";
  argument: Pattern;
};

Dependencies

This package depends on @babel/types for:

  • Type definitions for Babel AST nodes
  • isAssignmentPattern() utility function for detecting default parameters
  • isRestElement() utility function for detecting rest parameters

Use Cases

This helper is commonly used in Babel plugins and transformations where understanding function signatures is crucial:

  • Function wrapper generation: Creating wrapper functions that need to match the arity of the original
  • Code transformation: Transforming function calls based on parameter requirements
  • Static analysis: Analyzing code patterns that depend on function arity
  • Optimization: Optimizing function calls based on parameter usage patterns