or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel-helper-explode-class

Babel helper function that memoizes complex expressions in class definitions to prevent side effects during transformation

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/babel-helper-explode-class@6.24.x

To install, run

npx @tessl/cli install tessl/npm-babel-helper-explode-class@6.24.0

index.mddocs/

Babel Helper Explode Class

Babel Helper Explode Class is a specialized utility that safely transforms ES6+ class definitions during Babel compilation. It memoizes complex expressions in class elements (superclass expressions, decorators, computed properties) to prevent side effects and ensure proper evaluation order during the transformation process.

Package Information

  • Package Name: babel-helper-explode-class
  • Package Type: npm
  • Language: JavaScript (ES6+ with Flow type annotations)
  • Installation: npm install babel-helper-explode-class

Core Imports

import explodeClass from "babel-helper-explode-class";

For CommonJS:

const explodeClass = require("babel-helper-explode-class");

Basic Usage

import explodeClass from "babel-helper-explode-class";
import * as t from "babel-types";
import { parse } from "@babel/parser";
import traverse from "babel-traverse";

// Parse class code into AST
const code = `
class MyClass extends BaseClass {
  @decorator
  computedMethod() {}
}
`;

const ast = parse(code, { 
  sourceType: "module",
  plugins: ["decorators-legacy"] 
});

// Transform using babel-traverse
traverse(ast, {
  Class(path) {
    // Apply explode class transformation
    explodeClass(path);
  }
});

// The class will be transformed to memoize complex expressions:
// var _BaseClass = BaseClass;
// var _decorator = decorator;
// class MyClass extends _BaseClass {
//   @_decorator
//   computedMethod() {}
// }

Architecture

The helper operates within Babel's AST transformation pipeline:

  • Memoization Strategy: Creates temporary variables for complex expressions that might have side effects
  • Decorator Handling: Ensures correct evaluation order by reversing decorator arrays and binding member expressions
  • Computed Properties: Safely handles computed method/property keys by memoizing their expressions
  • Side Effect Prevention: Identifies impure expressions and replaces them with temporary variable references

Capabilities

Class Path Transformation

Processes a Babel class path to memoize complex expressions and prevent side effects during transformation.

/**
 * Main helper function that processes class AST nodes to memoize complex expressions
 * @param classPath - Babel NodePath representing a class definition (untyped in function signature)
 * @throws Will throw if classPath is not a valid class node
 */
export default function explodeClass(classPath): void;

The function performs these transformations on the class:

  1. Superclass Memoization: If the class extends a complex expression, it's memoized
  2. Decorator Processing: Class decorators are processed in reverse order and bound if they're member expressions
  3. Method Processing: For each class method:
    • Computed property keys are memoized if they're complex expressions
    • Method decorators are processed and memoized (Note: Due to a bug in the source code, this currently re-processes class decorators instead of method decorators)
  4. Expression Insertion: All memoized expressions are inserted as assignment statements before the class declaration

Key Behaviors:

  • Only memoizes impure expressions (expressions that might have side effects)
  • Skips memoization for pure expressions like literals and simple identifiers
  • Creates temporary variables using Babel's scope-aware unique identifier generation
  • Maintains original evaluation semantics while making transformations safe

Error Handling:

  • Throws an assertion error if the provided path is not a class node (via classPath.assertClass())
  • Relies on Babel's internal error handling for invalid AST operations

Types

// Flow type definitions used by the function
type NodePath = import("babel-traverse").NodePath;

Note: The internal MemoizedExpression structure is not part of the public API but represents how expressions are stored internally:

// Internal memoized expressions (not exported, for reference only)
interface MemoizedExpression {
  operator: "=";
  left: Identifier;      // Temporary variable created by Babel
  right: Expression;     // Original complex expression
}

Dependencies

This helper relies on several Babel ecosystem packages:

  • babel-traverse: For NodePath type definitions and AST manipulation
  • babel-types: For AST node creation and utilities (t.assignmentExpression, t.expressionStatement)
  • babel-helper-bindify-decorators: For handling decorator binding on member expressions
  • babel-runtime: For Babel's runtime helpers (if needed during transformation)

Integration Notes

This helper is designed to be used within Babel plugins during the transformation phase. It's typically called when processing class nodes to ensure that complex expressions are safely handled before other transformations are applied.

Common Integration Pattern:

// In a Babel plugin
export default function() {
  return {
    visitor: {
      Class(path) {
        // First explode the class to handle complex expressions
        explodeClass(path);
        
        // Then apply other class transformations
        // ...
      }
    }
  };
}