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

Incorrectly captured this when destructuring async result #8632

Closed
SarahAnnTolsma opened this issue Feb 12, 2024 · 2 comments · Fixed by #8633
Closed

Incorrectly captured this when destructuring async result #8632

SarahAnnTolsma opened this issue Feb 12, 2024 · 2 comments · Fixed by #8633
Assignees
Labels
Milestone

Comments

@SarahAnnTolsma
Copy link

Describe the bug

When destructuring the result of a call to Promise.all(), the this context is captured incorrectly. This results in a runtime error.

Input code

export class Test
{
    public async bad(): Promise<void>
    {
        let foo = false;

        [ foo ] = await Promise.all(
            [
                this.foo(),
            ]
        );
    }
    
    public async good(): Promise<void>
    {
        let [ foo ] = await Promise.all(
            [
                this.foo(),
            ]);
    }

    private async foo(): Promise<boolean>
    {
        return true;
    }
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false
    },
    "target": "es2015",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "commonjs"
  },
  "minify": false,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.4.0&code=H4sIAAAAAAAAA7WOQQ6CMBBF95xilpAYDiDqGVy4MywGGLRJZUg7RY3h7kKRGnTjxr9oOv2vk0e3lo1AqdFaOJCV6BHBkNYVWpWA9t6UUGAVJ2vYG74oS5uOVbXz1MSO0SRQM8MWatSWsig0R%2F%2BeDw1eUcm8JUWt4wB5cDGNkbOy6fA7TlaLLg9Tkvlr789v8RPzT%2Bb%2FcAxqk5ZRHQq9vDz%2F1iqYNWHzaWZInGlAjKN5V%2F8EpDjXyrABAAA%3D&config=H4sIAAAAAAAAA1VPSQ7DIAy88wrkcw9tpV76hz4CUSciYhN2pKIofw%2BkkDY3exbPeBFSwkQannIpY1miSoTp2AtC2bP6FAQ4RySdTGS4dJapUoOyhDu0fhlglUbk6kK6X2%2BP5gAbAmF3NMwZb4b8n6mDiwmJzsIqVX60eE4ULRVceM872X6pfWuDcs0FPxH8lD3xuA6GXt3OaUaxbkujvRocAQAA

SWC Info output

No response

Expected behavior

Both the good and bad functions should correctly capture this by using _this.foo() in the call to Promise.all().

Actual behavior

Only good is transpiling correctly, and bad is using this., which will be undefined at runtime due to the use of _async_to_generator.

Version

1.4.0

Additional context

This bug does not exist on 1.3.7

@SarahAnnTolsma SarahAnnTolsma changed the title Incorrectly captured this when destructuring Promise.all Incorrectly captured this when destructuring async result Feb 12, 2024
@SarahAnnTolsma
Copy link
Author

SarahAnnTolsma commented Feb 12, 2024

It looks like this also happens when not using Promise.all, so here is an updated and smaller repro:

export class Test
{
    public async bad(): Promise<void>
    {
        let foo = false;

        [ foo ] = await this.foo();
    }
    
    public async good(): Promise<void>
    {
        let [ foo ] = await this.foo();
    }

    private async foo(): Promise<boolean[]>
    {
        return [ true ];
    }
}

@kdy1 kdy1 added this to the Planned milestone Feb 13, 2024
@magic-akari magic-akari self-assigned this Feb 13, 2024
kdy1 pushed a commit that referenced this issue Feb 13, 2024
@kdy1 kdy1 modified the milestones: Planned, v1.4.1 Feb 13, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented Mar 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 Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

4 participants