4
4
*/
5
5
'use strict'
6
6
7
+ /**
8
+ * @typedef {import('vue-eslint-parser').AST.ESLintExpression } Expression
9
+ * @typedef {import('vue-eslint-parser').AST.ESLintObjectExpression } ObjectExpression
10
+ * @typedef {import('vue-eslint-parser').AST.ESLintPattern } Pattern
11
+ */
12
+ /**
13
+ * @typedef {import('../utils').ComponentObjectProp } ComponentObjectProp
14
+ * @typedef {ComponentObjectProp & { value: ObjectExpression} } ComponentObjectPropObject
15
+ */
16
+
7
17
const utils = require ( '../utils' )
8
18
9
19
const NATIVE_TYPES = new Set ( [
@@ -39,14 +49,14 @@ module.exports = {
39
49
40
50
/**
41
51
* Checks if the passed prop is required
42
- * @param {Property } prop - Property AST node for a single prop
52
+ * @param {ComponentObjectPropObject } prop - Property AST node for a single prop
43
53
* @return {boolean }
44
54
*/
45
55
function propIsRequired ( prop ) {
46
56
const propRequiredNode = prop . value . properties
47
57
. find ( p =>
48
58
p . type === 'Property' &&
49
- p . key . name === 'required' &&
59
+ utils . getStaticPropertyName ( p ) === 'required' &&
50
60
p . value . type === 'Literal' &&
51
61
p . value . value === true
52
62
)
@@ -56,38 +66,38 @@ module.exports = {
56
66
57
67
/**
58
68
* Checks if the passed prop has a default value
59
- * @param {Property } prop - Property AST node for a single prop
69
+ * @param {ComponentObjectPropObject } prop - Property AST node for a single prop
60
70
* @return {boolean }
61
71
*/
62
72
function propHasDefault ( prop ) {
63
73
const propDefaultNode = prop . value . properties
64
74
. find ( p =>
65
- p . key &&
66
- ( p . key . name === 'default' || p . key . value === 'default' )
75
+ p . type === 'Property' && utils . getStaticPropertyName ( p ) === 'default'
67
76
)
68
77
69
78
return Boolean ( propDefaultNode )
70
79
}
71
80
72
81
/**
73
82
* Finds all props that don't have a default value set
74
- * @param {Array } props - Vue component's "props" node
75
- * @return {Array } Array of props without "default" value
83
+ * @param {ComponentObjectProp[] } props - Vue component's "props" node
84
+ * @return {ComponentObjectProp[] } Array of props without "default" value
76
85
*/
77
86
function findPropsWithoutDefaultValue ( props ) {
78
87
return props
79
88
. filter ( prop => {
80
89
if ( prop . value . type !== 'ObjectExpression' ) {
81
- return ( prop . value . type !== 'CallExpression' && prop . value . type !== 'Identifier' ) || NATIVE_TYPES . has ( prop . value . name )
90
+ return ( prop . value . type !== 'CallExpression' && prop . value . type !== 'Identifier' ) ||
91
+ ( prop . value . type === 'Identifier' && NATIVE_TYPES . has ( prop . value . name ) )
82
92
}
83
93
84
- return ! propIsRequired ( prop ) && ! propHasDefault ( prop )
94
+ return ! propIsRequired ( /** @type { ComponentObjectPropObject } */ ( prop ) ) && ! propHasDefault ( /** @type { ComponentObjectPropObject } */ ( prop ) )
85
95
} )
86
96
}
87
97
88
98
/**
89
99
* Detects whether given value node is a Boolean type
90
- * @param {Node } value
100
+ * @param {Expression | Pattern } value
91
101
* @return {Boolean }
92
102
*/
93
103
function isValueNodeOfBooleanType ( value ) {
@@ -104,15 +114,16 @@ module.exports = {
104
114
105
115
/**
106
116
* Detects whether given prop node is a Boolean
107
- * @param {Node } prop
117
+ * @param {ComponentObjectProp } prop
108
118
* @return {Boolean }
109
119
*/
110
120
function isBooleanProp ( prop ) {
111
121
const value = utils . unwrapTypes ( prop . value )
112
122
113
123
return isValueNodeOfBooleanType ( value ) || (
114
124
value . type === 'ObjectExpression' &&
115
- value . properties . find ( p =>
125
+ value . properties . some ( p =>
126
+ p . type === 'Property' &&
116
127
p . key . type === 'Identifier' &&
117
128
p . key . name === 'type' &&
118
129
isValueNodeOfBooleanType ( p . value )
@@ -122,8 +133,8 @@ module.exports = {
122
133
123
134
/**
124
135
* Excludes purely Boolean props from the Array
125
- * @param {Array } props - Array with props
126
- * @return {Array }
136
+ * @param {ComponentObjectProp[] } props - Array with props
137
+ * @return {ComponentObjectProp[] }
127
138
*/
128
139
function excludeBooleanProps ( props ) {
129
140
return props . filter ( prop => ! isBooleanProp ( prop ) )
@@ -135,9 +146,9 @@ module.exports = {
135
146
136
147
return utils . executeOnVue ( context , ( obj ) => {
137
148
const props = utils . getComponentProps ( obj )
138
- . filter ( prop => prop . key && prop . value && ! prop . node . shorthand )
149
+ . filter ( prop => prop . key && prop . value && ! ( prop . node . type === 'Property' && prop . node . shorthand ) )
139
150
140
- const propsWithoutDefault = findPropsWithoutDefaultValue ( props )
151
+ const propsWithoutDefault = findPropsWithoutDefaultValue ( /** @type { ComponentObjectProp[] } */ ( props ) )
141
152
const propsToReport = excludeBooleanProps ( propsWithoutDefault )
142
153
143
154
for ( const prop of propsToReport ) {
0 commit comments