Skip to content

Commit 9f104af

Browse files
authored
refactor(es): Deduplicate EvalFinder (#10472)
1 parent ca85958 commit 9f104af

File tree

4 files changed

+6
-49
lines changed

4 files changed

+6
-49
lines changed

crates/swc_ecma_minifier/src/compress/optimize/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
77
use swc_atoms::Atom;
88
use swc_common::{pass::Repeated, util::take::Take, Spanned, SyntaxContext, DUMMY_SP};
99
use swc_ecma_ast::*;
10+
use swc_ecma_transforms_base::rename::contains_eval;
1011
use swc_ecma_transforms_optimization::debug_assert_valid;
1112
use swc_ecma_usage_analyzer::{analyzer::UsageAnalyzer, marks::Marks};
1213
use swc_ecma_utils::{
@@ -31,9 +32,7 @@ use crate::{
3132
mode::Mode,
3233
option::{CompressOptions, MangleOptions},
3334
program_data::{ProgramData, ScopeData, VarUsageInfoFlags},
34-
util::{
35-
contains_eval, contains_leaping_continue_with_label, make_number, ExprOptExt, ModuleItemExt,
36-
},
35+
util::{contains_leaping_continue_with_label, make_number, ExprOptExt, ModuleItemExt},
3736
};
3837

3938
mod arguments;

crates/swc_ecma_minifier/src/util/mod.rs

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use swc_atoms::Atom;
77
use swc_common::{util::take::Take, Span, Spanned, DUMMY_SP};
88
use swc_ecma_ast::*;
99
use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene};
10-
use swc_ecma_utils::{stack_size::maybe_grow_default, DropSpan, ModuleItemLike, StmtLike, Value};
10+
use swc_ecma_utils::{DropSpan, ModuleItemLike, StmtLike, Value};
1111
use swc_ecma_visit::{noop_visit_type, visit_mut_pass, visit_obj_and_computed, Visit, VisitWith};
1212

1313
pub(crate) mod base54;
@@ -476,48 +476,6 @@ pub fn now() -> Option<Instant> {
476476
}
477477
}
478478

479-
pub(crate) fn contains_eval<N>(node: &N, include_with: bool) -> bool
480-
where
481-
N: VisitWith<EvalFinder>,
482-
{
483-
let mut v = EvalFinder {
484-
found: false,
485-
include_with,
486-
};
487-
488-
node.visit_with(&mut v);
489-
v.found
490-
}
491-
492-
pub(crate) struct EvalFinder {
493-
found: bool,
494-
include_with: bool,
495-
}
496-
497-
impl Visit for EvalFinder {
498-
noop_visit_type!();
499-
500-
visit_obj_and_computed!();
501-
502-
fn visit_expr(&mut self, n: &Expr) {
503-
maybe_grow_default(|| n.visit_children_with(self));
504-
}
505-
506-
fn visit_ident(&mut self, i: &Ident) {
507-
if i.sym == "eval" {
508-
self.found = true;
509-
}
510-
}
511-
512-
fn visit_with_stmt(&mut self, s: &WithStmt) {
513-
if self.include_with {
514-
self.found = true;
515-
} else {
516-
s.visit_children_with(self);
517-
}
518-
}
519-
}
520-
521479
#[allow(unused)]
522480
pub(crate) fn dump_program(p: &Program) -> String {
523481
#[cfg(feature = "debug")]

crates/swc_ecma_transforms_base/src/rename/eval.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use swc_ecma_ast::*;
22
use swc_ecma_utils::stack_size::maybe_grow_default;
33
use swc_ecma_visit::{noop_visit_type, visit_obj_and_computed, Visit, VisitWith};
44

5-
pub(crate) fn contains_eval<N>(node: &N, include_with: bool) -> bool
5+
pub fn contains_eval<N>(node: &N, include_with: bool) -> bool
66
where
77
N: VisitWith<EvalFinder>,
88
{
@@ -15,7 +15,7 @@ where
1515
v.found
1616
}
1717

18-
pub(crate) struct EvalFinder {
18+
pub struct EvalFinder {
1919
found: bool,
2020
include_with: bool,
2121
}

crates/swc_ecma_transforms_base/src/rename/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ use swc_ecma_visit::{
1010
noop_visit_mut_type, visit_mut_pass, Fold, VisitMut, VisitMutWith, VisitWith,
1111
};
1212

13+
pub use self::eval::contains_eval;
1314
#[cfg(feature = "concurrent-renamer")]
1415
use self::renamer_concurrent::{Send, Sync};
1516
#[cfg(not(feature = "concurrent-renamer"))]
1617
use self::renamer_single::{Send, Sync};
1718
use self::{
1819
analyzer::Analyzer,
1920
collector::{collect_decls, CustomBindingCollector, IdCollector},
20-
eval::contains_eval,
2121
ops::Operator,
2222
};
2323
use crate::hygiene::Config;

0 commit comments

Comments
 (0)