6
6
7
7
const utils = require ( '../utils' )
8
8
9
+ const NATIVE_TYPES = new Set ( [
10
+ 'String' ,
11
+ 'Number' ,
12
+ 'Boolean' ,
13
+ 'Function' ,
14
+ 'Object' ,
15
+ 'Array' ,
16
+ 'Symbol'
17
+ ] )
18
+
9
19
// ------------------------------------------------------------------------------
10
20
// Rule Definition
11
21
// ------------------------------------------------------------------------------
@@ -32,7 +42,7 @@ module.exports = {
32
42
* @return {boolean }
33
43
*/
34
44
function propIsRequired ( prop ) {
35
- const propRequiredNode = utils . unwrapTypes ( prop . value ) . properties
45
+ const propRequiredNode = prop . value . properties
36
46
. find ( p =>
37
47
p . type === 'Property' &&
38
48
p . key . name === 'required' &&
@@ -49,7 +59,7 @@ module.exports = {
49
59
* @return {boolean }
50
60
*/
51
61
function propHasDefault ( prop ) {
52
- const propDefaultNode = utils . unwrapTypes ( prop . value ) . properties
62
+ const propDefaultNode = prop . value . properties
53
63
. find ( p =>
54
64
p . key &&
55
65
( p . key . name === 'default' || p . key . value === 'default' )
@@ -60,15 +70,14 @@ module.exports = {
60
70
61
71
/**
62
72
* Finds all props that don't have a default value set
63
- * @param {Property } propsNode - Vue component's "props" node
73
+ * @param {Array } props - Vue component's "props" node
64
74
* @return {Array } Array of props without "default" value
65
75
*/
66
- function findPropsWithoutDefaultValue ( propsNode ) {
67
- return propsNode . value . properties
68
- . filter ( prop => prop . type === 'Property' )
76
+ function findPropsWithoutDefaultValue ( props ) {
77
+ return props
69
78
. filter ( prop => {
70
- if ( utils . unwrapTypes ( prop . value ) . type !== 'ObjectExpression' ) {
71
- return true
79
+ if ( prop . value . type !== 'ObjectExpression' ) {
80
+ return ( prop . value . type !== 'CallExpression' && prop . value . type !== 'Identifier' ) || NATIVE_TYPES . has ( prop . value . name )
72
81
}
73
82
74
83
return ! propIsRequired ( prop ) && ! propHasDefault ( prop )
@@ -124,22 +133,16 @@ module.exports = {
124
133
// ----------------------------------------------------------------------
125
134
126
135
return utils . executeOnVue ( context , ( obj ) => {
127
- const propsNode = obj . properties
128
- . find ( p =>
129
- p . type === 'Property' &&
130
- p . key . type === 'Identifier' &&
131
- p . key . name === 'props' &&
132
- p . value . type === 'ObjectExpression'
133
- )
134
-
135
- if ( ! propsNode ) return
136
+ const props = utils . getPropsProperties ( obj )
137
+ . props
138
+ . filter ( prop => prop . value && ! prop . node . shorthand )
136
139
137
- const propsWithoutDefault = findPropsWithoutDefaultValue ( propsNode )
140
+ const propsWithoutDefault = findPropsWithoutDefaultValue ( props )
138
141
const propsToReport = excludeBooleanProps ( propsWithoutDefault )
139
142
140
143
for ( const prop of propsToReport ) {
141
144
context . report ( {
142
- node : prop ,
145
+ node : prop . node ,
143
146
message : `Prop '{{propName}}' requires default value to be set.` ,
144
147
data : {
145
148
propName : prop . key . name
0 commit comments