fix(es/compat): Hoist env in function params (#4210)

This commit is contained in:
magic-akari 2022-03-31 21:00:19 +08:00 committed by GitHub
parent 809c87e8b7
commit 6a27a0ce88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 1 deletions

View File

@ -0,0 +1,24 @@
{
"jsc": {
"externalHelpers": true,
"parser": {
"syntax": "typescript",
"decorators": true,
"tsx": true
},
"transform": {
"legacyDecorator": true,
"decoratorMetadata": false
},
"target": "es5",
"loose": false,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false
}

View File

@ -0,0 +1,7 @@
export class CompanyBgStore {
public corpName = 123;
public getBusinessInfo = async (corpName = this.corpName) => {
console.log(corpName);
};
}

View File

@ -0,0 +1,21 @@
import * as swcHelpers from "@swc/helpers";
import regeneratorRuntime from "regenerator-runtime";
export var CompanyBgStore = function CompanyBgStore() {
"use strict";
swcHelpers.classCallCheck(this, CompanyBgStore);
this.corpName = 123;
var _this = this;
this.getBusinessInfo = swcHelpers.asyncToGenerator(regeneratorRuntime.mark(function _callee() {
var corpName, _args = arguments;
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
corpName = _args.length > 0 && _args[0] !== void 0 ? _args[0] : _this.corpName;
console.log(corpName);
case 2:
case "end":
return _ctx.stop();
}
}, _callee);
}));
};

View File

@ -309,7 +309,7 @@ impl Actual {
Expr::Arrow(arrow_expr @ ArrowExpr { is_async: true, .. }) => { Expr::Arrow(arrow_expr @ ArrowExpr { is_async: true, .. }) => {
let mut state = FnEnvHoister::default(); let mut state = FnEnvHoister::default();
arrow_expr.body.visit_mut_with(&mut state); arrow_expr.visit_mut_with(&mut state);
self.hoist_stmts.extend(state.to_stmt()); self.hoist_stmts.extend(state.to_stmt());

View File

@ -3557,6 +3557,32 @@ const p = Z((f)=>_asyncToGenerator(function*(n = 0) {
" "
); );
test!(
Syntax::default(),
|_| async_to_generator(Default::default()),
issue_4208,
"
function foo() {
const bar = async (baz = this.baz) => {
console.log(this);
}
}
",
"
function foo() {
var _this = this;
const bar = function() {
var _ref = _asyncToGenerator(function*(baz = _this.baz) {
console.log(_this);
});
return function bar() {
return _ref.apply(this, arguments);
};
}();
}
"
);
#[testing::fixture("tests/async-to-generator/**/exec.js")] #[testing::fixture("tests/async-to-generator/**/exec.js")]
fn exec(input: PathBuf) { fn exec(input: PathBuf) {
let input = read_to_string(&input).unwrap(); let input = read_to_string(&input).unwrap();