CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel--plugin-transform-block-scoped-functions

Babel plugin to ensure function declarations at the block level are block scoped

89

1.04x
Overview
Eval results
Files

task.mdevals/scenario-1/

Switch-Scoped Function Analyzer

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.

Requirements

Your tool should:

  1. Parse JavaScript source code that may contain switch statements with function declarations in their case blocks
  2. Identify all function declarations that appear within switch case bodies
  3. Report the following for each detected function:
    • The function name
    • The case value (or "default" if in a default case)
    • Whether the code is in strict mode
    • The function type (regular, async, generator, or async generator)
  4. Output the results as a JSON array

Constraints

  • The tool should handle both strict mode and non-strict mode code
  • The tool should correctly identify async functions, generator functions, and async generator functions
  • The tool should process only switch case bodies, not other types of blocks
  • The output must be valid JSON

Dependencies { .dependencies }

@babel/plugin-transform-block-scoped-functions { .dependency }

A Babel plugin that ensures function declarations at the block level are block scoped.

@babel/core { .dependency }

The core Babel transformation library for parsing and transforming JavaScript code.

@babel/parser { .dependency }

JavaScript parser used by Babel to generate an Abstract Syntax Tree (AST).

Test Cases

Test 1: Basic Switch Case Function { @test }

Input Code:

switch (action) {
  case 'create':
    function handleCreate() {
      return 'created';
    }
    break;
}

Expected Output:

[
  {
    "functionName": "handleCreate",
    "caseValue": "create",
    "strictMode": false,
    "functionType": "regular"
  }
]

Test 2: Multiple Cases with Functions { @test }

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"
  }
]

Test 3: Strict Mode Detection { @test }

Input Code:

'use strict';
switch (mode) {
  case 'strict':
    function strictFunc() {
      return 'strict';
    }
}

Expected Output:

[
  {
    "functionName": "strictFunc",
    "caseValue": "strict",
    "strictMode": true,
    "functionType": "regular"
  }
]

Implementation Files

  • analyzer.js - Main analyzer implementation
  • analyzer.test.js - Test suite with the test cases above

Notes

  • Focus on accurate detection of function declarations within switch cases
  • Ensure proper handling of different function types (regular, async, generator, async generator)
  • The tool should be able to detect strict mode context correctly
  • Consider using AST traversal techniques for robust parsing

Install with Tessl CLI

npx tessl i tessl/npm-babel--plugin-transform-block-scoped-functions

tile.json