or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

babel-plugin-transform-eval

babel-plugin-transform-eval is a Babel plugin that transforms eval() calls containing string literals by parsing and compiling the string content at transform time. This plugin optimizes static eval expressions during compilation, reducing runtime evaluation overhead while maintaining code functionality.

Package Information

  • Package Name: babel-plugin-transform-eval
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install --save-dev babel-plugin-transform-eval

Core Imports

// CommonJS (for Babel configuration)
const transformEval = require("babel-plugin-transform-eval");

// ES6 Modules (for programmatic usage)
import transformEval from "babel-plugin-transform-eval";

Basic Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["transform-eval"]
}

Via CLI

babel --plugins transform-eval script.js

Via Node API

const babel = require("babel-core");

const result = babel.transform(code, {
  plugins: ["transform-eval"]
});

Architecture

The plugin implements Babel's visitor pattern to traverse the Abstract Syntax Tree (AST) and identify CallExpression nodes that match eval() calls. It uses Babel's evaluation engine to safely determine string literal values at compile time, then parses and replaces those eval calls with their corresponding AST structures.

Key components:

  • AST Visitor: Traverses CallExpression nodes looking for eval calls
  • Evaluation Engine: Uses Babel's confident evaluation to extract string literals
  • Parse Integration: Leverages Babel's parser to convert strings to AST
  • Safe Replacement: Only transforms expressions with confident string evaluation

Capabilities

Plugin Factory Function

The main export creates a Babel plugin that transforms eval() calls with string literals.

/**
 * Creates a Babel plugin that transforms eval() calls containing string literals
 * @param {Object} context - Babel plugin context
 * @param {Function} context.parse - Babel's parse function for parsing JavaScript code
 * @param {Object} context.traverse - Babel's traverse utility with removeProperties method
 * @returns {Object} Babel plugin object with visitor pattern
 */
export default function ({ parse, traverse }) {
  return {
    visitor: {
      CallExpression(path) {
        // Plugin implementation
      }
    }
  };
}

Transformation Examples:

Input:

eval("(() => 'foo')");

Output:

(function () { return 'foo'; });

Note: The plugin replaces the entire eval() call with the parsed AST of the string content, effectively inlining the evaluated code at compile time.

Plugin Object Structure

The plugin returns a standard Babel plugin object implementing the visitor pattern.

/**
 * Babel plugin object structure returned by the factory function
 * @typedef {Object} BabelPlugin
 * @property {Object} visitor - AST node visitors for transformation
 * @property {Function} visitor.CallExpression - Visitor function for CallExpression nodes
 */
const BabelPlugin = {
  visitor: {
    /**
     * Visitor function for CallExpression nodes
     * @param {Object} path - Babel path object for the CallExpression node
     * @returns {Object|undefined} Replacement AST program or undefined for no transformation
     */
    CallExpression(path) {
      // Implementation handles eval() transformation
    }
  }
};

CallExpression Visitor

Handles the transformation logic for eval() calls with string literal arguments.

/**
 * Visitor function that processes CallExpression nodes
 * @param {Object} path - Babel path object for the CallExpression node
 * @returns {Object|undefined} Replacement AST program or undefined for no transformation
 */
CallExpression(path) {
  // Validates eval call with single argument
  // Evaluates argument to get confident string value
  // Parses string as JavaScript and returns program AST
}

Transformation Conditions:

  • Callee must be identifier named "eval"
  • Must have exactly one argument
  • Argument evaluation must be confident (deterministic)
  • Evaluated value must be a string

Safety Features:

  • Returns early if evaluation is not confident
  • Skips transformation for non-string values
  • Only processes single-argument eval calls
  • Uses Babel's removeProperties to clean AST

Error Handling

The plugin gracefully handles various edge cases without throwing errors:

  • Non-eval calls: Ignores other function calls entirely
  • Multiple arguments: Skips eval calls with != 1 argument
  • Non-confident evaluation: Skips when argument value cannot be determined statically
  • Non-string values: Skips when evaluated argument is not a string
  • Parse errors: Babel's parse function handles malformed JavaScript strings

Configuration

Plugin Registration

The plugin can be configured in multiple ways:

Package name registration:

{
  "plugins": ["transform-eval"]
}

Full path registration:

{
  "plugins": ["babel-plugin-transform-eval"]
}

Programmatic registration:

const babel = require("babel-core");
const transformEval = require("babel-plugin-transform-eval");

babel.transform(code, {
  plugins: [transformEval]
});

Dependencies

Runtime Dependencies:

  • babel-runtime@^6.22.0 - Babel runtime support

Development Dependencies:

  • babel-helper-plugin-test-runner@^6.22.0 - Test runner helper

The plugin is designed for Babel 6.x and integrates with the broader Babel ecosystem for JavaScript transformation workflows.