Skip to content

Commit f4d4314

Browse files
committed
[Refactor] no-unstable-nested-components: use isCreateElement util (f25a8ec)
1 parent ddff237 commit f4d4314

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

lib/rules/no-unstable-nested-components.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const Components = require('../util/Components');
99
const docsUrl = require('../util/docsUrl');
10+
const isCreateElement = require('../util/isCreateElement');
1011
const report = require('../util/report');
1112

1213
// ------------------------------------------------------------------------------
@@ -42,33 +43,33 @@ function startsWithRender(text) {
4243
/**
4344
* Get closest parent matching given matcher
4445
* @param {ASTNode} node The AST node
46+
* @param {Context} context eslint context
4547
* @param {Function} matcher Method used to match the parent
4648
* @returns {ASTNode} The matching parent node, if any
4749
*/
48-
function getClosestMatchingParent(node, matcher) {
50+
function getClosestMatchingParent(node, context, matcher) {
4951
if (!node || !node.parent || node.parent.type === 'Program') {
5052
return;
5153
}
5254

53-
if (matcher(node.parent)) {
55+
if (matcher(node.parent, context)) {
5456
return node.parent;
5557
}
5658

57-
return getClosestMatchingParent(node.parent, matcher);
59+
return getClosestMatchingParent(node.parent, context, matcher);
5860
}
5961

6062
/**
6163
* Matcher used to check whether given node is a `createElement` call
6264
* @param {ASTNode} node The AST node
65+
* @param {Context} context eslint context
6366
* @returns {Boolean} True if node is a `createElement` call, false if not
6467
*/
65-
function isCreateElementMatcher(node) {
68+
function isCreateElementMatcher(node, context) {
6669
return (
6770
node
6871
&& node.type === 'CallExpression'
69-
&& node.callee
70-
&& node.callee.property
71-
&& node.callee.property.name === 'createElement'
72+
&& isCreateElement(node, context)
7273
);
7374
}
7475

@@ -133,9 +134,10 @@ function isMapCall(node) {
133134
/**
134135
* Check whether given node is `ReturnStatement` of a React hook
135136
* @param {ASTNode} node The AST node
137+
* @param {Context} context eslint context
136138
* @returns {Boolean} True if node is a `ReturnStatement` of a React hook, false if not
137139
*/
138-
function isReturnStatementOfHook(node) {
140+
function isReturnStatementOfHook(node, context) {
139141
if (
140142
!node
141143
|| !node.parent
@@ -144,7 +146,7 @@ function isReturnStatementOfHook(node) {
144146
return false;
145147
}
146148

147-
const callExpression = getClosestMatchingParent(node, isCallExpressionMatcher);
149+
const callExpression = getClosestMatchingParent(node, context, isCallExpressionMatcher);
148150
return (
149151
callExpression
150152
&& callExpression.callee
@@ -159,9 +161,10 @@ function isReturnStatementOfHook(node) {
159161
* <Component>{() => <div />}</Component>
160162
* ```
161163
* @param {ASTNode} node The AST node
164+
* @param {Context} context eslint context
162165
* @returns {Boolean} True if component is declared inside a render prop, false if not
163166
*/
164-
function isComponentInRenderProp(node) {
167+
function isComponentInRenderProp(node, context) {
165168
if (
166169
node
167170
&& node.parent
@@ -183,7 +186,7 @@ function isComponentInRenderProp(node) {
183186
return true;
184187
}
185188

186-
const jsxExpressionContainer = getClosestMatchingParent(node, isJSXExpressionContainerMatcher);
189+
const jsxExpressionContainer = getClosestMatchingParent(node, context, isJSXExpressionContainerMatcher);
187190

188191
// Check whether prop name indicates accepted patterns
189192
if (
@@ -345,12 +348,12 @@ module.exports = {
345348
return false;
346349
}
347350

348-
const createElementParent = getClosestMatchingParent(node, isCreateElementMatcher);
351+
const createElementParent = getClosestMatchingParent(node, context, isCreateElementMatcher);
349352

350353
return (
351354
createElementParent
352355
&& createElementParent.arguments
353-
&& createElementParent.arguments[1] === getClosestMatchingParent(node, isObjectExpressionMatcher)
356+
&& createElementParent.arguments[1] === getClosestMatchingParent(node, context, isObjectExpressionMatcher)
354357
);
355358
}
356359

@@ -363,7 +366,7 @@ module.exports = {
363366
* @returns {Boolean} True if node is a component declared inside prop, false if not
364367
*/
365368
function isComponentInProp(node) {
366-
const jsxAttribute = getClosestMatchingParent(node, isJSXAttributeOfExpressionContainerMatcher);
369+
const jsxAttribute = getClosestMatchingParent(node, context, isJSXAttributeOfExpressionContainerMatcher);
367370

368371
if (!jsxAttribute) {
369372
return isComponentInsideCreateElementsProp(node);
@@ -406,14 +409,14 @@ module.exports = {
406409

407410
if (
408411
// Support allowAsProps option
409-
(isDeclaredInsideProps && (allowAsProps || isComponentInRenderProp(node)))
412+
(isDeclaredInsideProps && (allowAsProps || isComponentInRenderProp(node, context)))
410413

411414
// Prevent reporting components created inside Array.map calls
412415
|| isMapCall(node)
413416
|| isMapCall(node.parent)
414417

415418
// Do not mark components declared inside hooks (or falsly '() => null' clean-up methods)
416-
|| isReturnStatementOfHook(node)
419+
|| isReturnStatementOfHook(node, context)
417420

418421
// Do not mark objects containing render methods
419422
|| isDirectValueOfRenderProperty(node)
@@ -430,6 +433,7 @@ module.exports = {
430433
// Get the closest parent component
431434
const parentComponent = getClosestMatchingParent(
432435
node,
436+
context,
433437
(nodeToMatch) => components.get(nodeToMatch)
434438
);
435439

0 commit comments

Comments
 (0)