Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Nov 22, 2023
1 parent e8625c1 commit 6bba847
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
10 changes: 9 additions & 1 deletion crates/swc_ecma_minifier/src/compress/pure/dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use swc_ecma_utils::{extract_var_ids, ExprExt, StmtExt, StmtLike, Value};
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};

use super::Pure;
use crate::{compress::util::is_fine_for_if_cons, maybe_par, util::ModuleItemExt};
use crate::{
compress::util::is_fine_for_if_cons,
maybe_par,
util::{contains_await_or_yield, ModuleItemExt},
};

/// Methods related to option `dead_code`.
impl Pure<'_> {
Expand Down Expand Up @@ -285,6 +289,10 @@ impl Pure<'_> {
_ => return,
};

if contains_await_or_yield(last) {
return;
}

fn drop<T: StmtLike>(stmt: &mut T, last: &Stmt, need_break: bool) -> bool {
match stmt.as_stmt_mut() {
Some(s) if s.eq_ignore_span(last) => {
Expand Down
44 changes: 44 additions & 0 deletions crates/swc_ecma_minifier/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,50 @@ impl Visit for LeapFinder {
}
}

#[allow(unused)]
pub(crate) fn contains_await_or_yield<N>(n: &N) -> bool
where
N: VisitWith<YieldOrAwaitFinder>,
{
let mut v = YieldOrAwaitFinder::default();
n.visit_with(&mut v);
v.found_yield || v.found_await
}

#[derive(Default)]
pub(crate) struct YieldOrAwaitFinder {
found_await: bool,
found_yield: bool,
}

impl Visit for YieldOrAwaitFinder {
noop_visit_type!();

fn visit_arrow_expr(&mut self, _: &ArrowExpr) {}

fn visit_await_expr(&mut self, n: &AwaitExpr) {
n.visit_children_with(self);

self.found_await = true;
}

fn visit_class_method(&mut self, _: &ClassMethod) {}

fn visit_constructor(&mut self, _: &Constructor) {}

fn visit_function(&mut self, _: &Function) {}

fn visit_getter_prop(&mut self, _: &GetterProp) {}

fn visit_setter_prop(&mut self, _: &SetterProp) {}

fn visit_yield_expr(&mut self, n: &YieldExpr) {
n.visit_children_with(self);

self.found_yield = true;
}
}

/// This method returns true only if `T` is `var`. (Not `const` or `let`)
pub(crate) fn is_hoisted_var_decl_without_init<T>(t: &T) -> bool
where
Expand Down

0 comments on commit 6bba847

Please sign in to comment.