or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel--plugin-transform-new-target

Transforms new.target meta property for environments that don't natively support it

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/plugin-transform-new-target@7.27.x

To install, run

npx @tessl/cli install tessl/npm-babel--plugin-transform-new-target@7.27.0

index.mddocs/

@babel/plugin-transform-new-target

A Babel plugin that transforms the new.target meta property for environments that don't natively support it. This plugin converts new.target expressions to equivalent conditional expressions that check if the current function was called with the new operator.

Package Information

  • Package Name: @babel/plugin-transform-new-target
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation: npm install --save-dev @babel/plugin-transform-new-target

Core Imports

// ES Modules
import pluginTransformNewTarget from "@babel/plugin-transform-new-target";

// CommonJS
const pluginTransformNewTarget = require("@babel/plugin-transform-new-target");

// For plugin development (internal dependencies)
import { declare } from "@babel/helper-plugin-utils";
import { types as t, type NodePath } from "@babel/core";

Basic Usage

Add the plugin to your Babel configuration:

babel.config.js:

module.exports = {
  plugins: ["@babel/plugin-transform-new-target"]
};

With options (using the default export):

import pluginTransformNewTarget from "@babel/plugin-transform-new-target";

export default {
  plugins: [pluginTransformNewTarget]
};

Programmatic usage:

import { transform } from "@babel/core";
import pluginTransformNewTarget from "@babel/plugin-transform-new-target";

const result = transform(code, {
  plugins: [pluginTransformNewTarget]
});

Capabilities

Plugin Factory Function

Creates a Babel transformation plugin that processes new.target expressions.

/**
 * Default export - Babel plugin factory function created with declare() helper
 * Uses declare() from @babel/helper-plugin-utils for plugin creation
 * @param api - Babel API object with version assertion and utilities
 * @returns Babel plugin object with visitor methods
 */
export default declare((api: BabelAPI) => PluginObj);

// The declare function signature (from @babel/helper-plugin-utils)
function declare(builder: (api: BabelAPI) => PluginObj): (api: BabelAPI) => PluginObj;

interface BabelAPI {
  assertVersion(version: number | string): void;
}

// Global utility function used internally by Babel plugins
type REQUIRED_VERSION = (version: number | string) => number | string;

interface PluginObj {
  name: string;
  visitor: VisitorObject;
}

interface VisitorObject {
  MetaProperty(path: NodePath): void;
}

Transformation Behavior

The plugin transforms new.target expressions based on their context:

In Class Constructors

// Input
class Foo {
  constructor() {
    new.target;
  }
}

// Output
class Foo {
  constructor() {
    this.constructor;
  }
}

In Regular Functions

// Input
function Foo() {
  new.target;
}

// Output
function Foo() {
  this instanceof Foo ? this.constructor : void 0;
}

// Method assignment gets unique generated identifier
Foo.prototype.test = function _target() {
  this instanceof _target ? this.constructor : void 0;
};

In Function Expressions

// Input
var Bar = function() {
  new.target;
};

// Output (plugin generates unique identifier automatically)
var Bar = function _target() {
  this instanceof _target ? this.constructor : void 0;
};

In Class Methods

// Input
class Foo {
  test() {
    new.target;
  }
}

// Output
class Foo {
  test() {
    void 0;
  }
}

Error Handling

The plugin validates new.target usage and throws build-time errors for invalid contexts:

  • Arrow Functions: new.target in arrow functions throws an error
  • Global Scope: new.target outside of functions/classes throws an error
  • Error Message: "new.target must be under a (non-arrow) function or a class."

Plugin Configuration

This plugin requires zero configuration and takes no options. It automatically processes all new.target expressions found in the code according to the transformation rules above. Simply add it to your Babel plugins array and it will handle all transformations automatically.

Types

// Internal types used by the plugin (not exported)
interface NodePath {
  get(key: string): NodePath;
  isIdentifier(opts?: { name: string }): boolean;
  findParent(callback: (path: NodePath) => boolean): NodePath | null;
  buildCodeFrameError(message: string): Error;
  replaceWith(node: Node): void;
  scope: Scope;
}

interface Scope {
  buildUndefinedNode(): Node;
  generateUidIdentifier(name: string): Identifier;
  hasOwnBinding(name: string): boolean;
  bindingIdentifierEquals(name: string, node: Node): boolean;
  rename(name: string): void;
  parent: Scope;
}