A return with a value in a constructor replaces or drops the new instance
What it finds
constructor() { return { id: 1 }; }: an object returned from a constructor replaces the instance that new created, so the fields and methods of the class are lost, and a primitive returned from one is ignored. Either way the return does not do what it seems to.
return; and return this; are left alone. A constructor that returns another object on purpose, as a cached instance or a proxy, is a known pattern, which is why the rule is strict.
From the rule's source, instance_replaced_by_return.rs.
class Point { x = 0; constructor(x: number) { if (x < 0) { return { x: 0 };tx0 reports: this constructor returns a value } if (x === 0) { return; } this.x = x; const scale = () => { return x * 2; }; return this; }} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/ts:instance-replaced-by-return" = "warning"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:instance-replaced-by-return] reason="..."tx0 explain tx0/ts:instance-replaced-by-return shows the same in the terminal. Found a false positive? Report it.