Babel helper function that memoizes complex expressions in class definitions to prevent side effects during transformation
npx @tessl/cli install tessl/npm-babel-helper-explode-class@6.24.0Babel 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.
npm install babel-helper-explode-classimport explodeClass from "babel-helper-explode-class";For CommonJS:
const explodeClass = require("babel-helper-explode-class");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() {}
// }The helper operates within Babel's AST transformation pipeline:
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:
Key Behaviors:
Error Handling:
classPath.assertClass())// 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
}This helper relies on several Babel ecosystem packages:
t.assignmentExpression, t.expressionStatement)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
// ...
}
}
};
}