fix(es/utils): Fix contains_top_level_await (#5335)

This commit is contained in:
magic-akari 2022-07-29 12:01:37 +08:00 committed by GitHub
parent 731c4bc4f3
commit 1d10c3e837
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2481,7 +2481,11 @@ pub struct TopLevelAwait {
impl Visit for TopLevelAwait { impl Visit for TopLevelAwait {
noop_visit_type!(); noop_visit_type!();
fn visit_stmts(&mut self, _: &[Stmt]) {} fn visit_stmt(&mut self, n: &Stmt) {
if !self.found {
n.visit_children_with(self);
}
}
fn visit_param(&mut self, _: &Param) {} fn visit_param(&mut self, _: &Param) {}
@ -2503,6 +2507,37 @@ impl Visit for TopLevelAwait {
}; };
} }
fn visit_prop(&mut self, prop: &Prop) {
match prop {
Prop::KeyValue(KeyValueProp {
key: PropName::Computed(computed),
..
})
| Prop::Getter(GetterProp {
key: PropName::Computed(computed),
..
})
| Prop::Setter(SetterProp {
key: PropName::Computed(computed),
..
})
| Prop::Method(MethodProp {
key: PropName::Computed(computed),
..
}) => computed.visit_children_with(self),
_ => {}
}
}
fn visit_for_of_stmt(&mut self, for_of_stmt: &ForOfStmt) {
if for_of_stmt.await_token.is_some() {
self.found = true;
return;
}
for_of_stmt.visit_children_with(self);
}
fn visit_await_expr(&mut self, _: &AwaitExpr) { fn visit_await_expr(&mut self, _: &AwaitExpr) {
self.found = true; self.found = true;
} }
@ -2566,4 +2601,25 @@ mod test {
); );
p.parse_module().unwrap() p.parse_module().unwrap()
} }
fn has_top_level_await(text: &str) -> bool {
let module = parse_module(text);
contains_top_level_await(&module)
}
#[test]
fn top_level_await_block() {
assert!(has_top_level_await("if (maybe) { await test; }"))
}
#[test]
fn top_level_await_for_of() {
assert!(has_top_level_await("for await (let iter of []){}"))
}
#[test]
fn top_level_export_await() {
assert!(has_top_level_await("export const foo = await 1;"));
assert!(has_top_level_await("export default await 1;"));
}
} }