Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule "no-life-cycle-call" does not allow for subclassing #573

Closed
jscharett opened this issue Apr 24, 2018 · 5 comments
Closed

Rule "no-life-cycle-call" does not allow for subclassing #573

jscharett opened this issue Apr 24, 2018 · 5 comments

Comments

@jscharett
Copy link

If I extend a component and I override a lifecycle hook, this rule prevents me from calling the super method. While I agree with the premise of the rule, it should, however, allow me to call the super method when overriding.

@rafaelss95
Copy link
Collaborator

IMHO, this rule should prevent this as it does right now because any parent's lifecycle should be called by Angular itself. See angular/angular#23091 and its related issues.

@mgechev
Copy link
Owner

mgechev commented Apr 24, 2018

@jscharett makes sense to me. @rafaelss95 would you want to check if the receiver of the method call isn't super?

@kevinbuhmann
Copy link
Contributor

kevinbuhmann commented Apr 24, 2018

@rafaelss95: If I understand correctly, the issue you linked to is a bug where Angular does not call a lifecycle hook that is defined in a TypeScript mixin, not a base class which is what this issue is referring to.

Angular cannot and should not call a super implementation of a lifecycle hook when it is overridden in a derived class.

Consider the following:

class BaseComponent {
  ngOnInit() {
    console.log('base component init');
  }
}

@Component({...})
class MyComponent {
  ngOnInit() {
    super.ngOnInit();
    console.log('my component init');
  }
}

The super.ngOnInit(); line should be allowed by the no-lifecycle-call rule because that is ONLY way BaseComponent#ngOnInit can be called in this case.

If you don't provide an implementation of ngOnInit in MyComponent, BaseComponent#ngOnInit would be invoked when Angular invokes MyComponent#ngOnInit simply because of how extends works. But when you override a method, the outside world (i.e Angular) cannot and should not call the super implementation.

kevinbuhmann added a commit to kevinbuhmann/codelyzer that referenced this issue Apr 24, 2018
kevinbuhmann added a commit to kevinbuhmann/codelyzer that referenced this issue Apr 24, 2018
@rafaelss95
Copy link
Collaborator

rafaelss95 commented Apr 25, 2018

It's a little bit strange because at some point it worked according to this issue.

However, I've to agree with you, it makes sense to explicitly call using super as we does for every parent class.

@kevinbuhmann
Copy link
Contributor

kevinbuhmann commented Apr 25, 2018

If both classes are decorated, it works differently. I'm not sure of the exact behavior when the base component class is decorated, though.

kevinbuhmann added a commit to kevinbuhmann/codelyzer that referenced this issue Apr 25, 2018
kevinbuhmann added a commit to kevinbuhmann/codelyzer that referenced this issue Apr 25, 2018
kevinbuhmann added a commit to kevinbuhmann/codelyzer that referenced this issue Apr 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants