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

Block-Scoped Function Transpiler

Build a JavaScript code transpiler that transforms function declarations within block statements to ensure proper block-level scoping.

Requirements

Your transpiler should:

  1. Transform Block-Level Function Declarations: Convert function declarations inside block statements (curly braces) into let-bound variable declarations with function expressions. Preserve the original function name in the variable binding.

  2. Handle Strict Mode Contexts: Apply transformation to all function declarations when code is in strict mode. In non-strict mode, only transform async functions and generator functions.

  3. Process Switch Case Statements: Transform function declarations within switch case bodies using the same rules as block statements.

  4. Skip Function Body Contexts: Do not transform function declarations that are direct children of function bodies

Implementation

@generates

Test Cases

  • A function declaration inside a block in strict mode transforms to a let-bound variable declaration @test
  • An async function inside a block in non-strict mode transforms to a let-bound variable declaration @test
  • A generator function inside a block in non-strict mode transforms to a let-bound variable declaration @test
  • A regular function inside a block in non-strict mode remains unchanged @test
  • A function declaration in a switch case transforms correctly @test
  • A function declaration directly inside a function body is not transformed @test

API

/**
 * Transforms JavaScript code to ensure block-scoped function declarations.
 *
 * @param {string} code - The JavaScript code to transform
 * @param {Object} options - Transformation options
 * @param {boolean} options.strictMode - Whether to treat the code as strict mode
 * @returns {string} The transformed JavaScript code
 */
function transform(code, options) {
  // Implementation
}

module.exports = { transform };

Dependencies { .dependencies }

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

Provides block-scoped function transformation capabilities.

@satisfied-by

Install with Tessl CLI

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

tile.json