fix(es/minifier): typeof class should be function (#9522)
Some checks failed
CI / Cargo fmt (push) Has been cancelled
CI / Cargo clippy (push) Has been cancelled
CI / Check license of dependencies (push) Has been cancelled
CI / Check (macos-latest) (push) Has been cancelled
CI / Check (ubuntu-latest) (push) Has been cancelled
CI / Check (windows-latest) (push) Has been cancelled
CI / Test wasm (binding_core_wasm) (push) Has been cancelled
CI / Test wasm (binding_minifier_wasm) (push) Has been cancelled
CI / Test wasm (binding_typescript_wasm) (push) Has been cancelled
CI / List crates (push) Has been cancelled
CI / Test node bindings - ${{ matrix.os }} (macos-latest) (push) Has been cancelled
CI / Test node bindings - ${{ matrix.os }} (windows-latest) (push) Has been cancelled
CI / Test with @swc/cli (push) Has been cancelled
CI / Miri (better_scoped_tls) (push) Has been cancelled
CI / Miri (string_enum) (push) Has been cancelled
CI / Miri (swc) (push) Has been cancelled
CI / Miri (swc_bundler) (push) Has been cancelled
CI / Miri (swc_ecma_codegen) (push) Has been cancelled
CI / Miri (swc_ecma_minifier) (push) Has been cancelled
Benchmark / Bench everything (push) Has been cancelled
CI / Test - ${{ matrix.settings.crate }} - ${{ matrix.settings.os }} (push) Has been cancelled
CI / Done (push) Has been cancelled

**Related issue:**

Closes https://github.com/swc-project/swc/issues/9453
This commit is contained in:
CPunisher 2024-09-03 16:04:44 +08:00 committed by GitHub
parent 6bdc4f7e5e
commit c7fdd6b69b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 19 additions and 6 deletions

View File

@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_minifier: patch
---
fix(es/minifier): typeof class should be function rather than object

View File

@ -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(..) => {
@ -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());
}
_ => {}
}
}

View File

@ -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 {

View File

@ -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 {});

View File

@ -0,0 +1,2 @@
"use strict";
console.log("function"), console.log("function"), console.log("function"), console.log("function");