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

Function Wrapper Generator

Summary

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.

Background

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.

Requirements

Implement a function generateWrapper(functionNode) that:

  1. Accepts a function AST node as input
  2. Analyzes the function's parameter list to determine how many parameters are required
  3. Generates and returns a new wrapper function AST node that:
    • Preserves the same parameter signature as the original function
    • At runtime, checks that arguments.length is at least the required count
    • Throws an Error with message "Insufficient arguments" if the check fails
    • Otherwise, calls the original function with all provided arguments and returns its result

Parameter Analysis Rules

  • Regular parameters (simple identifiers) are required
  • Parameters with default values are optional
  • Rest parameters are optional
  • The required count is the number of parameters before the first optional or rest parameter

Example Transformations

// 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: 2

Dependencies { .dependencies }

@babel/types { .dependency }

Provides utilities for creating and manipulating JavaScript AST nodes.

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

Helper function for analyzing function parameter lists.

Implementation

Create a file src/wrapperGenerator.js that exports a function generateWrapper(functionNode) which returns a wrapper function AST node.

The wrapper function should:

  • Use arguments.length to check the number of provided arguments
  • Compare against the required parameter count
  • Throw an error if insufficient arguments are provided
  • Use spread syntax to forward all arguments to the original function

Test Cases { .test }

Create test file src/wrapperGenerator.test.js:

Test 1: All required parameters { .test }

// @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 2: Mixed required and optional parameters { .test }

// @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 3: Rest parameters { .test }

// @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 succeed

Install with Tessl CLI

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

tile.json