swc icon indicating copy to clipboard operation
swc copied to clipboard

Generators don't maintain block scopes

Open kcirtaptrick opened this issue 3 years ago • 1 comments

Describe the bug

Block scoped variables in generators are mutated out of scope, async code within generator has unexpected behavior.

Input code

console.log([
  ...(function* () {
    for (const i of [1, 2, 3]) {
      Promise.resolve().then(() => {
        console.log(`async: ${i}`);
      });
      yield i;
    }
  })(),
]);

Config

none

Playground link

https://play.swc.rs/?version=1.2.164&code=H4sIAAAAAAAAA03NwQoCIRAG4LtP8R86jCFCdSvqGfa%2BLGyYlmAOqAXL4rtnVEu3mfk%2F%2FjEcMwerA1%2BpF4DWmtwjmuI5rkESczsCjhPINFvgwQ79RmGrsBt%2BAOgS3322OtlW%2BLQkdbnZSK3ieFoQYP4ejuc8RbPHavZ1lIevqcs0eRsu8J%2B1indEUomhgRflW74FuAAAAA%3D%3D&config=H4sIAAAAAAAAA0WMTQrEIAxG75K1286id5hDBCctFv9IUhgR714tlu7C915ehUMsrBUyshCPS0pU%2FMMKZAOKZZcVTNf6tKEXagYUeScdiiyd%2BZSEJjUQXHRbGSWbQmYSeRHG3T9m66GQfucYKmjJdAc%2F0N7G%2FHPynaLySe0Cj9ke9LUAAAA%3D

Expected behavior

[ 1, 2, 3 ]
async: 1
async: 2
async: 3

variables should maintain their scope and not be mutated by future scopes for async references

Actual behavior

[ 1, 2, 3 ]
async: 3
async: 3
async: 3

i was mutated, causing all async references to become 3

Version

1.2.164

Additional context

No response

kcirtaptrick avatar Apr 08 '22 17:04 kcirtaptrick

When will this problem be solved?

zhusiyi111 avatar Aug 05 '22 08:08 zhusiyi111

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.

github-actions[bot] avatar Oct 16 '22 00:10 github-actions[bot]

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-bot avatar Nov 15 '22 12:11 swc-bot