Skip to content

Commit 2efd805

Browse files
authored
fix(eslint-plugin): fix prefer-contact-latest-from rule to detect inject (#3946)
1 parent ee00ed4 commit 2efd805

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

modules/eslint-plugin/spec/rules/prefer-concat-latest-from.spec.ts

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,47 @@ const valid: () => RunTests['valid'] = () => [
7070
)
7171
constructor(private readonly actions$: Actions) {}
7272
}`,
73+
`
74+
import { Actions } from '@ngrx/effects'
75+
import { of, withLatestFrom } from 'rxjs'
76+
import { inject } from '@angular/core'
77+
class Ok3 {
78+
readonly effect: CreateEffectMetadata
79+
readonly actions$ = inject(Actions)
80+
constructor() {
81+
this.effect = createEffect(() => ({ scheduler = asyncScheduler } = {}) => {
82+
return actions$.pipe(
83+
ofType(ProductDetailPage.loaded),
84+
concatMap((action) =>
85+
of(action).pipe(withLatestFrom(this.store.select(selectProducts))),
86+
),
87+
mergeMapTo(of({ type: 'noop' })),
88+
)
89+
}, { dispatch: false })
90+
}
91+
}`,
92+
`
93+
import { Actions } from '@ngrx/effects'
94+
import { of, withLatestFrom } from 'rxjs'
95+
import { inject } from '@angular/core'
96+
class Ok4 {
97+
private readonly actions$ = inject(Actions)
98+
effect = createEffect(() =>
99+
condition
100+
? this.actions$.pipe(
101+
ofType(ProductDetailPage.loaded),
102+
concatMap((action) =>
103+
of(action).pipe(
104+
withLatestFrom(this.store.select$(something), (one, other) =>
105+
somethingElse(),
106+
),
107+
),
108+
),
109+
mergeMap(([action, products]) => of(products)),
110+
)
111+
: this.actions$.pipe(),
112+
)
113+
}`,
73114
];
74115

75116
const invalid: () => RunTests['invalid'] = () => [
@@ -265,6 +306,104 @@ class NotOk3 {
265306
)
266307
: this.actions$.pipe()
267308
})
309+
}`,
310+
}
311+
),
312+
fromFixture(
313+
`
314+
import { Actions } from '@ngrx/effects'
315+
import { of, withLatestFrom } from 'rxjs'
316+
import { inject } from '@angular/core'
317+
318+
class NotOk4 {
319+
private readonly actions$ = inject(Actions);
320+
effect = createEffect(() =>
321+
this.actions$.pipe(
322+
ofType(CollectionApiActions.addBookSuccess),
323+
withLatestFrom((action) =>
324+
~~~~~~~~~~~~~~ [${messageId}]
325+
this.store.select(fromBooks.selectCollectionBookIds),
326+
),
327+
switchMap(([action, bookCollection]) => {
328+
return of({ type: 'noop' })
329+
}),
330+
),
331+
)
332+
}`,
333+
{
334+
output: `
335+
import { Actions, concatLatestFrom } from '@ngrx/effects'
336+
import { of, withLatestFrom } from 'rxjs'
337+
import { inject } from '@angular/core'
338+
339+
class NotOk4 {
340+
private readonly actions$ = inject(Actions);
341+
effect = createEffect(() =>
342+
this.actions$.pipe(
343+
ofType(CollectionApiActions.addBookSuccess),
344+
concatLatestFrom((action) =>
345+
this.store.select(fromBooks.selectCollectionBookIds),
346+
),
347+
switchMap(([action, bookCollection]) => {
348+
return of({ type: 'noop' })
349+
}),
350+
),
351+
)
352+
}`,
353+
}
354+
),
355+
fromFixture(
356+
`
357+
import { Actions } from '@ngrx/effects'
358+
import { of, withLatestFrom } from 'rxjs'
359+
import { inject } from '@angular/core'
360+
361+
class NotOk5 {
362+
readonly effect: CreateEffectMetadata
363+
readonly actions$ = inject(Actions)
364+
365+
constructor() {
366+
this.effect = createEffect(
367+
() =>
368+
({ debounce = 300 } = {}) =>
369+
condition
370+
? actions$.pipe()
371+
: actions$.pipe(
372+
ofType(CollectionApiActions.addBookSuccess),
373+
withLatestFrom(() => this.store.select(fromBooks.selectCollectionBookIds)),
374+
~~~~~~~~~~~~~~ [${messageId}]
375+
switchMap(([action, bookCollection]) => {
376+
return of({ type: 'noop' })
377+
}),
378+
),
379+
)
380+
}
381+
}`,
382+
{
383+
output: `
384+
import { Actions, concatLatestFrom } from '@ngrx/effects'
385+
import { of, withLatestFrom } from 'rxjs'
386+
import { inject } from '@angular/core'
387+
388+
class NotOk5 {
389+
readonly effect: CreateEffectMetadata
390+
readonly actions$ = inject(Actions)
391+
392+
constructor() {
393+
this.effect = createEffect(
394+
() =>
395+
({ debounce = 300 } = {}) =>
396+
condition
397+
? actions$.pipe()
398+
: actions$.pipe(
399+
ofType(CollectionApiActions.addBookSuccess),
400+
concatLatestFrom(() => this.store.select(fromBooks.selectCollectionBookIds)),
401+
switchMap(([action, bookCollection]) => {
402+
return of({ type: 'noop' })
403+
}),
404+
),
405+
)
406+
}
268407
}`,
269408
}
270409
),

0 commit comments

Comments
 (0)