Fixes & optimizations to es6.spec.arrowFunctions.
This commit is contained in:
parent
15ae0b669b
commit
deaf03dd28
@ -1,5 +1,5 @@
|
||||
(function (instance, arrowFn) {
|
||||
if (instance instanceof arrowFn) {
|
||||
(function (innerThis, boundThis) {
|
||||
if (innerThis !== boundThis) {
|
||||
throw new TypeError("Cannot instantiate an arrow function");
|
||||
}
|
||||
});
|
||||
|
||||
@ -8,19 +8,14 @@ export function ArrowFunctionExpression(node, parent, scope, file) {
|
||||
if (node.shadow) return;
|
||||
node.shadow = { this: false };
|
||||
|
||||
var {id} = node;
|
||||
var expr = node;
|
||||
|
||||
if (!id) {
|
||||
id = scope.parent.generateDeclaredUidIdentifier("arrow");
|
||||
expr = t.assignmentExpression("=", id, expr);
|
||||
}
|
||||
var boundThis = t.thisExpression();
|
||||
boundThis._shadowedFunctionLiteral = false;
|
||||
|
||||
// make sure that arrow function won't be instantiated
|
||||
t.ensureBlock(node).body.unshift(t.expressionStatement(t.callExpression(file.addHelper("new-arrow-check"), [
|
||||
t.thisExpression(),
|
||||
id
|
||||
boundThis
|
||||
])));
|
||||
|
||||
return t.callExpression(t.memberExpression(expr, t.identifier("bind")), [t.thisExpression()]);
|
||||
return t.callExpression(t.memberExpression(node, t.identifier("bind")), [t.thisExpression()]);
|
||||
}
|
||||
|
||||
@ -23,13 +23,11 @@ function remap(path, key, create) {
|
||||
}
|
||||
|
||||
export function ThisExpression(node) {
|
||||
if (!node._shadowedFunctionLiteral) {
|
||||
return remap(this, "this", () => t.thisExpression());
|
||||
}
|
||||
return remap(this, "this", () => t.thisExpression());
|
||||
}
|
||||
|
||||
export function ReferencedIdentifier(node) {
|
||||
if (node.name === "arguments" && !node._shadowedFunctionLiteral) {
|
||||
if (node.name === "arguments") {
|
||||
return remap(this, "arguments", () => t.identifier("arguments"));
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,9 +64,13 @@ export function inType(types) {
|
||||
|
||||
export function inShadow(key) {
|
||||
var path = this;
|
||||
var dontShadow = path.node._shadowedFunctionLiteral;
|
||||
if (dontShadow !== undefined) {
|
||||
return !dontShadow;
|
||||
}
|
||||
while (path) {
|
||||
if (path.isFunction()) {
|
||||
var {shadow} = path.node;
|
||||
var { shadow } = path.node;
|
||||
if (shadow && (shadow === true || shadow[key] !== false)) {
|
||||
return path;
|
||||
} else {
|
||||
|
||||
@ -1,25 +1,20 @@
|
||||
"use strict";
|
||||
|
||||
var _arrow;
|
||||
|
||||
function _newArrowCheck(instance, arrowFn) { if (instance instanceof arrowFn) { throw new TypeError("Cannot instantiate an arrow function"); } }
|
||||
|
||||
arr.map((_arrow = function (x) {
|
||||
_newArrowCheck(this, _arrow);
|
||||
var _this = this;
|
||||
|
||||
arr.map((function (x) {
|
||||
babelHelpers.newArrowCheck(this, _this);
|
||||
return x * x;
|
||||
}).bind(this));
|
||||
var f = (function f(x, y) {
|
||||
_newArrowCheck(this, f);
|
||||
|
||||
babelHelpers.newArrowCheck(this, _this);
|
||||
return x * y;
|
||||
}).bind(this);
|
||||
(function () {
|
||||
var _arrow2;
|
||||
|
||||
return (_arrow2 = function () {
|
||||
_newArrowCheck(this, _arrow2);
|
||||
var _this2 = this;
|
||||
|
||||
return (function () {
|
||||
babelHelpers.newArrowCheck(this, _this2);
|
||||
return this;
|
||||
}).bind(this);
|
||||
})();
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
{
|
||||
"externalHelpers": true,
|
||||
"optional": ["es6.spec.arrowFunctions"]
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user