Skip to content

Commit c7fdd6b

Browse files
authored
fix(es/minifier): typeof class should be function (#9522)
**Related issue:** Closes #9453
1 parent 6bdc4f7 commit c7fdd6b

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

.changeset/six-hounds-hammer.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_minifier: patch
4+
---
5+
6+
fix(es/minifier): typeof class should be function rather than object

crates/swc_ecma_minifier/src/compress/optimize/inline.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl Optimizer<'_> {
156156

157157
if !usage.reassigned {
158158
match init {
159-
Expr::Fn(..) | Expr::Arrow(..) => {
159+
Expr::Fn(..) | Expr::Arrow(..) | Expr::Class(..) => {
160160
self.typeofs.insert(ident.to_id(), "function".into());
161161
}
162162
Expr::Array(..) | Expr::Object(..) => {
@@ -569,12 +569,9 @@ impl Optimizer<'_> {
569569
if !usage.reassigned {
570570
trace_op!("typeofs: Storing typeof `{}{:?}`", i.sym, i.ctxt);
571571
match &*decl {
572-
Decl::Fn(..) => {
572+
Decl::Fn(..) | Decl::Class(..) => {
573573
self.typeofs.insert(i.to_id(), "function".into());
574574
}
575-
Decl::Class(..) => {
576-
self.typeofs.insert(i.to_id(), "object".into());
577-
}
578575
_ => {}
579576
}
580577
}

crates/swc_ecma_minifier/src/compress/optimize/ops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl Optimizer<'_> {
370370
}
371371
}
372372

373-
Expr::Arrow(..) | Expr::Fn(..) => {
373+
Expr::Arrow(..) | Expr::Fn(..) | Expr::Class(..) => {
374374
report_change!("Converting typeof to 'function' as we know the value");
375375
self.changed = true;
376376
*e = Lit::Str(Str {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
class x {}
3+
const y = x;
4+
const z = class {};
5+
console.log(typeof x);
6+
console.log(typeof y);
7+
console.log(typeof z);
8+
console.log(typeof class {});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
console.log("function"), console.log("function"), console.log("function"), console.log("function");

0 commit comments

Comments
 (0)