turbopack_ecmascript/analyzer/
top_level_await.rs

1use swc_core::{
2    common::Span,
3    ecma::{
4        ast::*,
5        visit::{Visit, VisitWith, noop_visit_type},
6    },
7};
8
9/// Checks if the program contains a top level await, if it does it will returns
10/// the span of the first await.
11pub(crate) fn has_top_level_await(m: &Program) -> Option<Span> {
12    let mut visitor = TopLevelAwaitVisitor::default();
13
14    m.visit_with(&mut visitor);
15
16    visitor.top_level_await_span
17}
18
19#[derive(Default)]
20struct TopLevelAwaitVisitor {
21    top_level_await_span: Option<Span>,
22}
23
24macro_rules! noop {
25    ($name:ident, $T:path) => {
26        fn $name(&mut self, _: &$T) {}
27    };
28}
29
30impl Visit for TopLevelAwaitVisitor {
31    fn visit_await_expr(&mut self, n: &AwaitExpr) {
32        if self.top_level_await_span.is_none() {
33            self.top_level_await_span = Some(n.span);
34        }
35    }
36
37    // prevent non top level items from visiting their children
38    noop_visit_type!();
39    noop!(visit_arrow_expr, ArrowExpr);
40    noop!(visit_constructor, Constructor);
41    noop!(visit_function, Function);
42
43    fn visit_getter_prop(&mut self, n: &GetterProp) {
44        n.key.visit_children_with(self);
45    }
46
47    fn visit_setter_prop(&mut self, n: &SetterProp) {
48        n.key.visit_children_with(self);
49    }
50
51    fn visit_class_prop(&mut self, n: &ClassProp) {
52        n.key.visit_children_with(self);
53        n.decorators.visit_children_with(self);
54        if n.is_static {
55            n.value.visit_children_with(self);
56        }
57    }
58
59    fn visit_private_prop(&mut self, n: &PrivateProp) {
60        n.decorators.visit_children_with(self);
61        if n.is_static {
62            n.value.visit_children_with(self);
63        }
64    }
65
66    fn visit_auto_accessor(&mut self, n: &AutoAccessor) {
67        n.key.visit_children_with(self);
68        n.decorators.visit_children_with(self);
69        if n.is_static {
70            n.value.visit_children_with(self);
71        }
72    }
73}