forked from cartant/eslint-plugin-rxjs
-
Notifications
You must be signed in to change notification settings - Fork 3
feat(no-subscribe-in-pipe)!: new rule to forbid calling subscribe within pipe operators #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JasonWeinzierl
merged 4 commits into
JasonWeinzierl:main
from
Danevandy99:no-subscribe-in-pipe
Dec 4, 2024
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Disallow calling of `subscribe` within any RxJS operator inside a `pipe` (`rxjs-x/no-subscribe-in-pipe`) | ||
|
||
💼 This rule is enabled in the following configs: ✅ `recommended`, 🔒 `strict`. | ||
|
||
💭 This rule requires [type information](https://typescript-eslint.io/linting/typed-linting). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
This rule effects failures if `subscribe` is called within any operator inside a `pipe` operation. | ||
|
||
## Rule details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
import { of } from "rxjs"; | ||
import { map } from "rxjs/operators"; | ||
|
||
of(42, 54).pipe( | ||
map(value => { | ||
of(value).subscribe(console.log); // This will trigger the rule | ||
return value * 2; | ||
}) | ||
).subscribe(result => console.log(result)); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
import { of } from "rxjs"; | ||
import { map, tap } from "rxjs/operators"; | ||
|
||
of(42, 54).pipe( | ||
tap(value => console.log(value)), | ||
map(value => value * 2) | ||
).subscribe(result => console.log(result)); | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { AST_NODE_TYPES, TSESTree as es } from '@typescript-eslint/utils'; | ||
import { getTypeServices } from '../etc'; | ||
import { ruleCreator } from '../utils'; | ||
|
||
export const noSubscribeInPipeRule = ruleCreator({ | ||
defaultOptions: [], | ||
meta: { | ||
docs: { | ||
description: | ||
'Disallow calling of `subscribe` within any RxJS operator inside a `pipe`.', | ||
recommended: 'recommended', | ||
requiresTypeChecking: true, | ||
}, | ||
fixable: undefined, | ||
hasSuggestions: false, | ||
messages: { | ||
forbidden: 'Subscribe calls within pipe operators are forbidden.', | ||
}, | ||
schema: [], | ||
type: 'problem', | ||
}, | ||
name: 'no-subscribe-in-pipe', | ||
create: (context) => { | ||
const { couldBeObservable, couldBeType } = getTypeServices(context); | ||
|
||
function isWithinPipe(node: es.Node): boolean { | ||
let parent = node.parent; | ||
|
||
while (parent) { | ||
if ( | ||
parent.type === AST_NODE_TYPES.CallExpression | ||
&& parent.callee.type === AST_NODE_TYPES.MemberExpression | ||
&& parent.callee.property.type === AST_NODE_TYPES.Identifier | ||
&& parent.callee.property.name === 'pipe' | ||
) { | ||
return true; | ||
} | ||
parent = node.parent; | ||
} | ||
return false; | ||
} | ||
|
||
return { | ||
'CallExpression > MemberExpression[property.name=\'subscribe\']': ( | ||
node: es.MemberExpression, | ||
) => { | ||
if ( | ||
!couldBeObservable(node.object) | ||
&& !couldBeType(node.object, 'Subscribable') | ||
) { | ||
return; | ||
} | ||
|
||
if (isWithinPipe(node)) { | ||
context.report({ | ||
messageId: 'forbidden', | ||
node: node.property, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
import { stripIndent } from 'common-tags'; | ||
import { noSubscribeInPipeRule } from '../../src/rules/no-subscribe-in-pipe'; | ||
import { fromFixture } from '../etc'; | ||
import { ruleTester } from '../rule-tester'; | ||
|
||
ruleTester({ types: true }).run('no-subscribe-in-pipe', noSubscribeInPipeRule, { | ||
valid: [ | ||
stripIndent` | ||
// subscribe outside of pipe | ||
import { of } from "rxjs"; | ||
of(47).subscribe(value => { | ||
console.log(value); | ||
}); | ||
`, | ||
stripIndent` | ||
// pipe without subscribe | ||
import { of } from "rxjs"; | ||
import { map } from "rxjs/operators"; | ||
of(47).pipe( | ||
map(x => x * 2) | ||
).subscribe(value => { | ||
console.log(value); | ||
}); | ||
`, | ||
stripIndent` | ||
// nested pipes without subscribe | ||
import { of } from "rxjs"; | ||
import { map, mergeMap } from "rxjs/operators"; | ||
of(47).pipe( | ||
mergeMap(x => of(x).pipe( | ||
map(y => y * 2) | ||
)) | ||
).subscribe(value => { | ||
console.log(value); | ||
}); | ||
`, | ||
stripIndent` | ||
// subscribe in a function outside pipe | ||
import { of } from "rxjs"; | ||
import { map } from "rxjs/operators"; | ||
const logValue = (value) => of(value).subscribe(console.log); | ||
of(47).pipe( | ||
map(x => x * 2) | ||
).subscribe(logValue); | ||
`, | ||
stripIndent` | ||
// subscribe method on a non-Observable object inside pipe | ||
import { of } from "rxjs"; | ||
import { map } from "rxjs/operators"; | ||
const customObject = { subscribe: () => {} }; | ||
of(47).pipe( | ||
map(x => { | ||
customObject.subscribe(); | ||
return x * 2; | ||
}) | ||
).subscribe(); | ||
`, | ||
stripIndent` | ||
// subscribe as a variable name inside pipe | ||
import { of } from "rxjs"; | ||
import { map } from "rxjs/operators"; | ||
of(47).pipe( | ||
map(x => { | ||
const subscribe = 5; | ||
return x * subscribe; | ||
}) | ||
).subscribe(); | ||
`, | ||
stripIndent` | ||
// subscribe in a comment inside pipe | ||
import { of } from "rxjs"; | ||
import { map } from "rxjs/operators"; | ||
of(47).pipe( | ||
map(x => { | ||
// of(x).subscribe(console.log); | ||
return x * 2; | ||
}) | ||
).subscribe(); | ||
`, | ||
stripIndent` | ||
// subscribe as a string inside pipe | ||
import { of } from "rxjs"; | ||
import { map } from "rxjs/operators"; | ||
of(47).pipe( | ||
map(x => { | ||
console.log("subscribe"); | ||
return x * 2; | ||
}) | ||
).subscribe(); | ||
`, | ||
stripIndent` | ||
// subscribe inside of an Observable constructor | ||
import { Observable, of } from "rxjs"; | ||
|
||
new Observable<number>(subscriber => { | ||
of(42).subscribe(subscriber); | ||
}).subscribe(); | ||
`, | ||
], | ||
invalid: [ | ||
fromFixture( | ||
stripIndent` | ||
// subscribe inside map operator | ||
import { of } from "rxjs"; | ||
import { map } from "rxjs/operators"; | ||
of(47).pipe( | ||
map(x => { | ||
of(x).subscribe(console.log); | ||
~~~~~~~~~ [forbidden] | ||
return x * 2; | ||
}) | ||
).subscribe(); | ||
`, | ||
), | ||
fromFixture( | ||
stripIndent` | ||
// subscribe inside mergeMap operator | ||
import { of } from "rxjs"; | ||
import { mergeMap } from "rxjs/operators"; | ||
of(47).pipe( | ||
mergeMap(x => of(x).pipe( | ||
map(y => { | ||
of(y).subscribe(console.log); | ||
~~~~~~~~~ [forbidden] | ||
return y * 2; | ||
}) | ||
)) | ||
).subscribe(); | ||
`, | ||
), | ||
fromFixture( | ||
stripIndent` | ||
// subscribe inside tap operator | ||
import { of } from "rxjs"; | ||
import { tap } from "rxjs/operators"; | ||
of(47).pipe( | ||
tap(x => { | ||
of(x).subscribe(console.log); | ||
~~~~~~~~~ [forbidden] | ||
}) | ||
).subscribe(); | ||
`, | ||
), | ||
fromFixture( | ||
stripIndent` | ||
// subscribe inside switchMap operator | ||
import { of } from "rxjs"; | ||
import { switchMap } from "rxjs/operators"; | ||
of(47).pipe( | ||
switchMap(x => { | ||
of(x).subscribe(console.log); | ||
~~~~~~~~~ [forbidden] | ||
return of(x * 2); | ||
}) | ||
).subscribe(); | ||
`, | ||
), | ||
fromFixture( | ||
stripIndent` | ||
// subscribe inside nested pipes | ||
import { of } from "rxjs"; | ||
import { map, mergeMap } from "rxjs/operators"; | ||
of(47).pipe( | ||
mergeMap(x => of(x).pipe( | ||
map(y => { | ||
of(y).subscribe(console.log); | ||
~~~~~~~~~ [forbidden] | ||
return y * 2; | ||
}) | ||
)) | ||
).subscribe(); | ||
`, | ||
), | ||
fromFixture( | ||
stripIndent` | ||
// subscribe inside a deeply nested function in pipe | ||
import { of } from "rxjs"; | ||
import { map } from "rxjs/operators"; | ||
of(47).pipe( | ||
map(x => { | ||
const nestedFunc = () => { | ||
const deeplyNested = () => { | ||
of(x).subscribe(console.log); | ||
~~~~~~~~~ [forbidden] | ||
}; | ||
deeplyNested(); | ||
}; | ||
nestedFunc(); | ||
return x * 2; | ||
}) | ||
).subscribe(); | ||
`, | ||
), | ||
fromFixture( | ||
stripIndent` | ||
// subscribe in a ternary operator in pipe | ||
import { of } from "rxjs"; | ||
import { map } from "rxjs/operators"; | ||
of(47).pipe( | ||
map(x => x > 50 ? x : of(x).subscribe(console.log)) | ||
~~~~~~~~~ [forbidden] | ||
).subscribe(); | ||
`, | ||
), | ||
], | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.