Helper function to get function arity for Babel transformations
npx @tessl/cli install tessl/npm-babel-helper-get-function-arity@6.24.0A 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.
npm install babel-helper-get-function-arityimport getFunctionArity from "babel-helper-get-function-arity";For CommonJS:
const getFunctionArity = require("babel-helper-get-function-arity");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)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:
node.params arrayb = 10)...args)Usage in Babel Ecosystem:
This helper is used by Babel plugins to:
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>;
}t.isAssignmentPattern, t.isRestElement)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)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);
};