Socket
Socket
Sign inDemoInstall

@babel/plugin-transform-new-target

Package Overview
Dependencies
77
Maintainers
4
Versions
74
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @babel/plugin-transform-new-target

Transforms new.target meta property


Version published
Weekly downloads
22M
decreased by-0.52%
Maintainers
4
Install size
4.16 kB
Created
Weekly downloads
 

Package description

What is @babel/plugin-transform-new-target?

The @babel/plugin-transform-new-target npm package is designed to transform instances of the `new.target` meta-property within functions. This is particularly useful for compiling ES6 code to be compatible with environments that do not support the `new.target` syntax. The `new.target` property is used within constructors to determine if the constructor was called with the `new` operator, allowing constructors to identify how they were invoked and to behave differently if necessary.

What are @babel/plugin-transform-new-target's main functionalities?

Transforming new.target

This feature allows the transformation of `new.target` usage into a form that can be understood by environments that do not support this syntax. It checks if the constructor was called with `new` and throws an error if it wasn't, ensuring that the constructor behaves as intended even in older JavaScript environments.

function MyConstructor() {\n  if (!new.target) {\n    throw 'MyConstructor must be called with new';\n  }\n  console.log('MyConstructor was called with new');\n}\n// Transformed Code:\nfunction MyConstructor() {\n  var _newtarget = this instanceof MyConstructor ? this.constructor : void 0;\n  if (!_newtarget) {\n    throw 'MyConstructor must be called with new';\n  }\n  console.log('MyConstructor was called with new');\n}

Other packages similar to @babel/plugin-transform-new-target

Readme

Source

@babel/plugin-transform-new-target

This plugins allows babel to transform new.target meta property into a (correct in most cases) this.constructor expression.

Example

function Foo() {
  console.log(new.target);
}

Foo(); // => undefined
new Foo(); // => Foo
class Foo {
  constructor() {
    console.log(new.target);
  }
}

class Bar extends Foo {
}

new Foo(); // => Foo
new Bar(); // => Bar

Caveats

This plugin relies on this.constructor, which means super must already have been called when using untransformed classes.

class Foo {}

class Bar extends Foo {
  constructor() {
    // This will be a problem if classes aren't transformed to ES5
    new.target;
    super();
  }
}

Additionally, this plugin cannot transform all Reflect.construct cases when using newTarget with ES5 function classes (transformed ES6 classes).

function Foo() {
  console.log(new.target);
}

// Bar extends Foo in ES5
function Bar() {
  Foo.call(this);
}
Bar.prototype = Object.create(Foo.prototype);
Bar.prototype.constructor = Bar;

// Baz does not extend Foo
function Baz() {}

Reflect.construct(Foo, []); // => Foo (correct)
Reflect.construct(Foo, [], Bar); // => Bar (correct)

Reflect.construct(Bar, []); // => Bar (incorrect, though this is how ES5
                            // inheritience is commonly implemented.)
Reflect.construct(Foo, [], Baz); // => undefined (incorrect)

Installation

npm install --save-dev @babel/plugin-transform-new-target

Usage

.babelrc

{
  "plugins": ["@babel/plugin-transform-new-target"]
}

Via CLI

babel --plugins @babel/plugin-transform-new-target script.js

Via Node API

require("@babel/core").transform("code", {
  plugins: ["@babel/plugin-transform-new-target"]
});

Keywords

FAQs

Last updated on 02 Dec 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc