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 for Babel transformations

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

To install, run

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

index.mddocs/

Babel Helper Get Function Arity

A Babel helper utility that determines the effective arity (number of parameters) of JavaScript functions by analyzing their parameter lists. This function counts parameters up to the first default parameter or rest element, making it essential for Babel transformations that need to understand function signatures.

Package Information

  • Package Name: babel-helper-get-function-arity
  • Package Type: npm
  • Language: JavaScript (ES6)
  • 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 function AST nodes
const regularFunction = {
  params: [
    t.identifier("a"),
    t.identifier("b"), 
    t.identifier("c")
  ]
};

const functionWithDefaults = {
  params: [
    t.identifier("a"),
    t.assignmentPattern(t.identifier("b"), t.numericLiteral(10)),
    t.identifier("c")
  ]
};

const functionWithRest = {
  params: [
    t.identifier("a"),
    t.identifier("b"),
    t.restElement(t.identifier("rest"))
  ]
};

// Get effective arity
console.log(getFunctionArity(regularFunction));      // 3
console.log(getFunctionArity(functionWithDefaults)); // 1 (stops at first default)
console.log(getFunctionArity(functionWithRest));     // 2 (stops at rest element)

Capabilities

Function Arity Calculation

Determines the effective arity of a function by counting parameters up to the first assignment pattern (default parameter) or rest element.

/**
 * Gets the effective arity of a function node
 * @param {Object} node - Function AST node with params property
 * @returns {number} The effective arity of the function
 */
function getFunctionArity(node);

Algorithm Details:

  • Iterates through the node.params array
  • Returns the index of the first parameter that is either:
    • An AssignmentPattern (default parameter like b = 10)
    • A RestElement (rest parameter like ...args)
  • If no such parameters are found, returns the total length of the params array

Usage in Babel Ecosystem:

This helper is used by Babel plugins to:

  • Parameter transformation: Determine how many parameters to preserve when transforming functions with default parameters
  • Function wrapping: Generate wrapper functions with the correct number of parameters to maintain the original function's arity
  • AST manipulation: Understand function signatures during compilation phases

Types

interface FunctionNode {
  params: Array<Parameter>;
}

type Parameter = 
  | Identifier 
  | AssignmentPattern 
  | RestElement 
  | ObjectPattern 
  | ArrayPattern
  | MemberExpression;

// From babel-types
interface AssignmentPattern {
  type: "AssignmentPattern";
  left: Pattern;
  right: Expression;
}

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

interface MemberExpression {
  type: "MemberExpression";
  object: Expression;
  property: Expression;
  computed: boolean;
}

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

interface ObjectPattern {
  type: "ObjectPattern";
  properties: Array<Property | RestProperty>;
}

interface ArrayPattern {
  type: "ArrayPattern";
  elements: Array<Pattern | null>;
}

Dependencies

  • babel-types: Provides AST node type checking functions (t.isAssignmentPattern, t.isRestElement)
  • babel-runtime: Babel runtime helpers (used in compiled output)

Real-world Examples

ES2015 Parameter Transform

Used in babel-plugin-transform-es2015-parameters to determine parameter cutoff:

// Input function with mixed parameters
async function example(a, b = 1, c, d = 3) {}

// getFunctionArity returns 1, so parameters are cut at index 1
// Resulting transformed function signature: example(_x)

Function Name Helper

Used in babel-helper-function-name to preserve arity in wrapper functions:

// When creating wrapper functions, maintains original arity
const wrapper = function(x1, x2) { // arity preserved
  return originalFunction.apply(this, arguments);
};