Skip to content

Commit

Permalink
[Refactor] improve performance for detecting class components
Browse files Browse the repository at this point in the history
  • Loading branch information
golopot committed Apr 10, 2022
1 parent 8992a2b commit 5daf9e0
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions lib/util/Components.js
Expand Up @@ -259,20 +259,30 @@ function componentRule(rule, context) {
const utils = {

/**
* Check if the node is a React ES5 component
* Check if an ObjectExpression is a React ES5 component
*
* @param {ASTNode} node The AST node being checked.
* @returns {Boolean} True if the node is a React ES5 component, false if not
*/
isES5Component(node) {
if (!node.parent) {
if (!node.parent || !node.parent.callee) {
return false;
}
return new RegExp(`^(${pragma}\\.)?${createClass}$`).test(sourceCode.getText(node.parent.callee));
const callee = node.parent.callee;
switch (callee.type) {
// React.createClass({})
case 'MemberExpression':
return callee.object.name === pragma && callee.property.name === createClass;
// createClass({})
case 'Identifier':
return callee.name === createClass;
default:
return false;
}
},

/**
* Check if the node is a React ES6 component
* Check if a class is a React ES6 component
*
* @param {ASTNode} node The AST node being checked.
* @returns {Boolean} True if the node is a React ES6 component, false if not
Expand All @@ -285,7 +295,16 @@ function componentRule(rule, context) {
if (!node.superClass) {
return false;
}
return new RegExp(`^(${pragma}\\.)?(Pure)?Component$`).test(sourceCode.getText(node.superClass));

switch (node.superClass.type) {
case 'MemberExpression':
return node.superClass.object.name === pragma
&& /^(Pure)?Component$/.test(node.superClass.property.name);
case 'Identifier':
return /^(Pure)?Component$/.test(node.superClass.name);
default:
return false;
}
},

/**
Expand Down

0 comments on commit 5daf9e0

Please sign in to comment.