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

Calling a private optional chained field fails with TypeError is not a function #8003

Closed
1 task done
smvv opened this issue Sep 25, 2023 · 1 comment · Fixed by #8031
Closed
1 task done

Calling a private optional chained field fails with TypeError is not a function #8003

smvv opened this issue Sep 25, 2023 · 1 comment · Fixed by #8031
Assignees
Labels
Milestone

Comments

@smvv
Copy link
Contributor

smvv commented Sep 25, 2023

Describe the bug

When target is es2017 (< es2022), the optional chained private field in method search becomes undefined() causing this runtime error:

: _class_private_field_get1.call)(this)

TypeError: (intermediate value)(intermediate value)(intermediate value) is not a function

Input code

class Foo {
  #priv
  search() {
    this.#priv?.()
  }
}

console.log(new Foo().search())

Config

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

Playground link

https://play.swc.rs/?version=1.3.74&code=H4sIAAAAAAAAA0vOSSwuVnDLz1eo5lJQUC4oyiwD0sWpiUXJGRqaYEEFhZKMzGI9sJy9noYmUKiWq5aLKzk%2Frzg%2FJ1UvJz9dIy%2B1HGSIhqYeTKumAgCEDmZWWwAAAA%3D%3D&config=H4sIAAAAAAAAA1VPSQ7CMAy85xWRzxyAA0j8gUdEwa2Csil2JaKqfydpk0Jv9iye8SykhDdpeMi5jGWJKhGmfS8IZc%2FqUxDgHJF0MpHh1FmmSg3KEq7QsjHAKo3I1YV0PV%2FuzQE2BMLuaJgz3gz5P1MHFxMSHYVVqvxo8ZgoWiq48JpWsv1S%2B24NbvAT9bD9MBh6dienCcXyBZ3DefIXAQAA

SWC Info output

No response

Expected behavior

I would expect that the function is not invoked:

search() {
  var _class_private_field_get1;
  (_class_private_field_get1 = _class_private_field_get(this, _priv)) === null || _class_private_field_get1 === void 0 ? void 0 : _class_private_field_get1.call(this);
}

Related, tsc generates:

search() {
  var _a;
  (_a = __classPrivateFieldGet(this, _Foo_priv, "f")) === null || _a === void 0 ? void 0 : _a.call(this);
}

Actual behavior

The search method is transpiled as:

search() {
  var _class_private_field_get1;
  ((_class_private_field_get1 = _class_private_field_get(this, _priv)) === null || _class_private_field_get1 === void 0 ? void 0 : _class_private_field_get1.call)(this);
}

notice the _class_private_field_get1.call)(this).

Version

1.3.74

Additional context

Issue related to private optional chaining: #3610 (but that issue does not call the property's value)

@smvv smvv added the C-bug label Sep 25, 2023
@kdy1 kdy1 added this to the Planned milestone Sep 25, 2023
@kdy1 kdy1 self-assigned this Sep 25, 2023
kdy1 pushed a commit that referenced this issue Sep 29, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.91 Oct 1, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 31, 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 Oct 31, 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