1
- import { TSESTree as es } from '@typescript-eslint/utils' ;
2
- import { getTypeServices } from '../etc' ;
1
+ import { TSESTree as es , ESLintUtils } from '@typescript-eslint/utils' ;
2
+ import { getTypeServices , isIdentifier , isMemberExpression , isObjectExpression , isProperty } from '../etc' ;
3
3
import { ruleCreator } from '../utils' ;
4
4
5
5
export const noIgnoredErrorRule = ruleCreator ( {
@@ -18,18 +18,59 @@ export const noIgnoredErrorRule = ruleCreator({
18
18
} ,
19
19
name : 'no-ignored-error' ,
20
20
create : ( context ) => {
21
+ const { getTypeAtLocation } = ESLintUtils . getParserServices ( context ) ;
21
22
const { couldBeObservable, couldBeFunction } = getTypeServices ( context ) ;
22
23
24
+ function isMissingErrorCallback ( callExpression : es . CallExpression ) : boolean {
25
+ if ( callExpression . arguments . length >= 2 ) {
26
+ return false ;
27
+ }
28
+ return couldBeFunction ( callExpression . arguments [ 0 ] ) ;
29
+ }
30
+
31
+ function isObjMissingError ( arg : es . ObjectExpression ) : boolean {
32
+ return ! arg . properties . some (
33
+ property =>
34
+ isProperty ( property )
35
+ && isIdentifier ( property . key )
36
+ && property . key . name === 'error' ,
37
+ ) ;
38
+ }
39
+
40
+ function isTypeMissingError ( arg : es . Identifier ) : boolean {
41
+ const argType = getTypeAtLocation ( arg ) ;
42
+ return ! argType ?. getProperties ( ) . some ( p => p . name === 'error' ) ;
43
+ }
44
+
45
+ function isMissingErrorProperty ( callExpression : es . CallExpression ) : boolean {
46
+ if ( callExpression . arguments . length !== 1 ) {
47
+ return false ;
48
+ }
49
+
50
+ const [ arg ] = callExpression . arguments ;
51
+
52
+ if ( isObjectExpression ( arg ) ) {
53
+ return isObjMissingError ( arg ) ;
54
+ }
55
+ if ( isIdentifier ( arg ) ) {
56
+ return isTypeMissingError ( arg ) ;
57
+ }
58
+ if ( isMemberExpression ( arg ) && isIdentifier ( arg . property ) ) {
59
+ return isTypeMissingError ( arg . property ) ;
60
+ }
61
+ return false ;
62
+ }
63
+
23
64
return {
24
65
'CallExpression[arguments.length > 0] > MemberExpression > Identifier[name=\'subscribe\']' :
25
66
( node : es . Identifier ) => {
26
67
const memberExpression = node . parent as es . MemberExpression ;
27
68
const callExpression = memberExpression . parent as es . CallExpression ;
28
69
29
70
if (
30
- callExpression . arguments . length < 2
71
+ ( isMissingErrorCallback ( callExpression )
72
+ || isMissingErrorProperty ( callExpression ) )
31
73
&& couldBeObservable ( memberExpression . object )
32
- && couldBeFunction ( callExpression . arguments [ 0 ] )
33
74
) {
34
75
context . report ( {
35
76
messageId : 'forbidden' ,
0 commit comments