Skip to content

eslint/constructor-super Nursery

What it does

Requires super() calls in constructors of derived classes and disallows super() calls in constructors of non-derived classes.

Why is this bad?

In JavaScript, calling super() in the constructor of a derived class (a class that extends another class) is required. Failing to do so will result in a ReferenceError at runtime. Conversely, calling super() in a non-derived class is a syntax error.

Examples

Examples of incorrect code for this rule:

js
// Missing super() call
class A extends B {
  constructor() {}
}

// super() in non-derived class
class A {
  constructor() {
    super();
  }
}

// super() only in some code paths
class C extends D {
  constructor() {
    if (condition) {
      super();
    }
  }
}

Examples of correct code for this rule:

js
// Proper super() call in derived class
class A extends B {
  constructor() {
    super();
  }
}

// No super() in non-derived class
class A {
  constructor() {}
}

// super() in all code paths
class C extends D {
  constructor() {
    if (condition) {
      super();
    } else {
      super();
    }
  }
}

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny constructor-super
json
{
  "rules": {
    "constructor-super": "error"
  }
}

References

Released under the MIT License.