Platform-specific native binary for SWC TypeScript/JavaScript compiler on macOS ARM64 architecture
89
Build a code transformation tool that converts modern JavaScript code with class features to an older JavaScript version compatible with legacy environments.
Your tool should transform JavaScript source code to make it compatible with older JavaScript environments. Specifically, it should handle:
Your tool will receive JavaScript code as a string containing:
# syntax)Return the transformed JavaScript code as a string that is compatible with older JavaScript environments.
Create a file transformer.js that exports a function transformCode(sourceCode):
sourceCode (string) - Modern JavaScript code with class featuresA fast TypeScript/JavaScript compiler for code transformation and transpilation.
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');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');Install with Tessl CLI
npx tessl i tessl/npm-swc--core-darwin-arm64docs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10