constructor-super

Require super() to be called exactly once in the constructor of a derived class

error by default

In a class with extends, the constructor must call super() before it uses this or returns. Without the call, new throws a ReferenceError. A second call throws too. These errors only show up when that path through the constructor runs.

The rule follows every path through the constructor. It reports a path that has no super(), a path that calls it twice, and a super() in a class that extends something that can't be a constructor, such as null.

This rule is on by default in JavaScript files. It's off in TypeScript files, where TypeScript already reports a missing super().

Configure#

On by default as an error. To turn it off:

bunfig.toml
[lint.rules]
"constructor-super" = "off" # or "warn"

Examples#

example.js1 problem
class Admin extends User {
  constructor(name) {
    this.name = name;
  }
}
example.js1 problem
class Admin extends User {
  constructor(name) {
    if (name) {
      super(name);
    }
  }
}
example.js1 problem
class Admin extends User {
  constructor(name) {
    super(name);
    super(name.trim());
  }
}
example.js1 problem
class Dictionary extends null {
  constructor() {
    super();
  }
}
example.jsNo problems
class Admin extends User {
  constructor(name) {
    super(name);
    this.role = "admin";
  }
}

class Guest extends User {
  constructor(name) {
    if (name) {
      super(name);
    } else {
      super("Guest");
    }
  }
}

// Without a constructor, `super()` is called for you.
class Member extends User {}