Babel plugin that compiles ES2015 computed properties to ES5-compatible code
88
A utility that transforms modern JavaScript objects with computed method properties into ES5-compatible code for legacy environments.
You need to build a transpiler tool that accepts JavaScript source code containing objects with computed method properties and converts them to ES5-compatible syntax. The tool should handle dynamic method names and produce code that works in older JavaScript environments.
Your solution must:
[methodName]() { ... })@generates
/**
* Transforms JavaScript code with computed method properties to ES5-compatible code
* @param {string} code - The JavaScript source code to transform
* @param {object} options - Optional configuration for the transformation
* @param {boolean} options.loose - Whether to use loose mode (simpler output)
* @returns {string} The transpiled ES5-compatible code
*/
function transpile(code, options = {}) {
// IMPLEMENTATION HERE
}
module.exports = { transpile };var obj = { [name]() { return 42; } };, the transpile function produces valid ES5 code that creates an object with a dynamically named method @testvar obj = { [method1]() { return 1; }, [method2]() { return 2; } };, the transpile function produces ES5 code that correctly defines both methods @testvar obj = { ["get" + suffix]() { return value; } };, the transpile function produces ES5 code that evaluates the expression for the method name @test{ loose: true }, the transpile function produces simpler ES5 code using direct property assignment @testProvides the transformation logic for computed properties including methods.
@satisfied-by
Provides the core Babel transformation engine for parsing and transforming JavaScript code.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/npm-babel-plugin-transform-es2015-computed-propertiesdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10