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

Class Feature Transformer

Build a tool that transforms modern JavaScript class code containing advanced features into backwards-compatible JavaScript. The tool should handle private fields, private methods, and super calls, making them work in environments that don't natively support these features.

Requirements

Your tool should accept JavaScript source code as input and return transformed code that:

  1. Converts private fields (fields starting with #) into a backwards-compatible implementation
  2. Transforms private methods (methods starting with #) into an accessible form for older environments
  3. Properly transforms super calls in constructors and methods

The transformation should preserve the semantic behavior of the original code while making it executable in environments that lack native support for these features.

Input and Output

  • Input: A string containing JavaScript class code with modern features
  • Output: A string containing the transformed JavaScript code

Test Cases

Test 1: Transform Private Fields { .test-case }

Given a class with private fields:

class Counter {
  #count = 0;

  increment() {
    this.#count++;
  }

  getCount() {
    return this.#count;
  }
}

The transformed code should implement private field functionality that works in older environments. The behavior should be identical: private fields remain inaccessible from outside the class.

@test

Test 2: Transform Private Methods { .test-case }

Given a class with private methods:

class Validator {
  #validate(value) {
    return value > 0;
  }

  check(value) {
    return this.#validate(value);
  }
}

The transformed code should maintain the private nature of the method while ensuring it works in environments without native private method support.

@test

Test 3: Transform Class with Super Calls { .test-case }

Given a class hierarchy with super calls:

class Parent {
  constructor(name) {
    this.name = name;
  }

  greet() {
    return `Hello, ${this.name}`;
  }
}

class Child extends Parent {
  constructor(name, age) {
    super(name);
    this.age = age;
  }

  greet() {
    return super.greet() + ` (${this.age} years old)`;
  }
}

The transformed code should properly handle both constructor super calls and method super calls, ensuring the inheritance chain works correctly.

@test

Implementation

@generates

API

/**
 * Transforms JavaScript code containing advanced class features
 * into backwards-compatible JavaScript.
 *
 * @param {string} code - The source code to transform
 * @param {Object} options - Transformation options (optional)
 * @returns {string} The transformed code
 */
function transformClassFeatures(code, options = {});

module.exports = { transformClassFeatures };

Dependencies { .dependencies }

@babel/core { .dependency }

Provides the core Babel transformation API for parsing and transforming JavaScript code.

@babel/plugin-proposal-class-properties { .dependency }

Transforms class properties including private fields and private methods.

@babel/plugin-transform-classes { .dependency }

Handles class transformation including super calls and inheritance.

Install with Tessl CLI

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

tile.json