Skip to content

Commit 21e7fc5

Browse files
committed
Add support for array of props
1 parent c73f5b4 commit 21e7fc5

File tree

2 files changed

+94
-162
lines changed

2 files changed

+94
-162
lines changed

lib/rules/require-valid-default-prop.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ module.exports = {
4646
)
4747
}
4848

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+
4960
function ucFirst (text) {
5061
return text[0].toUpperCase() + text.slice(1)
5162
}
@@ -91,28 +102,30 @@ module.exports = {
91102

92103
for (const prop of properties) {
93104
const type = getPropertyNode(prop.value, 'type')
94-
let typeName = type ? type.value.name : null
95-
if (!NATIVE_TYPES.has(typeName)) {
105+
if (!type) {
96106
return
97107
}
98108

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
101115
}
102116

103117
const def = getPropertyNode(prop.value, 'default')
104118
if (!def) return
105119

106120
const defType = getValueType(def.value)
107-
if (defType === typeName) return
121+
if (typeNames.has(defType)) return
108122

109123
context.report({
110124
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}}.",
112126
data: {
113127
name: prop.key.name,
114-
defType,
115-
typeName
128+
types: Array.from(typeNames).join(' or ').toLowerCase()
116129
}
117130
})
118131
}

0 commit comments

Comments
 (0)