@@ -46,6 +46,17 @@ module.exports = {
46
46
)
47
47
}
48
48
49
+ function getTypes ( node ) {
50
+ if ( node . type === 'Identifier' ) {
51
+ return [ node . name ]
52
+ } else if ( node . type === 'ArrayExpression' ) {
53
+ return node . elements
54
+ . filter ( item => item . type === 'Identifier' )
55
+ . map ( item => item . name )
56
+ }
57
+ return [ ]
58
+ }
59
+
49
60
function ucFirst ( text ) {
50
61
return text [ 0 ] . toUpperCase ( ) + text . slice ( 1 )
51
62
}
@@ -91,28 +102,30 @@ module.exports = {
91
102
92
103
for ( const prop of properties ) {
93
104
const type = getPropertyNode ( prop . value , 'type' )
94
- let typeName = type ? type . value . name : null
95
- if ( ! NATIVE_TYPES . has ( typeName ) ) {
105
+ if ( ! type ) {
96
106
return
97
107
}
98
108
99
- if ( typeName === 'Object' || typeName === 'Array' ) {
100
- typeName = 'Function'
109
+ const typeNames = new Set ( getTypes ( type . value )
110
+ . map ( item => item === 'Object' || item === 'Array' ? 'Function' : item ) // Object and Array require function
111
+ . filter ( item => NATIVE_TYPES . has ( item ) ) )
112
+
113
+ if ( typeNames . size === 0 ) { // There is no native types detected
114
+ return
101
115
}
102
116
103
117
const def = getPropertyNode ( prop . value , 'default' )
104
118
if ( ! def ) return
105
119
106
120
const defType = getValueType ( def . value )
107
- if ( defType === typeName ) return
121
+ if ( typeNames . has ( defType ) ) return
108
122
109
123
context . report ( {
110
124
node : def ,
111
- message : "Type of default value prop '{{name}}' is a '{{defType}}' but should be a '{{typeName}}' ." ,
125
+ message : "Type of default value prop '{{name}}' must be a {{types}} ." ,
112
126
data : {
113
127
name : prop . key . name ,
114
- defType,
115
- typeName
128
+ types : Array . from ( typeNames ) . join ( ' or ' ) . toLowerCase ( )
116
129
}
117
130
} )
118
131
}
0 commit comments