CtrlK
BlogDocsLog inGet started
Tessl Logo

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

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

95

1.06x
Overview
Eval results
Files

task.mdevals/scenario-2/

Function Arity Analyzer

Build a tool that analyzes different types of JavaScript functions to determine their effective arity (number of required parameters).

Requirements

Create a utility that accepts various JavaScript function AST nodes and returns the count of required parameters for each function. The utility should:

  1. Accept any type of function AST node (function declarations, function expressions, arrow functions, object methods, class methods, etc.)
  2. Analyze the function's parameter list to identify required vs optional parameters
  3. Return the number of required parameters (stopping at the first optional or rest parameter)
  4. Handle edge cases like empty parameter lists and functions with only optional parameters

Input Format

Your tool should accept Babel AST nodes representing different function types. Each function node will have a params array containing parameter nodes.

Output Format

Return a number representing the count of required parameters for the given function.

Test Cases

  • Given a regular function with three parameters, the arity is 3 @test
  • Given a function expression with two regular parameters and one default parameter, the arity is 2 @test
  • Given an arrow function with one regular parameter followed by a rest parameter, the arity is 1 @test
  • Given a function with only optional parameters, the arity is 0 @test

Implementation

@generates

API

import * as t from "@babel/types";

/**
 * Analyzes a function AST node to determine its effective arity
 * @param node - A Babel AST node representing any type of function
 * @returns The number of required parameters
 */
export function analyzeFunctionArity(node: t.Function): number;

Dependencies { .dependencies }

@babel/types { .dependency }

Provides AST node type definitions and utility functions for working with Babel's abstract syntax tree, including type guards for identifying different parameter types.

@satisfied-by

@babel/helper-get-function-arity { .dependency }

Provides helper functionality to determine the effective arity of JavaScript functions by analyzing their parameter lists.

@satisfied-by

Install with Tessl CLI

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

tile.json