diff --git a/crates/swc/tests/tsc-references/async/es5/functionDeclarations/asyncFunctionDeclarationCapturesArguments_es5/input.ts/es2015.1.normal/output.js b/crates/swc/tests/tsc-references/async/es5/functionDeclarations/asyncFunctionDeclarationCapturesArguments_es5/input.ts/es2015.1.normal/output.js index 87bf88683d8..81572560b14 100644 --- a/crates/swc/tests/tsc-references/async/es5/functionDeclarations/asyncFunctionDeclarationCapturesArguments_es5/input.ts/es2015.1.normal/output.js +++ b/crates/swc/tests/tsc-references/async/es5/functionDeclarations/asyncFunctionDeclarationCapturesArguments_es5/input.ts/es2015.1.normal/output.js @@ -38,9 +38,9 @@ class C { return _fn.apply(this, arguments); } function _fn() { - _fn = _asyncToGenerator((function*() { + _fn = _asyncToGenerator(function*() { yield other.apply(this, arguments); - }).bind(this)); + }); return _fn.apply(this, arguments); } } diff --git a/crates/swc/tests/tsc-references/async/es5/functionDeclarations/asyncFunctionDeclarationCapturesArguments_es5/input.ts/es5.1.normal/output.js b/crates/swc/tests/tsc-references/async/es5/functionDeclarations/asyncFunctionDeclarationCapturesArguments_es5/input.ts/es5.1.normal/output.js index be69e40e3fd..6e54521cf30 100644 --- a/crates/swc/tests/tsc-references/async/es5/functionDeclarations/asyncFunctionDeclarationCapturesArguments_es5/input.ts/es5.1.normal/output.js +++ b/crates/swc/tests/tsc-references/async/es5/functionDeclarations/asyncFunctionDeclarationCapturesArguments_es5/input.ts/es5.1.normal/output.js @@ -77,7 +77,7 @@ var C = // @target: ES5 return _ctx.stop(); } }, _callee, this); - }).bind(this)); + })); return _fn.apply(this, arguments); } } diff --git a/crates/swc/tests/tsc-references/async/es5/functionDeclarations/asyncFunctionDeclarationCapturesArguments_es5/input.ts/es5.2.minified/output.js b/crates/swc/tests/tsc-references/async/es5/functionDeclarations/asyncFunctionDeclarationCapturesArguments_es5/input.ts/es5.2.minified/output.js index 0aba48da1c7..d790323ebd4 100644 --- a/crates/swc/tests/tsc-references/async/es5/functionDeclarations/asyncFunctionDeclarationCapturesArguments_es5/input.ts/es5.2.minified/output.js +++ b/crates/swc/tests/tsc-references/async/es5/functionDeclarations/asyncFunctionDeclarationCapturesArguments_es5/input.ts/es5.2.minified/output.js @@ -54,7 +54,7 @@ var C = function() { return _ctx.stop(); } }, _callee, this); - }).bind(this))).apply(this, arguments); + }))).apply(this, arguments); } } } diff --git a/crates/swc_ecma_transforms_compat/src/es2017/async_to_generator.rs b/crates/swc_ecma_transforms_compat/src/es2017/async_to_generator.rs index be289d08883..43ff47c1189 100644 --- a/crates/swc_ecma_transforms_compat/src/es2017/async_to_generator.rs +++ b/crates/swc_ecma_transforms_compat/src/es2017/async_to_generator.rs @@ -269,7 +269,6 @@ impl VisitMut for Actual { assign_expr.visit_mut_children_with(self); self.in_prototype_assignment = false; - //*expr = Expr::Assign(assign_expr.take()); return; } _ => {} @@ -906,7 +905,7 @@ impl Actual { ident: None, function: f.take(), }, - self.in_object_prop || self.in_prototype_assignment, + true, ); if is_decl { diff --git a/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs b/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs index b4cb7ea16a3..c1d589cd186 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2017_async_to_generator.rs @@ -222,45 +222,45 @@ async function s(x, ...args) { "#, r#" function s(x) { - return _s.apply(this, arguments); - } - function _s() { - _s = _asyncToGenerator((function*(x) { - for(let _len1 = arguments.length, args = new Array(_len1 > 1 ? _len1 - 1 : 0), _key1 = 1; _key1 < _len1; _key1++){ - args[_key1 - 1] = arguments[_key1]; - } - var _this = this, _arguments = arguments; - let t = function(y, a) { - var _t = _asyncToGenerator(function*(y, a) { - var _this1 = _this, _arguments1 = _arguments; - let r = function(z, b) { - var _r = _asyncToGenerator(function*(z, b) { - for(let _len = arguments.length, innerArgs = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++){ - innerArgs[_key - 2] = arguments[_key]; - } - yield z; - console.log(_this1, innerArgs, _arguments1); - return _this1.x; - }); - function r() { - return _r.apply(this, arguments); - } - return r; - }(); - yield r(); - console.log(_this, args, _arguments); - return _this.g(r); - }); - function t() { - return _t.apply(this, arguments); - } - return t; - }(); - yield t(); - return this.h(t); - }).bind(this)); - return _s.apply(this, arguments); - } + return _s.apply(this, arguments); +} +function _s() { + _s = _asyncToGenerator(function*(x) { + for(let _len1 = arguments.length, args = new Array(_len1 > 1 ? _len1 - 1 : 0), _key1 = 1; _key1 < _len1; _key1++){ + args[_key1 - 1] = arguments[_key1]; + } + var _this = this, _arguments = arguments; + let t = function(y, a) { + var _t = _asyncToGenerator(function*(y, a) { + var _this1 = _this, _arguments1 = _arguments; + let r = function(z, b) { + var _r = _asyncToGenerator(function*(z, b) { + for(let _len = arguments.length, innerArgs = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++){ + innerArgs[_key - 2] = arguments[_key]; + } + yield z; + console.log(_this1, innerArgs, _arguments1); + return _this1.x; + }); + function r() { + return _r.apply(this, arguments); + } + return r; + }(); + yield r(); + console.log(_this, args, _arguments); + return _this.g(r); + }); + function t() { + return _t.apply(this, arguments); + } + return t; + }(); + yield t(); + return this.h(t); + }); + return _s.apply(this, arguments); +} "# ); @@ -1724,21 +1724,21 @@ async function s(x, ...args) { return _s.apply(this, arguments); } function _s() { - _s = _asyncToGenerator((function*(x, ...args) { - let t = _asyncToGenerator((function*(y, a) { - let r = _asyncToGenerator((function*(z, b, ...innerArgs) { - yield z; - console.log(this, innerArgs, arguments); - return this.x; - }).bind(this)).bind(this); - yield r(); - console.log(this, args, arguments); - return this.g(r); - }).bind(this)).bind(this); - yield t(); - return this.h(t); - }).bind(this)); - return _s.apply(this, arguments); + _s = _asyncToGenerator(function*(x, ...args) { + let t = _asyncToGenerator((function*(y, a) { + let r = _asyncToGenerator((function*(z, b, ...innerArgs) { + yield z; + console.log(this, innerArgs, arguments); + return this.x; + }).bind(this)).bind(this); + yield r(); + console.log(this, args, arguments); + return this.g(r); + }).bind(this)).bind(this); + yield t(); + return this.h(t); + }); + return _s.apply(this, arguments); } "# ); @@ -3007,6 +3007,29 @@ export default async function() { " ); +test_exec!( + Syntax::default(), + |_| async_to_generator(), + function_parameters, + " +class A { + waitForinit = () => Promise.resolve(3); + + doTest() { + throw new Error('should not be called'); + } +} + +jest.spyOn(A.prototype, 'doTest').mockImplementation(async function() { + const ret = await this.waitForinit(); + return ret; +}); + +const a = new A(); +expect(a.doTest()).resolves.toEqual(3); +" +); + #[testing::fixture("tests/fixture/async-to-generator/**/exec.js")] fn exec(input: PathBuf) { let input = read_to_string(&input).unwrap();