A constructor uses this before super(...), omits it in a derived class or calls it without a parent
What it finds
The constructor of a class that extends another must call super(...) before it uses this and before it ends; a class that extends nothing has no parent constructor to call. Each of these mistakes throws when the class is instantiated:
- a derived constructor without any super(...) call, that neither returns nor throws; - this or super.member used in a statement before the first statement that calls super(...) directly in the constructor body; - super(...) in a class without extends, or one that extends null.
Only calls directly in the constructor body count as the first call, so code that calls super in both branches of an if is not reported for using this after it.
From the rule's source, derived_constructor_super.rs.
class Base { constructor(public id: number) {}}class Early extends Base { name: string; constructor(id: number) { this.name = "early";tx0 reports: this is used before super(...) is called super(id); }}class Missing extends Base { constructor(id: number) {tx0 reports: this constructor of a derived class never calls super(...) console.log(id); }}class Orphan { constructor() { super();tx0 reports: super(...) is called in a class that extends nothing }}class Fine extends Base { constructor(id: number) { const later = () => this.id; super(id); later(); }} Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/ts:derived-constructor-super" = "off"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/ts:derived-constructor-super] reason="..."tx0 explain tx0/ts:derived-constructor-super shows the same in the terminal. Found a false positive? Report it.