@@ -30,6 +30,9 @@ const MAP_SCHEMA = {
30
30
message : {
31
31
type : 'string'
32
32
} ,
33
+ isRequired : {
34
+ type : 'boolean'
35
+ } ,
33
36
fix : {
34
37
type : 'object' ,
35
38
required : [ 'propName' ] ,
@@ -80,32 +83,48 @@ module.exports = {
80
83
}
81
84
}
82
85
86
+ function reportRequiredProps ( { node, name, prop, message : customMessage } ) {
87
+ try {
88
+ const message = `The '${ name } ' component's prop '${ prop } ' is required. ${ customMessage } ` ;
89
+ context . report ( {
90
+ node : node ,
91
+ message
92
+ } ) ;
93
+ } catch ( err ) {
94
+ console . log ( 'Found error in: ' , context . getFilename ( ) ) ;
95
+ }
96
+ }
97
+
83
98
const { deprecations} = context . options [ 0 ] ;
84
99
const organizedDeprecations = organizeDeprecations ( deprecations ) ;
85
100
86
101
const imports = [ ] ;
87
102
88
103
function checkPropDeprecation ( node , fixNode , propName , deprecatedPropList , componentName ) {
89
104
const deprecatedProp = _ . find ( deprecatedPropList , { prop : propName } ) ;
90
- if ( deprecatedProp ) {
105
+ if ( deprecatedProp && ! deprecatedProp . isRequired ) {
91
106
const { prop, message, fix} = deprecatedProp ;
92
107
reportDeprecatedProps ( { node, name : componentName , prop, message, fixNode, fix} ) ;
93
108
}
109
+
110
+ return ! ! deprecatedProp ;
94
111
}
95
112
96
113
function testAttributeForDeprecation ( attribute , deprecatedPropList , componentName ) {
114
+ let wasFound = false ;
97
115
if ( attribute . type === 'JSXAttribute' ) {
98
- checkPropDeprecation ( attribute , attribute . name , attribute . name . name , deprecatedPropList , componentName ) ;
116
+ wasFound = checkPropDeprecation ( attribute , attribute . name , attribute . name . name , deprecatedPropList , componentName ) ;
99
117
} else if ( attribute . type === 'JSXSpreadAttribute' ) {
100
118
const spreadSource = findValueNodeOfIdentifier ( attribute . argument . name , context . getScope ( ) ) ;
101
119
if ( spreadSource ) {
102
120
_ . forEach ( spreadSource . properties , property => {
103
121
const key = _ . get ( property , 'key' ) ;
104
122
const propName = _ . get ( property , 'key.name' ) ;
105
- checkPropDeprecation ( key , key , propName , deprecatedPropList , componentName ) ;
123
+ wasFound = checkPropDeprecation ( key , key , propName , deprecatedPropList , componentName ) ;
106
124
} ) ;
107
125
}
108
126
}
127
+ return wasFound ;
109
128
}
110
129
111
130
function deprecationCheck ( node ) {
@@ -123,13 +142,29 @@ module.exports = {
123
142
currentImport ,
124
143
deprecationSource
125
144
) ;
126
-
145
+
127
146
foundPossibleDeprecations . forEach ( foundPossibleDeprecation => {
128
- const deprecatedPropList = foundPossibleDeprecation . props ;
147
+ const deprecatedPropList = [ ...foundPossibleDeprecation . props ] ;
148
+ const requiredPropList = _ . remove ( deprecatedPropList , p => ! ! p . isRequired ) ;
129
149
const attributes = node . attributes ;
150
+
151
+ /* handle deprecated props */
152
+ if ( ! _ . isEmpty ( deprecatedPropList ) ) {
153
+ attributes . forEach ( attribute => {
154
+ testAttributeForDeprecation ( attribute , deprecatedPropList , componentName ) ;
155
+ } ) ;
156
+ }
157
+
158
+ /* handle required props */
159
+ let foundAttribute = false ;
130
160
attributes . forEach ( attribute => {
131
- testAttributeForDeprecation ( attribute , deprecatedPropList , componentName ) ;
161
+ foundAttribute = foundAttribute || testAttributeForDeprecation ( attribute , requiredPropList , componentName ) ;
132
162
} ) ;
163
+
164
+ if ( ! foundAttribute && requiredPropList [ 0 ] ) {
165
+ const prop = requiredPropList [ 0 ] ;
166
+ reportRequiredProps ( { node, name : componentName , prop : prop . prop , message : prop . message } )
167
+ }
133
168
} ) ;
134
169
}
135
170
}
0 commit comments