Babel plugin to ensure function declarations at the block level are block scoped
89
Create a code analysis tool that processes JavaScript code containing switch statements and reports on function declarations within switch cases. The tool should identify function declarations in switch case bodies and provide information about their scoping behavior.
Your tool should:
A Babel plugin that ensures function declarations at the block level are block scoped.
The core Babel transformation library for parsing and transforming JavaScript code.
JavaScript parser used by Babel to generate an Abstract Syntax Tree (AST).
Input Code:
switch (action) {
case 'create':
function handleCreate() {
return 'created';
}
break;
}Expected Output:
[
{
"functionName": "handleCreate",
"caseValue": "create",
"strictMode": false,
"functionType": "regular"
}
]Input Code:
switch (type) {
case 1:
function processOne() { return 1; }
break;
case 2:
async function processTwo() { return 2; }
break;
default:
function* processDefault() { yield 0; }
}Expected Output:
[
{
"functionName": "processOne",
"caseValue": "1",
"strictMode": false,
"functionType": "regular"
},
{
"functionName": "processTwo",
"caseValue": "2",
"strictMode": false,
"functionType": "async"
},
{
"functionName": "processDefault",
"caseValue": "default",
"strictMode": false,
"functionType": "generator"
}
]Input Code:
'use strict';
switch (mode) {
case 'strict':
function strictFunc() {
return 'strict';
}
}Expected Output:
[
{
"functionName": "strictFunc",
"caseValue": "strict",
"strictMode": true,
"functionType": "regular"
}
]analyzer.js - Main analyzer implementationanalyzer.test.js - Test suite with the test cases aboveInstall with Tessl CLI
npx tessl i tessl/npm-babel--plugin-transform-block-scoped-functions