Skip to content

Commit

Permalink
fix(es/transforms): Fix this in async arrow class properties (#3252)
Browse files Browse the repository at this point in the history
swc_ecma_transforms_compat:
 - `async_to_generator`: Fix `this` in async class properties. (vercel/next.js#33283)
 - Add a test for #3228 (Closes #3228).
  • Loading branch information
kdy1 committed Jan 14, 2022
1 parent 1f49793 commit 7c19e26
Show file tree
Hide file tree
Showing 32 changed files with 304 additions and 413 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ function _asyncToGenerator(fn) {
// https://github.com/Microsoft/TypeScript/issues/24722
class A {
constructor(){
this.b = _asyncToGenerator((function*(...args) {
var _this = this;
this.b = _asyncToGenerator(function*(...args) {
yield Promise.resolve();
const obj = {
["a"]: ()=>this
["a"]: ()=>_this
}; // computed property name after `await` triggers case
}).bind(this)).bind(this);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,29 @@ function _defineProperty(obj, key, value) {
var A = function A() {
"use strict";
_classCallCheck(this, A);
var _this = this;
// @target: es5
// @lib: esnext, dom
// @downlevelIteration: true
// https://github.com/Microsoft/TypeScript/issues/24722
this.b = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
var _len, args, _key, _this, obj, _args = arguments;
var _len, args, _key, obj, _args = arguments;
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
for(_len = _args.length, args = new Array(_len), _key = 0; _key < _len; _key++){
args[_key] = _args[_key];
}
_this = this;
_ctx.next = 4;
_ctx.next = 3;
return Promise.resolve();
case 4:
case 3:
obj = _defineProperty({}, "a", function() {
return _this;
}); // computed property name after `await` triggers case
case 5:
case 4:
case "end":
return _ctx.stop();
}
}, _callee, this);
}).bind(this)).bind(this);
}, _callee);
}));
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ var A = function() {
"use strict";
!function(instance, Constructor) {
if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
}(this, A), this.b = (function(fn) {
}(this, A);
var _this = this;
this.b = (function(fn) {
return function() {
var self = this, args = arguments;
return new Promise(function(resolve, reject) {
Expand All @@ -35,20 +37,20 @@ var A = function() {
});
};
})(regeneratorRuntime.mark(function _callee() {
var _len, args, _key, _this, obj, _args = arguments;
var _len, args, _key, obj, _args = arguments;
return regeneratorRuntime.wrap(function(_ctx) {
for(;;)switch(_ctx.prev = _ctx.next){
case 0:
for(_len = _args.length, args = new Array(_len), _key = 0; _key < _len; _key++)args[_key] = _args[_key];
return _this = this, _ctx.next = 4, Promise.resolve();
case 4:
return _ctx.next = 3, Promise.resolve();
case 3:
obj = _defineProperty({}, "a", function() {
return _this;
});
case 5:
case 4:
case "end":
return _ctx.stop();
}
}, _callee, this);
}).bind(this)).bind(this);
}, _callee);
}));
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ function _asyncToGenerator(fn) {
class C {
method() {
function other() {}
var _this = this;
var _this = this, _arguments = arguments;
var fn = function() {
var _ref = _asyncToGenerator((function*() {
return yield other.apply(this, arguments);
}).bind(_this)).bind(_this);
var _ref = _asyncToGenerator(function*() {
return yield other.apply(_this, _arguments);
});
return function fn() {
return _ref.apply(this, arguments);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,22 @@ var C = // @target: es2017
key: "method",
value: function method() {
var other = function other() {};
var _this = this;
var _this = this, _arguments = arguments;
var fn = function() {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
var _args = arguments;
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
_ctx.next = 2;
return other.apply(this, _args);
return other.apply(_this, _arguments);
case 2:
return _ctx.abrupt("return", _ctx.sent);
case 3:
case "end":
return _ctx.stop();
}
}, _callee, this);
}).bind(_this)).bind(_this);
}, _callee);
}));
return function fn() {
return _ref.apply(this, arguments);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import regeneratorRuntime from "regenerator-runtime";
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg), value = info.value;
} catch (error) {
reject(error);
return;
}
info.done ? resolve(value) : Promise.resolve(value).then(_next, _throw);
}
function _defineProperties(target, props) {
for(var i = 0; i < props.length; i++){
var descriptor = props[i];
Expand All @@ -22,42 +13,10 @@ var C = function() {
if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
}(this, C);
}
return Constructor = C, protoProps = [
return protoProps = [
{
key: "method",
value: function() {
var other = function() {}, _this = this;
(function() {
(function(fn) {
return function() {
var self = this, args = arguments;
return new Promise(function(resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(void 0);
});
};
})(regeneratorRuntime.mark(function _callee() {
var _args = arguments;
return regeneratorRuntime.wrap(function(_ctx) {
for(;;)switch(_ctx.prev = _ctx.next){
case 0:
return _ctx.next = 2, other.apply(this, _args);
case 2:
return _ctx.abrupt("return", _ctx.sent);
case 3:
case "end":
return _ctx.stop();
}
}, _callee, this);
}).bind(_this)).bind(_this);
})();
}
value: function() {}
}
], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), C;
], _defineProperties((Constructor = C).prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), C;
}();
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ function _asyncToGenerator(fn) {
class C {
method() {
function other() {}
var _this = this;
var _this = this, _arguments = arguments;
var fn = function() {
var _ref = _asyncToGenerator((function*() {
return yield other.apply(this, arguments);
}).bind(_this)).bind(_this);
var _ref = _asyncToGenerator(function*() {
return yield other.apply(_this, _arguments);
});
return function fn() {
return _ref.apply(this, arguments);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,22 @@ var C = // @target: ES5
key: "method",
value: function method() {
var other = function other() {};
var _this = this;
var _this = this, _arguments = arguments;
var fn = function() {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
var _args = arguments;
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
_ctx.next = 2;
return other.apply(this, _args);
return other.apply(_this, _arguments);
case 2:
return _ctx.abrupt("return", _ctx.sent);
case 3:
case "end":
return _ctx.stop();
}
}, _callee, this);
}).bind(_this)).bind(_this);
}, _callee);
}));
return function fn() {
return _ref.apply(this, arguments);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import regeneratorRuntime from "regenerator-runtime";
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg), value = info.value;
} catch (error) {
reject(error);
return;
}
info.done ? resolve(value) : Promise.resolve(value).then(_next, _throw);
}
function _defineProperties(target, props) {
for(var i = 0; i < props.length; i++){
var descriptor = props[i];
Expand All @@ -22,42 +13,10 @@ var C = function() {
if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
}(this, C);
}
return Constructor = C, protoProps = [
return protoProps = [
{
key: "method",
value: function() {
var other = function() {}, _this = this;
(function() {
(function(fn) {
return function() {
var self = this, args = arguments;
return new Promise(function(resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(void 0);
});
};
})(regeneratorRuntime.mark(function _callee() {
var _args = arguments;
return regeneratorRuntime.wrap(function(_ctx) {
for(;;)switch(_ctx.prev = _ctx.next){
case 0:
return _ctx.next = 2, other.apply(this, _args);
case 2:
return _ctx.abrupt("return", _ctx.sent);
case 3:
case "end":
return _ctx.stop();
}
}, _callee, this);
}).bind(_this)).bind(_this);
})();
}
value: function() {}
}
], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), C;
], _defineProperties((Constructor = C).prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), C;
}();
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ function _asyncToGenerator(fn) {
class C {
method() {
function other() {}
var _this = this;
var _this = this, _arguments = arguments;
var fn = function() {
var _ref = _asyncToGenerator((function*() {
return yield other.apply(this, arguments);
}).bind(_this)).bind(_this);
var _ref = _asyncToGenerator(function*() {
return yield other.apply(_this, _arguments);
});
return function fn() {
return _ref.apply(this, arguments);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,22 @@ var C = // @target: ES6
key: "method",
value: function method() {
var other = function other() {};
var _this = this;
var _this = this, _arguments = arguments;
var fn = function() {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
var _args = arguments;
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
_ctx.next = 2;
return other.apply(this, _args);
return other.apply(_this, _arguments);
case 2:
return _ctx.abrupt("return", _ctx.sent);
case 3:
case "end":
return _ctx.stop();
}
}, _callee, this);
}).bind(_this)).bind(_this);
}, _callee);
}));
return function fn() {
return _ref.apply(this, arguments);
};
Expand Down
Loading

1 comment on commit 7c19e26

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 7c19e26 Previous: 1f49793 Ratio
full_es2015 188965166 ns/iter (± 11052697) 165723412 ns/iter (± 7772123) 1.14
full_es2016 198381902 ns/iter (± 9427389) 176019064 ns/iter (± 10989190) 1.13
full_es2017 197909708 ns/iter (± 8295635) 174904411 ns/iter (± 11592844) 1.13
full_es2018 196401544 ns/iter (± 10447292) 173455287 ns/iter (± 9659761) 1.13
full_es2019 195589341 ns/iter (± 11214799) 172122930 ns/iter (± 10182733) 1.14
full_es2020 173621807 ns/iter (± 7944322) 152882523 ns/iter (± 7333878) 1.14
full_es3 243160820 ns/iter (± 15830534) 213728620 ns/iter (± 8754141) 1.14
full_es5 248955859 ns/iter (± 15352611) 217404147 ns/iter (± 17580547) 1.15
parser 829382 ns/iter (± 65198) 696160 ns/iter (± 16830) 1.19

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.