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

feat(eslint-plugin): add new rule require-super-ondestroy #4611

Conversation

markoblagdan
Copy link
Contributor

@markoblagdan markoblagdan commented Dec 1, 2024

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[x] Code style update (formatting, local variables)
[x] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[x] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

No message from ESLint when overriding ngOnDestroy() in a class extending ComponentStore from @ngrx/component-store and not calling the parent's method via super.ngOnDestroy().

Closes #4505

What is the new behavior?

Running eslint or opening the relevant class file in an editor supporting ESLint shows an error message when overriding ngOnDestroy() in a class extending ComponentStore and not calling the parent's method via super.ngOnDestroy().

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Copy link

netlify bot commented Dec 1, 2024

Deploy Preview for ngrx-io ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit c0fb1d3
🔍 Latest deploy log https://app.netlify.com/sites/ngrx-io/deploys/675769cd4d979100083df5be
😎 Deploy Preview https://deploy-preview-4611--ngrx-io.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@markoblagdan
Copy link
Contributor Author

markoblagdan commented Dec 1, 2024

Maybe I should've mentioned the following in the PR under Other information, wasn't sure:

  • I do not handle the case when ComponentStore is a custom class not imported from @ngrx/component-store, which means this rule will raise a false error if one extends from a custom ComponentStore class and overrides ngOnDestroy. Seemed like a weird edge case so I did not handle it, but let me know if it would be better to do so anyway
  • considered adding a fixer function, but I did not find a good way to handle varying user and editor tab settings, so automatically writing the required line in say, the last line of the overriden method, would mess up formatting

Additional changes unrelated to #4505 (not sure if it was OK to include this):

  • added a new util function for the component-store rules, getNgrxComponentStoreNames, and used it where appropriate
  • removed deprecated method call context.getSourceCode() and replaced it with the suggested context.sourceCode property
  • removed a line in ng-add schema.ts which disables the @typescript-eslint/no-empty-interface rule

@markoblagdan markoblagdan changed the title feat(eslint-plugin): add new rule require-super-ondestroy #4505 feat(eslint-plugin): add new rule require-super-ondestroy (#4505) Dec 1, 2024
@markoblagdan markoblagdan force-pushed the feature/eslint-plugin/require-super-ondestroy branch from e7ab372 to 54dc7b8 Compare December 7, 2024 09:59
@markoblagdan markoblagdan changed the title feat(eslint-plugin): add new rule require-super-ondestroy (#4505) feat(eslint-plugin): add new rule require-super-ondestroy Dec 8, 2024
@markoblagdan markoblagdan force-pushed the feature/eslint-plugin/require-super-ondestroy branch from 8bea121 to 7afc1d9 Compare December 9, 2024 22:03
@markoblagdan markoblagdan force-pushed the feature/eslint-plugin/require-super-ondestroy branch from 7afc1d9 to c0fb1d3 Compare December 9, 2024 22:06
Copy link
Member

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @markoblagdan !

@markostanimirovic markostanimirovic merged commit 2ac4372 into ngrx:main Dec 10, 2024
11 checks passed
) {
hasNgrxComponentStoreImport = true;
},
[`ClassDeclaration[superClass.name=${componentStoreClassName}] ${ngOnDestroyMethodSelector}:not(:has(CallExpression[callee.object.type='Super'][callee.property.name='ngOnDestroy'])) > .key`](
Copy link
Contributor

@amakhrov amakhrov Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this uses :has - which is technically supported by the underlying esquery, but is not officially supported by eslint. It's not mentioned in the docs here, and also see this issue: eslint/eslint#14076 (comment).

do you think this approach is reasonably future-proof?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not aware of this - doesn't seem like support for it would be dropped, but maybe it's safer to use another approach.

@timdeschryver If I wanted to make a PR with an alternative to using :has, what would be the best way - should we create a new issue and I make a bugfix PR referencing it, or some other way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to keep it as-is.
There are multiple rules using :has, and I like it because it's easy to read.
If it does become deprecated/removed in the future we can update it.

@markoblagdan markoblagdan deleted the feature/eslint-plugin/require-super-ondestroy branch December 12, 2024 20:21
@markoblagdan markoblagdan restored the feature/eslint-plugin/require-super-ondestroy branch December 12, 2024 20:21
@markoblagdan markoblagdan deleted the feature/eslint-plugin/require-super-ondestroy branch December 14, 2024 13:20
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.

ESLint rule: require-super-ondestroy
4 participants