Skip to content

feat(eslint-plugin): add rule require-lifecycle-on-prototype #2259

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

Conversation

reduckted
Copy link
Contributor

Fixes #1002

This rule ensures that lifecycle methods are defined against the object's prototype instead of the object's instance.

Rather than limiting this rule to only check within component, directive, pipe and service (injectable) classes, the rule checks assignments everywhere. I think this is better because it will also catch cases where helper functions are used to incorrectly add lifecycle methods. For example, it will catch the problem with this code:

export function interceptCleanup(component) {
    (component as any).ngOnDestroy = () => {
        // Do some cleanup...
    };
}

Copy link

nx-cloud bot commented Feb 24, 2025

View your CI Pipeline Execution ↗ for commit 1f2bf7e.

Command Status Duration Result
nx run-many -t e2e-suite --parallel 1 ✅ Succeeded 26s View ↗
nx run-many -t test --codeCoverage ✅ Succeeded 1m 51s View ↗
nx run-many -t build,typecheck,check-rule-docs,... ✅ Succeeded 1m 25s View ↗
nx-cloud record -- pnpm nx sync:check ✅ Succeeded 4s View ↗
nx-cloud record -- pnpm format-check ✅ Succeeded 5s View ↗
nx run-many -t test ✅ Succeeded 30s View ↗
nx run-many -t build ✅ Succeeded 13s View ↗

☁️ Nx Cloud last updated this comment at 2025-02-24 09:38:40 UTC

Copy link

codecov bot commented Feb 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.61%. Comparing base (53a2afe) to head (1f2bf7e).
Report is 23 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2259      +/-   ##
==========================================
+ Coverage   90.53%   90.61%   +0.08%     
==========================================
  Files         179      181       +2     
  Lines        3559     3591      +32     
  Branches      600      605       +5     
==========================================
+ Hits         3222     3254      +32     
  Misses        183      183              
  Partials      154      154              
Flag Coverage Δ
unittest 90.61% <100.00%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/eslint-plugin/src/index.ts 67.64% <ø> (+0.48%) ⬆️
...plugin/src/rules/require-lifecycle-on-prototype.ts 100.00% <100.00%> (ø)
...ests/rules/require-lifecycle-on-prototype/cases.ts 100.00% <100.00%> (ø)

@JamesHenry JamesHenry merged commit 22b899e into angular-eslint:main Mar 2, 2025
14 checks passed
@JamesHenry
Copy link
Member

Thanks a lot!

@reduckted reduckted deleted the feature/require-lifecycle-on-prototype branch March 2, 2025 20:48
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

Successfully merging this pull request may close these issues.

New rule suggestion: angular hooks need to have access to this
2 participants