5
5
6
6
'use strict' ;
7
7
8
+ const minimatch = require ( 'minimatch' ) ;
8
9
const Components = require ( '../util/Components' ) ;
9
10
const docsUrl = require ( '../util/docsUrl' ) ;
10
11
const astUtil = require ( '../util/ast' ) ;
@@ -32,12 +33,13 @@ function generateErrorMessageWithParentName(parentName) {
32
33
}
33
34
34
35
/**
35
- * Check whether given text starts with `render`. Comparison is case-sensitive .
36
+ * Check whether given text matches the pattern passed in .
36
37
* @param {string } text Text to validate
38
+ * @param {string } pattern Pattern to match against
37
39
* @returns {boolean }
38
40
*/
39
- function startsWithRender ( text ) {
40
- return typeof text === 'string' && text . startsWith ( 'render' ) ;
41
+ function propMatchesRenderPropPattern ( text , pattern ) {
42
+ return typeof text === 'string' && minimatch ( text , pattern ) ;
41
43
}
42
44
43
45
/**
@@ -165,15 +167,16 @@ function isReturnStatementOfHook(node, context) {
165
167
* ```
166
168
* @param {ASTNode } node The AST node
167
169
* @param {Context } context eslint context
170
+ * @param {string } propNamePattern a pattern to match render props against
168
171
* @returns {boolean } True if component is declared inside a render prop, false if not
169
172
*/
170
- function isComponentInRenderProp ( node , context ) {
173
+ function isComponentInRenderProp ( node , context , propNamePattern ) {
171
174
if (
172
175
node
173
176
&& node . parent
174
177
&& node . parent . type === 'Property'
175
178
&& node . parent . key
176
- && startsWithRender ( node . parent . key . name )
179
+ && propMatchesRenderPropPattern ( node . parent . key . name , propNamePattern )
177
180
) {
178
181
return true ;
179
182
}
@@ -202,7 +205,7 @@ function isComponentInRenderProp(node, context) {
202
205
const propName = jsxExpressionContainer . parent . name . name ;
203
206
204
207
// Starts with render, e.g. <Component renderFooter={() => <div />} />
205
- if ( startsWithRender ( propName ) ) {
208
+ if ( propMatchesRenderPropPattern ( propName , propNamePattern ) ) {
206
209
return true ;
207
210
}
208
211
@@ -222,16 +225,17 @@ function isComponentInRenderProp(node, context) {
222
225
* <Component rows={ [{ render: () => <div /> }] } />
223
226
* ```
224
227
* @param {ASTNode } node The AST node
228
+ * @param {string } propNamePattern The pattern to match render props against
225
229
* @returns {boolean } True if component is declared inside a render property, false if not
226
230
*/
227
- function isDirectValueOfRenderProperty ( node ) {
231
+ function isDirectValueOfRenderProperty ( node , propNamePattern ) {
228
232
return (
229
233
node
230
234
&& node . parent
231
235
&& node . parent . type === 'Property'
232
236
&& node . parent . key
233
237
&& node . parent . key . type === 'Identifier'
234
- && startsWithRender ( node . parent . key . name )
238
+ && propMatchesRenderPropPattern ( node . parent . key . name , propNamePattern )
235
239
) ;
236
240
}
237
241
@@ -277,13 +281,17 @@ module.exports = {
277
281
allowAsProps : {
278
282
type : 'boolean' ,
279
283
} ,
284
+ propNamePattern : {
285
+ type : 'string' ,
286
+ } ,
280
287
} ,
281
288
additionalProperties : false ,
282
289
} ] ,
283
290
} ,
284
291
285
292
create : Components . detect ( ( context , components , utils ) => {
286
293
const allowAsProps = context . options . some ( ( option ) => option && option . allowAsProps ) ;
294
+ const propNamePattern = ( context . options [ 0 ] || { } ) . propNamePattern || 'render*' ;
287
295
288
296
/**
289
297
* Check whether given node is declared inside class component's render block
@@ -418,7 +426,7 @@ module.exports = {
418
426
419
427
if (
420
428
// Support allowAsProps option
421
- ( isDeclaredInsideProps && ( allowAsProps || isComponentInRenderProp ( node , context ) ) )
429
+ ( isDeclaredInsideProps && ( allowAsProps || isComponentInRenderProp ( node , context , propNamePattern ) ) )
422
430
423
431
// Prevent reporting components created inside Array.map calls
424
432
|| isMapCall ( node )
@@ -428,7 +436,7 @@ module.exports = {
428
436
|| isReturnStatementOfHook ( node , context )
429
437
430
438
// Do not mark objects containing render methods
431
- || isDirectValueOfRenderProperty ( node )
439
+ || isDirectValueOfRenderProperty ( node , propNamePattern )
432
440
433
441
// Prevent reporting nested class components twice
434
442
|| isInsideRenderMethod ( node )
0 commit comments