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-3/

Block-Scoped Function Transformer

Build a Babel transformation tool that ensures proper block scoping behavior for function declarations in mixed strict/non-strict mode JavaScript codebases.

Background

When migrating legacy JavaScript codebases to modern standards, function declarations within blocks (like if statements, loops, and switch cases) can behave inconsistently across different environments. Your task is to create a transformation pipeline that handles these functions appropriately based on their context.

Requirements

Your solution should transform JavaScript code to ensure block-scoped functions are properly handled according to these rules:

  1. Strict Mode Context: All function declarations within blocks should be transformed to ensure block-level scoping
  2. Non-Strict Mode Context: Only async and generator functions within blocks should be transformed; regular functions should maintain their original hoisting behavior
  3. Switch Cases: Function declarations in switch case bodies should be transformed following the same rules as block statements
  4. Ordering: Transformed declarations should appear at the appropriate position within their containing blocks

Implementation

Create a Node.js command-line tool that:

  1. Accepts a JavaScript file path as input
  2. Parses and transforms the code according to the requirements above
  3. Outputs the transformed code to stdout
  4. Returns a non-zero exit code if transformation fails

@generates

Test Cases

Your implementation must handle these scenarios:

  • Given a file with a regular function declaration in a block within strict mode code, the function is transformed to a block-scoped let binding @test
  • Given a file with an async function declaration in a block within non-strict mode code, the async function is transformed to a block-scoped let binding @test
  • Given a file with a regular function declaration in a block within non-strict mode code, the function is not transformed @test
  • Given a file with a function declaration in a switch case within strict mode code, the function is transformed appropriately @test

API

/**
 * Transforms JavaScript code to ensure proper block-scoped function behavior
 *
 * @param {string} code - The JavaScript code to transform
 * @returns {string} The transformed code
 * @throws {Error} If the code cannot be parsed or transformed
 */
function transformCode(code);

module.exports = { transformCode };

Dependencies { .dependencies }

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

A Babel plugin that transforms function declarations at the block level to be block scoped

@satisfied-by

@babel/core { .dependency }

Provides the core Babel transformation APIs for parsing and transforming JavaScript

@satisfied-by

Install with Tessl CLI

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

tile.json