This repository was archived by the owner on Jun 6, 2019. It is now read-only.
forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire-valid-default-prop.js
131 lines (113 loc) · 3.8 KB
/
require-valid-default-prop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* @fileoverview Enforces props default values to be valid.
* @author Armano
*/
'use strict'
const utils = require('../utils')
const NATIVE_TYPES = new Set([
'String',
'Number',
'Boolean',
'Function',
'Object',
'Array',
'Symbol'
])
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'enforce props default values to be valid',
category: 'essential',
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/master/docs/rules/require-valid-default-prop.md'
},
fixable: null,
schema: []
},
create (context) {
// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------
function isPropertyIdentifier (node) {
return node.type === 'Property' && node.key.type === 'Identifier'
}
function getPropertyNode (obj, name) {
return obj.properties.find(p =>
isPropertyIdentifier(p) &&
p.key.name === name
)
}
function getTypes (node) {
if (node.type === 'Identifier') {
return [node.name]
} else if (node.type === 'ArrayExpression') {
return node.elements
.filter(item => item.type === 'Identifier')
.map(item => item.name)
}
return []
}
function ucFirst (text) {
return text[0].toUpperCase() + text.slice(1)
}
function getValueType (node) {
if (node.type === 'CallExpression') { // Symbol(), Number() ...
if (node.callee.type === 'Identifier' && NATIVE_TYPES.has(node.callee.name)) {
return node.callee.name
}
} else if (node.type === 'TemplateLiteral') { // String
return 'String'
} else if (node.type === 'Literal') { // String, Boolean, Number
if (node.value === null) return null
const type = ucFirst(typeof node.value)
if (NATIVE_TYPES.has(type)) {
return type
}
} else if (node.type === 'ArrayExpression') { // Array
return 'Array'
} else if (node.type === 'ObjectExpression') { // Object
return 'Object'
}
// FunctionExpression, ArrowFunctionExpression
return null
}
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
return utils.executeOnVue(context, obj => {
const props = obj.properties.find(p =>
isPropertyIdentifier(p) &&
p.key.name === 'props' &&
p.value.type === 'ObjectExpression'
)
if (!props) return
const properties = props.value.properties.filter(p =>
isPropertyIdentifier(p) &&
p.value.type === 'ObjectExpression'
)
for (const prop of properties) {
const type = getPropertyNode(prop.value, 'type')
if (!type) continue
const typeNames = new Set(getTypes(type.value)
.map(item => item === 'Object' || item === 'Array' ? 'Function' : item) // Object and Array require function
.filter(item => NATIVE_TYPES.has(item)))
// There is no native types detected
if (typeNames.size === 0) continue
const def = getPropertyNode(prop.value, 'default')
if (!def) continue
const defType = getValueType(def.value)
if (!defType || typeNames.has(defType)) continue
context.report({
node: def,
message: "Type of the default value for '{{name}}' prop must be a {{types}}.",
data: {
name: prop.key.name,
types: Array.from(typeNames).join(' or ').toLowerCase()
}
})
}
})
}
}