Skip to content

Commit 18de0a6

Browse files
golopotljharb
authored andcommitted
[Refactor] improve performance by avoiding unnecessary Components.detect
1 parent 2bf54d7 commit 18de0a6

30 files changed

+337
-271
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2424
* [Refactor] improve performance for detecting class components ([#3267][] @golopot)
2525
* [Refactor] [`no-deprecated`]: improve performance ([#3271][] @golopot)
2626
* [Refactor] [`no-did-mount-set-state`], [`no-did-update-set-state`], [`no-will-update-set-state`]: improve performance ([#3272][] @golopot)
27+
* [Refactor] improve performance by avoiding unnecessary `Components.detect` ([#3273][] @golopot)
2728

29+
[#3273]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3273
2830
[#3272]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3272
2931
[#3271]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3271
3032
[#3267]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3267

lib/rules/display-name.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const values = require('object.values');
99

1010
const Components = require('../util/Components');
1111
const astUtil = require('../util/ast');
12+
const componentUtil = require('../util/componentUtil');
1213
const docsUrl = require('../util/docsUrl');
1314
const propsUtil = require('../util/props');
1415
const report = require('../util/report');
@@ -106,7 +107,7 @@ module.exports = {
106107
astUtil.isFunctionLikeExpression(node)
107108
&& node.parent
108109
&& (node.parent.type === 'VariableDeclarator' || node.parent.type === 'Property' || node.parent.method === true)
109-
&& (!node.parent.parent || !utils.isES5Component(node.parent.parent))
110+
&& (!node.parent.parent || !componentUtil.isES5Component(node.parent.parent, context))
110111
);
111112

112113
if (
@@ -192,7 +193,7 @@ module.exports = {
192193
},
193194

194195
ObjectExpression(node) {
195-
if (!utils.isES5Component(node)) {
196+
if (!componentUtil.isES5Component(node, context)) {
196197
return;
197198
}
198199
if (ignoreTranspilerName || !hasTranspilerName(node)) {

lib/rules/jsx-indent.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const astUtil = require('../util/ast');
3636
const docsUrl = require('../util/docsUrl');
3737
const reportC = require('../util/report');
3838
const jsxUtil = require('../util/jsx');
39-
const isCreateElement = require('../util/isCreateElement');
4039

4140
// ------------------------------------------------------------------------------
4241
// Rule Definition
@@ -428,7 +427,7 @@ module.exports = {
428427
}
429428
if (
430429
!fn
431-
|| !jsxUtil.isReturningJSX((n) => isCreateElement(n, context), node, context, true)
430+
|| !jsxUtil.isReturningJSX(node, context, true)
432431
) {
433432
return;
434433
}

lib/rules/jsx-no-bind.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
'use strict';
99

1010
const propName = require('jsx-ast-utils/propName');
11-
const Components = require('../util/Components');
1211
const docsUrl = require('../util/docsUrl');
1312
const jsxUtil = require('../util/jsx');
1413
const report = require('../util/report');
@@ -63,7 +62,7 @@ module.exports = {
6362
}],
6463
},
6564

66-
create: Components.detect((context) => {
65+
create(context) {
6766
const configuration = context.options[0] || {};
6867

6968
// Keep track of all the variable names pointing to a bind call,
@@ -198,5 +197,5 @@ module.exports = {
198197
}
199198
},
200199
};
201-
}),
200+
},
202201
};

lib/rules/no-access-state-in-setstate.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'use strict';
77

88
const docsUrl = require('../util/docsUrl');
9-
const Components = require('../util/Components');
9+
const componentUtil = require('../util/componentUtil');
1010
const report = require('../util/report');
1111

1212
// ------------------------------------------------------------------------------
@@ -29,7 +29,7 @@ module.exports = {
2929
messages,
3030
},
3131

32-
create: Components.detect((context, components, utils) => {
32+
create(context) {
3333
function isSetStateCall(node) {
3434
return node.type === 'CallExpression'
3535
&& node.callee.property
@@ -48,7 +48,7 @@ module.exports = {
4848
}
4949

5050
function isClassComponent() {
51-
return !!(utils.getParentES6Component() || utils.getParentES5Component());
51+
return !!(componentUtil.getParentES6Component(context) || componentUtil.getParentES5Component(context));
5252
}
5353

5454
// The methods array contains all methods or functions that are using this.state
@@ -183,5 +183,5 @@ module.exports = {
183183
});
184184
},
185185
};
186-
}),
186+
},
187187
};

lib/rules/no-arrow-function-lifecycle.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const values = require('object.values');
99

1010
const Components = require('../util/Components');
1111
const astUtil = require('../util/ast');
12+
const componentUtil = require('../util/componentUtil');
1213
const docsUrl = require('../util/docsUrl');
1314
const lifecycleMethods = require('../util/lifecycleMethods');
1415
const report = require('../util/report');
@@ -44,7 +45,7 @@ module.exports = {
4445
fixable: 'code',
4546
},
4647

47-
create: Components.detect((context, components, utils) => {
48+
create: Components.detect((context, components) => {
4849
/**
4950
* @param {Array} properties list of component properties
5051
*/
@@ -57,7 +58,7 @@ module.exports = {
5758
const propertyName = astUtil.getPropertyName(node);
5859
const nodeType = node.value.type;
5960
const isLifecycleMethod = (
60-
node.static && !utils.isES5Component(node)
61+
node.static && !componentUtil.isES5Component(node, context)
6162
? lifecycleMethods.static
6263
: lifecycleMethods.instance
6364
).indexOf(propertyName) > -1;

lib/rules/no-deprecated.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
'use strict';
99

1010
const values = require('object.values');
11-
12-
const Components = require('../util/Components');
1311
const astUtil = require('../util/ast');
12+
const componentUtil = require('../util/componentUtil');
1413
const docsUrl = require('../util/docsUrl');
1514
const pragmaUtil = require('../util/pragma');
1615
const testReactVersion = require('../util/version').testReactVersion;
@@ -104,7 +103,7 @@ module.exports = {
104103
schema: [],
105104
},
106105

107-
create: Components.detect((context, components, utils) => {
106+
create(context) {
108107
const pragma = pragmaUtil.getFromContext(context);
109108
const deprecated = getDeprecated(pragma);
110109

@@ -167,7 +166,10 @@ module.exports = {
167166
* @param {ASTNode} node The AST node being checked.
168167
*/
169168
function checkLifeCycleMethods(node) {
170-
if (utils.isES5Component(node) || utils.isES6Component(node)) {
169+
if (
170+
componentUtil.isES5Component(node, context)
171+
|| componentUtil.isES6Component(node, context)
172+
) {
171173
const methods = getLifeCycleMethods(node);
172174
methods.forEach((method) => checkDeprecation(node, method.name, method.node));
173175
}
@@ -221,5 +223,5 @@ module.exports = {
221223
ClassExpression: checkLifeCycleMethods,
222224
ObjectExpression: checkLifeCycleMethods,
223225
};
224-
}),
226+
},
225227
};

lib/rules/no-direct-mutation-state.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'use strict';
88

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

@@ -99,7 +100,7 @@ module.exports = {
99100
return;
100101
}
101102
const item = getOuterMemberExpression(node.left);
102-
if (utils.isStateMemberExpression(item)) {
103+
if (componentUtil.isStateMemberExpression(item)) {
103104
const mutations = (component && component.mutations) || [];
104105
mutations.push(node.left.object);
105106
components.set(node, {
@@ -115,7 +116,7 @@ module.exports = {
115116
return;
116117
}
117118
const item = getOuterMemberExpression(node.argument);
118-
if (utils.isStateMemberExpression(item)) {
119+
if (componentUtil.isStateMemberExpression(item)) {
119120
const mutations = (component && component.mutations) || [];
120121
mutations.push(item);
121122
components.set(node, {

lib/rules/no-redundant-should-component-update.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
'use strict';
66

7-
const Components = require('../util/Components');
87
const astUtil = require('../util/ast');
8+
const componentUtil = require('../util/componentUtil');
99
const docsUrl = require('../util/docsUrl');
1010
const report = require('../util/report');
1111

@@ -31,7 +31,7 @@ module.exports = {
3131
schema: [],
3232
},
3333

34-
create: Components.detect((context, components, utils) => {
34+
create(context) {
3535
/**
3636
* Checks for shouldComponentUpdate property
3737
* @param {ASTNode} node The AST node being checked.
@@ -65,7 +65,7 @@ module.exports = {
6565
* @param {ASTNode} node The AST node being checked.
6666
*/
6767
function checkForViolation(node) {
68-
if (utils.isPureComponent(node)) {
68+
if (componentUtil.isPureComponent(node, context)) {
6969
const hasScu = hasShouldComponentUpdate(node);
7070
if (hasScu) {
7171
const className = getNodeName(node);
@@ -83,5 +83,5 @@ module.exports = {
8383
ClassDeclaration: checkForViolation,
8484
ClassExpression: checkForViolation,
8585
};
86-
}),
86+
},
8787
};

lib/rules/no-string-refs.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
'use strict';
77

8-
const Components = require('../util/Components');
8+
const componentUtil = require('../util/componentUtil');
99
const docsUrl = require('../util/docsUrl');
1010
const report = require('../util/report');
1111

@@ -40,7 +40,7 @@ module.exports = {
4040
}],
4141
},
4242

43-
create: Components.detect((context, components, utils) => {
43+
create(context) {
4444
const detectTemplateLiterals = context.options[0] ? context.options[0].noTemplateLiterals : false;
4545
/**
4646
* Checks if we are using refs
@@ -49,7 +49,7 @@ module.exports = {
4949
*/
5050
function isRefsUsage(node) {
5151
return !!(
52-
(utils.getParentES6Component() || utils.getParentES5Component())
52+
(componentUtil.getParentES6Component(context) || componentUtil.getParentES5Component(context))
5353
&& node.object.type === 'ThisExpression'
5454
&& node.property.name === 'refs'
5555
);
@@ -115,5 +115,5 @@ module.exports = {
115115
}
116116
},
117117
};
118-
}),
118+
},
119119
};

lib/rules/no-typos.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
const PROP_TYPES = Object.keys(require('prop-types'));
88
const Components = require('../util/Components');
99
const docsUrl = require('../util/docsUrl');
10+
const componentUtil = require('../util/componentUtil');
1011
const report = require('../util/report');
1112
const lifecycleMethods = require('../util/lifecycleMethods');
1213

@@ -202,7 +203,7 @@ module.exports = {
202203
},
203204

204205
'ClassProperty, PropertyDefinition'(node) {
205-
if (!node.static || !utils.isES6Component(node.parent.parent)) {
206+
if (!node.static || !componentUtil.isES6Component(node.parent.parent, context)) {
206207
return;
207208
}
208209

@@ -223,7 +224,7 @@ module.exports = {
223224

224225
if (
225226
relatedComponent
226-
&& (utils.isES6Component(relatedComponent.node) || (
227+
&& (componentUtil.isES6Component(relatedComponent.node, context) || (
227228
relatedComponent.node.type !== 'ClassDeclaration' && utils.isReturningJSX(relatedComponent.node)))
228229
&& (node.parent && node.parent.type === 'AssignmentExpression' && node.parent.right)
229230
) {
@@ -232,15 +233,15 @@ module.exports = {
232233
},
233234

234235
MethodDefinition(node) {
235-
if (!utils.isES6Component(node.parent.parent)) {
236+
if (!componentUtil.isES6Component(node.parent.parent, context)) {
236237
return;
237238
}
238239

239240
reportErrorIfLifecycleMethodCasingTypo(node);
240241
},
241242

242243
ObjectExpression(node) {
243-
const component = utils.isES5Component(node) && components.get(node);
244+
const component = componentUtil.isES5Component(node, context) && components.get(node);
244245

245246
if (!component) {
246247
return;

lib/rules/no-unsafe.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
'use strict';
77

8-
const Components = require('../util/Components');
98
const astUtil = require('../util/ast');
9+
const componentUtil = require('../util/componentUtil');
1010
const docsUrl = require('../util/docsUrl');
1111
const testReactVersion = require('../util/version').testReactVersion;
1212
const report = require('../util/report');
@@ -44,7 +44,7 @@ module.exports = {
4444
],
4545
},
4646

47-
create: Components.detect((context, components, utils) => {
47+
create(context) {
4848
const config = context.options[0] || {};
4949
const checkAliases = config.checkAliases || false;
5050

@@ -133,7 +133,7 @@ module.exports = {
133133
* @param {ASTNode} node The AST node being checked.
134134
*/
135135
function checkLifeCycleMethods(node) {
136-
if (utils.isES5Component(node) || utils.isES6Component(node)) {
136+
if (componentUtil.isES5Component(node, context) || componentUtil.isES6Component(node, context)) {
137137
const methods = getLifeCycleMethods(node);
138138
methods.forEach((method) => checkUnsafe(node, method));
139139
}
@@ -144,5 +144,5 @@ module.exports = {
144144
ClassExpression: checkLifeCycleMethods,
145145
ObjectExpression: checkLifeCycleMethods,
146146
};
147-
}),
147+
},
148148
};

lib/rules/no-unused-class-component-methods.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
'use strict';
77

8-
const Components = require('../util/Components');
98
const docsUrl = require('../util/docsUrl');
9+
const componentUtil = require('../util/componentUtil');
1010
const report = require('../util/report');
1111

1212
// ------------------------------------------------------------------------------
@@ -115,7 +115,7 @@ module.exports = {
115115
],
116116
},
117117

118-
create: Components.detect((context, components, utils) => {
118+
create: ((context) => {
119119
let classInfo = null;
120120

121121
// Takes an ObjectExpression node and adds all named Property nodes to the
@@ -171,13 +171,13 @@ module.exports = {
171171

172172
return {
173173
ClassDeclaration(node) {
174-
if (utils.isES6Component(node)) {
174+
if (componentUtil.isES6Component(node, context)) {
175175
classInfo = getInitialClassInfo(node, true);
176176
}
177177
},
178178

179179
ObjectExpression(node) {
180-
if (utils.isES5Component(node)) {
180+
if (componentUtil.isES5Component(node, context)) {
181181
classInfo = getInitialClassInfo(node, false);
182182
}
183183
},

0 commit comments

Comments
 (0)