-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add new rule require-super-ondestroy (#4611)
Closes #4505
- Loading branch information
1 parent
4d34dc4
commit 2ac4372
Showing
22 changed files
with
267 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
export interface Schema { | ||
config: 'all' | 'store' | 'effects' | 'component-store' | 'signals'; | ||
} |
121 changes: 121 additions & 0 deletions
121
modules/eslint-plugin/spec/rules/component-store/require-super-ondestroy.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { ESLintUtils } from '@typescript-eslint/utils'; | ||
import { InvalidTestCase, ValidTestCase } from '@typescript-eslint/rule-tester'; | ||
import rule, { | ||
messageId, | ||
} from '../../../src/rules/component-store/require-super-ondestroy'; | ||
import { fromFixture, ruleTester } from '../../utils'; | ||
import path = require('path'); | ||
|
||
type MessageIds = ESLintUtils.InferMessageIdsTypeFromRule<typeof rule>; | ||
type Options = ESLintUtils.InferOptionsTypeFromRule<typeof rule>; | ||
|
||
const valid: () => (string | ValidTestCase<Options>)[] = () => [ | ||
` | ||
import { ComponentStore } from '@ngrx/component-store'; | ||
class BooksStore extends ComponentStore<BooksState> | ||
{ | ||
}`, | ||
` | ||
import { ComponentStore } from '@ngrx/component-store'; | ||
class BooksStore extends ComponentStore<BooksState> implements OnDestroy | ||
{ | ||
override ngOnDestroy(): void { | ||
super.ngOnDestroy(); | ||
} | ||
}`, | ||
` | ||
import { ComponentStore } from '@ngrx/component-store'; | ||
class BooksStore extends ComponentStore<BooksState> implements OnDestroy | ||
{ | ||
cleanUp() {} | ||
override ngOnDestroy(): void { | ||
this.cleanUp(); | ||
super.ngOnDestroy(); | ||
} | ||
}`, | ||
` | ||
import { ComponentStore } from '@ngrx/component-store'; | ||
class BooksStore extends ComponentStore<BooksState> implements OnDestroy | ||
{ | ||
cleanUp() {} | ||
override ngOnDestroy(): void { | ||
super.ngOnDestroy(); | ||
this.cleanUp(); | ||
} | ||
}`, | ||
` | ||
import { ComponentStore } from '@ngrx/component-store'; | ||
class BooksStore extends ComponentStore<BooksState> implements OnDestroy | ||
{ | ||
cleanUp() {} | ||
override ngOnDestroy(): void { | ||
this.cleanUp(); | ||
super.ngOnDestroy(); | ||
this.cleanUp(); | ||
} | ||
}`, | ||
` | ||
import { ComponentStore } from '../components/component-store'; | ||
class BooksStore extends ComponentStore implements OnDestroy | ||
{ | ||
cleanUp() {} | ||
override ngOnDestroy(): void { | ||
this.cleanUp(); | ||
} | ||
}`, | ||
]; | ||
|
||
const invalid: () => InvalidTestCase<MessageIds, Options>[] = () => [ | ||
fromFixture(` | ||
import { ComponentStore } from '@ngrx/component-store'; | ||
class BooksStore extends ComponentStore<BooksState> implements OnDestroy { | ||
override ngOnDestroy(): void { | ||
~~~~~~~~~~~ [${messageId}] | ||
} | ||
}`), | ||
fromFixture(` | ||
import { ComponentStore } from '@ngrx/component-store'; | ||
class BooksStore extends ComponentStore<BooksState> implements OnDestroy { | ||
cleanUp() {} | ||
override ngOnDestroy(): void { | ||
~~~~~~~~~~~ [${messageId}] | ||
this.cleanUp(); | ||
} | ||
}`), | ||
fromFixture(` | ||
import { ComponentStore } from '@ngrx/component-store'; | ||
class BooksStore extends ComponentStore<BooksState> implements OnDestroy { | ||
override ngOnDestroy(): void { | ||
~~~~~~~~~~~ [${messageId}] | ||
super.ngOnDestroy; | ||
} | ||
}`), | ||
fromFixture(` | ||
import { ComponentStore } from '@ngrx/component-store'; | ||
class BooksStore extends ComponentStore<BooksState> implements OnDestroy { | ||
override ngOnDestroy(): void { | ||
~~~~~~~~~~~ [${messageId}] | ||
super.get(); | ||
} | ||
}`), | ||
]; | ||
|
||
ruleTester().run(path.parse(__filename).name, rule, { | ||
valid: valid(), | ||
invalid: invalid(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
modules/eslint-plugin/src/rules/component-store/require-super-ondestroy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import path = require('path'); | ||
import { createRule } from '../../rule-creator'; | ||
import { TSESTree } from '@typescript-eslint/types'; | ||
|
||
export const messageId = 'requireSuperOnDestroy'; | ||
type MessageIds = typeof messageId; | ||
type Options = readonly []; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
name: path.parse(__filename).name, | ||
meta: { | ||
type: 'problem', | ||
ngrxModule: 'component-store', | ||
docs: { | ||
description: | ||
'Overriden ngOnDestroy method in component stores require a call to super.ngOnDestroy().', | ||
}, | ||
schema: [], | ||
messages: { | ||
[messageId]: | ||
"Call super.ngOnDestroy() inside a component store's ngOnDestroy method.", | ||
}, | ||
}, | ||
defaultOptions: [], | ||
create: (context) => { | ||
const ngOnDestroyMethodSelector = `MethodDefinition[key.name='ngOnDestroy']`; | ||
const componentStoreClassName = 'ComponentStore'; | ||
|
||
let hasNgrxComponentStoreImport = false; | ||
|
||
return { | ||
[`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, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.