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() when constructor has spread in args #678

Closed
kirill-konshin opened this issue Jan 14, 2021 · 0 comments

Comments

@kirill-konshin
Copy link

kirill-konshin commented Jan 14, 2021

When processing a class that looks like this:

class A {
    constructor(opts) {
        console.log(opts);
    }
}

export class B extends A {
    version = 5;

    constructor(opts) {
        super(opts);
        this.dbName = 'xxx';
    }
}

export class C extends A {
    version = 5;

    constructor({ dbName = 'xxx', ...opts }) {
        super(opts);
        this.dbName = dbName;
    }
}

Using following command:

esbuild test.js --outfile=foo.js --target=es2015

I am getting

class A {
  constructor(opts) {
    console.log(opts);
  }
}
export class B extends A {
  constructor(opts) {
    super(opts);
    __publicField(this, "version", 5); // <-------------------------- CORRECT
    this.dbName = "xxx";
  }
}
export class C extends A {
  constructor(_a) {
    __publicField(this, "version", 5); // <-------------------------- WHY?!
    var {dbName = "xxx"} = _a, opts = __rest(_a, ["dbName"]);
    super(opts);
    this.dbName = dbName;
  }
}

As you see __publicField are called before super();, which results in Uncaught ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor.

There was a similar issue before: #242

Esbuild version is 0.8.32.

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

Successfully merging a pull request may close this issue.

1 participant