fix(es/transforms/compat): Apply regenerator for default function declarations (#2681)

swc_ecma_transforms_compat:
 - `regenerator`: Handle `DefaultDecl::Fn`. (Closes #2677)
This commit is contained in:
OJ Kwon 2021-11-08 23:07:48 -08:00 committed by GitHub
parent 9824fdaae2
commit 8fe0d2583f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 163 additions and 0 deletions

View File

@ -360,6 +360,35 @@ impl Fold for Actual {
}
}
fn fold_module_item(&mut self, item: ModuleItem) -> ModuleItem {
match item {
// if fn is ExportDefaultDecl, fn is not FnDecl but FnExpr
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(export_default)) => {
if let DefaultDecl::Fn(expr) = export_default.decl {
let expr = if !expr.function.is_async {
expr
} else {
let function = self.fold_fn(expr.ident.clone(), expr.function, true);
FnExpr {
ident: expr.ident,
function,
}
};
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(ExportDefaultDecl {
decl: expr.into(),
..export_default
}))
} else {
let item =
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultDecl(export_default));
item.fold_children_with(self)
}
}
_ => item.fold_children_with(self),
}
}
fn fold_method_prop(&mut self, prop: MethodProp) -> MethodProp {
let prop = prop.fold_children_with(self);

View File

@ -2883,6 +2883,140 @@ myclass.handle();
"
);
test!(
Syntax::default(),
|_| {
let top_level_mark = Mark::fresh(Mark::root());
chain!(
async_to_generator(),
regenerator(Default::default(), top_level_mark)
)
},
issue_2677_1,
"
async function region() {
}
export async function otherCall() {
await region();
}
export default async function someCall() {
await region();
}
",
"
var regeneratorRuntime = require('regenerator-runtime');
function _region() {
_region = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
case 'end':
return _ctx.stop();
}
}, _callee);
}));
return _region.apply(this, arguments);
}
function region() {
return _region.apply(this, arguments);
}
function _otherCall() {
_otherCall = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
_ctx.next = 2;
return region();
case 2:
case 'end':
return _ctx.stop();
}
}, _callee);
}));
return _otherCall.apply(this, arguments);
}
export function otherCall() {
return _otherCall.apply(this, arguments);
}
function _someCall() {
_someCall = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
_ctx.next = 2;
return region();
case 2:
case 'end':
return _ctx.stop();
}
}, _callee);
}));
return _someCall.apply(this, arguments);
}
export default function someCall() {
return _someCall.apply(this, arguments);
}
"
);
test!(
Syntax::default(),
|_| {
let top_level_mark = Mark::fresh(Mark::root());
chain!(
async_to_generator(),
regenerator(Default::default(), top_level_mark)
)
},
issue_2677_2,
"
async function region() {
}
export default async function() {
await region();
}
",
"
var regeneratorRuntime = require('regenerator-runtime');
function _region() {
_region = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
case 'end':
return _ctx.stop();
}
}, _callee);
}));
return _region.apply(this, arguments);
}
function region() {
return _region.apply(this, arguments);
}
function _ref() {
_ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
_ctx.next = 2;
return region();
case 2:
case 'end':
return _ctx.stop();
}
}, _callee);
}));
return _ref.apply(this, arguments);
}
export default function() {
return _ref.apply(this, arguments);
}
"
);
#[testing::fixture("tests/fixture/async-to-generator/**/exec.js")]
fn exec(input: PathBuf) {
let input = read_to_string(&input).unwrap();