Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@babel/plugin-transform-classes
Advanced tools
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.
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'; } }
Transforms ES2015+ arrow functions into equivalent ES5 function expressions. Similar in that it provides transformation of modern JavaScript features to ES5, but focuses on arrow functions instead of classes.
Transforms ES2015+ destructuring assignments and parameters into equivalent ES5 code. Similar in that it provides transformation of modern JavaScript syntax to ES5, but focuses on destructuring rather than class syntax.
Transforms ES2015+ spread syntax for arrays and function calls into equivalent ES5 code. Similar in that it provides transformation of modern JavaScript features to ES5, but focuses on spread syntax instead of class syntax.
Compile ES2015 classes to ES5
When extending a native class (e.g., class extends Array {}
), the super class
needs to be wrapped. This is needed to workaround two problems:
SuperClass.apply(/* ... */)
, but native
classes aren't callable and thus throw in this case.Array
) always return a new object. Instead of
returning it, Babel should treat it as the new this
.The wrapper works on IE11 and every other browser with Object.setPrototypeOf
or __proto__
as fallback.
There is NO IE <= 10 support. If you need IE <= 10 it's recommended that you don't extend natives.
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;
}();
npm install --save-dev @babel/plugin-transform-classes
.babelrc
(Recommended).babelrc
// without options
{
"plugins": ["@babel/plugin-transform-classes"]
}
// with options
{
"plugins": [
["@babel/plugin-transform-classes", {
"loose": true
}]
]
}
babel --plugins @babel/plugin-transform-classes script.js
require("@babel/core").transform("code", {
plugins: ["@babel/plugin-transform-classes"]
});
loose
boolean
, defaults to false
.
Please note that in loose mode class methods are enumerable. This is not in line with the spec and you may run into issues.
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.
FAQs
Compile ES2015 classes to ES5
We found that @babel/plugin-transform-classes demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.