7
7
8
8
const Components = require ( '../util/Components' ) ;
9
9
const docsUrl = require ( '../util/docsUrl' ) ;
10
+ const isCreateElement = require ( '../util/isCreateElement' ) ;
10
11
const report = require ( '../util/report' ) ;
11
12
12
13
// ------------------------------------------------------------------------------
@@ -42,33 +43,33 @@ function startsWithRender(text) {
42
43
/**
43
44
* Get closest parent matching given matcher
44
45
* @param {ASTNode } node The AST node
46
+ * @param {Context } context eslint context
45
47
* @param {Function } matcher Method used to match the parent
46
48
* @returns {ASTNode } The matching parent node, if any
47
49
*/
48
- function getClosestMatchingParent ( node , matcher ) {
50
+ function getClosestMatchingParent ( node , context , matcher ) {
49
51
if ( ! node || ! node . parent || node . parent . type === 'Program' ) {
50
52
return ;
51
53
}
52
54
53
- if ( matcher ( node . parent ) ) {
55
+ if ( matcher ( node . parent , context ) ) {
54
56
return node . parent ;
55
57
}
56
58
57
- return getClosestMatchingParent ( node . parent , matcher ) ;
59
+ return getClosestMatchingParent ( node . parent , context , matcher ) ;
58
60
}
59
61
60
62
/**
61
63
* Matcher used to check whether given node is a `createElement` call
62
64
* @param {ASTNode } node The AST node
65
+ * @param {Context } context eslint context
63
66
* @returns {Boolean } True if node is a `createElement` call, false if not
64
67
*/
65
- function isCreateElementMatcher ( node ) {
68
+ function isCreateElementMatcher ( node , context ) {
66
69
return (
67
70
node
68
71
&& node . type === 'CallExpression'
69
- && node . callee
70
- && node . callee . property
71
- && node . callee . property . name === 'createElement'
72
+ && isCreateElement ( node , context )
72
73
) ;
73
74
}
74
75
@@ -133,9 +134,10 @@ function isMapCall(node) {
133
134
/**
134
135
* Check whether given node is `ReturnStatement` of a React hook
135
136
* @param {ASTNode } node The AST node
137
+ * @param {Context } context eslint context
136
138
* @returns {Boolean } True if node is a `ReturnStatement` of a React hook, false if not
137
139
*/
138
- function isReturnStatementOfHook ( node ) {
140
+ function isReturnStatementOfHook ( node , context ) {
139
141
if (
140
142
! node
141
143
|| ! node . parent
@@ -144,7 +146,7 @@ function isReturnStatementOfHook(node) {
144
146
return false ;
145
147
}
146
148
147
- const callExpression = getClosestMatchingParent ( node , isCallExpressionMatcher ) ;
149
+ const callExpression = getClosestMatchingParent ( node , context , isCallExpressionMatcher ) ;
148
150
return (
149
151
callExpression
150
152
&& callExpression . callee
@@ -159,9 +161,10 @@ function isReturnStatementOfHook(node) {
159
161
* <Component>{() => <div />}</Component>
160
162
* ```
161
163
* @param {ASTNode } node The AST node
164
+ * @param {Context } context eslint context
162
165
* @returns {Boolean } True if component is declared inside a render prop, false if not
163
166
*/
164
- function isComponentInRenderProp ( node ) {
167
+ function isComponentInRenderProp ( node , context ) {
165
168
if (
166
169
node
167
170
&& node . parent
@@ -183,7 +186,7 @@ function isComponentInRenderProp(node) {
183
186
return true ;
184
187
}
185
188
186
- const jsxExpressionContainer = getClosestMatchingParent ( node , isJSXExpressionContainerMatcher ) ;
189
+ const jsxExpressionContainer = getClosestMatchingParent ( node , context , isJSXExpressionContainerMatcher ) ;
187
190
188
191
// Check whether prop name indicates accepted patterns
189
192
if (
@@ -345,12 +348,12 @@ module.exports = {
345
348
return false ;
346
349
}
347
350
348
- const createElementParent = getClosestMatchingParent ( node , isCreateElementMatcher ) ;
351
+ const createElementParent = getClosestMatchingParent ( node , context , isCreateElementMatcher ) ;
349
352
350
353
return (
351
354
createElementParent
352
355
&& createElementParent . arguments
353
- && createElementParent . arguments [ 1 ] === getClosestMatchingParent ( node , isObjectExpressionMatcher )
356
+ && createElementParent . arguments [ 1 ] === getClosestMatchingParent ( node , context , isObjectExpressionMatcher )
354
357
) ;
355
358
}
356
359
@@ -363,7 +366,7 @@ module.exports = {
363
366
* @returns {Boolean } True if node is a component declared inside prop, false if not
364
367
*/
365
368
function isComponentInProp ( node ) {
366
- const jsxAttribute = getClosestMatchingParent ( node , isJSXAttributeOfExpressionContainerMatcher ) ;
369
+ const jsxAttribute = getClosestMatchingParent ( node , context , isJSXAttributeOfExpressionContainerMatcher ) ;
367
370
368
371
if ( ! jsxAttribute ) {
369
372
return isComponentInsideCreateElementsProp ( node ) ;
@@ -406,14 +409,14 @@ module.exports = {
406
409
407
410
if (
408
411
// Support allowAsProps option
409
- ( isDeclaredInsideProps && ( allowAsProps || isComponentInRenderProp ( node ) ) )
412
+ ( isDeclaredInsideProps && ( allowAsProps || isComponentInRenderProp ( node , context ) ) )
410
413
411
414
// Prevent reporting components created inside Array.map calls
412
415
|| isMapCall ( node )
413
416
|| isMapCall ( node . parent )
414
417
415
418
// Do not mark components declared inside hooks (or falsly '() => null' clean-up methods)
416
- || isReturnStatementOfHook ( node )
419
+ || isReturnStatementOfHook ( node , context )
417
420
418
421
// Do not mark objects containing render methods
419
422
|| isDirectValueOfRenderProperty ( node )
@@ -430,6 +433,7 @@ module.exports = {
430
433
// Get the closest parent component
431
434
const parentComponent = getClosestMatchingParent (
432
435
node ,
436
+ context ,
433
437
( nodeToMatch ) => components . get ( nodeToMatch )
434
438
) ;
435
439
0 commit comments