Helper functions for defining object property maps in Babel transformations
npx @tessl/cli install tessl/npm-babel-helper-define-map@6.26.0babel-helper-define-map provides helper functions for defining and manipulating object property maps in Babel transformations. It includes utilities for converting class methods and properties into object descriptors, handling computed properties, decorators, and inheritance relationships.
npm install babel-helper-define-mapimport * as defineMap from "babel-helper-define-map";For CommonJS:
const defineMap = require("babel-helper-define-map");import * as defineMap from "babel-helper-define-map";
import * as t from "babel-types";
// Create a mutator map for collecting properties
const mutatorMap = {};
// Push properties to the map (typically in a visitor)
const map = defineMap.push(mutatorMap, node, "value", file, scope);
// Convert to property descriptors for Object.defineProperty
const propertyDescriptors = defineMap.toDefineObject(mutatorMap);
// Or convert to class-style object
const classObject = defineMap.toClassObject(mutatorMap);Core function for accumulating class/object properties into descriptor maps with conflict detection and inheritance handling.
/**
* Pushes a property node to a mutator map with conflict detection and inheritance handling
* @param {Object} mutatorMap - The target mutator map to accumulate properties
* @param {Object} node - AST node representing the property/method (ClassMethod, ObjectMethod, ClassProperty, etc.)
* @param {string} kind - Property kind ("value", "get", "set", "initializer")
* @param {Object} file - Babel file object for error reporting with source location
* @param {Object} [scope] - Optional scope for function name inference
* @returns {Object} The property map entry for the given node
*/
function push(mutatorMap, node, kind, file, scope?);Usage Example:
import * as defineMap from "babel-helper-define-map";
// In a Babel visitor
ClassMethod(path, state) {
const mutatorMap = {};
const map = defineMap.push(
mutatorMap,
path.node,
"value",
state.file,
path.scope
);
// map contains the processed property descriptor
}Checks if any property in the mutator map has computed keys, which affects how the final object should be generated.
/**
* Checks if any property in the mutator map has computed keys
* @param {Object} mutatorMap - The mutator map to check
* @returns {boolean} True if any property has computed keys
*/
function hasComputed(mutatorMap);Converts a mutator map to a class-style object expression with property descriptors.
/**
* Converts a mutator map to a class-style object expression
* @param {Object} mutatorMap - The mutator map to convert
* @returns {Object} Object expression representing the class properties
*/
function toClassObject(mutatorMap);Converts a class object to computed object format with explicit key properties, used when dealing with computed property keys.
/**
* Converts a class object to computed object format with explicit key properties
* @param {Object} obj - Class object with properties to convert
* @returns {Object} Array expression with computed key properties
*/
function toComputedObjectFromClass(obj);Converts mutator map to Object.defineProperty-compatible format with property attributes (writable, configurable, enumerable).
/**
* Converts mutator map to Object.defineProperty-compatible format
* @param {Object} mutatorMap - The mutator map to convert
* @returns {Object} Object expression with writable, configurable, enumerable attributes
*/
function toDefineObject(mutatorMap);Usage Example:
import * as defineMap from "babel-helper-define-map";
// After accumulating properties with push()
const propertyDescriptors = defineMap.toDefineObject(mutatorMap);
// Generate Object.defineProperty call
const definePropertiesCall = t.callExpression(
t.memberExpression(t.identifier("Object"), t.identifier("defineProperties")),
[targetObject, propertyDescriptors]
);babel-helper-define-map follows a multi-step transformation pattern:
push() to collect class/object properties into a mutator maphasComputed() to determine if computed property handling is neededtoClassObject(), toDefineObject(), or toComputedObjectFromClass() to create final descriptorsThe package handles complex scenarios including:
_inherits arrayThe package throws the following errors:
buildCodeFrameError for proper source location reporting// Mutator map structure (internal representation)
interface MutatorMap {
[key: string]: {
_inherits: Array<Object>; // AST nodes this property inherits from
_key: Object; // The property key AST node
_computed?: boolean; // Whether the key is computed
decorators?: Object; // Array expression of decorators
value?: Object; // Property value (for data properties)
get?: Object; // Getter function (for accessor properties)
set?: Object; // Setter function (for accessor properties)
initializer?: Object; // Initializer function (for class fields)
writable?: Object; // Boolean literal for property writability
configurable?: Object; // Boolean literal for property configurability
enumerable?: Object; // Boolean literal for property enumerability
};
}