or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

babel-plugin-transform-class-constructor-call

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

Package Information

  • Package Name: babel-plugin-transform-class-constructor-call
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install --save-dev babel-plugin-transform-class-constructor-call

Core Imports

This 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"]
});

Basic Usage

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 constructor

Architecture

The plugin follows Babel's standard plugin architecture:

  • Plugin Factory: Main function that receives Babel utilities and returns plugin configuration
  • Visitor Pattern: Uses Babel's visitor pattern to traverse and transform AST nodes
  • Code Generation: Uses babel-template for generating transformation code
  • Dependency: Inherits from babel-plugin-syntax-class-constructor-call for parsing support

Capabilities

Plugin Factory Function

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

AST Transformation

The plugin transforms classes containing call constructor() methods by:

  1. Detection: Identifies classes with constructorCall kind methods
  2. Wrapper Generation: Creates a wrapper function that detects invocation context
  3. Dual Behavior: Routes to constructor or function call based on this instanceof check
  4. Prototype Chain: Maintains proper prototype relationships with __proto__

The transformation enables:

  • new Class() - Normal constructor invocation returning instance
  • Class() - Function call using the call constructor() method

Configuration Options

Via .babelrc:

{
  "plugins": ["transform-class-constructor-call"]
}

Via CLI:

babel --plugins transform-class-constructor-call script.js

Via Node API:

require("babel-core").transform("code", {
  plugins: ["transform-class-constructor-call"]
});

Types

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

Dependencies

The plugin requires these Babel ecosystem packages:

  • babel-template: For generating AST templates
  • babel-plugin-syntax-class-constructor-call: For parsing call constructor() syntax
  • babel-runtime: For runtime support of transformed code

Error Handling

The plugin handles various edge cases:

  • Missing Constructor Call: Classes without call constructor() are left unchanged
  • Export Handling: Properly handles classes that are default exports
  • Scope Management: Generates unique identifiers to avoid naming conflicts
  • Already Visited: Uses a symbol to prevent duplicate processing of the same class

Limitations

  • Deprecated Status: The ECMAScript proposal was withdrawn
  • Babel 6 Only: This version is specifically for Babel 6.x
  • Syntax Dependency: Requires babel-plugin-syntax-class-constructor-call for parsing
  • Reflection Requirement: Generated code uses Reflect.construct which requires modern environments or polyfills