Babel plugin to ensure function declarations at the block level are block scoped
89
Build a tool that analyzes JavaScript code to determine whether function declarations within block statements need to be transformed based on strict mode context.
In JavaScript, function declarations within block statements (like if blocks or standalone blocks) have different scoping rules depending on whether the code runs in strict mode. Your task is to create an analyzer that:
Your analyzer should handle the following cases:
For each function declaration found within a block statement:
@generates
/**
* Analyzes JavaScript code and returns information about function declarations
* that need to be transformed based on strict mode rules.
*
* @param {string} code - The JavaScript code to analyze
* @returns {Array<FunctionInfo>} Array of objects describing functions that need transformation
*
* Each FunctionInfo object contains:
* - name: string - The function name
* - type: string - 'regular', 'async', 'generator', or 'async-generator'
* - line: number - The line number where the function is declared
* - needsTransform: boolean - Whether this function needs transformation
* - reason: string - Why it needs (or doesn't need) transformation
*/
function analyzeBlockScopedFunctions(code) {
// Implementation here
}
module.exports = { analyzeBlockScopedFunctions };Provides JavaScript parsing capabilities to convert source code into an Abstract Syntax Tree (AST).
Provides AST traversal capabilities to walk through and analyze the parsed syntax tree.
Provides utilities for checking and working with AST node types.
Install with Tessl CLI
npx tessl i tessl/npm-babel--plugin-transform-block-scoped-functions