swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.22k stars 1.23k forks source link

minifier: `typeof <class>` should be evaluated as `"function"`, not `"object"` #9453

Closed hyp3rflow closed 2 months ago

hyp3rflow commented 2 months ago

Describe the bug

typeof <class> should be evaluated as a "function" because classes have [[Call]] internal slot like a function. MDN reference also noted this behavior.

Input code

"use strict";
class x {}
console.log(typeof x);

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2022",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": true,
        "booleans": true,
        "booleans_as_integers": false,
        "collapse_vars": true,
        "comparisons": true,
        "computed_props": true,
        "conditionals": true,
        "dead_code": true,
        "directives": true,
        "drop_console": false,
        "drop_debugger": true,
        "evaluate": true,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": true,
        "hoist_vars": false,
        "if_return": true,
        "join_vars": true,
        "keep_classnames": false,
        "keep_fargs": true,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": true,
        "negate_iife": true,
        "properties": true,
        "reduce_funcs": false,
        "reduce_vars": false,
        "side_effects": true,
        "switches": true,
        "typeofs": true,
        "unsafe": false,
        "unsafe_arrows": false,
        "unsafe_comps": false,
        "unsafe_Function": false,
        "unsafe_math": false,
        "unsafe_symbols": false,
        "unsafe_methods": false,
        "unsafe_proto": false,
        "unsafe_regexp": false,
        "unsafe_undefined": false,
        "unused": true,
        "const_to_let": true,
        "pristine_globals": true
      },
      "mangle": false
    },
    "externalHelpers": true
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": false
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.7.12&code=H4sIAAAAAAAAA1MqLU5VKC4pykwuUbLmSs5JLC5WqFCoruVKzs8rzs9J1cvJT9coqSxIzU9TqNC0BgDpvrE7LwAAAA%3D%3D&config=H4sIAAAAAAAAA32VO5LbMAyG%2B5zCozpFxkWKHCCzTc7AoUlQppcPDQF6rdnx3QNR8mPXkDoLH36ABED488du153QdH92n%2FyTPwZdEMr9my04JtIXtnRgokZT%2FEDdzxs94YScDgjNdJ1JR7r0QE2F%2B1%2F7%2FaLoQs4IN8Viiz55Nz7nNDkOBRCfbGzlkDVCIvyqX1jJHxOgUp%2Fth5wD6LRBlEblE0EPRQpscgh6QFBnXYQo00l18ZilFBOsBFYNJQ8iT9aTz4lzvlIL2iqTLQjIFzDkzyDJOBfLEvL1hPs0bOFQ%2B771%2BZsazjpUTUJOuLSW8GmFqMfskZSrSSrhDFdqMMOluN%2BV3qkCVEt61Z2yTys9eQfgCgSNmHQEKW7zcDxPa2q3qfTJ8cjSKHCeb%2BmWCXouqvLeCZWdKgOFvNTNArYamCprpOMseKV86C0ocI5nRQiNH57MUUpK4wDZCYD7q500VTNQ91e4wqcHsYH%2F8i1JHrDFI2o6rlMc4yGHjQQR6JjthgO3gvI6LrwlLsM6r8kCjwZY0aViA69LgB8AZRXavnyZDX4eHFH1IR8ea2JxuN73cNSpf7z3L6sYLgSFV8wbhAHKU4Tm0MVsa1Mu%2FwBT8%2Be9%2Fbt7ON1W9P1Wncd%2FN%2BWc8%2FofsXtHPU4GAAA%3D

SWC Info output

No response

Expected behavior

class x {}
console.log("function");

Actual behavior

class x {}
console.log("object");

Version

1.7.12

Additional context

No response

mischnic commented 2 months ago

https://github.com/swc-project/swc/blob/f95cdab3a323fcb72f197fc5529821933434a2d2/crates/swc_ecma_minifier/src/compress/optimize/inline.rs#L575-L577

swc-bot commented 1 month ago

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.