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

Generated constructor uses this before calling super() #242

Closed
bpierre opened this issue Jul 9, 2020 · 0 comments
Closed

Generated constructor uses this before calling super() #242

bpierre opened this issue Jul 9, 2020 · 0 comments

Comments

@bpierre
Copy link

bpierre commented Jul 9, 2020

In certain cases, esbuild can generate a constructor that will use this before super() gets called.

You can reproduce it with this TS code and --target=es2019:

export class A {}

export class B extends A {
  #e: string
  constructor(c: { d: any }) {
    super()
    this.#e = c.d ?? 'test'
  }
  f() {
    return this.#e
  }
}

Which transforms into:

var __accessCheck = (obj, member, msg) => {
  if (!member.has(obj))
    throw TypeError("Cannot " + msg);
};
var __privateGet = (obj, member, getter) => {
  __accessCheck(obj, member, "read from private field");
  return getter ? getter.call(obj) : member.get(obj);
};
var __privateSet = (obj, member, value, setter) => {
  __accessCheck(obj, member, "write to private field");
  setter ? setter.call(obj, value) : member.set(obj, value);
  return value;
};
var _e;
export class A {
}
export class B extends A {
  constructor(c) {
    _e.set(this, void 0); // ReferenceError: must call super constructor
                          // before using 'this' in derived class constructor.
    var _a;
    super();
    __privateSet(this, _e, (_a = c.d) != null ? _a : "test");
  }
  f() {
    return __privateGet(this, _e);
  }
}
_e = new WeakMap();

It seems to be a combination of using a private field, the ?? operator, and a nested value, c.d, which creates _a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant