Babel plugin that compiles ES2015 computed properties to ES5-compatible code
88
Build a Babel plugin that transforms objects with computed property keys into ES5-compatible code. The plugin must optimize AST structure for large objects by using chunking.
Transform objects with computed properties like { [expr]: value } into ES5 code that uses a helper function. The critical requirement is handling large objects efficiently:
_defineProperty(targetObject, key, value)Small object (3 computed properties)
// Input
const obj = {
[key1]: "value1",
[key2]: "value2",
[key3]: "value3"
};
// Output: Single nested call chain
const obj = _defineProperty(
_defineProperty(
_defineProperty({}, key1, "value1"),
key2, "value2"
),
key3, "value3"
);Large object (22 computed properties)
// Input
const obj = {
[k1]: "v1", [k2]: "v2", [k3]: "v3", [k4]: "v4", [k5]: "v5",
[k6]: "v6", [k7]: "v7", [k8]: "v8", [k9]: "v9", [k10]: "v10",
[k11]: "v11", [k12]: "v12", [k13]: "v13", [k14]: "v14", [k15]: "v15",
[k16]: "v16", [k17]: "v17", [k18]: "v18", [k19]: "v19", [k20]: "v20",
[k21]: "v21", [k22]: "v22"
};
// Output: Three chunks (10 + 10 + 2)
// Uses temporary variable _obj to hold object between chunks
// Chunk 1 processes properties 1-10
// Chunk 2 processes properties 11-20 on _obj
// Chunk 3 processes properties 21-22 on _obj@generates
/**
* Babel plugin that transforms computed properties with AST optimization.
*
* @returns {Object} Babel plugin object with visitor methods
*/
export default function() {
return {
name: "transform-computed-properties-chunked",
visitor: {
// Plugin visitor implementation
}
};
}Provides the core Babel transformation APIs including types, template builders, and visitor patterns.
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