Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrongly transform function Infinity to 1/0 #8465

Closed
hardfist opened this issue Jan 1, 2024 · 5 comments · Fixed by #8471
Closed

wrongly transform function Infinity to 1/0 #8465

hardfist opened this issue Jan 1, 2024 · 5 comments · Fixed by #8471
Assignees
Labels
Milestone

Comments

@hardfist
Copy link
Contributor

hardfist commented Jan 1, 2024

Describe the bug

Infinity is wrongly transformed to 1/0

Input code

function Infinity(){
    console.log('xxx');
}
export default Infinity;

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es5",
    "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
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.100&code=H4sIAAAAAAAAA0srzUsuyczPU%2FDMS8vMyyyp1NCs5lIAguT8vOL8nFS9nPx0DfWKigp1TWuuWq7UioL8ohKFlNS0xNKcErgmawCsssEKRwAAAA%3D%3D&config=H4sIAAAAAAAAA32US5LjIAyG932KlNe9nVnMAXo3Z6AICIc0Ri4k0nF15e4jY%2BcxHTk7W59%2BCSSh77fdrjuS6%2F7svuVTfkZbCMrtXyw0ZbZnsXTgBkuuxJG79ys90oyCTQTNdFlIx7b0wE1Fv1b3LiESXN1X2xBzDNNjQofDWIDowSZWiVcHyEz%2F61dW8GsGXOqjfY%2BYwOYXxFgyMTP0ULTADlOyI4E52aJEmU9qSyTUUsywMngzFhxVnn3kiFlyPlMP1huHHhQUCziOJ9BkkktkmeR6yn0a9rCvfd%2Ba%2FEMNJ5uqZSUnnFtL5LRK1ANGYhNq1kq4wI0aLHAt7k9lDKYA15KfdUeMeaMnnwBSgWSJsh1Ai9s8gszTljq8VMYcZGR5UrjMt3bLDL0U1cQYlMrOlYHCUetmAV8dzJV12nFWvFE%2Bih4MhCCzooSmr8juoCXlaQQMCpD%2B2qBN1QLM7RVu8PlBvMAfckvWB2z1GCwftilNwx7TiwQD8AH9CwdpBeM2LrIlzuM2r9mDjAZ41aVSA89LQB4Ao0ltWT7NhjwPiWj6hPv7mlgdLrclPNjc39%2F7soffVoduQF8bXDf83N9lL%2F%2Fu7k7XLXw7eBfp71XZkl7%2BAVPL6HotBgAA

SWC Info output

1.3.100

Expected behavior

don't turn to 1/0 like terser

function o(){console.log("xxx")}export default o;

Actual behavior

export default 1 / 0;

Version

1.3.100

Additional context

No response

@Austaras
Copy link
Member

Austaras commented Jan 1, 2024

Declare unwritable and unconfigurable global variable in top level is a TypeError under strict mode which is implied by es module. I don't believe there's a "standard" way to deal with immediate runtime error, just like we ignore TDZ.

@magic-akari
Copy link
Member

@magic-akari magic-akari self-assigned this Jan 1, 2024
@Austaras
Copy link
Member

Austaras commented Jan 1, 2024

#8336 is easily fixable -- just don't rename top level declaration. But this issue is not -- the correct semantic, ignoring that error, is 1/0.

@Austaras
Copy link
Member

Austaras commented Jan 1, 2024

I was wrong. ESM top level var is not in global context, so they can safely shadow undefined/Infinity/NaN.

@kdy1 kdy1 closed this as completed in #8471 Jan 4, 2024
kdy1 pushed a commit that referenced this issue Jan 4, 2024
…` correctly (#8471)

**Description:**

For following code
```js
var NaN = 1
console.log(NaN)
```
Result would be

|Envirnoment|Result|
|-|-|
|Non strict script(browser, nodejs repl)|NaN|
|Non strict script(nodejs script)|1|
|Strict script(browser, nodejs repl)|runtime error|
|Strict script(nodejs script)|1|
|ESM|1|

So SWC choose to behave like browser in script mode and confirm to esm
standard.


**Related issue:**

 - Closes #8465
@kdy1 kdy1 modified the milestones: Planned, v1.3.103 Jan 15, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented Feb 14, 2024

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.

@swc-project swc-project locked as resolved and limited conversation to collaborators Feb 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.