Add ESLint rule to prevent Observable-returning method calls in the async pipe (#5933)#5934
Add ESLint rule to prevent Observable-returning method calls in the async pipe (#5933)#5934Aryakoste wants to merge 3 commits into
Conversation
|
Thank you for the PR @Aryakoste! You may have already seen this, but looks like this is failing a few build tests that will need to be addressed. |
| * RecursiveAstVisitor, which correctly covers the full expression AST regardless of what | ||
| * @angular-eslint/template-parser exposes to ESLint's selector engine. | ||
| */ | ||
| class AsyncPipedCallVisitor extends RecursiveAstVisitor { |
There was a problem hiding this comment.
Good catch on those selectors, I wasn't aware of that!
There was a problem hiding this comment.
↑ those CI failures are due to the new lint rule
This rule does not attempt to fix existing offending call sites — per the issue, migrating those (...) is a separate follow-up.
The rule is not auto-fixable, since that migration can't be done mechanically.
It won't be possible to auto-fix, but we'll still need to sort out that "follow up step" within this PR as we did in previous PRs that introduced new configuration or rules.
|
Ok will do that |
|
@ybnd @lgeggleston I have done the migration of all the files. |
|
@Aryakoste : You may have seen this already, but it looks like this PR is failing ~12 unit tests. We'd obviously need those to pass before we can look for additional reviewers. |
|
@tdonohue Yeah i have solved those pushing them |
References
We commonly call a component method directly as the input of the
asyncpipe in templates, e.g.{{ getFoo$() | async }}. Because the method runs as part of template evaluation, Angular re-executes the whole RxJS pipeline (subscribe + unsubscribe) on every change-detection cycle instead of once — a real, recurring perf issue in this codebase (confirmed instances inaudit-table,ePerson-data, andsection-coar-notifycomponents).This PR adds a new
dspace-angular-htmlESLint rule,no-method-call-with-async-pipe, that flags this pattern anywhere it appears in a template — interpolations, bound attributes/events,@if/@for/@switchblocks,@defertriggers,@let, and object/array literals or optional-chaining wrapped around the pipe.lint/src/rules/html/no-method-call-with-async-pipe.tslint/src/rules/html/index.tsand enabled in.eslintrc.jsonnpm run docs:lint(docs/lint/html/index.md,docs/lint/html/rules/no-method-call-with-async-pipe.md)Implementation note
The straightforward approach — an ESLint selector like
BindingPipe[name="async"] > Call— turned out to miss common real-world cases, because@angular-eslint/template-parser's own visitor-keys map doesn't cover every expression AST node type (e.g.SafePropertyRead,LiteralMap), so ESLint's traversal silently stops before reaching calls wrapped in things like(getFoo$() | async)?.lengthor[ngClass]="{x: getFoo$() | async}". To get full, correct coverage, the rule instead walks each template expression with@angular/compiler's ownRecursiveAstVisitor, which handles every AST node type regardless of that gap.This rule does not attempt to fix existing offending call sites — per the issue, migrating those (subscribe in
ngOnInit(), push into aBehaviorSubject,| asyncon that, unsubscribe inngOnDestroy()) is a separate follow-up. The rule is not auto-fixable, since that migration can't be done mechanically.Checklist
This checklist provides a reminder of what we are going to look for when reviewing your PR. You do not need to complete this checklist prior creating your PR (draft PRs are always welcome).
However, reviewers may request that you complete any actions in this list if you have not done so. If you are unsure about an item in the checklist, don't hesitate to ask. We're here to help!
mainbranch of code (unless it is a backport or is fixing an issue specific to an older branch).npm run lintnpm run check-circ-deps)package.json), I've made sure their licenses align with the DSpace BSD License based on the Licensing of Contributions documentation.