CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel-plugin-transform-eval

Babel plugin that transforms eval() calls containing string literals by parsing and compiling the string content at transform time

Overall
score

98%

Overview
Eval results
Files

task.mdevals/scenario-6/

Dead Code Eliminator Plugin

Build a Babel plugin that analyzes JavaScript code and removes unreachable dead code based on compile-time evaluation of conditional expressions.

Overview

Your task is to implement a Babel plugin that identifies and eliminates dead code branches in if statements where the condition can be statically evaluated to a constant boolean value at compile time. The plugin should evaluate simple expressions and remove unreachable code paths.

Requirements

Core Functionality

Your plugin should handle the following scenarios:

  1. Literal Conditions: Evaluate if statements with boolean literal conditions
  2. Binary Expressions: Evaluate comparison operations (e.g., 1 > 2, 5 === 5) at compile time
  3. Arithmetic in Conditions: Evaluate arithmetic expressions in conditional contexts (e.g., if (2 + 2 === 4))
  4. Remove Alternate Branch: When condition is always true, remove the else branch
  5. Remove Consequent Branch: When condition is always false, remove the if branch and keep only the else branch content

Behavior Specifications

  • When a condition evaluates to true, replace the entire if statement with just the consequent block's statements
  • When a condition evaluates to false and there is no else branch, remove the entire if statement
  • When a condition evaluates to false and there is an else branch, replace the entire if statement with the alternate block's statements
  • Do not attempt to evaluate conditions that reference variables or functions (these are dynamic)
  • Preserve all other code unchanged

Test Cases

The following test cases should pass:

Test 1: Remove dead branch with true condition

  • Input code with if (true) should keep only the consequent statements @test
// Input
if (true) {
  console.log("always runs");
} else {
  console.log("never runs");
}

// Expected output
console.log("always runs");

Test 2: Remove dead branch with false condition

  • Input code with if (false) should remove the if statement entirely or keep only else branch @test
// Input
if (false) {
  console.log("never runs");
} else {
  console.log("always runs");
}

// Expected output
console.log("always runs");

Test 3: Evaluate binary comparison expressions

  • Input code with compile-time evaluable comparisons should eliminate dead branches @test
// Input
if (5 > 10) {
  console.log("dead code");
} else {
  console.log("this remains");
}

// Expected output
console.log("this remains");

Test 4: Preserve dynamic conditions

  • Input code with variable conditions should remain unchanged @test
// Input
const x = 5;
if (x > 10) {
  console.log("maybe");
}

// Expected output (unchanged)
const x = 5;
if (x > 10) {
  console.log("maybe");
}

Implementation

@generates

API

/**
 * Dead Code Eliminator Plugin
 *
 * A Babel plugin that removes unreachable code based on static analysis
 * of conditional expressions.
 *
 * @returns {Object} Babel plugin object with visitor methods
 */
module.exports = function() {
  return {
    visitor: {
      // Your implementation here
    }
  };
};

Dependencies { .dependencies }

@babel/core { .dependency }

Provides the core Babel transformation APIs.

@babel/traverse { .dependency }

Provides AST traversal utilities and the visitor pattern.

@babel/types { .dependency }

Provides utilities for AST node type checking and manipulation.

Install with Tessl CLI

npx tessl i tessl/npm-babel-plugin-transform-eval

tile.json