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 properties are lowered when combine with static properties #2416

Closed
bakura10 opened this issue Jul 25, 2022 · 5 comments · Fixed by #3167
Closed

Private properties are lowered when combine with static properties #2416

bakura10 opened this issue Jul 25, 2022 · 5 comments · Fixed by #3167

Comments

@bakura10
Copy link

bakura10 commented Jul 25, 2022

Hi,

I have initially reported that in #2158 but I thought it may be useful to report it as an individual issue as the original issue is a bit different.

As of today, even when setting esnext as a target, esbuild improperly lower private syntax when one class contains a static method and bundling is enabled.

For instance, this:

export class Test {
  #foo = 5;
}

is properly transformed to:

// js/foo.js
var Test = class {
  #foo = 5;
};

export {
  Test
};

However, as soon as a static property enter into the game, everything is lowered, although esnext should support both syntax natively:

export class Test {
  static bar = 1;
  #foo = 5;
}

Is transformed to:

var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
var __privateAdd = (obj, member, value) => {
  if (member.has(obj))
    throw TypeError("Cannot add the same private member more than once");
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};

// js/foo.js
var _foo;
var Test = class {
  constructor() {
    __privateAdd(this, _foo, 5);
  }
};
_foo = new WeakMap();
__publicField(Test, "bar", 1);
export {
  Test
};
@hyrious
Copy link

hyrious commented Jul 25, 2022

Reproduction Link, it only occurs when bundle: true.

@evanw
Copy link
Owner

evanw commented Jul 25, 2022

Yes, that's true. This is likely not a bug but a limitation of esbuild's single-pass design. See this comment for an example.

@bakura10
Copy link
Author

Is there any workaround for this ? Maybe could we pass an option to instruct Esbuild we are not using static field with new ?

@guiled
Copy link

guiled commented Sep 11, 2022

I've just encountered this strange behaviour. Why doing var Test = class {} ? (as class Test{}can be hidden within a (function () {})() ? Why calling an external __publicField() where it could be only Test.bar = 1 ?

@luwes
Copy link

luwes commented Mar 25, 2023

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.

5 participants