swc-project / swc

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

Ternaries with `-0 : 0 ` are removed by compress conditionals #7111

Closed frec-kenneth closed 1 year ago

frec-kenneth commented 1 year ago

Describe the bug

We use decimal.js on our site to manage numbers. It's a popular library.

We found that when we upgraded Next to v13, we saw more negative zeros in production.

Input code

console.log(Math.random()>0.5?-0:0)

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2022",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": false,
        "booleans": false,
        "booleans_as_integers": false,
        "collapse_vars": false,
        "comparisons": false,
        "computed_props": false,
        "conditionals": true,
        "dead_code": false,
        "directives": false,
        "drop_console": false,
        "drop_debugger": false,
        "evaluate": false,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": false,
        "hoist_vars": false,
        "if_return": false,
        "join_vars": false,
        "keep_classnames": false,
        "keep_fargs": false,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": false,
        "negate_iife": false,
        "properties": false,
        "reduce_funcs": false,
        "reduce_vars": false,
        "side_effects": false,
        "switches": false,
        "typeofs": false,
        "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": false,
        "const_to_let": false,
        "pristine_globals": false
      },
      "mangle": false
    }
  },
  "module": {
    "type": "systemjs"
  },
  "minify": false,
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.3.41&code=H4sIAAAAAAAAA0vOzyvOz0nVy8lP1%2FBNLMnQK0rMS8nP1dC0M9Aztdc1sDLQBADE0KwtIwAAAA%3D%3D&config=H4sIAAAAAAAAA32UO5LbMAyGe5%2FCozpFxkWKPUC6nIFDi6BMhyQ0BOi1Z8d3D%2FTwIwmoTsIHEMQPEF%2B7%2Fb47U9997L%2FkU35GWwjK818sdMtsr2LpoE%2BW%2BhJG7r496Jkm5G0kmE33hXRsywA8R9Hh%2B%2BGwRnQRkeARsdpSyMHf3nP2mMYCRG82scqRNUFm%2Bjt%2BZQU%2FNXBEjGDzFjKWTMgMAxTNrccY7UhgLrbBk6gWCNUkE60MzowFR90hu8ABs9gFc6nv1IF1pkcHSqQLBXoOF9COdZJOAjNJjS3s4FiHYW73vxwuNlbLWihc597IjRV4wkBsfFW1WGBLiIU2RA7eFOBatJxnDLkV9xtAZIiWKNukCjV7eJmtNtwMDdnL%2FPJN4TLsaqUZBpHWhOA1fSd9oHBQUxZwtYdJ4H4DN8Sg4MCA9zI1Kv4M3J%2FUtHwbAb1GpNNWLWMBpvkwVz69jw38UwplfdRWj2T51KZ0S0eMGwkS8AndhoO0g7GNi6yN69jmNTuQAQGnulRSwfRu2TCaOO%2FQ%2FydEXoqcaYaIR%2FuqbvW4P7dzsnl4Pf9lQe9Why6hqzNcV%2F%2FU42Vh%2F%2BheTo%2Fd%2FLxDF%2BjXI3JaVrv7Hw2oLXNGBgAA

Expected behavior

console.log(Math.random() > 0.5 ? -0 : 0);

Actual behavior

console.log(-0)

Version

1.3.41

Additional context

While 0 === -0 is true in JavaScript, they are different, and this condition should not be removed.

frec-kenneth commented 1 year ago

Wow, that was fast! Thanks!

swc-bot commented 1 year 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.