fix(bundler): Do not emit useless constructor (#11668)

This commit is contained in:
dave caruso
2024-06-06 19:02:06 -07:00
committed by GitHub
parent c6187e3e3a
commit 2cba070756
2 changed files with 36 additions and 1 deletions

View File

@@ -20202,7 +20202,7 @@ fn NewParser_(
class.properties = class_properties.items;
if (instance_members.items.len > 0 or class.extends != null) {
if (instance_members.items.len > 0) {
if (constructor_function == null) {
var properties = ListManaged(Property).fromOwnedSlice(p.allocator, class.properties);
var constructor_stmts = ListManaged(Stmt).init(p.allocator);

View File

@@ -1136,6 +1136,41 @@ describe("bundler", () => {
},
},
});
itBundled("edgecase/NoUselessConstructorTS", {
files: {
"/entry.ts": `
class A {
constructor(...args) {
console.log(JSON.stringify({ args, self: this }));
}
field = 1;
}
class B extends A {}
class C extends A { field = 2 }
class D extends A { public field = 3 }
class E extends A { constructor(public y: number, a) { super(a); }; public field = 4 }
new A("arg1", "arg2");
new B("arg1", "arg2");
new C("arg1", "arg2");
new D("arg1", "arg2");
new E("arg1", "arg2");
`,
},
run: {
stdout: `
{"args":["arg1","arg2"],"self":{"field":1}}
{"args":["arg1","arg2"],"self":{"field":1}}
{"args":["arg1","arg2"],"self":{"field":1}}
{"args":["arg1","arg2"],"self":{"field":1}}
{"args":["arg2"],"self":{"field":1}}
`,
},
onAfterBundle(api) {
const content = api.readFile("out.js");
const count = content.split("constructor").length - 1;
expect(count, "should only emit two constructors: " + content).toBe(2);
},
});
// TODO(@paperdave): test every case of this. I had already tested it manually, but it may break later
const requireTranspilationListESM = [