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 the constructor has a spread argument and the super call is in an if/else block. #2134

Closed
Lakuna opened this issue Mar 27, 2022 · 2 comments

Comments

@Lakuna
Copy link

Lakuna commented Mar 27, 2022

This issue is almost identical to #678.

When processing a class similar to this:

export class Foo extends Array {
	constructor(...data) {
		if (true) {
			super(...data);
		} else {
			super(...data);
		}
	}

	bar = "bar";
}

Using the following command:

esbuild test.js --target=es2015

The returned code is as follows:

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;
};
export class Foo extends Array {
  constructor(...data) {
    __publicField(this, "bar", "bar");
    if (true) {
      super(...data);
    } else {
      super(...data);
    }
  }
}

The call to __publicField(this, "bar", "bar"); occurs before either instance of super(...data);. The issue appears to occur for the same reason as #678, but only when there are multiple possible super calls.

The esbuild version is 0.14.28.

@evanw
Copy link
Owner

evanw commented Mar 28, 2022

Thanks for the report. I've been meaning to fix this for a while. TypeScript typically doesn't allow this so I never hit this myself, but of course people still want to be able to do this in JavaScript. Here's the TypeScript compile error for this:

A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.

@Lakuna Lakuna closed this as completed Mar 29, 2022
@Lakuna Lakuna reopened this Mar 29, 2022
@Lakuna
Copy link
Author

Lakuna commented Mar 29, 2022

Ignore that, sorry.

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

2 participants