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

Cannot assign to private field with loose=true #6581

Closed
ytoune opened this issue Dec 6, 2022 · 3 comments · Fixed by #6691
Closed

Cannot assign to private field with loose=true #6581

ytoune opened this issue Dec 6, 2022 · 3 comments · Fixed by #6691
Assignees
Labels
Milestone

Comments

@ytoune
Copy link

ytoune commented Dec 6, 2022

Describe the bug

Unable to assign to private field if loose in config is set to true

Input code

class Hoge {
    #hoge: number
    constructor() {
        this.#hoge = 42
    }
}

Config

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

Playground link

https://play.swc.rs/?version=1.3.21&code=H4sIAAAAAAAAA0vOSSwuVvDIT09VqOblUgAC5Qwgx0ohrzQ3KbUIIpScn1dcUlSaXJJfpKEJUwcCJRmZxXpgDQq2CiZGEIlaXq5aAOAmk3JXAAAA&config=H4sIAAAAAAAAA0WOTQoCMQyF75L1LNSFYO%2FgIULNDJX%2BkWTAUnp321pw9%2FLey5dUeIsFUyEjC%2FFQUqLiBwxoySSWXVbYQKVbyie1rpEP0t4guV2uj576lIR%2B%2BQbBRbeXgbIpZCYRMDt6GRHGw9MaW0eF9DqHUee1ibxD%2BzPWnpPnKs4XviYz9TO2AAAA

Expected behavior

compiled code might look like below

constructor(){
  Object.defineProperty(this, _hoge, {
    writable: true,
    value: void 0
  });
  this[_hoge] = 42;
}

Actual behavior

compiled code

constructor(){
  Object.defineProperty(this, _hoge, {
    writable: true,
    value: void 0
  });
  _classPrivateFieldLooseBase(this, _hoge)[_hoge] = 42;
}

this behaves like

constructor(){
  this[_hoge] = void 0;
  this[_hoge][_hoge] = 42;
  // Uncaught TypeError: Cannot set properties of undefined (setting '__private_0__hoge')
}

Version

1.3.21

Additional context

No response

@ytoune ytoune added the C-bug label Dec 6, 2022
@kdy1 kdy1 added this to the Planned milestone Dec 6, 2022
@ytoune
Copy link
Author

ytoune commented Dec 6, 2022

this is in version 1.2.155 and above

version 1.2.154

constructor(){
  _classPrivateFieldInit(this, _hoge, {
    writable: true,
    value: void 0
  });
  _classPrivateFieldSet(this, _hoge, 42);
}

@ytoune
Copy link
Author

ytoune commented Dec 6, 2022

function _classPrivateFieldLooseBase(receiver, privateKey) {
if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
throw new TypeError("attempted to use private field on non-instance");
}
return receiver[privateKey];
}

export default function _classPrivateFieldBase(receiver, privateKey) {
if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
throw new TypeError("attempted to use private field on non-instance");
}
return receiver;
}

@kdy1 kdy1 self-assigned this Dec 20, 2022
kdy1 added a commit that referenced this issue Dec 20, 2022
@kdy1 kdy1 modified the milestones: Planned, 1.3.24 Dec 21, 2022
@swc-bot
Copy link
Collaborator

swc-bot commented Jan 20, 2023

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 Jan 20, 2023
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