Skip to content

Commit

Permalink
fix(es/minifier): typeof class should be function (#9522)
Browse files Browse the repository at this point in the history
**Related issue:**

Closes #9453
  • Loading branch information
CPunisher authored Sep 3, 2024
1 parent 6bdc4f7 commit c7fdd6b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
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 {});
2 changes: 2 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9453/output.js
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");

0 comments on commit c7fdd6b

Please sign in to comment.