bun icon indicating copy to clipboard operation
bun copied to clipboard

Class constructors are created when needed

Open dylan-conway opened this issue 2 years ago • 2 comments

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);

dylan-conway avatar Feb 13 '23 18:02 dylan-conway

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

ThatOneBro avatar Feb 13 '23 20:02 ThatOneBro

Whatever the solution ends up being, thank you so much for putting the time into this!

DavidPesta avatar Feb 13 '23 22:02 DavidPesta