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

Function Signature Analyzer

Build a static analysis tool that analyzes JavaScript/TypeScript code to generate a report on function signatures and their parameter requirements.

Requirements

Your tool should analyze JavaScript source code files and produce a JSON report containing:

  1. Function inventory: List all functions found in the code with their names and locations
  2. Parameter analysis: For each function, report:
    • Total number of parameters
    • Number of required parameters
    • Whether the function has optional parameters (default values)
    • Whether the function uses rest parameters
  3. Summary statistics: Aggregate metrics about the analyzed codebase:
    • Total number of functions analyzed
    • Average number of required parameters
    • Count of functions with high arity (more than 3 required parameters)

The tool should handle all JavaScript function types including:

  • Function declarations
  • Function expressions
  • Arrow functions
  • Object methods
  • Class methods

Implementation

@generates

API

/**
 * Analyzes JavaScript source code and generates a function signature report
 *
 * @param {string} sourceCode - The JavaScript source code to analyze
 * @returns {Object} Analysis report with function details and summary statistics
 *
 * Report structure:
 * {
 *   functions: [
 *     {
 *       name: string,          // Function name or "<anonymous>"
 *       type: string,          // Function type (e.g., "FunctionDeclaration")
 *       totalParams: number,   // Total parameter count
 *       requiredParams: number, // Required parameter count
 *       hasOptional: boolean,  // Has default parameters
 *       hasRest: boolean       // Has rest parameter
 *     }
 *   ],
 *   summary: {
 *     totalFunctions: number,
 *     avgRequiredParams: number,
 *     highArityCount: number  // Functions with > 3 required params
 *   }
 * }
 */
function analyzeFunctions(sourceCode) {
  // IMPLEMENTATION HERE
}

module.exports = { analyzeFunctions };

Test Cases

  • Given source code with a function that has 2 regular parameters, the tool reports totalParams as 2 and requiredParams as 2 @test
  • Given source code with a function that has 1 regular parameter and 1 default parameter, the tool reports totalParams as 2 and requiredParams as 1 @test
  • Given source code with a function that has 2 regular parameters and a rest parameter, the tool reports totalParams as 3, requiredParams as 2, and hasRest as true @test
  • Given source code with multiple functions, the summary statistics correctly calculate totalFunctions and avgRequiredParams @test

Dependencies { .dependencies }

@babel/parser { .dependency }

Parses JavaScript source code into an Abstract Syntax Tree (AST).

@babel/traverse { .dependency }

Traverses the AST to find and visit function nodes.

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

Determines the effective arity (required parameter count) of functions.

@babel/types { .dependency }

Provides utilities for checking AST node types and properties.

Install with Tessl CLI

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

tile.json