66
77const utils = require ( '../utils' )
88
9+ const NATIVE_TYPES = new Set ( [
10+ 'String' ,
11+ 'Number' ,
12+ 'Boolean' ,
13+ 'Function' ,
14+ 'Object' ,
15+ 'Array' ,
16+ 'Symbol'
17+ ] )
18+
919// ------------------------------------------------------------------------------
1020// Rule Definition
1121// ------------------------------------------------------------------------------
@@ -21,7 +31,7 @@ module.exports = {
2131 schema : [ ]
2232 } ,
2333
24- create : function ( context ) {
34+ create ( context ) {
2535 // ----------------------------------------------------------------------
2636 // Helpers
2737 // ----------------------------------------------------------------------
@@ -32,7 +42,7 @@ module.exports = {
3242 * @return {boolean }
3343 */
3444 function propIsRequired ( prop ) {
35- const propRequiredNode = utils . unwrapTypes ( prop . value ) . properties
45+ const propRequiredNode = prop . value . properties
3646 . find ( p =>
3747 p . type === 'Property' &&
3848 p . key . name === 'required' &&
@@ -49,7 +59,7 @@ module.exports = {
4959 * @return {boolean }
5060 */
5161 function propHasDefault ( prop ) {
52- const propDefaultNode = utils . unwrapTypes ( prop . value ) . properties
62+ const propDefaultNode = prop . value . properties
5363 . find ( p =>
5464 p . key &&
5565 ( p . key . name === 'default' || p . key . value === 'default' )
@@ -60,15 +70,14 @@ module.exports = {
6070
6171 /**
6272 * 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
6474 * @return {Array } Array of props without "default" value
6575 */
66- function findPropsWithoutDefaultValue ( propsNode ) {
67- return propsNode . value . properties
68- . filter ( prop => prop . type === 'Property' )
76+ function findPropsWithoutDefaultValue ( props ) {
77+ return props
6978 . 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 )
7281 }
7382
7483 return ! propIsRequired ( prop ) && ! propHasDefault ( prop )
@@ -124,28 +133,21 @@ module.exports = {
124133 // ----------------------------------------------------------------------
125134
126135 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 . getComponentProps ( obj )
137+ . filter ( prop => prop . key && prop . value && ! prop . node . shorthand )
136138
137- const propsWithoutDefault = findPropsWithoutDefaultValue ( propsNode )
139+ const propsWithoutDefault = findPropsWithoutDefaultValue ( props )
138140 const propsToReport = excludeBooleanProps ( propsWithoutDefault )
139141
140- propsToReport . forEach ( prop => {
142+ for ( const prop of propsToReport ) {
141143 context . report ( {
142- node : prop ,
144+ node : prop . node ,
143145 message : `Prop '{{propName}}' requires default value to be set.` ,
144146 data : {
145147 propName : prop . key . name
146148 }
147149 } )
148- } )
150+ }
149151 } )
150152 }
151153}
0 commit comments