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

Block-Scoped Function Transformer

Build a code transformation tool that converts block-scoped function declarations into properly scoped variable declarations. Your tool should preserve function identity while ensuring correct block-level scoping semantics.

Requirements

Your transformer should:

  1. Transform function declarations within block statements ({ }) into let-bound variable declarations
  2. Preserve the original function name as the variable identifier
  3. Convert named function declarations to anonymous function expressions in the variable assignment
  4. Apply transformations conditionally based on strict mode and function type (async/generator)

Behavior Specification

Basic Transformation

In strict mode, convert block-scoped function declarations to let-bound variables:

// Input (strict mode)
{
  function greet() {
    return "hello";
  }
}

// Expected Output
{
  let greet = function() {
    return "hello";
  };
}

Mode-Specific Behavior

  • Strict Mode: Transform ALL function declarations within blocks
  • Non-Strict Mode: Only transform async and generator functions; leave regular functions untransformed

Special Function Types

The transformer must handle:

  • Regular functions
  • Async functions (async function)
  • Generator functions (function*)
  • Async generator functions

Exclusion Rules

Do NOT transform functions that are:

  • Direct children of function bodies
  • Within export declaration contexts

Implementation

@generates

Implement your solution using the Babel transformation pipeline to process JavaScript AST and transform function declarations appropriately.

API

/**
 * Transform block-scoped function declarations into let-bound variables
 * @param {string} code - JavaScript code to transform
 * @param {object} options - Transformation options
 * @param {boolean} options.strictMode - Whether to treat code as strict mode
 * @returns {string} Transformed JavaScript code
 */
function transformBlockScopedFunctions(code, options) {
  // Implementation here
}

module.exports = { transformBlockScopedFunctions };

Test Cases

  • Transforms a regular function in a block statement in strict mode @test
  • Does not transform a regular function in a block statement in non-strict mode @test
  • Transforms an async function in a block statement regardless of strict mode @test
  • Transforms a generator function in a block statement regardless of strict mode @test
  • Preserves function name when transforming to let-bound variable @test
  • Skips transformation for function declarations directly within function bodies @test

Dependencies { .dependencies }

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

Provides block-scoped function transformation capabilities for JavaScript code.

@satisfied-by

Install with Tessl CLI

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

tile.json