From 4b0c6de5158b1adbf13294df25427fe7da97a0e7 Mon Sep 17 00:00:00 2001 From: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> Date: Wed, 14 Sep 2022 04:44:45 +0200 Subject: [PATCH] fix(eslint-plugin): avoid-combining-selectors with arrays should warn (#3566) Closes #3566 --- .../rules/avoid-combining-selectors.spec.ts | 32 +++++++++++++++++++ .../rules/store/avoid-combining-selectors.ts | 17 +++++++--- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/modules/eslint-plugin/spec/rules/avoid-combining-selectors.spec.ts b/modules/eslint-plugin/spec/rules/avoid-combining-selectors.spec.ts index 0eb25e1fba..e87d26b460 100644 --- a/modules/eslint-plugin/spec/rules/avoid-combining-selectors.spec.ts +++ b/modules/eslint-plugin/spec/rules/avoid-combining-selectors.spec.ts @@ -80,6 +80,24 @@ class NotOk { ` import { Store } from '@ngrx/store' +class NotOkWithArrays { + readonly vm$: Observable + + 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)), @@ -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), diff --git a/modules/eslint-plugin/src/rules/store/avoid-combining-selectors.ts b/modules/eslint-plugin/src/rules/store/avoid-combining-selectors.ts index b1a5cff04d..6854c3aa6c 100644 --- a/modules/eslint-plugin/src/rules/store/avoid-combining-selectors.ts +++ b/modules/eslint-plugin/src/rules/store/avoid-combining-selectors.ts @@ -42,14 +42,21 @@ export default createRule({ 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; }, }; },