Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Perf] component detection: improve performance by avoiding traversing parents unnecessarily #3459

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [`hook-use-state`]: add `allowDestructuredState` option ([#3449][] @ljharb)
* add [`sort-default-props`] and deprecate [`jsx-sort-default-props`] ([#1861][] @alexzherdev)

### Changed
* [Perf] component detection: improve performance by avoiding traversing parents unnecessarily ([#3459][] @golopot)

[#3459]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3459
[#3449]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3449
[#3424]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3429
[#1861]: https://github.com/jsx-eslint/eslint-plugin-react/pull/1861
Expand Down
39 changes: 18 additions & 21 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Components {
set(node, props) {
const list = Lists.get(this);
let component = list[getId(node)];
while (!component) {
while (!component || component.confidence < 1) {
node = node.parent;
if (!node) {
return;
Expand Down Expand Up @@ -477,7 +477,6 @@ function componentRule(rule, context) {
}

if (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') {
const isMethod = parent.type === 'Property' && parent.method;
const isPropertyAssignment = parent.type === 'AssignmentExpression'
&& parent.left.type === 'MemberExpression';
const isModuleExportsAssignment = isPropertyAssignment
Expand Down Expand Up @@ -562,6 +561,18 @@ function componentRule(rule, context) {
return undefined;
}

if (
node.parent.type === 'Property' && (
(node.parent.method && !node.parent.computed) // case: { f() { return ... } }
|| (!node.id && !node.parent.computed) // case: { f: () => ... }
)
) {
if (isFirstLetterCapitalized(node.parent.key.name) && utils.isReturningJSX(node)) {
return node;
}
return undefined;
}

// Case like `React.memo(() => <></>)` or `React.forwardRef(...)`
const pragmaComponentWrapper = utils.getPragmaComponentWrapper(node);
if (pragmaComponentWrapper && utils.isReturningJSXOrNull(node)) {
Expand All @@ -576,10 +587,6 @@ function componentRule(rule, context) {
return undefined;
}

if (isMethod && !isFirstLetterCapitalized(node.parent.key.name)) {
return utils.isReturningJSX(node) ? node : undefined;
}

if (node.id) {
return isFirstLetterCapitalized(node.id.name) ? node : undefined;
}
Expand Down Expand Up @@ -853,13 +860,8 @@ function componentRule(rule, context) {
return;
}

const component = utils.getParentComponent();
if (
!component
|| (component.parent && component.parent.type === 'JSXExpressionContainer')
) {
// Ban the node if we cannot find a parent component
components.add(node, 0);
const component = utils.getStatelessComponent(node);
if (!component) {
return;
}
components.add(component, 2);
Expand All @@ -871,7 +873,7 @@ function componentRule(rule, context) {
return;
}

node = utils.getParentComponent();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to find parent components here.

node = utils.getStatelessComponent(node);
if (!node) {
return;
}
Expand All @@ -884,13 +886,8 @@ function componentRule(rule, context) {
return;
}

const component = utils.getParentComponent();
if (
!component
|| (component.parent && component.parent.type === 'JSXExpressionContainer')
) {
// Ban the node if we cannot find a parent component
components.add(node, 0);
const component = utils.getStatelessComponent(node);
if (!component) {
return;
}
components.add(component, 2);
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/rules/destructuring-assignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ ruleTester.run('destructuring-assignment', rule, {
},
{
code: `
var Hello = React.createClass({
var Hello = createReactClass({
render: function() {
return <Text>{this.props.foo}</Text>;
}
Comment on lines 464 to 465
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be reported because React.createClass is not the correct pragma here. So there should be no component detected thus no report.

Expand Down