Helper function to get function arity by analyzing parameter lists for assignment patterns and rest parameters
95
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.
Your implementation should:
b = 10)...args)(a, b, c) with all regular parameters, return 3(a, b = 10, c) with a default parameter, return 1 (only a is required before the first optional parameter)(a, b, ...rest) with a rest parameter, return 2 (parameters before the rest parameter)() { } with no parameters, return 0(a = 1, b = 2) with all optional parameters, return 0@generates
/**
* 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;(a, b, c), it returns 3 @test(a, b = 10), it returns 1 @test(a, b, ...rest), it returns 2 @test(), it returns 0 @testProvides function arity analysis capabilities for Babel AST nodes.
Provides type definitions and utilities for working with Babel AST nodes.
Install with Tessl CLI
npx tessl i tessl/npm-babel--helper-get-function-aritydocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10