diff --git a/modules/eslint-plugin/spec/rules/avoid-dispatching-multiple-actions-sequentially.spec.ts b/modules/eslint-plugin/spec/rules/avoid-dispatching-multiple-actions-sequentially.spec.ts index 08c8ff946e..be228407e3 100644 --- a/modules/eslint-plugin/spec/rules/avoid-dispatching-multiple-actions-sequentially.spec.ts +++ b/modules/eslint-plugin/spec/rules/avoid-dispatching-multiple-actions-sequentially.spec.ts @@ -62,6 +62,36 @@ ngOnInit() { this.store.dispatch(anotherOne()); }); } +}`, + // https://github.com/ngrx/platform/issues/3513 + ` +import { Store } from '@ngrx/store' + +class Ok { +constructor(private store: Store) {} + +ngOnInit() { + this.store.dispatch(one()); + + this.store.subscribe(() => { + this.store.dispatch(anotherOne()); + }); +} +}`, + // https://github.com/ngrx/platform/issues/3513 + ` +import { Store } from '@ngrx/store' + +class Ok { +constructor(private store: Store) {} + +ngOnInit() { + this.store.dispatch(anotherOne()); + + this.store.subscribe(() => { + this.store.dispatch(one()); + }); +} }`, ]; diff --git a/modules/eslint-plugin/src/rules/store/avoid-dispatching-multiple-actions-sequentially.ts b/modules/eslint-plugin/src/rules/store/avoid-dispatching-multiple-actions-sequentially.ts index 2e118ba681..4a2b024c57 100644 --- a/modules/eslint-plugin/src/rules/store/avoid-dispatching-multiple-actions-sequentially.ts +++ b/modules/eslint-plugin/src/rules/store/avoid-dispatching-multiple-actions-sequentially.ts @@ -32,17 +32,22 @@ export default createRule({ return {}; } - const collectedDispatches = new Set(); + const collectedDispatches: TSESTree.CallExpression[] = []; return { [`BlockStatement > ExpressionStatement > ${dispatchExpression( storeNames )}`](node: TSESTree.CallExpression) { - collectedDispatches.add(node); + collectedDispatches.push(node); }, 'BlockStatement:exit'() { - if (collectedDispatches.size > 1) { - for (const node of collectedDispatches) { + const withSameParent = collectedDispatches.filter((d1) => + collectedDispatches.some( + (d2) => d2 !== d1 && d2.parent?.parent === d1.parent?.parent + ) + ); + if (withSameParent.length > 1) { + for (const node of withSameParent) { context.report({ node, messageId, @@ -50,7 +55,7 @@ export default createRule({ } } - collectedDispatches.clear(); + collectedDispatches.length = 0; }, }; },