This Babel plugin transforms ES5 object initializer property mutators (getter and setter shorthand syntax) into explicit Object.defineProperties calls. It enables compatibility with environments that don't support ES5 property mutator syntax by converting getter/setter shorthand to property descriptors.
npm install --save-dev babel-plugin-transform-es5-property-mutators// Direct plugin import (for programmatic usage)
const transformES5PropertyMutators = require("babel-plugin-transform-es5-property-mutators");For typical Babel configuration, the plugin is referenced by name string in configuration files.
{
"plugins": ["transform-es5-property-mutators"]
}babel --plugins transform-es5-property-mutators script.jsrequire("babel-core").transform("code", {
plugins: ["transform-es5-property-mutators"]
});This plugin uses Babel's visitor pattern to transform AST nodes:
babel-helper-define-map for creating property descriptor mappingsObject.defineProperties callsThe transformation pipeline:
kind === "get" or kind === "set")babel-helper-define-map.push() to collect property descriptorsbabel-helper-define-map.toDefineObject() to create property descriptor objectObject.defineProperties(target, descriptors) callInput (ES5 property mutator syntax):
var obj = {
get foo() {
return "bar";
},
set foo(value) {
this._foo = value;
}
};Output (Object.defineProperties):
var obj = Object.defineProperties({}, {
foo: {
get: function () {
return "bar";
},
set: function (value) {
this._foo = value;
},
configurable: true,
enumerable: true
}
});The main export is a factory function that creates a Babel plugin when given the Babel API.
/**
* Creates a Babel plugin for transforming ES5 property mutators
* @param {Object} babel - Babel API object with destructured types
* @param {BabelTypes} babel.types - Babel types utilities (destructured as 't')
* @returns {BabelPlugin} Plugin configuration object with visitor pattern
*/
export default function ({ types: t }) {
return {
visitor: {
ObjectExpression(path, file) {
// Implementation processes getter/setter properties
// and replaces with Object.defineProperties calls
}
}
};
}The plugin returns a standard Babel plugin configuration object.
interface BabelPlugin {
/** AST visitor methods for transformation */
visitor: BabelVisitor;
}
interface BabelVisitor {
/**
* Visitor method for ObjectExpression AST nodes
* Transforms objects containing getter/setter properties to Object.defineProperties calls
* @param {Object} path - Babel AST path object for ObjectExpression node
* @param {Object} file - Babel file object for error reporting and context
*/
ObjectExpression(path: BabelPath, file: BabelFile): void;
}The plugin processes ObjectExpression nodes and:
kind === "get" or kind === "set")babel-helper-define-map to create property descriptor mappingsObject.defineProperties(target, descriptors)All transformed properties receive these descriptors:
configurable: true - Property can be deleted or modifiedenumerable: true - Property appears in object enumerationget: function() { ... } - Getter function (when present)set: function(value) { ... } - Setter function (when present)[expr]: value) - only literal identifiers are processedObject.defineProperties supportcomputed: true are explicitly filtered out and not transformedThe plugin relies on:
babel-helper-define-map@^6.24.1 - Core dependency providing:
push(mutatorMap, prop, null, file) - Collects property descriptorstoDefineObject(mutatorMap) - Converts collected descriptors to Object.defineProperties formatbabel-runtime@^6.22.0 - Runtime helpers for transpiled codeThe plugin uses specific functions from babel-helper-define-map:
// From babel-helper-define-map
function push(mutatorMap: Object, node: Object, kind: string, file: Object): Object;
function toDefineObject(mutatorMap: Object): Object;The toDefineObject function automatically adds these property descriptors:
configurable: true - Property can be deleted or reconfiguredenumerable: true - Property appears in enumerationwritable: true - Added for value properties (not getters/setters)// Babel API interfaces used by the plugin
interface BabelAPI {
types: BabelTypes;
}
interface BabelPath {
node: ObjectExpression;
replaceWith(newNode: CallExpression): void;
}
interface BabelFile {
buildCodeFrameError(node: ASTNode, message: string): Error;
}
interface ObjectExpression {
properties: Property[];
}
interface Property {
kind: "get" | "set" | "init" | "method";
computed: boolean;
key: Identifier | Literal;
value?: Expression;
}