swc_ecma_transforms_base/rename/
eval.rs1use swc_ecma_ast::*;
2use swc_ecma_utils::stack_size::maybe_grow_default;
3use swc_ecma_visit::{noop_visit_type, visit_obj_and_computed, Visit, VisitWith};
4
5pub fn contains_eval<N>(node: &N, include_with: bool) -> bool
6where
7 N: VisitWith<EvalFinder>,
8{
9 let mut v = EvalFinder {
10 found: false,
11 include_with,
12 };
13
14 node.visit_with(&mut v);
15 v.found
16}
17
18pub struct EvalFinder {
19 found: bool,
20 include_with: bool,
21}
22
23impl Visit for EvalFinder {
24 noop_visit_type!();
25
26 visit_obj_and_computed!();
27
28 fn visit_callee(&mut self, c: &Callee) {
29 if c.as_expr().is_some_and(|e| e.is_ident_ref_to("eval")) {
30 self.found = true;
31 } else {
32 c.visit_children_with(self);
33 }
34 }
35
36 fn visit_export_default_specifier(&mut self, _: &ExportDefaultSpecifier) {}
37
38 fn visit_export_named_specifier(&mut self, _: &ExportNamedSpecifier) {}
39
40 fn visit_export_namespace_specifier(&mut self, _: &ExportNamespaceSpecifier) {}
41
42 fn visit_expr(&mut self, n: &Expr) {
43 if self.found {
44 return;
45 }
46 maybe_grow_default(|| n.visit_children_with(self));
47 }
48
49 fn visit_stmt(&mut self, n: &Stmt) {
50 if self.found {
51 return;
52 }
53 n.visit_children_with(self)
54 }
55
56 fn visit_named_export(&mut self, e: &NamedExport) {
57 if e.src.is_some() {
58 return;
59 }
60
61 e.visit_children_with(self);
62 }
63
64 fn visit_prop_name(&mut self, p: &PropName) {
65 if let PropName::Computed(n) = p {
66 n.visit_with(self);
67 }
68 }
69
70 fn visit_with_stmt(&mut self, s: &WithStmt) {
71 if self.include_with {
72 self.found = true;
73 } else {
74 s.visit_children_with(self);
75 }
76 }
77}