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

private class fields don't work with compatibility-mode=extended #2887

Closed
mstoykov opened this issue Jan 31, 2023 · 1 comment · Fixed by #3456
Closed

private class fields don't work with compatibility-mode=extended #2887

mstoykov opened this issue Jan 31, 2023 · 1 comment · Fixed by #3456

Comments

@mstoykov
Copy link
Contributor

mstoykov commented Jan 31, 2023

Brief summary

private class fields are supported by goja but not by the internal babel. Which means that if babel needs to be used (at this point only for ESM support) it will give you an error on the private field

class Value {
  #internal_private_value;

  constructor(value) {
    this.#internal_private_value = value;
  }

  get value() { return this.#internal_private_value; }
}

const test_object = new Value(5);
console.log(test_object.value);
export default function() { }

will error out with

: Unexpected character '#' (2:2)
  1 | class Value {
> 2 |   #internal_private_value;
    |   ^
  3 |
  4 |   constructor(value) {
  5 |     this.#internal_private_value = value; at <internal/k6/compiler/lib/babel.min.js>:2:28536(100)

But if you use exports.default = function () {} at the end it will work as normal

k6 version

v0.42.0

OS

all

Docker version and image (if applicable)

No response

Steps to reproduce the problem

Run

class Value {
  #internal_private_value;

  constructor(value) {
    this.#internal_private_value = value;
  }

  get value() { return this.#internal_private_value; }
}

const test_object = new Value(5);
console.log(test_object.value);
export default function() { }

Expected behaviour

It works.

Actual behaviour

babel gives you an error

From this community forum post

@mstoykov
Copy link
Contributor Author

The fix for this as with other cases where babel gives error but goja will handle the code is to not use babel which at this point is again only used for ESM support.

This hopefully will be picked up in the next release(s) as the major blockage of lack of async/await support is gone.

@mstoykov mstoykov linked a pull request Jul 8, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant