Babel plugin to ensure function declarations at the block level are block scoped
89
A Babel plugin that analyzes function declarations and identifies their parent context by traversing the AST structure.
{ function foo() {} }, the plugin reports {name: "foo", context: "block"} @testfunction outer() { function inner() {} }, the plugin reports {name: "inner", context: "function-body"} for the inner function @testexport function bar() {}, the plugin reports {name: "bar", context: "export"} @test{ async function fetchData() {} }, the plugin reports {name: "fetchData", context: "block", async: true} @test{ function* generator() {} }, the plugin reports {name: "generator", context: "block", generator: true} @test@generates
/**
* Creates a Babel plugin that analyzes function declarations and their parent context.
*
* The plugin collects analysis results that can be accessed via the plugin state.
*
* Each result object contains:
* - name: string - the function name
* - context: string - one of "block", "function-body", or "export"
* - async: boolean - whether the function is async (default: false)
* - generator: boolean - whether the function is a generator (default: false)
*
* @returns {Object} Babel plugin object with visitor methods
*/
export default function analyzerPlugin() {
return {
name: "function-declaration-context-analyzer",
visitor: {
// Visitor methods to implement
}
};
}Provides the Babel plugin system and AST types for code transformation and analysis.
Install with Tessl CLI
npx tessl i tessl/npm-babel--plugin-transform-block-scoped-functions