fix(es/transforms/compat): Reduce .bind(this) (#2806)

swc_ecma_transforms_compat:
 - `async_to_generator`: Reduce explicit `.bind(this)`.
This commit is contained in:
OJ Kwon 2021-11-20 00:04:27 -08:00 committed by GitHub
parent 43586d0b7c
commit 26734d44eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 60 deletions

View File

@ -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);
}
}

View File

@ -77,7 +77,7 @@ var C = // @target: ES5
return _ctx.stop();
}
}, _callee, this);
}).bind(this));
}));
return _fn.apply(this, arguments);
}
}

View File

@ -54,7 +54,7 @@ var C = function() {
return _ctx.stop();
}
}, _callee, this);
}).bind(this))).apply(this, arguments);
}))).apply(this, arguments);
}
}
}

View File

@ -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 {

View File

@ -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();