This rule raises an issue when an Angular lifecycle method is declared but has an empty body with no implementation.
Empty lifecycle methods in Angular components, directives, and services serve no purpose and create unnecessary code clutter. These methods are
typically created when implementing lifecycle interfaces like OnInit, OnDestroy, or AfterViewInit, but then
left without any implementation.
Empty lifecycle methods can:
Angular will call these methods during the component lifecycle even when they’re empty, which means they consume processing time without providing any value.
While empty lifecycle methods don’t cause runtime errors, they negatively impact code maintainability and can slightly affect performance due to unnecessary method calls during Angular’s lifecycle execution.
Remove the empty lifecycle method entirely. If you don’t need to implement the lifecycle hook, simply don’t declare the method.
@Component()
class MyComponent implements OnInit {
ngOnInit() {} // Noncompliant
}
@Component()
class MyComponent {
// No ngOnInit method needed if there's no implementation
}
If you plan to implement the method later, add a TODO comment or implement the actual logic immediately.
@Component()
class MyComponent implements OnDestroy {
ngOnDestroy() {} // Noncompliant
}
@Component()
class MyComponent implements OnDestroy {
ngOnDestroy() {
// Clean up subscriptions
this.subscription.unsubscribe();
}
}