-
-
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 avoid-combining-component-store-selectors ru…
…le (#4043)
- Loading branch information
1 parent
f2514ba
commit 0bff440
Showing
13 changed files
with
385 additions
and
7 deletions.
There are no files selected for viewing
215 changes: 215 additions & 0 deletions
215
modules/eslint-plugin/spec/rules/avoid-combining-component-store-selectors.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,215 @@ | ||
import type { | ||
ESLintUtils, | ||
TSESLint, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import { fromFixture } from 'eslint-etc'; | ||
import * as path from 'path'; | ||
import rule, { | ||
messageId, | ||
} from '../../src/rules/component-store/avoid-combining-component-store-selectors'; | ||
import { ruleTester } from '../utils'; | ||
|
||
type MessageIds = ESLintUtils.InferMessageIdsTypeFromRule<typeof rule>; | ||
type Options = ESLintUtils.InferOptionsTypeFromRule<typeof rule>; | ||
type RunTests = TSESLint.RunTests<MessageIds, Options>; | ||
|
||
const validConstructor: () => RunTests['valid'] = () => [ | ||
` | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class Ok extends ComponentStore<MoviesState> { | ||
movies$ = this.select((state) => state.movies); | ||
selectedId$ = this.select((state) => state.selectedId); | ||
movie$ = this.select( | ||
this.movies$, | ||
this.selectedId$, | ||
([movies, selectedId]) => movies[selectedId] | ||
); | ||
constructor() { | ||
super({ movies: [] }) | ||
} | ||
}`, | ||
` | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class Ok { | ||
readonly movies$ = this.store.select((state) => state.movies); | ||
readonly selectedId$ = this.store.select((state) => state.selectedId); | ||
readonly movie$ = this.store.select( | ||
this.movies$, | ||
this.selectedId$, | ||
([movies, selectedId]) => movies[selectedId] | ||
); | ||
constructor(private readonly store: ComponentStore<MoviesState>) {} | ||
}`, | ||
` | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class Ok { | ||
movie$: Observable<unknown> | ||
constructor(customStore: ComponentStore<MoviesState>) { | ||
const movies = customStore.select((state) => state.movies); | ||
const selectedId = this.customStore.select((state) => state.selectedId); | ||
this.movie$ = this.customStore.select( | ||
this.movies$, | ||
this.selectedId$, | ||
([movies, selectedId]) => movies[selectedId] | ||
); | ||
} | ||
}`, | ||
` | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class Ok { | ||
vm$ = combineLatest(this.somethingElse(), this.customStore.select(selectItems)) | ||
constructor(customStore: ComponentStore<MoviesState>) {} | ||
}`, | ||
` | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class Ok extends ComponentStore<MoviesState> { | ||
vm$ = combineLatest(this.select(selectItems), this.somethingElse()) | ||
}`, | ||
]; | ||
|
||
const validInject: () => RunTests['valid'] = () => [ | ||
` | ||
import { inject } from '@angular/core' | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class Ok { | ||
readonly store = inject(ComponentStore<MoviesState>) | ||
readonly movies$ = this.store.select((state) => state.movies); | ||
readonly selectedId$ = this.store.select((state) => state.selectedId); | ||
readonly movie$ = this.store.select( | ||
this.movies$, | ||
this.selectedId$, | ||
([movies, selectedId]) => movies[selectedId] | ||
); | ||
}`, | ||
` | ||
import { inject } from '@angular/core' | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class Ok { | ||
readonly store = inject(ComponentStore<MoviesState>) | ||
readonly vm$ = combineLatest(this.store.select(selectItems), somethingElse()) | ||
}`, | ||
]; | ||
|
||
const invalidConstructor: () => RunTests['invalid'] = () => [ | ||
fromFixture(` | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class NotOk extends ComponentStore<MoviesState> { | ||
movie$ = combineLatest( | ||
this.select((state) => state.movies), | ||
this.select((state) => state.selectedId), | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] | ||
); | ||
constructor() { | ||
super({ movies: [] }) | ||
} | ||
}`), | ||
fromFixture(` | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class NotOk extends ComponentStore<MoviesState> { | ||
movie$ = combineLatest( | ||
this.select((state) => state.movies), | ||
this.select((state) => state.selectedId), | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] | ||
this.select((state) => state.selectedId), | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] | ||
); | ||
constructor() { | ||
super({ movies: [] }) | ||
} | ||
}`), | ||
fromFixture(` | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class NotOk { | ||
movie$ = combineLatest( | ||
this.moviesState.select((state) => state.movies), | ||
this.moviesState.select((state) => state.selectedId), | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] | ||
); | ||
constructor(private readonly moviesState: ComponentStore<MoviesState>) {} | ||
}`), | ||
fromFixture(` | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class NotOk { | ||
movie$ = combineLatest( | ||
this.moviesState.select((state) => state.movies), | ||
this.moviesState.select((state) => state.selectedId), | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] | ||
this.moviesState.select((state) => state.selectedId), | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] | ||
); | ||
constructor(private readonly moviesState: ComponentStore<MoviesState>) {} | ||
}`), | ||
fromFixture(` | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class NotOk { | ||
movie$: Observable<unknown> | ||
constructor(store: ComponentStore<MoviesState>) { | ||
this.movie$ = combineLatest( | ||
store.select((state) => state.movies), | ||
store.select((state) => state.selectedId) | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] | ||
); | ||
} | ||
} | ||
`), | ||
fromFixture(` | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class NotOk { | ||
movie$: Observable<unknown> | ||
constructor(store: ComponentStore<MoviesState>) { | ||
this.movie$ = combineLatest( | ||
store.select((state) => state.movies), | ||
store.select((state) => state.selectedId), | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] | ||
store.select((state) => state.selectedId) | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] | ||
); | ||
} | ||
} | ||
`), | ||
]; | ||
|
||
const invalidInject: () => RunTests['invalid'] = () => [ | ||
fromFixture(` | ||
import { inject } from '@angular/core' | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class NotOk { | ||
readonly componentStore = inject(ComponentStore<MoviesState>) | ||
readonly movie$ = combineLatest( | ||
this.componentStore.select((state) => state.movies), | ||
this.componentStore.select((state) => state.selectedId), | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] | ||
); | ||
}`), | ||
fromFixture(` | ||
import { inject } from '@angular/core' | ||
import { ComponentStore } from '@ngrx/component-store' | ||
class NotOk { | ||
readonly store = inject(ComponentStore<MoviesState>) | ||
readonly movie$ = combineLatest( | ||
this.store.select((state) => state.movies), | ||
this.store.select((state) => state.selectedId), | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] | ||
this.store.select((state) => state.selectedId), | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}] | ||
); | ||
}`), | ||
]; | ||
|
||
ruleTester().run(path.parse(__filename).name, rule, { | ||
valid: [...validConstructor(), ...validInject()], | ||
invalid: [...invalidConstructor(), ...invalidInject()], | ||
}); |
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
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
58 changes: 58 additions & 0 deletions
58
modules/eslint-plugin/src/rules/component-store/avoid-combining-component-store-selectors.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,58 @@ | ||
import type { TSESTree } from '@typescript-eslint/experimental-utils'; | ||
import * as path from 'path'; | ||
import { createRule } from '../../rule-creator'; | ||
import { | ||
asPattern, | ||
getNgRxComponentStores, | ||
namedExpression, | ||
} from '../../utils'; | ||
export const messageId = 'avoidCombiningComponentStoreSelectors'; | ||
type MessageIds = typeof messageId; | ||
type Options = readonly []; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
name: path.parse(__filename).name, | ||
meta: { | ||
type: 'suggestion', | ||
ngrxModule: 'component-store', | ||
docs: { | ||
description: 'Prefer combining selectors at the selector level.', | ||
recommended: 'warn', | ||
}, | ||
schema: [], | ||
messages: { | ||
[messageId]: 'Combine selectors at the selector level.', | ||
}, | ||
}, | ||
defaultOptions: [], | ||
create: (context) => { | ||
const { identifiers = [] } = getNgRxComponentStores(context); | ||
const storeNames = identifiers.length > 0 ? asPattern(identifiers) : null; | ||
|
||
const thisSelects = `CallExpression[callee.object.type='ThisExpression'][callee.property.name='select']`; | ||
const storeSelects = storeNames ? namedExpression(storeNames) : null; | ||
|
||
const selectsInArray: TSESTree.CallExpression[] = []; | ||
return { | ||
[`ClassDeclaration[superClass.name=/Store/] CallExpression[callee.name='combineLatest'] ${thisSelects} ~ ${thisSelects}`]( | ||
node: TSESTree.CallExpression | ||
) { | ||
selectsInArray.push(node); | ||
}, | ||
[`CallExpression[callee.name='combineLatest'] ${storeSelects} ~ ${storeSelects}`]( | ||
node: TSESTree.CallExpression | ||
) { | ||
selectsInArray.push(node); | ||
}, | ||
[`CallExpression[callee.name='combineLatest']:exit`]() { | ||
for (const node of selectsInArray) { | ||
context.report({ | ||
node, | ||
messageId, | ||
}); | ||
} | ||
selectsInArray.length = 0; | ||
}, | ||
}; | ||
}, | ||
}); |
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.