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

Arrow functions inside of getters/setters on object literals use the wrong this #8992

Closed
KyleDavidE opened this issue May 28, 2024 · 1 comment · Fixed by #8993
Closed

Arrow functions inside of getters/setters on object literals use the wrong this #8992

KyleDavidE opened this issue May 28, 2024 · 1 comment · Fixed by #8993
Assignees
Labels
Milestone

Comments

@KyleDavidE
Copy link

KyleDavidE commented May 28, 2024

Describe the bug

Arrow functions inside of getters use the this context of the surrounding context, rather than of the getter.

Input code

const myObj = {
  get foo(){
    return () => this;
  },
};

const fn = myObj.foo;

// should be true
console.log(fn() === myObj);

Config

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

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.5.16&code=H4sIAAAAAAAAAy2MQQ6EIBAE7yT8oY%2BQbPQBhv3C%2FkEd1A3LJDAcNsa%2Fi%2BhterqrJo5Z8Pt%2Fxi8cdq2AhQSe2diWgERSUoSxcG%2FIuuXh%2Bh8vrY56aTU1hY%2BVb56uwq3oe%2BSVS5gxEiQVurccqAu8GB8vpXsoO5x3oIfBiwAAAA%3D%3D&config=H4sIAAAAAAAAA2WPSw7CMAxE95wi8potLLgDh7CCW6XKT3aQiKrenTSfAmJj2TPPHnk9KQWLaLiptbRliMhCfMxFkewTvooCpB2KZhMTnIe7yG5NaIWqtDUHEvJMqW7JpeNgQxAaeNec8WbK34E6uMgk8gvuKPrZ0n9cLeDC41nd%2FknKkVr%2BFT7QSDsug5H72Gx3tzcE4cPeFgEAAA%3D%3D

SWC Info output

No response

Expected behavior

This is bound inside of the getter

var myObj = {
    get foo () {
        var _this = this;
        return function() {
            return _this;
        };
    }
};

// ...

Actual behavior

This is bound outside of the getter

var _this = this;
var myObj = {
    get foo () {
        return function() {
            return _this;
        };
    }
};

// ...

Version

1.5.16 (repros on every version I tried, including all of 1.5.x)

Additional context

No response

@kdy1 kdy1 added this to the Planned milestone May 28, 2024
@kdy1 kdy1 self-assigned this May 28, 2024
magic-akari added a commit that referenced this issue May 31, 2024
…operties (#8993)

**Related issue:**

 - Closes #8992

---------

Co-authored-by: magic-akari <[email protected]>
@kdy1 kdy1 modified the milestones: Planned, v1.5.25 Jun 5, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented Jul 5, 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 Jul 5, 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.

3 participants