Skip to content
New issue

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

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

Already on GitHub? # to your account

[Bug]: Re-assignment to class during static field initialization #6307

Closed
p51lee opened this issue Nov 1, 2022 · 7 comments · Fixed by #6362
Closed

[Bug]: Re-assignment to class during static field initialization #6307

p51lee opened this issue Nov 1, 2022 · 7 comments · Fixed by #6362
Labels
Milestone

Comments

@p51lee
Copy link

p51lee commented Nov 1, 2022

Describe the bug

Mutable assignment to class name should throw TypeError during an initialization of static fields.
However, the transpiled code terminates without any error.

Input code

class x { static y = x = 0 ; }

Config

No response

Playground link

https://play.swc.rs/?version=1.3.11&code=H4sIAAAAAAAAA0vOSSwuVqhQqFYoLkksyUxWqFSwBXJtFQwUrBVqFQB8P0%2FWHwAAAA%3D%3D&config=H4sIAAAAAAAAA0WMTQrEIAxG75K1286id5hDBCctFv9IUhgR714tlu7C915ehUMsrBUyshCPS0pU%2FMMKZAOKZZcVTNf6tKEXagYUeScdiiyd%2BZSEJjUQXHRbGSWbQmYSeRHG3T9m66GQfucYKmjJdAc%2F0N7G%2FHPynaLySe0Cj9ke9LUAAAA%3D

Expected behavior

Mutable assignment to class name should throw TypeError during an initialization of static fields:

$ node input.js
input.js:1
class x { static y = x = 0 ; }
                       ^

TypeError: Assignment to constant variable.
    at <static_initializer> (/Users/p51lee/dev/esmeta-plrg/input.js:1:24)
    at Object.<anonymous> (/Users/p51lee/dev/esmeta-plrg/input.js:1:1)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.11.0

Actual behavior

The output code terminates without any error.

$ node output.js
// terminates without error

Version

1.3.11 (default SWC playground)

Additional context

No response

@p51lee p51lee added the C-bug label Nov 1, 2022
@kdy1 kdy1 added this to the Planned milestone Nov 2, 2022
@p51lee
Copy link
Author

p51lee commented Nov 2, 2022

I have also submitted a bug report to babel: babel/babel#15000

@Austaras
Copy link
Member

Austaras commented Nov 3, 2022

I believe swc should report error in this case -- swc reject const reassignment, although it is valid JS. Or we could drop const reassignment lint, that would also make swc consistent.

@kdy1
Copy link
Member

kdy1 commented Nov 3, 2022

I think we should catch this too.

We should move hard errors to compile time as much as possible

@Austaras
Copy link
Member

Austaras commented Nov 3, 2022

I'm a bit cautious about this -- const reassignment is not "hard error", it's runtime error

try {
    const a = 1
    a = 2
} catch (e) {
    console.log(e)
}

But I don't believe anyone would rely on this.

@kdy1
Copy link
Member

kdy1 commented Nov 3, 2022

Oh, you are right... But same here. I don't expect people to rely on it

@swc-bot
Copy link
Collaborator

swc-bot commented Dec 12, 2022

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 Dec 12, 2022
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Development

Successfully merging a pull request may close this issue.

5 participants