Skip to content

Commit d6ece7f

Browse files
committed
[Perf] component detection: improve performance by avoiding traversing parents unnecessarily
1 parent c56cc56 commit d6ece7f

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

lib/util/Components.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Components {
109109
set(node, props) {
110110
const list = Lists.get(this);
111111
let component = list[getId(node)];
112-
while (!component) {
112+
while (!component || component.confidence < 1) {
113113
node = node.parent;
114114
if (!node) {
115115
return;
@@ -477,7 +477,6 @@ function componentRule(rule, context) {
477477
}
478478

479479
if (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') {
480-
const isMethod = parent.type === 'Property' && parent.method;
481480
const isPropertyAssignment = parent.type === 'AssignmentExpression'
482481
&& parent.left.type === 'MemberExpression';
483482
const isModuleExportsAssignment = isPropertyAssignment
@@ -562,6 +561,22 @@ function componentRule(rule, context) {
562561
return undefined;
563562
}
564563

564+
// case: { f() { return ... } }
565+
if (node.parent.type === 'Property' && node.parent.method && !node.parent.computed) {
566+
if (!isFirstLetterCapitalized(node.parent.key.name)) {
567+
return undefined;
568+
}
569+
return utils.isReturningJSX(node) ? node : undefined;
570+
}
571+
572+
// case: { f: () => ... }
573+
if (node.parent.type === 'Property' && !node.id && !node.parent.computed) {
574+
if (!isFirstLetterCapitalized(node.parent.key.name)) {
575+
return undefined;
576+
}
577+
return utils.isReturningJSX(node) ? node : undefined;
578+
}
579+
565580
// Case like `React.memo(() => <></>)` or `React.forwardRef(...)`
566581
const pragmaComponentWrapper = utils.getPragmaComponentWrapper(node);
567582
if (pragmaComponentWrapper && utils.isReturningJSXOrNull(node)) {
@@ -576,10 +591,6 @@ function componentRule(rule, context) {
576591
return undefined;
577592
}
578593

579-
if (isMethod && !isFirstLetterCapitalized(node.parent.key.name)) {
580-
return utils.isReturningJSX(node) ? node : undefined;
581-
}
582-
583594
if (node.id) {
584595
return isFirstLetterCapitalized(node.id.name) ? node : undefined;
585596
}
@@ -853,13 +864,8 @@ function componentRule(rule, context) {
853864
return;
854865
}
855866

856-
const component = utils.getParentComponent();
857-
if (
858-
!component
859-
|| (component.parent && component.parent.type === 'JSXExpressionContainer')
860-
) {
861-
// Ban the node if we cannot find a parent component
862-
components.add(node, 0);
867+
const component = utils.getStatelessComponent(node);
868+
if (!component) {
863869
return;
864870
}
865871
components.add(component, 2);
@@ -871,7 +877,7 @@ function componentRule(rule, context) {
871877
return;
872878
}
873879

874-
node = utils.getParentComponent();
880+
node = utils.getStatelessComponent(node);
875881
if (!node) {
876882
return;
877883
}
@@ -884,13 +890,8 @@ function componentRule(rule, context) {
884890
return;
885891
}
886892

887-
const component = utils.getParentComponent();
888-
if (
889-
!component
890-
|| (component.parent && component.parent.type === 'JSXExpressionContainer')
891-
) {
892-
// Ban the node if we cannot find a parent component
893-
components.add(node, 0);
893+
const component = utils.getStatelessComponent(node);
894+
if (!component) {
894895
return;
895896
}
896897
components.add(component, 2);

tests/lib/rules/destructuring-assignment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ ruleTester.run('destructuring-assignment', rule, {
459459
},
460460
{
461461
code: `
462-
var Hello = React.createClass({
462+
var Hello = createReactClass({
463463
render: function() {
464464
return <Text>{this.props.foo}</Text>;
465465
}

0 commit comments

Comments
 (0)