Skip to content

Commit

Permalink
feat(eslint-plugin): add new rule require-super-ondestroy
Browse files Browse the repository at this point in the history
  • Loading branch information
markoblagdan committed Dec 9, 2024
1 parent 1ecc69f commit 8bea121
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class BooksStore extends ComponentStore implements OnDestroy
override ngOnDestroy(): void {
this.cleanUp();
super.ngOnDestroy();
}
}`,
];
Expand Down Expand Up @@ -113,18 +112,6 @@ class BooksStore extends ComponentStore<BooksState> implements OnDestroy {
~~~~~~~~~~~ [${messageId}]
super.get();
}
}`),
fromFixture(`
import { ComponentStore } from '../components/component-store';
class BooksStore extends ComponentStore implements OnDestroy
{
cleanUp() {}
override ngOnDestroy(): void {
~~~~~~~~~~~ [${messageId}]
this.cleanUp();
}
}`),
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,23 @@ export default createRule<Options, MessageIds>({
defaultOptions: [],
create: (context) => {
const ngOnDestroyMethodSelector = `MethodDefinition[key.name='ngOnDestroy']`;
const componentStoreClassName = 'ComponentStore';

let hasNgrxComponentStoreImport = false;

return {
[`ClassDeclaration[superClass.name=ComponentStore] ${ngOnDestroyMethodSelector}:not(:has(CallExpression[callee.object.type='Super'][callee.property.name='ngOnDestroy'])) > .key`](
[`ImportDeclaration[source.value='@ngrx/component-store'] ImportSpecifier[imported.name='${componentStoreClassName}']`](
_: TSESTree.ImportSpecifier
) {
hasNgrxComponentStoreImport = true;
},
[`ClassDeclaration[superClass.name=${componentStoreClassName}] ${ngOnDestroyMethodSelector}:not(:has(CallExpression[callee.object.type='Super'][callee.property.name='ngOnDestroy'])) > .key`](
node: TSESTree.Identifier
) {
if (!hasNgrxComponentStoreImport) {
return;
}

context.report({
node,
messageId,
Expand Down

0 comments on commit 8bea121

Please sign in to comment.