Helper function to get function arity by analyzing parameter lists for assignment patterns and rest parameters
95
Build a utility that analyzes JavaScript function AST nodes to extract information about their signatures and generate composition helpers.
Analyze function AST nodes to determine their effective arity (required parameter count).
(a, b, c), the analyzer returns arity 3 @test(a, b = 10, c), the analyzer returns arity 1 because b has a default value @test(a, b, ...rest), the analyzer returns arity 2 because rest parameters are not counted @testGenerate curried wrapper functions based on AST analysis of the original function's arity.
(a, b) => a + b, generate a curried wrapper that allows calling with one argument at a time @test(x, y = 5) => x * y, generate a curried wrapper that only requires the first parameter @test@generates
/**
* Analyzes a function AST node and returns its effective arity
* (number of required parameters before the first optional or rest parameter).
*
* @param {import('@babel/types').Function} functionNode - A Babel AST function node
* @returns {number} The effective arity of the function
*/
function getArity(functionNode) {
// IMPLEMENTATION HERE
}
/**
* Generates a curried wrapper function based on the arity of a function AST node.
* Returns AST nodes representing a curried version of the input function.
*
* @param {import('@babel/types').Function} functionNode - A Babel AST function node
* @returns {import('@babel/types').FunctionExpression} An AST node for the curried wrapper
*/
function generateCurriedWrapper(functionNode) {
// IMPLEMENTATION HERE
}
module.exports = {
getArity,
generateCurriedWrapper,
};Helper function to determine the effective arity of JavaScript functions.
@satisfied-by
Provides utilities for creating and manipulating Babel AST nodes.
@satisfied-by
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