Helper function to get function arity by analyzing parameter lists for assignment patterns and rest parameters
95
Build a function wrapper generator that creates validation wrappers around existing functions. The wrapper should check that the minimum required number of arguments is provided before calling the original function.
When working with JavaScript functions that have mixed required and optional parameters, it's useful to generate wrappers that enforce the required parameter count at runtime. This prevents errors from functions being called with insufficient arguments.
Implement a function generateWrapper(functionNode) that:
arguments.length is at least the required count// Input function with 3 required parameters
function example1(a, b, c) {}
// Required count: 3
// Input function with 1 required, 2 optional parameters
function example2(a, b = 10, c = 20) {}
// Required count: 1
// Input function with 2 required, 1 rest parameter
function example3(x, y, ...rest) {}
// Required count: 2Provides utilities for creating and manipulating JavaScript AST nodes.
Helper function for analyzing function parameter lists.
Create a file src/wrapperGenerator.js that exports a function generateWrapper(functionNode) which returns a wrapper function AST node.
The wrapper function should:
arguments.length to check the number of provided argumentsCreate test file src/wrapperGenerator.test.js:
// @test: Test 1: All required parameters
const functionNode = t.functionExpression(
t.identifier("allRequired"),
[
t.identifier("a"),
t.identifier("b"),
t.identifier("c")
],
t.blockStatement([
t.returnStatement(
t.binaryExpression("+",
t.binaryExpression("+", t.identifier("a"), t.identifier("b")),
t.identifier("c")
)
)
])
);
const wrapper = generateWrapper(functionNode);
// The wrapper should require 3 arguments
// Calling with 2 arguments should throw
// Calling with 3+ arguments should succeed// @test: Test 2: Mixed required and optional parameters
const functionNode = t.functionExpression(
t.identifier("mixedParams"),
[
t.identifier("x"),
t.assignmentPattern(t.identifier("y"), t.numericLiteral(10))
],
t.blockStatement([
t.returnStatement(
t.binaryExpression("*", t.identifier("x"), t.identifier("y"))
)
])
);
const wrapper = generateWrapper(functionNode);
// The wrapper should require only 1 argument (x is required, y is optional)
// Calling with 0 arguments should throw
// Calling with 1+ arguments should succeed// @test: Test 3: Rest parameters
const functionNode = t.functionExpression(
t.identifier("withRest"),
[
t.identifier("first"),
t.identifier("second"),
t.restElement(t.identifier("rest"))
],
t.blockStatement([
t.returnStatement(
t.arrayExpression([t.identifier("first"), t.identifier("second")])
)
])
);
const wrapper = generateWrapper(functionNode);
// The wrapper should require 2 arguments (first and second are required, rest is optional)
// Calling with 1 argument should throw
// Calling with 2+ arguments should succeedInstall 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