From 7afc1d99b6c5979f594d80bcd51f8b91b2a9bc28 Mon Sep 17 00:00:00 2001 From: markoblagdan Date: Mon, 9 Dec 2024 22:02:25 +0000 Subject: [PATCH] feat(eslint-plugin): add new rule require-super-ondestroy --- .../require-super-ondestroy.spec.ts | 13 ------------- .../component-store/require-super-ondestroy.ts | 14 +++++++++++++- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/modules/eslint-plugin/spec/rules/component-store/require-super-ondestroy.spec.ts b/modules/eslint-plugin/spec/rules/component-store/require-super-ondestroy.spec.ts index 3c5d90ab25..ee06d720f6 100644 --- a/modules/eslint-plugin/spec/rules/component-store/require-super-ondestroy.spec.ts +++ b/modules/eslint-plugin/spec/rules/component-store/require-super-ondestroy.spec.ts @@ -71,7 +71,6 @@ class BooksStore extends ComponentStore implements OnDestroy override ngOnDestroy(): void { this.cleanUp(); - super.ngOnDestroy(); } }`, ]; @@ -113,18 +112,6 @@ class BooksStore extends ComponentStore 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(); - } }`), ]; diff --git a/modules/eslint-plugin/src/rules/component-store/require-super-ondestroy.ts b/modules/eslint-plugin/src/rules/component-store/require-super-ondestroy.ts index d4e6b05c6f..e1d6068683 100644 --- a/modules/eslint-plugin/src/rules/component-store/require-super-ondestroy.ts +++ b/modules/eslint-plugin/src/rules/component-store/require-super-ondestroy.ts @@ -24,11 +24,23 @@ export default createRule({ 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,