fix(es/fixer): Handle async in the left of for of (#5625)

This commit is contained in:
magic-akari 2022-08-26 12:47:46 +08:00 committed by GitHub
parent d538b72002
commit eb3b0e96e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 2 deletions

View File

@ -1,6 +1,6 @@
// @target: esnext
var async;
for (async of [
for ((async) of [
1,
2
]);

View File

@ -1,5 +1,5 @@
var async;
for (async of [
for ((async) of [
1,
2
]);

View File

@ -1,3 +1,4 @@
use swc_atoms::js_word;
use swc_common::{collections::AHashMap, comments::Comments, util::take::Take, Span, Spanned};
use swc_ecma_ast::*;
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};
@ -461,6 +462,31 @@ impl VisitMut for Fixer<'_> {
fn visit_mut_for_of_stmt(&mut self, s: &mut ForOfStmt) {
s.visit_mut_children_with(self);
if s.await_token.is_none() {
if let VarDeclOrPat::Pat(Pat::Ident(BindingIdent {
id:
id @ Ident {
sym: js_word!("async"),
..
},
..
})) = &mut s.left
{
let expr = Expr::Ident(id.take());
s.left = VarDeclOrPat::Pat(Pat::Expr(Box::new(expr)));
}
if let VarDeclOrPat::Pat(Pat::Expr(expr)) = &mut s.left {
if let Expr::Ident(Ident {
sym: js_word!("async"),
..
}) = &**expr
{
self.wrap(&mut *expr);
}
}
}
if let Expr::Seq(..) | Expr::Await(..) = &*s.right {
self.wrap(&mut s.right)
}