1
1
import { TSESTree as es , ESLintUtils } from '@typescript-eslint/utils' ;
2
- import { getTypeServices , isIdentifier , isImport , isMemberExpression , isObjectExpression , isProperty } from '../etc' ;
2
+ import { getTypeServices , isIdentifier , isMemberExpression , isObjectExpression , isProperty } from '../etc' ;
3
3
import { ruleCreator } from '../utils' ;
4
4
5
5
export const noIgnoredDefaultValueRule = ruleCreator ( {
@@ -18,7 +18,7 @@ export const noIgnoredDefaultValueRule = ruleCreator({
18
18
name : 'no-ignored-default-value' ,
19
19
create : ( context ) => {
20
20
const { getTypeAtLocation } = ESLintUtils . getParserServices ( context ) ;
21
- const { couldBeObservable } = getTypeServices ( context ) ;
21
+ const { couldBeObservable, couldBeType } = getTypeServices ( context ) ;
22
22
23
23
function checkConfigObj ( configArg : es . ObjectExpression ) {
24
24
if ( ! configArg . properties . some ( p => isProperty ( p ) && isIdentifier ( p . key ) && p . key . name === 'defaultValue' ) ) {
@@ -39,13 +39,25 @@ export const noIgnoredDefaultValueRule = ruleCreator({
39
39
}
40
40
}
41
41
42
- function checkFunctionArgs ( callExpression : es . CallExpression , reportNode : es . Node ) {
43
- const scope = context . sourceCode . getScope ( callExpression ) ;
44
- if ( ! isImport ( scope , 'firstValueFrom' , / ^ r x j s \/ ? / )
45
- && ! isImport ( scope , 'lastValueFrom' , / ^ r x j s \/ ? / ) ) {
42
+ function checkArg ( arg : es . Node ) {
43
+ if ( isIdentifier ( arg ) ) {
44
+ checkConfigType ( arg ) ;
45
+ return ;
46
+ } else if ( isMemberExpression ( arg ) && isIdentifier ( arg . property ) ) {
47
+ checkConfigType ( arg . property ) ;
48
+ return ;
49
+ }
50
+ if ( ! isObjectExpression ( arg ) ) {
51
+ return ;
52
+ }
53
+ checkConfigObj ( arg ) ;
54
+ }
55
+
56
+ function checkFunctionArgs ( node : es . Node , args : es . CallExpressionArgument [ ] ) {
57
+ if ( ! couldBeType ( node , 'firstValueFrom' , { name : / [ / \\ ] r x j s [ / \\ ] / } )
58
+ && ! couldBeType ( node , 'lastValueFrom' , { name : / [ / \\ ] r x j s [ / \\ ] / } ) ) {
46
59
return ;
47
60
}
48
- const { arguments : args } = callExpression ;
49
61
if ( ! args || args . length <= 0 ) {
50
62
return ;
51
63
}
@@ -56,62 +68,44 @@ export const noIgnoredDefaultValueRule = ruleCreator({
56
68
if ( ! configArg ) {
57
69
context . report ( {
58
70
messageId : 'forbidden' ,
59
- node : reportNode ,
71
+ node,
60
72
} ) ;
61
73
return ;
62
74
}
63
- if ( isIdentifier ( configArg ) ) {
64
- checkConfigType ( configArg ) ;
65
- return ;
66
- } else if ( isMemberExpression ( configArg ) && isIdentifier ( configArg . property ) ) {
67
- checkConfigType ( configArg . property ) ;
68
- return ;
69
- }
70
- if ( ! isObjectExpression ( configArg ) ) {
71
- return ;
72
- }
73
- checkConfigObj ( configArg ) ;
75
+ checkArg ( configArg ) ;
74
76
}
75
77
76
- function checkOperatorArgs ( callExpression : es . CallExpression , reportNode : es . Node ) {
77
- const scope = context . sourceCode . getScope ( callExpression ) ;
78
- if ( ! isImport ( scope , 'first' , / ^ r x j s \/ ? / )
79
- && ! isImport ( scope , 'last' , / ^ r x j s \/ ? / ) ) {
78
+ function checkOperatorArgs ( node : es . Node , args : es . CallExpressionArgument [ ] ) {
79
+ if ( ! couldBeType ( node , 'first' , { name : / [ / \\ ] r x j s [ / \\ ] / } )
80
+ && ! couldBeType ( node , 'last' , { name : / [ / \\ ] r x j s [ / \\ ] / } ) ) {
80
81
return ;
81
82
}
82
- const { arguments : args } = callExpression ;
83
83
84
84
if ( ! args || args . length <= 0 ) {
85
85
context . report ( {
86
86
messageId : 'forbidden' ,
87
- node : reportNode ,
87
+ node,
88
88
} ) ;
89
89
return ;
90
90
}
91
91
const [ arg ] = args ;
92
- if ( isIdentifier ( arg ) ) {
93
- checkConfigType ( arg ) ;
94
- return ;
95
- } else if ( isMemberExpression ( arg ) && isIdentifier ( arg . property ) ) {
96
- checkConfigType ( arg . property ) ;
97
- return ;
98
- }
99
- if ( ! isObjectExpression ( arg ) ) {
100
- return ;
101
- }
102
- checkConfigObj ( arg ) ;
92
+ checkArg ( arg ) ;
103
93
}
104
94
105
95
return {
106
- 'CallExpression[callee.name=/^(firstValueFrom|lastValueFrom)$/]' : (
107
- node : es . CallExpression ,
108
- ) => {
109
- checkFunctionArgs ( node , node . callee ) ;
96
+ 'CallExpression[callee.name=/^(firstValueFrom|lastValueFrom)$/]' : ( node : es . CallExpression ) => {
97
+ checkFunctionArgs ( node . callee , node . arguments ) ;
98
+ } ,
99
+ 'CallExpression[callee.property.name=/^(firstValueFrom|lastValueFrom)$/]' : ( node : es . CallExpression ) => {
100
+ const memberExpression = node . callee as es . MemberExpression ;
101
+ checkFunctionArgs ( memberExpression . property , node . arguments ) ;
102
+ } ,
103
+ 'CallExpression[callee.property.name=\'pipe\'] > CallExpression[callee.name=/^(first|last)$/]' : ( node : es . CallExpression ) => {
104
+ checkOperatorArgs ( node . callee , node . arguments ) ;
110
105
} ,
111
- 'CallExpression[callee.property.name=\'pipe\'] > CallExpression[callee.name=/^(first|last)$/]' : (
112
- node : es . CallExpression ,
113
- ) => {
114
- checkOperatorArgs ( node , node . callee ) ;
106
+ 'CallExpression[callee.property.name=\'pipe\'] > CallExpression[callee.property.name=/^(first|last)$/]' : ( node : es . CallExpression ) => {
107
+ const memberExpression = node . callee as es . MemberExpression ;
108
+ checkOperatorArgs ( memberExpression . property , node . arguments ) ;
115
109
} ,
116
110
} ;
117
111
} ,
0 commit comments