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

feat(es/minifier): Implement optional catch binding #9657

Merged
merged 3 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2876,6 +2876,21 @@ impl VisitMut for Optimizer<'_> {
n.finalizer.visit_mut_with(self);
}

fn visit_mut_catch_clause(&mut self, n: &mut CatchClause) {
n.visit_mut_children_with(self);

if self.options.ecma < EsVersion::Es2019 || !self.options.unused {
return;
}

if let Some(param) = &mut n.param {
self.take_pat_if_unused(param, None, false);
if param.is_invalid() {
n.param = None;
}
}
}

#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
fn visit_mut_unary_expr(&mut self, n: &mut UnaryExpr) {
let ctx = Ctx {
Expand Down
1 change: 0 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ impl Optimizer<'_> {
&& v.usage_count == 0
&& !v.reassigned
&& v.property_mutation_count == 0
&& !v.declared_as_catch_param
Austaras marked this conversation as resolved.
Show resolved Hide resolved
{
self.changed = true;
report_change!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function f1() {
return 1;
} catch (ex) {
return 2;
} finally {
} finally{
return 3;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ var a = 1;
function f() {
try {
x();
} catch (a) {
var a;
}
} catch (a) {}
}
f();
console.log(a);
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ var a = 1;
!function() {
try {
x();
} catch (a) {
var a;
}
} catch (a) {}
}();
console.log(a);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var a = "FAIL";
(function () {
(function() {
try {
throw 1;
} catch (o) {
Expand Down
Loading