7
7
/**
8
8
* @typedef {import('../utils').ComponentProp } ComponentProp
9
9
* @typedef {import('../utils').ComponentObjectProp } ComponentObjectProp
10
+ * @typedef {import('../utils').ComponentTypeProp } ComponentTypeProp
10
11
* @typedef {ComponentObjectProp & { value: ObjectExpression} } ComponentObjectPropObject
11
12
*/
12
13
@@ -137,18 +138,23 @@ module.exports = {
137
138
138
139
/**
139
140
* @param {ComponentProp[] } props
140
- * @param {boolean } [withDefaults]
141
- * @param { { [key: string]: Expression | undefined } } [withDefaultsExpressions]
141
+ * @param {(prop: ComponentObjectProp|ComponentTypeProp)=>boolean } [ignore]
142
142
*/
143
- function processProps ( props , withDefaults , withDefaultsExpressions ) {
143
+ function processProps ( props , ignore ) {
144
144
for ( const prop of props ) {
145
- if ( prop . type === 'object' && ! prop . node . shorthand ) {
145
+ if ( prop . type === 'object' ) {
146
+ if ( prop . node . shorthand ) {
147
+ continue
148
+ }
146
149
if ( ! isWithoutDefaultValue ( prop ) ) {
147
150
continue
148
151
}
149
152
if ( isBooleanProp ( prop ) ) {
150
153
continue
151
154
}
155
+ if ( ignore ?. ( prop ) ) {
156
+ continue
157
+ }
152
158
const propName =
153
159
prop . propName == null
154
160
? `[${ context . getSourceCode ( ) . getText ( prop . node . key ) } ]`
@@ -161,38 +167,57 @@ module.exports = {
161
167
propName
162
168
}
163
169
} )
164
- } else if (
165
- prop . type === 'type' &&
166
- withDefaults &&
167
- withDefaultsExpressions
168
- ) {
170
+ } else if ( prop . type === 'type' ) {
169
171
if ( prop . required ) {
170
172
continue
171
173
}
172
174
if ( prop . types . length === 1 && prop . types [ 0 ] === 'Boolean' ) {
173
175
continue
174
176
}
175
- if ( ! withDefaultsExpressions [ prop . propName ] ) {
176
- context . report ( {
177
- node : prop . node ,
178
- messageId : `missingDefault` ,
179
- data : {
180
- propName : prop . propName
181
- }
182
- } )
177
+ if ( ignore ?. ( prop ) ) {
178
+ continue
183
179
}
180
+ context . report ( {
181
+ node : prop . node ,
182
+ messageId : `missingDefault` ,
183
+ data : {
184
+ propName : prop . propName
185
+ }
186
+ } )
184
187
}
185
188
}
186
189
}
187
190
188
191
return utils . compositingVisitors (
189
192
utils . defineScriptSetupVisitor ( context , {
190
193
onDefinePropsEnter ( node , props ) {
191
- processProps (
192
- props ,
193
- utils . hasWithDefaults ( node ) ,
194
+ const hasWithDefaults = utils . hasWithDefaults ( node )
195
+ const defaultsByWithDefaults =
194
196
utils . getWithDefaultsPropExpressions ( node )
195
- )
197
+ const isUsingPropsDestructure = utils . isUsingPropsDestructure ( node )
198
+ const defaultsByAssignmentPatterns =
199
+ utils . getDefaultPropExpressionsForPropsDestructure ( node )
200
+
201
+ processProps ( props , ( prop ) => {
202
+ if ( prop . type === 'type' ) {
203
+ if ( ! hasWithDefaults ) {
204
+ // If don't use withDefaults(), exclude it from the report.
205
+ return true
206
+ }
207
+ if ( defaultsByWithDefaults [ prop . propName ] ) {
208
+ return true
209
+ }
210
+ }
211
+ if ( ! isUsingPropsDestructure ) {
212
+ return false
213
+ }
214
+ if ( prop . propName == null ) {
215
+ // If using Props Destructure but the property name cannot be determined,
216
+ // it will be ignored.
217
+ return true
218
+ }
219
+ return Boolean ( defaultsByAssignmentPatterns [ prop . propName ] )
220
+ } )
196
221
}
197
222
} ) ,
198
223
utils . executeOnVue ( context , ( obj ) => {
0 commit comments