Skip to content

Commit 7e4c2c2

Browse files
chiawendtljharb
authored andcommitted
[Refactor] no-deprecated: improve performance
1 parent 96b3b06 commit 7e4c2c2

File tree

2 files changed

+60
-60
lines changed

2 files changed

+60
-60
lines changed

CHANGELOG.md

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

27+
[#3271]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3271
2628
[#3267]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3267
2729
[#3266]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3266
2830
[#3265]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3265

lib/rules/no-deprecated.js

Lines changed: 58 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,63 @@ const MODULES = {
2929
// Rule Definition
3030
// ------------------------------------------------------------------------------
3131

32+
function getDeprecated(pragma) {
33+
const deprecated = {};
34+
// 0.12.0
35+
deprecated[`${pragma}.renderComponent`] = ['0.12.0', `${pragma}.render`];
36+
deprecated[`${pragma}.renderComponentToString`] = ['0.12.0', `${pragma}.renderToString`];
37+
deprecated[`${pragma}.renderComponentToStaticMarkup`] = ['0.12.0', `${pragma}.renderToStaticMarkup`];
38+
deprecated[`${pragma}.isValidComponent`] = ['0.12.0', `${pragma}.isValidElement`];
39+
deprecated[`${pragma}.PropTypes.component`] = ['0.12.0', `${pragma}.PropTypes.element`];
40+
deprecated[`${pragma}.PropTypes.renderable`] = ['0.12.0', `${pragma}.PropTypes.node`];
41+
deprecated[`${pragma}.isValidClass`] = ['0.12.0'];
42+
deprecated['this.transferPropsTo'] = ['0.12.0', 'spread operator ({...})'];
43+
// 0.13.0
44+
deprecated[`${pragma}.addons.classSet`] = ['0.13.0', 'the npm module classnames'];
45+
deprecated[`${pragma}.addons.cloneWithProps`] = ['0.13.0', `${pragma}.cloneElement`];
46+
// 0.14.0
47+
deprecated[`${pragma}.render`] = ['0.14.0', 'ReactDOM.render'];
48+
deprecated[`${pragma}.unmountComponentAtNode`] = ['0.14.0', 'ReactDOM.unmountComponentAtNode'];
49+
deprecated[`${pragma}.findDOMNode`] = ['0.14.0', 'ReactDOM.findDOMNode'];
50+
deprecated[`${pragma}.renderToString`] = ['0.14.0', 'ReactDOMServer.renderToString'];
51+
deprecated[`${pragma}.renderToStaticMarkup`] = ['0.14.0', 'ReactDOMServer.renderToStaticMarkup'];
52+
// 15.0.0
53+
deprecated[`${pragma}.addons.LinkedStateMixin`] = ['15.0.0'];
54+
deprecated['ReactPerf.printDOM'] = ['15.0.0', 'ReactPerf.printOperations'];
55+
deprecated['Perf.printDOM'] = ['15.0.0', 'Perf.printOperations'];
56+
deprecated['ReactPerf.getMeasurementsSummaryMap'] = ['15.0.0', 'ReactPerf.getWasted'];
57+
deprecated['Perf.getMeasurementsSummaryMap'] = ['15.0.0', 'Perf.getWasted'];
58+
// 15.5.0
59+
deprecated[`${pragma}.createClass`] = ['15.5.0', 'the npm module create-react-class'];
60+
deprecated[`${pragma}.addons.TestUtils`] = ['15.5.0', 'ReactDOM.TestUtils'];
61+
deprecated[`${pragma}.PropTypes`] = ['15.5.0', 'the npm module prop-types'];
62+
// 15.6.0
63+
deprecated[`${pragma}.DOM`] = ['15.6.0', 'the npm module react-dom-factories'];
64+
// 16.9.0
65+
// For now the following life-cycle methods are just legacy, not deprecated:
66+
// `componentWillMount`, `componentWillReceiveProps`, `componentWillUpdate`
67+
// https://github.com/yannickcr/eslint-plugin-react/pull/1750#issuecomment-425975934
68+
deprecated.componentWillMount = [
69+
'16.9.0',
70+
'UNSAFE_componentWillMount',
71+
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. '
72+
+ 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.',
73+
];
74+
deprecated.componentWillReceiveProps = [
75+
'16.9.0',
76+
'UNSAFE_componentWillReceiveProps',
77+
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. '
78+
+ 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.',
79+
];
80+
deprecated.componentWillUpdate = [
81+
'16.9.0',
82+
'UNSAFE_componentWillUpdate',
83+
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. '
84+
+ 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.',
85+
];
86+
return deprecated;
87+
}
88+
3289
const messages = {
3390
deprecated: '{{oldMethod}} is deprecated since React {{version}}{{newMethod}}{{refs}}',
3491
};
@@ -49,67 +106,9 @@ module.exports = {
49106

50107
create: Components.detect((context, components, utils) => {
51108
const pragma = pragmaUtil.getFromContext(context);
52-
53-
function getDeprecated() {
54-
const deprecated = {};
55-
// 0.12.0
56-
deprecated[`${pragma}.renderComponent`] = ['0.12.0', `${pragma}.render`];
57-
deprecated[`${pragma}.renderComponentToString`] = ['0.12.0', `${pragma}.renderToString`];
58-
deprecated[`${pragma}.renderComponentToStaticMarkup`] = ['0.12.0', `${pragma}.renderToStaticMarkup`];
59-
deprecated[`${pragma}.isValidComponent`] = ['0.12.0', `${pragma}.isValidElement`];
60-
deprecated[`${pragma}.PropTypes.component`] = ['0.12.0', `${pragma}.PropTypes.element`];
61-
deprecated[`${pragma}.PropTypes.renderable`] = ['0.12.0', `${pragma}.PropTypes.node`];
62-
deprecated[`${pragma}.isValidClass`] = ['0.12.0'];
63-
deprecated['this.transferPropsTo'] = ['0.12.0', 'spread operator ({...})'];
64-
// 0.13.0
65-
deprecated[`${pragma}.addons.classSet`] = ['0.13.0', 'the npm module classnames'];
66-
deprecated[`${pragma}.addons.cloneWithProps`] = ['0.13.0', `${pragma}.cloneElement`];
67-
// 0.14.0
68-
deprecated[`${pragma}.render`] = ['0.14.0', 'ReactDOM.render'];
69-
deprecated[`${pragma}.unmountComponentAtNode`] = ['0.14.0', 'ReactDOM.unmountComponentAtNode'];
70-
deprecated[`${pragma}.findDOMNode`] = ['0.14.0', 'ReactDOM.findDOMNode'];
71-
deprecated[`${pragma}.renderToString`] = ['0.14.0', 'ReactDOMServer.renderToString'];
72-
deprecated[`${pragma}.renderToStaticMarkup`] = ['0.14.0', 'ReactDOMServer.renderToStaticMarkup'];
73-
// 15.0.0
74-
deprecated[`${pragma}.addons.LinkedStateMixin`] = ['15.0.0'];
75-
deprecated['ReactPerf.printDOM'] = ['15.0.0', 'ReactPerf.printOperations'];
76-
deprecated['Perf.printDOM'] = ['15.0.0', 'Perf.printOperations'];
77-
deprecated['ReactPerf.getMeasurementsSummaryMap'] = ['15.0.0', 'ReactPerf.getWasted'];
78-
deprecated['Perf.getMeasurementsSummaryMap'] = ['15.0.0', 'Perf.getWasted'];
79-
// 15.5.0
80-
deprecated[`${pragma}.createClass`] = ['15.5.0', 'the npm module create-react-class'];
81-
deprecated[`${pragma}.addons.TestUtils`] = ['15.5.0', 'ReactDOM.TestUtils'];
82-
deprecated[`${pragma}.PropTypes`] = ['15.5.0', 'the npm module prop-types'];
83-
// 15.6.0
84-
deprecated[`${pragma}.DOM`] = ['15.6.0', 'the npm module react-dom-factories'];
85-
// 16.9.0
86-
// For now the following life-cycle methods are just legacy, not deprecated:
87-
// `componentWillMount`, `componentWillReceiveProps`, `componentWillUpdate`
88-
// https://github.com/jsx-eslint/eslint-plugin-react/pull/1750#issuecomment-425975934
89-
deprecated.componentWillMount = [
90-
'16.9.0',
91-
'UNSAFE_componentWillMount',
92-
'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. '
93-
+ 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.',
94-
];
95-
deprecated.componentWillReceiveProps = [
96-
'16.9.0',
97-
'UNSAFE_componentWillReceiveProps',
98-
'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. '
99-
+ 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.',
100-
];
101-
deprecated.componentWillUpdate = [
102-
'16.9.0',
103-
'UNSAFE_componentWillUpdate',
104-
'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. '
105-
+ 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.',
106-
];
107-
return deprecated;
108-
}
109+
const deprecated = getDeprecated(pragma);
109110

110111
function isDeprecated(method) {
111-
const deprecated = getDeprecated();
112-
113112
return (
114113
deprecated
115114
&& deprecated[method]
@@ -122,7 +121,6 @@ module.exports = {
122121
if (!isDeprecated(methodName)) {
123122
return;
124123
}
125-
const deprecated = getDeprecated();
126124
const version = deprecated[methodName][0];
127125
const newMethod = deprecated[methodName][1];
128126
const refs = deprecated[methodName][2];

0 commit comments

Comments
 (0)