Socket
Socket
Sign inDemoInstall

@babel/plugin-transform-classes

Package Overview
Dependencies
81
Maintainers
4
Versions
101
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @babel/plugin-transform-classes

Compile ES2015 classes to ES5


Version published
Weekly downloads
24M
decreased by-0.5%
Maintainers
4
Created
Weekly downloads
 

Package description

What is @babel/plugin-transform-classes?

The @babel/plugin-transform-classes package is a plugin for Babel that transforms ES2015+ class syntax into equivalent ES5 code. This is useful for ensuring compatibility with environments that do not support the latest JavaScript features.

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

Class transformation

Transforms ES2015+ class syntax into equivalent ES5 constructor functions with prototype methods.

{"class MyClass { constructor(name) { this.name = name; } greet() { return 'Hello, ' + this.name; } } }

Super calls

Handles 'super' calls and transforms them into appropriate ES5 code that calls the parent class constructor or methods.

{"class ChildClass extends ParentClass { constructor(name) { super(name); } } }

Static methods

Transforms static methods in classes to static methods on the constructor function in ES5.

{"class MyClass { static myStaticMethod() { return 'I am static'; } } }

Instance properties

Transforms class instance properties into assignments within the constructor function.

{"class MyClass { myProperty = 'default value'; } }

Other packages similar to @babel/plugin-transform-classes

Readme

Source

@babel/plugin-transform-classes

Compile ES2015 classes to ES5

Caveats

Built-in classes such as Date, Array, DOM etc cannot be properly subclassed due to limitations in ES5 (for the classes plugin). You can try to use @babel/plugin-transform-builtin-extend based on Object.setPrototypeOf and Reflect.construct, but it also has some limitations.

Examples

In

class Test {
  constructor(name) {
    this.name = name;
  }

  logger () {
    console.log("Hello", this.name);
  }
}

Out

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Test = function () {
  function Test(name) {
    _classCallCheck(this, Test);

    this.name = name;
  }

  Test.prototype.logger = function logger() {
    console.log("Hello", this.name);
  };

  return Test;
}();

Installation

npm install --save-dev @babel/plugin-transform-classes

Usage

.babelrc

// without options
{
  "plugins": ["@babel/plugin-transform-classes"]
}

// with options
{
  "plugins": [
    ["@babel/plugin-transform-classes", {
      "loose": true
    }]
  ]
}

Via CLI

babel --plugins @babel/plugin-transform-classes script.js

Via Node API

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

Options

loose

boolean, defaults to false.

Method enumerability

Please note that in loose mode class methods are enumerable. This is not in line with the spec and you may run into issues.

Method assignment

Under loose mode, methods are defined on the class prototype with simple assignments instead of being defined. This can result in the following not working:

class Foo {
  set bar() {
    throw new Error("foo!");
  }
}

class Bar extends Foo {
  bar() {
    // will throw an error when this method is defined
  }
}

When Bar.prototype.foo is defined it triggers the setter on Foo. This is a case that is very unlikely to appear in production code however it's something to keep in mind.

Keywords

FAQs

Last updated on 01 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