-
-
Notifications
You must be signed in to change notification settings - Fork 679
/
Copy pathrequire-default-prop.js
151 lines (132 loc) · 4.28 KB
/
require-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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @fileoverview Require default value for props
* @author Michał Sajnóg <[email protected]> (http://github.com/michalsnik)
*/
'use strict'
const utils = require('../utils')
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'require default value for props',
category: 'strongly-recommended',
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.1/docs/rules/require-default-prop.md'
},
fixable: null, // or "code" or "whitespace"
schema: []
},
create: function (context) {
// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------
/**
* Checks if the passed prop is required
* @param {Property} prop - Property AST node for a single prop
* @return {boolean}
*/
function propIsRequired (prop) {
const propRequiredNode = prop.value.properties
.find(p =>
p.type === 'Property' &&
p.key.name === 'required' &&
p.value.type === 'Literal' &&
p.value.value === true
)
return Boolean(propRequiredNode)
}
/**
* Checks if the passed prop has a default value
* @param {Property} prop - Property AST node for a single prop
* @return {boolean}
*/
function propHasDefault (prop) {
const propDefaultNode = prop.value.properties
.find(p =>
p.key &&
(p.key.name === 'default' || p.key.value === 'default')
)
return Boolean(propDefaultNode)
}
/**
* Finds all props that don't have a default value set
* @param {Property} propsNode - Vue component's "props" node
* @return {Array} Array of props without "default" value
*/
function findPropsWithoutDefaultValue (propsNode) {
return propsNode.value.properties
.filter(prop => prop.type === 'Property')
.filter(prop => {
if (prop.value.type !== 'ObjectExpression') {
return true
}
return !propIsRequired(prop) && !propHasDefault(prop)
})
}
/**
* Detects whether given value node is a Boolean type
* @param {Node} value
* @return {Boolean}
*/
function isValueNodeOfBooleanType (value) {
return (
value.type === 'Identifier' &&
value.name === 'Boolean'
) || (
value.type === 'ArrayExpression' &&
value.elements.length === 1 &&
value.elements[0].type === 'Identifier' &&
value.elements[0].name === 'Boolean'
)
}
/**
* Detects whether given prop node is a Boolean
* @param {Node} prop
* @return {Boolean}
*/
function isBooleanProp (prop) {
const value = prop.value
return isValueNodeOfBooleanType(value) || (
value.type === 'ObjectExpression' &&
value.properties.find(p =>
p.key.type === 'Identifier' &&
p.key.name === 'type' &&
isValueNodeOfBooleanType(p.value)
)
)
}
/**
* Excludes purely Boolean props from the Array
* @param {Array} props - Array with props
* @return {Array}
*/
function excludeBooleanProps (props) {
return props.filter(prop => !isBooleanProp(prop))
}
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
return utils.executeOnVue(context, (obj) => {
const propsNode = obj.properties
.find(p =>
p.type === 'Property' &&
p.key.type === 'Identifier' &&
p.key.name === 'props' &&
p.value.type === 'ObjectExpression'
)
if (!propsNode) return
const propsWithoutDefault = findPropsWithoutDefaultValue(propsNode)
const propsToReport = excludeBooleanProps(propsWithoutDefault)
propsToReport.forEach(prop => {
context.report({
node: prop,
message: `Prop '{{propName}}' requires default value to be set.`,
data: {
propName: prop.key.name
}
})
})
})
}
}