Skip to content

Commit

Permalink
fix(eslint-plugin): avoid-combining-selectors with arrays should warn (
Browse files Browse the repository at this point in the history
…#3566)

Closes #3566
  • Loading branch information
timdeschryver authored Sep 14, 2022
1 parent aa46bbc commit 4b0c6de
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
32 changes: 32 additions & 0 deletions modules/eslint-plugin/spec/rules/avoid-combining-selectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ class NotOk {
`
import { Store } from '@ngrx/store'
class NotOkWithArrays {
readonly vm$: Observable<unknown>
constructor(store: Store, private store2: Store) {
this.vm$ = combineLatest([
store.select(selectItems),
store.select(selectOtherItems),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}]
this.store2.select(selectOtherItems),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}]
])
}
}`
),
fromFixture(
`
import { Store } from '@ngrx/store'
class NotOk1 {
vm$ = combineLatest(
this.store.pipe(select(selectItems)),
Expand All @@ -94,6 +112,20 @@ class NotOk1 {
`
import { Store } from '@ngrx/store'
class NotOkWithArray {
vm$ = combineLatest([
this.store.pipe(select(selectItems)),
this.store.pipe(select(selectOtherItems)),
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}]
])
constructor(private store: Store) {}
}`
),
fromFixture(
`
import { Store } from '@ngrx/store'
class NotOk2 {
vm$ = combineLatest(
this.customName.select(selectItems),
Expand Down
17 changes: 12 additions & 5 deletions modules/eslint-plugin/src/rules/store/avoid-combining-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,21 @@ export default createRule<Options, MessageIds>({
storeNames
)})` as const;

const selectsInArray: TSESTree.CallExpression[] = [];
return {
[`CallExpression[callee.name='combineLatest'][arguments.length>1] ${pipeableOrStoreSelect} ~ ${pipeableOrStoreSelect}`](
[`CallExpression[callee.name='combineLatest'] ${pipeableOrStoreSelect} ~ ${pipeableOrStoreSelect}`](
node: TSESTree.CallExpression
) {
context.report({
node,
messageId,
});
selectsInArray.push(node);
},
[`CallExpression[callee.name='combineLatest']:exit`]() {
for (const node of selectsInArray) {
context.report({
node,
messageId,
});
}
selectsInArray.length = 0;
},
};
},
Expand Down

0 comments on commit 4b0c6de

Please sign in to comment.