babel-plugin-transform-class-constructor-call (deprecated)

raw JSON →
6.24.1 verified Sat Apr 25 auth: no javascript deprecated

This deprecated Babel plugin (v6.24.1, last release 2017) transforms the withdrawn TC39 proposal for call constructor syntax in ES2015 classes. It allows a class to have a 'call constructor' that is invoked when the class is called without 'new', enabling patterns like Date() returning a string while new Date() returns an instance. The proposal was withdrawn and is superseded by decorators. This plugin is part of Babel v6 era and is not compatible with Babel v7+. Developers should not use it in new projects; instead, use modern patterns like static methods or decorators.

error Error: Plugin transform-class-constructor-call is deprecated
cause The plugin is deprecated and Babel may warn about it.
fix
Remove the plugin from configuration and refactor code.
error Error: Cannot find module 'babel-core'
cause Using npm packages for Babel v7 (e.g., @babel/core) instead of v6 (babel-core).
fix
Ensure 'babel-core' (v6) is installed: npm install --save-dev babel-core
error SyntaxError: Unexpected token
cause Attempting to use the call constructor syntax without the plugin enabled or with a different Babel version.
fix
Add this plugin to your Babel v6 configuration. For Babel v7, this syntax cannot be used.
deprecated This plugin is deprecated. The call constructor proposal was withdrawn. Use decorators or static methods instead.
fix Remove the plugin and refactor code to use static factory methods or decorators.
breaking Incompatible with Babel v7. The plugin is designed for Babel v6 and does not work with @babel/core v7+.
fix If using Babel v7, do not use this plugin. There is no direct replacement.
gotcha The plugin only works with Babel v6's 'babel-core' package. Using '@babel/core' (v7) will fail.
fix Use 'babel-core' version 6.x. Ensure no @babel scoped packages are installed.
gotcha Syntax is non-standard and will not be natively supported. Code relying on this plugin will not run in modern environments without transpilation.
fix Refactor to use a static method: static create() { ... } or use decorators.
npm install babel-plugin-transform-class-constructor-call
yarn add babel-plugin-transform-class-constructor-call
pnpm add babel-plugin-transform-class-constructor-call

Demonstrates usage of the deprecated call constructor plugin to allow a class to be callable without 'new'.

// Install: npm install --save-dev babel-plugin-transform-class-constructor-call
// .babelrc
{
  "plugins": ["transform-class-constructor-call"]
}

// Example usage in code.js
class Date {
  constructor() {
    this.date = new Date();
  }
  call constructor() {
    let date = new Date();
    return date.toString();
  }
}

let now = new Date(); // instance
let nowStr = Date(); // string
console.log(now instanceof Date); // true
console.log(typeof nowStr); // 'string'

// Transpile with Babel CLI:
// npx babel code.js --out-file compiled.js