Skip to content

this isn’t passed into async methods #10374

@thorn0

Description

@thorn0

Describe the bug

This might be pretty niche, but I need to always have access to this inside my custom _async_to_generator. In the project I'm working on, methods of different objects use different promise implementations, so I'm doing something like const Promise = this.Promise || globalThis.Promise in _async_to_generator. However, this doesn't work because this isn’t passed into async methods.

I wanted to find another reason to question the current behavior, so I came up with this example using eval.

Input code

class Foo {
    async bar() {
        eval('console.log(this)');
    }
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2015",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.11.21&code=H4sIAAAAAAAAA0vOSSwuVnDLz1eo5uVSAILE4sq8ZIWkxCINTZgQCKSWJeZoqCfn5xXn56Tq5eSna5RkZBZrqmtaQ9TU8nIBEQDs35CqTgAAAA%3D%3D&config=H4sIAAAAAAAAA1VPOw6DMAzdc4rIc4e2Ujv0Dj1ElBoUlJ9iIxUh7k4ICS2b38%2FPnoWUMJCGl5zzmEFUiTAdODM0eVbfzABqp0gnExkuTR1okzplCQu17AqwSj1ySdH9envUBNgQCFuics54003%2FnTq4mJDobNysyvcWz42itoILn7GI9ReeIu4XPOFnamXHYjD0bklOI4plBQmBQtEXAQAA

SWC Info output

No response

Expected behavior

this should be correctly set in async methods.

Actual behavior

this isn't passed.

Version

1.11.21

Additional context

Noticed also another bug with eval.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions