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-3/

Function Parameter Counter

Build a utility that analyzes JavaScript function Abstract Syntax Tree (AST) nodes and reports how many required parameters they have. The utility should identify where optional parameters begin in a function's parameter list.

Requirements

Your implementation should:

  1. Accept any JavaScript function AST node as input (function declarations, function expressions, arrow functions, class methods, etc.)
  2. Analyze the function's parameter list to determine how many parameters are required
  3. Return the count of required parameters, which is the position of the first optional parameter
  4. Consider a parameter optional if it:
    • Has a default value (e.g., b = 10)
    • Is a rest parameter (e.g., ...args)

Expected Behavior

  • For a function (a, b, c) with all regular parameters, return 3
  • For a function (a, b = 10, c) with a default parameter, return 1 (only a is required before the first optional parameter)
  • For a function (a, b, ...rest) with a rest parameter, return 2 (parameters before the rest parameter)
  • For a function () { } with no parameters, return 0
  • For a function (a = 1, b = 2) with all optional parameters, return 0

Implementation

@generates

API

/**
 * Analyzes a function AST node and returns the count of required parameters
 * @param node - A Babel AST node representing a function
 * @returns The number of required parameters
 */
export function getRequiredParamCount(node: Function): number;

Test Cases

  • Given a function with three regular parameters (a, b, c), it returns 3 @test
  • Given a function with a default parameter (a, b = 10), it returns 1 @test
  • Given a function with a rest parameter (a, b, ...rest), it returns 2 @test
  • Given a function with no parameters (), it returns 0 @test

Dependencies { .dependencies }

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

Provides function arity analysis capabilities for Babel AST nodes.

@babel/types { .dependency }

Provides type definitions and utilities for working with Babel AST nodes.

Install with Tessl CLI

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

tile.json