silvermine/eslint-config-silvermine

TypeScript: `no-empty-function` emits error for empty constructors with parameter properties

yokuze opened this issue · 3 comments

In TypeScript classes, you can declare class-level properties by adding an access modifier to constructor arguments, like this:

class Example {
   constructor(private _prop: string) {}
}

TypeScript will automatically set this._prop = _prop for you. Therefore, you will sometimes end up with constructors that have empty bodies.

The no-empty-function rule emits an error when a constructor's body is empty, even when the constructor has parameter properties. This should not be a linting error.

This should be an easy fix by changing the rule to:

no-empty-function: [ 'error', { 'allow': [ 'constructors' ] } ];

The above rule was added for TypeScript in #23.

FWIW: I verified that this addresses the issue of throwing a linting error in the example show in my previous comment.

Ideally, there would be a TypeScript-specific rule that would check for the presence of parameter properties in empty constructors and still throw a linting error for truly empty constructors, but I think this is the best we can do without implementing a custom rule. We'll just have to be aware of this in our code reviews.