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): typeof class should be function #9522

Merged
merged 5 commits into from
Sep 3, 2024
Merged
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
6 changes: 6 additions & 0 deletions .changeset/six-hounds-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_minifier: patch
---

fix(es/minifier): typeof class should be function rather than object
7 changes: 2 additions & 5 deletions crates/swc_ecma_minifier/src/compress/optimize/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl Optimizer<'_> {

if !usage.reassigned {
match init {
Expr::Fn(..) | Expr::Arrow(..) => {
Expr::Fn(..) | Expr::Arrow(..) | Expr::Class(..) => {
self.typeofs.insert(ident.to_id(), "function".into());
}
Expr::Array(..) | Expr::Object(..) => {
Expand Down Expand Up @@ -569,12 +569,9 @@ impl Optimizer<'_> {
if !usage.reassigned {
trace_op!("typeofs: Storing typeof `{}{:?}`", i.sym, i.ctxt);
match &*decl {
Decl::Fn(..) => {
Decl::Fn(..) | Decl::Class(..) => {
self.typeofs.insert(i.to_id(), "function".into());
}
Decl::Class(..) => {
self.typeofs.insert(i.to_id(), "object".into());
}
_ => {}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl Optimizer<'_> {
}
}

Expr::Arrow(..) | Expr::Fn(..) => {
Expr::Arrow(..) | Expr::Fn(..) | Expr::Class(..) => {
report_change!("Converting typeof to 'function' as we know the value");
self.changed = true;
*e = Lit::Str(Str {
Expand Down
8 changes: 8 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9453/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";
class x {}
const y = x;
const z = class {};
console.log(typeof x);
console.log(typeof y);
console.log(typeof z);
console.log(typeof class {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
console.log("function"), console.log("function"), console.log("function"), console.log("function");
Loading