1
1
import { TSESTree as es } from '@typescript-eslint/utils' ;
2
- import { getTypeServices , isIdentifier , isImport , isObjectExpression , isProperty } from '../etc' ;
2
+ import { getTypeServices , isIdentifier , isImport , isMemberExpression , isObjectExpression , isProperty } from '../etc' ;
3
3
import { ruleCreator } from '../utils' ;
4
4
5
5
export const noIgnoredDefaultValueRule = ruleCreator ( {
@@ -10,16 +10,16 @@ export const noIgnoredDefaultValueRule = ruleCreator({
10
10
requiresTypeChecking : true ,
11
11
} ,
12
12
messages : {
13
- forbidden : 'Ignoring the default value is forbidden.' ,
13
+ forbidden : 'Not specifying a default value is forbidden.' ,
14
14
} ,
15
15
schema : [ ] ,
16
16
type : 'problem' ,
17
17
} ,
18
18
name : 'no-ignored-default-value' ,
19
19
create : ( context ) => {
20
- const { couldBeObservable } = getTypeServices ( context ) ;
20
+ const { couldBeObservable, getType } = getTypeServices ( context ) ;
21
21
22
- function checkConfig ( configArg : es . ObjectExpression ) {
22
+ function checkConfigObj ( configArg : es . ObjectExpression ) {
23
23
if ( ! configArg . properties . some ( p => isProperty ( p ) && isIdentifier ( p . key ) && p . key . name === 'defaultValue' ) ) {
24
24
context . report ( {
25
25
messageId : 'forbidden' ,
@@ -28,6 +28,16 @@ export const noIgnoredDefaultValueRule = ruleCreator({
28
28
}
29
29
}
30
30
31
+ function checkConfigType ( configArg : es . Node ) {
32
+ const configArgType = getType ( configArg ) ;
33
+ if ( ! configArgType ?. getProperties ( ) . some ( p => p . name === 'defaultValue' ) ) {
34
+ context . report ( {
35
+ messageId : 'forbidden' ,
36
+ node : configArg ,
37
+ } ) ;
38
+ }
39
+ }
40
+
31
41
function checkFunctionArgs ( callExpression : es . CallExpression , reportNode : es . Node ) {
32
42
const scope = context . sourceCode . getScope ( callExpression ) ;
33
43
if ( ! isImport ( scope , 'firstValueFrom' , / ^ r x j s \/ ? / )
@@ -49,10 +59,17 @@ export const noIgnoredDefaultValueRule = ruleCreator({
49
59
} ) ;
50
60
return ;
51
61
}
62
+ if ( isIdentifier ( configArg ) ) {
63
+ checkConfigType ( configArg ) ;
64
+ return ;
65
+ } else if ( isMemberExpression ( configArg ) && isIdentifier ( configArg . property ) ) {
66
+ checkConfigType ( configArg . property ) ;
67
+ return ;
68
+ }
52
69
if ( ! isObjectExpression ( configArg ) ) {
53
70
return ;
54
71
}
55
- checkConfig ( configArg ) ;
72
+ checkConfigObj ( configArg ) ;
56
73
}
57
74
58
75
function checkOperatorArgs ( callExpression : es . CallExpression , reportNode : es . Node ) {
@@ -71,10 +88,17 @@ export const noIgnoredDefaultValueRule = ruleCreator({
71
88
return ;
72
89
}
73
90
const [ arg ] = args ;
91
+ if ( isIdentifier ( arg ) ) {
92
+ checkConfigType ( arg ) ;
93
+ return ;
94
+ } else if ( isMemberExpression ( arg ) && isIdentifier ( arg . property ) ) {
95
+ checkConfigType ( arg . property ) ;
96
+ return ;
97
+ }
74
98
if ( ! isObjectExpression ( arg ) ) {
75
99
return ;
76
100
}
77
- checkConfig ( arg ) ;
101
+ checkConfigObj ( arg ) ;
78
102
}
79
103
80
104
return {
0 commit comments