Skip to content

Commit

Permalink
fix(eslint-plugin): allow sequential dispatches in a different block …
Browse files Browse the repository at this point in the history
…context (#3515)

Closes #3513
  • Loading branch information
timdeschryver committed Aug 6, 2022
1 parent 4642919 commit faf446f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
}
}`,
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,30 @@ export default createRule<Options, MessageIds>({
return {};
}

const collectedDispatches = new Set<TSESTree.CallExpression>();
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,
});
}
}

collectedDispatches.clear();
collectedDispatches.length = 0;
},
};
},
Expand Down

0 comments on commit faf446f

Please sign in to comment.