Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
babel-plugin-transform-es2015-arrow-functions
Advanced tools
Compile ES2015 arrow functions to ES5
The babel-plugin-transform-es2015-arrow-functions package is a Babel plugin that transforms ES2015 (ES6) arrow functions into ES5 function expressions. This is useful for ensuring compatibility with environments that do not support ES2015 syntax.
Transform Arrow Functions
This feature transforms ES2015 arrow functions into ES5 function expressions. This is particularly useful for ensuring that your code runs in environments that do not support ES2015 syntax.
const add = (a, b) => a + b;
// Transformed to:
var add = function(a, b) {
return a + b;
};
Lexical 'this' Binding
Arrow functions do not have their own 'this' context and instead inherit 'this' from the parent scope. This feature ensures that the 'this' context is correctly bound when transforming arrow functions to ES5.
const obj = {
value: 10,
increment: function() {
setTimeout(() => {
this.value++;
}, 1000);
}
};
// Transformed to:
var obj = {
value: 10,
increment: function() {
var _this = this;
setTimeout(function() {
_this.value++;
}, 1000);
}
};
This plugin transforms ES2015 function names to ensure compatibility with older environments. While it focuses on function names rather than arrow functions, it serves a similar purpose of making ES2015 features compatible with ES5.
This plugin transforms ES2015 parameter features like default parameters and rest parameters into ES5-compatible code. It complements babel-plugin-transform-es2015-arrow-functions by handling other ES2015 syntax features.
This plugin transforms ES2015 template literals into ES5 string concatenations. Like babel-plugin-transform-es2015-arrow-functions, it ensures that modern JavaScript syntax can run in older environments.
Compile ES2015 arrow functions to ES5
In
var a = () => {};
var a = (b) => b;
const double = [1,2,3].map((num) => num * 2);
console.log(double); // [2,4,6]
var bob = {
_name: "Bob",
_friends: ["Sally", "Tom"],
printFriends() {
this._friends.forEach(f =>
console.log(this._name + " knows " + f));
}
};
console.log(bob.printFriends());
Out
var a = function () {};
var a = function (b) {
return b;
};
const double = [1, 2, 3].map(function (num) {
return num * 2;
});
console.log(double); // [2,4,6]
var bob = {
_name: "Bob",
_friends: ["Sally", "Tom"],
printFriends() {
var _this = this;
this._friends.forEach(function (f) {
return console.log(_this._name + " knows " + f);
});
}
};
console.log(bob.printFriends());
npm install --save-dev babel-plugin-transform-es2015-arrow-functions
.babelrc
(Recommended).babelrc
Without options:
{
"plugins": ["transform-es2015-arrow-functions"]
}
With options:
{
"plugins": [
["transform-es2015-arrow-functions", { "spec": true }]
]
}
babel --plugins transform-es2015-arrow-functions script.js
require("babel-core").transform("code", {
plugins: ["transform-es2015-arrow-functions"]
});
spec
boolean
, defaults to false
.
Using spec mode with the above example produces:
var _this = this;
var a = function a() {
babelHelpers.newArrowCheck(this, _this);
}.bind(this);
var a = function a(b) {
babelHelpers.newArrowCheck(this, _this);
return b;
}.bind(this);
const double = [1, 2, 3].map(function (num) {
babelHelpers.newArrowCheck(this, _this);
return num * 2;
}.bind(this));
console.log(double); // [2,4,6]
var bob = {
_name: "Bob",
_friends: ["Sally", "Tom"],
printFriends() {
var _this2 = this;
this._friends.forEach(function (f) {
babelHelpers.newArrowCheck(this, _this2);
return console.log(this._name + " knows " + f);
}.bind(this));
}
};
console.log(bob.printFriends());
This option enables the following:
Wrap the generated function in .bind(this)
and keeps uses of this
inside
the function as-is, instead of using a renamed this
.
Add a runtime check to ensure the functions are not instantiated.
Add names to arrow functions.
FAQs
Compile ES2015 arrow functions to ES5
The npm package babel-plugin-transform-es2015-arrow-functions receives a total of 1,647,970 weekly downloads. As such, babel-plugin-transform-es2015-arrow-functions popularity was classified as popular.
We found that babel-plugin-transform-es2015-arrow-functions demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 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.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.