A deprecated Babel plugin that transforms ES2015 classes to support constructor call syntax for dual-mode class invocation
npx @tessl/cli install tessl/npm-babel-plugin-transform-class-constructor-call@6.24.0A deprecated Babel plugin that transforms ES2015 classes to support constructor call syntax, enabling classes to be invoked both as constructors (with new) and as regular functions. Originally designed for a withdrawn ECMAScript proposal.
Status: Deprecated - The ECMAScript proposal was withdrawn and can be solved with decorators.
npm install --save-dev babel-plugin-transform-class-constructor-callThis is a Babel plugin that is configured in Babel's configuration, not imported directly:
// .babelrc configuration
{
"plugins": ["transform-class-constructor-call"]
}For programmatic use:
const babel = require("babel-core");
babel.transform(code, {
plugins: ["transform-class-constructor-call"]
});The plugin transforms classes with call constructor() methods to enable dual invocation patterns:
Input Code:
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
call constructor(x, y) {
return new Point(x, y);
}
}Output Code (after transformation):
let _Point = class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
};
var _PointCall = function (x, y) {
return new _Point(x, y);
};
var Point = function (...args) {
if (this instanceof Point) {
return Reflect.construct(_Point, args);
} else {
return _PointCall.apply(this, args);
}
};
Point.__proto__ = _Point;Usage of Transformed Class:
let p1 = new Point(1, 2); // Constructor call - returns instance
let p2 = Point(3, 4); // Function call - uses call constructorThe plugin follows Babel's standard plugin architecture:
babel-template for generating transformation codebabel-plugin-syntax-class-constructor-call for parsing supportThe main plugin export that creates a Babel plugin instance.
/**
* Creates a Babel plugin for transforming class constructor calls
* @param {Object} params - Babel plugin parameters
* @param {Object} params.types - Babel types utility (t)
* @returns {Object} Babel plugin configuration object
*/
function plugin({ types: t }): BabelPlugin;
interface BabelPlugin {
inherits: any; // babel-plugin-syntax-class-constructor-call
visitor: {
Class(path: any): void;
};
}The plugin transforms classes containing call constructor() methods by:
constructorCall kind methodsthis instanceof check__proto__The transformation enables:
new Class() - Normal constructor invocation returning instanceClass() - Function call using the call constructor() methodVia .babelrc:
{
"plugins": ["transform-class-constructor-call"]
}Via CLI:
babel --plugins transform-class-constructor-call script.jsVia Node API:
require("babel-core").transform("code", {
plugins: ["transform-class-constructor-call"]
});// Babel Plugin Configuration
interface BabelPlugin {
/** Inherits syntax parsing from babel-plugin-syntax-class-constructor-call */
inherits: any;
/** AST visitor methods */
visitor: {
/** Processes Class AST nodes */
Class(path: BabelPath): void;
};
}
// Babel AST Path (simplified)
interface BabelPath {
node: any;
scope: any;
parentPath: BabelPath;
get(key: string): BabelPath | BabelPath[];
replaceWithMultiple(nodes: any[]): void;
insertAfter(node: any): void;
remove(): void;
isExportDefaultDeclaration(): boolean;
}The plugin requires these Babel ecosystem packages:
call constructor() syntaxThe plugin handles various edge cases:
call constructor() are left unchangedbabel-plugin-syntax-class-constructor-call for parsingReflect.construct which requires modern environments or polyfills