You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a return value of a function is assigned to a let variable, and that variable is used behind a logical assignment, compression tries to remove that intermediate variable and move the function call directly after the logical assignment.
This changes the logic such that before compilation the function would have always been called, but after compilation it's moved behind the logical assignment and will be called conditionally.
Since the function can have a side-effect that should always run, this is incorrect behavior.
Input code
constmyArr=[];// function with side effectfunctionfoo(arr){arr.push('foo');return'foo';}leta;if(Math.random()>0.5){a=true;}// the function call below should always run// regardless of whether `a` is `undefined`letb=foo(myArr);// const seems to keep this line here instead of// moving it behind the logitcal nullish assignment// const b = foo(myArr);a??=b;console.log(a);console.log(myArr);
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.
Describe the bug
When a return value of a function is assigned to a
let
variable, and that variable is used behind a logical assignment, compression tries to remove that intermediate variable and move the function call directly after the logical assignment.This changes the logic such that before compilation the function would have always been called, but after compilation it's moved behind the logical assignment and will be called conditionally.
Since the function can have a side-effect that should always run, this is incorrect behavior.
Input code
Config
Playground link (or link to the minimal reproduction)
https://play.swc.rs/?version=1.3.74&code=H4sIAAAAAAAAA2WQTU7DMBCF9z7F2zXZpGxYRaXiAJygQsoknsQWjl35h6hC3J1xJFokvBrPzHvvs6fgU8Z6e40RJ1zee3U8Yi5%2ByjZ4bDYbJKtZ3VtzCA3F2OJLQY6U3bUk0xxkcGj7vRk5l%2Bixt3r1rRxnUK%2BUndG8UTZdJK%2FD2rR4wVP3fPcSghwLV0nFyIYfKBM5h5Fd2JBMKE6D3Ea3hFh8XY68UNSOU0KYsRkWdcRAA2zCULzm2XrWww4zSlJ9yP5uga4G0%2F4TiXlNyAEfzFchELETHcSMYWWDSUtAFazh0%2FoFVuzYWK93XhcWm4UVvjhnkwGlZBe%2Fss%2BPkH%2FxhPP5hFGquhAcd%2BLTkIz%2B3n%2FXfwDJh74ktAEAAA%3D%3D&config=H4sIAAAAAAAAA32UO3LjMAyGe5%2FCozrFjostcoDtcgYOTYIyvXxoCNCxJuO7L0TJjjeG1En48AMkAOJrt993ZzTd%2B%2F6LP%2Fln0AWhPP7ZgmMifWVLByZqNMUP1L3d6Rkn5HRAaKbbTDrSpQdqKjz8OhwWRRdyRrgrFlv0ybvxOafJcSiA%2BGRjK4esERLh%2F%2FqFlfw5ASr12X7MOYBOG0RpVD4R9FCkwCaHoAcEddFFiDKdVBePWUoxwUpg1VDyIPJkPfmcOOcrtaCtMtmCgHwBQ%2F4CkoxzsSwhX0%2B4T8MWjrXvW59%2FqOGiQ9Uk5IRrawmfVoh6yh5JuZqkEs5wpQYzXIr7U%2BmdKkC1pFfdOfu00pO%2FAFyBoBGTjiDFbR6O52lN7TaVPjkeWRoFzvMt3TJBz0VV3juhslNloJCXulnAVgNTZY10nAWvlA%2B9BQXO8awIofHTkzlJSWkcIDsBcH%2B1k6ZqBurxClf49CA28B%2B%2BJckDtnhETad1imM85rCRIAKdst1w4FZQXseFt8R1WOc1WeDRACu6VGzgdQnwA6CsQtuXL7PBz4Mjqj7ko7gmBp7z1sTDYrs9lnPUqf9eAvN%2B3i0OXcy2Nrhs%2Fqnp877%2B3X073Vfz4zadx4%2B7cjrJ7vYPdLfRTEUGAAA%3D
SWC Info output
No response
Expected behavior
In above input, the output should keep
b = foo(myArr)
like how it does when it's declared withconst
instead oflet
.Actual behavior
No response
Version
1.3.74
Additional context
No response
The text was updated successfully, but these errors were encountered: