or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel-helper-define-map

Helper functions for defining object property maps in Babel transformations

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/babel-helper-define-map@6.26.x

To install, run

npx @tessl/cli install tessl/npm-babel-helper-define-map@6.26.0

index.mddocs/

babel-helper-define-map

babel-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.

Package Information

  • Package Name: babel-helper-define-map
  • Package Type: npm
  • Language: JavaScript (ES6 modules)
  • Installation: npm install babel-helper-define-map

Core Imports

import * as defineMap from "babel-helper-define-map";

For CommonJS:

const defineMap = require("babel-helper-define-map");

Basic Usage

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);

Capabilities

Property Map Management

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
}

Computed Property Detection

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);

Class Object Generation

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);

Computed Object Transformation

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);

Property Descriptor Generation

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]
);

Architecture

babel-helper-define-map follows a multi-step transformation pattern:

  1. Accumulation Phase: Use push() to collect class/object properties into a mutator map
  2. Analysis Phase: Use hasComputed() to determine if computed property handling is needed
  3. Generation Phase: Use toClassObject(), toDefineObject(), or toComputedObjectFromClass() to create final descriptors

The package handles complex scenarios including:

  • Computed Properties: Dynamic property keys that need special handling
  • Decorators: Property decorators are accumulated and preserved
  • Inheritance: Properties from parent classes are tracked via _inherits array
  • Conflict Detection: Throws errors when duplicate properties are encountered
  • Function Naming: Automatic function name inference for anonymous functions

Error Handling

The package throws the following errors:

  • "Key conflict with sibling node": Thrown when duplicate properties are encountered in the same mutator map
  • Uses Babel's buildCodeFrameError for proper source location reporting

Types

// 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
  };
}