bun
bun copied to clipboard
Class constructors are created when needed
Inspired by output from #2060 (doesn't fix issue). Build output before change:
class Base {
constructor(data) {
Object.assign(this, data);
}
}
class Outer extends Base {
constructor() {
super(...arguments);
}
stuff;
things;
extra;
}
class Inner extends Base {
constructor() {
super(...arguments);
}
more;
greatness;
}
let outer = new Outer({
stuff: "Hello World",
things: 42,
extra: new Inner({
more: "Bun is becoming great!",
greatness: true
})
});
After this change:
class Base {
constructor(data) {
Object.assign(this, data);
}
}
class Outer extends Base {
stuff;
things;
extra;
}
class Inner extends Base {
more;
greatness;
}
let outer = new Outer({
stuff: "Hello World",
things: 42,
extra: new Inner({
more: "Bun is becoming great!",
greatness: true
})
});
Something like this will still create a constructor
function d1() {}
class A {
@d1 data: number = 0;
}
Build output:
import {
__decorateClass as __decorateClass_4b4920c627822e1f
} from "bun:wrap";
function d1() {
}
class A {
constructor() {
this.data = 0;
}
}
__decorateClass_4b4920c627822e1f([
d1
], A.prototype, "data", 2);
I have noticed that Node often does intentionally include "useless constructors" on subclasses as they are called in Eslint rules. I wonder if there is something we're missing there. That's just something that came to my mind when looking at this issue.
Although for this particular issue I don't think the constructor being explicit or not actually does anything as we have seen
Whatever the solution ends up being, thank you so much for putting the time into this!