Skip to content

Commit 161317e

Browse files
golopotljharb
authored andcommitted
[Refactor] improve performance for detecting class components
1 parent 8992a2b commit 161317e

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2121
* [Refactor] fix linter errors ([#3261][] @golopot)
2222
* [Docs] [`no-unused-prop-types`]: fix syntax errors ([#3259][] @mrdulin)
2323
* [Refactor] improve performance for detecting function components ([#3265][] @golopot)
24+
* [Refactor] improve performance for detecting class components ([#3267][] @golopot)
2425

26+
[#3267]: https://github.com/yannickcr/eslint-plugin-react/pull/3267
2527
[#3266]: https://github.com/yannickcr/eslint-plugin-react/pull/3266
2628
[#3265]: https://github.com/yannickcr/eslint-plugin-react/pull/3265
2729
[#3261]: https://github.com/yannickcr/eslint-plugin-react/pull/3261

lib/util/Components.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,20 +259,29 @@ function componentRule(rule, context) {
259259
const utils = {
260260

261261
/**
262-
* Check if the node is a React ES5 component
262+
* Check if an ObjectExpression is a React ES5 component
263263
*
264264
* @param {ASTNode} node The AST node being checked.
265265
* @returns {Boolean} True if the node is a React ES5 component, false if not
266266
*/
267267
isES5Component(node) {
268-
if (!node.parent) {
268+
if (!node.parent || !node.parent.callee) {
269269
return false;
270270
}
271-
return new RegExp(`^(${pragma}\\.)?${createClass}$`).test(sourceCode.getText(node.parent.callee));
271+
const callee = node.parent.callee;
272+
// React.createClass({})
273+
if (callee.type === 'MemberExpression') {
274+
return callee.object.name === pragma && callee.property.name === createClass;
275+
}
276+
// createClass({})
277+
if (callee.type === 'Identifier') {
278+
return callee.name === createClass;
279+
}
280+
return false;
272281
},
273282

274283
/**
275-
* Check if the node is a React ES6 component
284+
* Check if a class is a React ES6 component
276285
*
277286
* @param {ASTNode} node The AST node being checked.
278287
* @returns {Boolean} True if the node is a React ES6 component, false if not
@@ -285,7 +294,14 @@ function componentRule(rule, context) {
285294
if (!node.superClass) {
286295
return false;
287296
}
288-
return new RegExp(`^(${pragma}\\.)?(Pure)?Component$`).test(sourceCode.getText(node.superClass));
297+
if (node.superClass.type === 'MemberExpression') {
298+
return node.superClass.object.name === pragma
299+
&& /^(Pure)?Component$/.test(node.superClass.property.name);
300+
}
301+
if (node.superClass.type === 'Identifier') {
302+
return /^(Pure)?Component$/.test(node.superClass.name);
303+
}
304+
return false;
289305
},
290306

291307
/**

0 commit comments

Comments
 (0)