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
Reported:
class Admin extends User {
constructor(name) {
this.name = name;
}
}
class Admin extends User {
constructor(name) {
if (name) {
super(name);
}
}
}
class Admin extends User {
constructor(name) {
super(name);
super(name.trim());
}
}
class Dictionary extends null {
constructor() {
super();
}
}
Not reported:
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 {}