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

Code Block Scoping Analyzer

Build a tool that analyzes JavaScript code to determine whether function declarations within block statements need to be transformed based on strict mode context.

Problem Description

In JavaScript, function declarations within block statements (like if blocks or standalone blocks) have different scoping rules depending on whether the code runs in strict mode. Your task is to create an analyzer that:

  1. Parses JavaScript code and identifies function declarations within block statements
  2. Determines whether each block is in a strict mode context
  3. Decides which function declarations should be flagged for transformation based on strict mode rules and function type

Requirements

Your analyzer should handle the following cases:

Strict Mode Detection

  • Detect explicit "use strict" directives at the file or function level
  • Detect implicit strict mode (e.g., ES6 modules are automatically strict)
  • Properly propagate strict mode context through nested scopes

Function Declaration Analysis

For each function declaration found within a block statement:

  • In strict mode: all function declarations in blocks should be flagged for transformation
  • In non-strict mode: only async functions, generator functions, and async generator functions should be flagged for transformation
  • Regular function declarations in non-strict mode should NOT be flagged

Special Cases to Handle

  • Skip function declarations that are direct children of another function body (these are not block-scoped)
  • Skip function declarations within export declarations
  • Handle nested blocks correctly (inner blocks inherit strict mode from outer contexts)

Test Cases

  • Given a block with a regular function in strict mode, it returns that the function needs transformation @test
  • Given a block with a regular function in non-strict mode, it returns that the function does NOT need transformation @test
  • Given a block with an async function in non-strict mode, it returns that the function needs transformation @test
  • Given a block with a generator function in non-strict mode, it returns that the function needs transformation @test
  • Given a function body with a function declaration, it returns that the function does NOT need transformation @test

Implementation

@generates

API

/**
 * Analyzes JavaScript code and returns information about function declarations
 * that need to be transformed based on strict mode rules.
 *
 * @param {string} code - The JavaScript code to analyze
 * @returns {Array<FunctionInfo>} Array of objects describing functions that need transformation
 *
 * Each FunctionInfo object contains:
 * - name: string - The function name
 * - type: string - 'regular', 'async', 'generator', or 'async-generator'
 * - line: number - The line number where the function is declared
 * - needsTransform: boolean - Whether this function needs transformation
 * - reason: string - Why it needs (or doesn't need) transformation
 */
function analyzeBlockScopedFunctions(code) {
  // Implementation here
}

module.exports = { analyzeBlockScopedFunctions };

Dependencies { .dependencies }

@babel/parser { .dependency }

Provides JavaScript parsing capabilities to convert source code into an Abstract Syntax Tree (AST).

@babel/traverse { .dependency }

Provides AST traversal capabilities to walk through and analyze the parsed syntax tree.

@babel/types { .dependency }

Provides utilities for checking and working with AST node types.

Install with Tessl CLI

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

tile.json