Helper function to get function arity by analyzing parameter lists for assignment patterns and rest parameters
95
Create a Babel plugin that validates function calls to ensure they provide the minimum required number of arguments based on the function's arity.
When calling functions with insufficient arguments, JavaScript allows the call but may lead to unexpected behavior. Your task is to build a Babel plugin that analyzes function calls during compilation and adds runtime validation to ensure callers provide at least the minimum required arguments.
Your plugin should:
"Expected at least X arguments, but got Y"@generates
/**
* Babel plugin that validates function call arity at runtime
*
* @returns {Object} Babel plugin object with visitor methods
*/
export default function arityValidatorPlugin() {
// IMPLEMENTATION HERE
}function add(a, b) {}) gets wrapped with validation that ensures at least 2 arguments are provided @testfunction greet(name, title = "Mr.") {}) gets wrapped to require at least 1 argument @testfunction config(a = 1, b = 2) {}) gets wrapped to require 0 arguments @testHelper function to get function arity by analyzing parameter lists for assignment patterns and rest parameters.
Provides utilities for AST node type checking and creation.
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