|
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 | | - */ |
| 1 | +import { AST_NODE_TYPES, TSESTree as es } from '@typescript-eslint/utils'; |
| 2 | +import { getTypeServices } from '../etc'; |
| 3 | +import { ruleCreator } from '../utils'; |
5 | 4 |
|
6 | | -import { TSESTree as es } from "@typescript-eslint/experimental-utils"; |
7 | | -import { getParent, getTypeServices } from "eslint-etc"; |
8 | | -import { ruleCreator } from "../utils"; |
9 | | - |
10 | | -const rule = ruleCreator({ |
| 5 | +export const noSubscribeInPipeRule = ruleCreator({ |
11 | 6 | defaultOptions: [], |
12 | 7 | meta: { |
13 | 8 | docs: { |
14 | 9 | description: |
15 | | - "Forbids the calling of `subscribe` within any RxJS operator inside a `pipe`.", |
16 | | - recommended: "error", |
| 10 | + 'Disallow calling of `subscribe` within any RxJS operator inside a `pipe`.', |
| 11 | + recommended: 'recommended', |
| 12 | + requiresTypeChecking: true, |
17 | 13 | }, |
18 | 14 | fixable: undefined, |
19 | 15 | hasSuggestions: false, |
20 | 16 | messages: { |
21 | | - forbidden: "Subscribe calls within pipe operators are forbidden.", |
| 17 | + forbidden: 'Subscribe calls within pipe operators are forbidden.', |
22 | 18 | }, |
23 | 19 | schema: [], |
24 | | - type: "problem", |
| 20 | + type: 'problem', |
25 | 21 | }, |
26 | | - name: "no-subscribe-in-pipe", |
| 22 | + name: 'no-subscribe-in-pipe', |
27 | 23 | create: (context) => { |
28 | 24 | const { couldBeObservable, couldBeType } = getTypeServices(context); |
29 | 25 |
|
30 | 26 | function isWithinPipe(node: es.Node): boolean { |
31 | | - let parent = getParent(node); |
| 27 | + let parent = node.parent; |
| 28 | + |
32 | 29 | while (parent) { |
33 | 30 | if ( |
34 | | - parent.type === "CallExpression" && |
35 | | - parent.callee.type === "MemberExpression" && |
36 | | - parent.callee.property.type === "Identifier" && |
37 | | - parent.callee.property.name === "pipe" |
| 31 | + parent.type === AST_NODE_TYPES.CallExpression |
| 32 | + && parent.callee.type === AST_NODE_TYPES.MemberExpression |
| 33 | + && parent.callee.property.type === AST_NODE_TYPES.Identifier |
| 34 | + && parent.callee.property.name === 'pipe' |
38 | 35 | ) { |
39 | 36 | return true; |
40 | 37 | } |
41 | | - parent = getParent(parent); |
| 38 | + parent = node.parent; |
42 | 39 | } |
43 | 40 | return false; |
44 | 41 | } |
45 | 42 |
|
46 | 43 | return { |
47 | | - "CallExpression > MemberExpression[property.name='subscribe']": ( |
48 | | - node: es.MemberExpression |
| 44 | + 'CallExpression > MemberExpression[property.name=\'subscribe\']': ( |
| 45 | + node: es.MemberExpression, |
49 | 46 | ) => { |
50 | 47 | if ( |
51 | | - !couldBeObservable(node.object) && |
52 | | - !couldBeType(node.object, "Subscribable") |
| 48 | + !couldBeObservable(node.object) |
| 49 | + && !couldBeType(node.object, 'Subscribable') |
53 | 50 | ) { |
54 | 51 | return; |
55 | 52 | } |
56 | 53 |
|
57 | 54 | if (isWithinPipe(node)) { |
58 | 55 | context.report({ |
59 | | - messageId: "forbidden", |
| 56 | + messageId: 'forbidden', |
60 | 57 | node: node.property, |
61 | 58 | }); |
62 | 59 | } |
63 | 60 | }, |
64 | 61 | }; |
65 | 62 | }, |
66 | 63 | }); |
67 | | - |
68 | | -export = rule; |
0 commit comments