Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Remove pure function used by variable initialization #9107

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/iife.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,13 @@ impl Optimizer<'_> {
return;
}

trace_op!("iife: Checking pure");

if self.has_pure(call.span) {
log_abort!("iife: Has pure mark");
return;
}

trace_op!("iife: Checking callee");

match callee {
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,8 @@ impl Optimizer<'_> {
BlockStmtOrExpr::Expr(_) => false,
},
_ => false,
} && args.is_empty() =>
} && args.is_empty()
|| e.span().has_mark(self.marks.pure) =>
{
report_change!("ignore_return_value: Dropping a pure call");
self.changed = true;
Expand Down
5 changes: 5 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ impl<'b> Optimizer<'b> {
span.has_mark(self.marks.noinline)
}

/// Check for `/*#__PURE__*/`
pub(super) fn has_pure(&self, span: Span) -> bool {
span.has_mark(self.marks.pure)
}

/// RAII guard to change context temporarically
pub(super) fn with_ctx(&mut self, mut ctx: Ctx) -> WithCtx<'_, 'b> {
let mut scope_ctxt = ctx.scope;
Expand Down
7 changes: 4 additions & 3 deletions crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use swc_common::{collections::AHashMap, SyntaxContext};
use swc_common::{collections::AHashMap, Spanned, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_utils::{find_pat_ids, ExprCtx, ExprExt, IsEmpty, StmtExt};
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
Expand Down Expand Up @@ -1294,7 +1294,7 @@ where
..self.ctx
};

if self.marks.is_some() {
if let Some(marks) = self.marks {
if let VarDeclarator {
name: Pat::Ident(id),
init: Some(init),
Expand All @@ -1306,7 +1306,8 @@ where
self.used_recursively.insert(
id.clone(),
RecursiveUsage::Var {
can_ignore: !init.may_have_side_effects(&self.expr_ctx),
can_ignore: !init.may_have_side_effects(&self.expr_ctx)
|| init.span().has_mark(marks.pure),
},
);
e.init.visit_with(&mut *self.with_ctx(ctx));
Expand Down
Loading