|
| 1 | +/** |
| 2 | + * @license Use of this source code is governed by an MIT-style license that |
| 3 | + * can be found in the LICENSE file at https://github.com/cartant/eslint-plugin-rxjs |
| 4 | + */ |
| 5 | + |
| 6 | +import { stripIndent } from "common-tags"; |
| 7 | +import { fromFixture } from "eslint-etc"; |
| 8 | +import rule = require("../../source/rules/no-nested-pipe"); |
| 9 | +import { ruleTester } from "../utils"; |
| 10 | + |
| 11 | +ruleTester({ types: true }).run("no-nested-pipe", rule, { |
| 12 | + valid: [ |
| 13 | + stripIndent` |
| 14 | + // not nested in pipe |
| 15 | + import { Observable,of,switchMap } from "rxjs"; |
| 16 | + of(47).pipe(switchMap(value => { |
| 17 | + console.log('new' ,value); |
| 18 | + })).subscribe(value => { |
| 19 | + console.log(value); |
| 20 | + }) |
| 21 | + `, |
| 22 | + stripIndent` |
| 23 | + // not nested in pipe |
| 24 | + import { Observable,switchMap } from "rxjs"; |
| 25 | + of(47).pipe(switchMap(value => { |
| 26 | + return someFunction(value) |
| 27 | + })).subscribe(value => { |
| 28 | + console.log(value); |
| 29 | + }); |
| 30 | + function someFunction(someParam: Observable<any>): Observable<any> { |
| 31 | + return of(43).pipe( |
| 32 | + switchMap(value => {value + someParam}) |
| 33 | + ) |
| 34 | + } |
| 35 | + `, |
| 36 | + stripIndent` |
| 37 | + // not nested in pipe |
| 38 | + import { Observable,switchMap } from "rxjs"; |
| 39 | + of(47).pipe(switchMap(value => { |
| 40 | + return someFunction1(value) |
| 41 | + }), |
| 42 | + switchMap(value => { |
| 43 | + return someFunction2(value) |
| 44 | + }) |
| 45 | + ).subscribe(value => { |
| 46 | + console.log(value); |
| 47 | + }); |
| 48 | + function someFunction1(someParam: Observable<any>): Observable<any> { |
| 49 | + return of(43).pipe( |
| 50 | + switchMap(value => {value + someParam}) |
| 51 | + ) |
| 52 | + }; |
| 53 | + function someFunction2(someParam: Observable<any>): Observable<any> { |
| 54 | + return of(43).pipe( |
| 55 | + switchMap(value => {value + someParam}) |
| 56 | + ) |
| 57 | + } |
| 58 | +`, |
| 59 | + ], |
| 60 | + invalid: [ |
| 61 | + fromFixture( |
| 62 | + stripIndent` |
| 63 | + // nested in pipe |
| 64 | + import { of,switchMap,tap } from "rxjs"; |
| 65 | + of("foo").pipe( |
| 66 | + switchMap(value => { |
| 67 | + return of("bar").pipe(tap(value => { console.log(value)}) |
| 68 | + ~~~~ [forbidden] |
| 69 | + )}) |
| 70 | + ).subscribe(value => { |
| 71 | + console.log(value); |
| 72 | + }); |
| 73 | + ` |
| 74 | + ), |
| 75 | + fromFixture( |
| 76 | + stripIndent` |
| 77 | + // nested in pipe |
| 78 | + import { of,switchMap,tap } from "rxjs"; |
| 79 | + of("foo").pipe( |
| 80 | + switchMap(value => { |
| 81 | + return of("bar").pipe(tap(value => { console.log(value)}) |
| 82 | + ~~~~ [forbidden] |
| 83 | + )}), |
| 84 | + switchMap(value => { |
| 85 | + return of("bar").pipe(tap(value => { console.log(value)}) |
| 86 | + ~~~~ [forbidden] |
| 87 | + )}) |
| 88 | + ).subscribe(value => { |
| 89 | + console.log(value); |
| 90 | + }); |
| 91 | + ` |
| 92 | + ), |
| 93 | + ], |
| 94 | +}); |
0 commit comments