CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-swc--core-darwin-arm64

Platform-specific native binary for SWC TypeScript/JavaScript compiler on macOS ARM64 architecture

89

1.15x
Overview
Eval results
Files

task.mdevals/scenario-6/

JavaScript Code Transformer

Overview

Build a code transformation tool that converts modern JavaScript code with class features to an older JavaScript version compatible with legacy environments.

Requirements

Your tool should transform JavaScript source code to make it compatible with older JavaScript environments. Specifically, it should handle:

  1. Transform modern class syntax including public class properties and private fields
  2. Convert the code to ES5-compatible syntax
  3. Preserve the original functionality of the code
  4. Return the transformed code as a string

Input

Your tool will receive JavaScript code as a string containing:

  • Classes with public properties (field declarations without assignment)
  • Classes with private fields (using the # syntax)
  • Standard class methods
  • Class constructors

Output

Return the transformed JavaScript code as a string that is compatible with older JavaScript environments.

Implementation

Create a file transformer.js that exports a function transformCode(sourceCode):

  • Input: sourceCode (string) - Modern JavaScript code with class features
  • Output: Transformed JavaScript code (string) compatible with older environments

Dependencies { .dependencies }

@swc/core { .dependency }

A fast TypeScript/JavaScript compiler for code transformation and transpilation.

Test Cases

Test 1: Transform public class properties @test

Input Code:

class Person {
  name = "John";
  age = 30;

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

Expected Behavior: The transformed code should move the property initializations into the constructor and be compatible with ES5.

Test File: transformer.test.js

const { transformCode } = require('./transformer');

const inputCode = `
class Person {
  name = "John";
  age = 30;

  greet() {
    return \`Hello, I'm \${this.name}\`;
  }
}
`;

const result = transformCode(inputCode);

// Verify transformation happened
console.assert(!result.includes('name ='), 'Public properties should be transformed');
console.assert(result.includes('function'), 'Should contain ES5 function syntax');

// Verify functionality is preserved
eval(result);
const person = new Person();
console.assert(person.name === "John", 'Property initialization should work');
console.assert(person.greet() === "Hello, I'm John", 'Method should work correctly');
console.log('Test 1 passed');

Test 2: Transform private class fields @test

Input Code:

class Counter {
  #count = 0;

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

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

Expected Behavior: The transformed code should replace private fields with an ES5-compatible mechanism (e.g., WeakMap or other private implementation).

Test File: transformer.test.js

const { transformCode } = require('./transformer');

const inputCode = `
class Counter {
  #count = 0;

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

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

const result = transformCode(inputCode);

// Verify transformation happened
console.assert(!result.includes('#count'), 'Private fields should be transformed');

// Verify functionality is preserved
eval(result);
const counter = new Counter();
counter.increment();
counter.increment();
console.assert(counter.getCount() === 2, 'Private field should maintain state');
console.log('Test 2 passed');

Notes

  • Focus on correctness: the transformed code must preserve the original behavior
  • Assume all input code is syntactically valid modern JavaScript
  • You only need to handle the class features mentioned above
  • The output should be executable JavaScript code

Install with Tessl CLI

npx tessl i tessl/npm-swc--core-darwin-arm64

tile.json