no-deprecated.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * @fileoverview Prevent usage of deprecated methods
  3. * @author Yannick Croissant
  4. * @author Scott Feeney
  5. * @author Sergei Startsev
  6. */
  7. 'use strict';
  8. const entries = require('object.entries');
  9. const astUtil = require('../util/ast');
  10. const componentUtil = require('../util/componentUtil');
  11. const docsUrl = require('../util/docsUrl');
  12. const pragmaUtil = require('../util/pragma');
  13. const testReactVersion = require('../util/version').testReactVersion;
  14. const report = require('../util/report');
  15. // ------------------------------------------------------------------------------
  16. // Constants
  17. // ------------------------------------------------------------------------------
  18. const MODULES = {
  19. react: ['React'],
  20. 'react-addons-perf': ['ReactPerf', 'Perf'],
  21. 'react-dom': ['ReactDOM'],
  22. 'react-dom/server': ['ReactDOMServer'],
  23. };
  24. // ------------------------------------------------------------------------------
  25. // Rule Definition
  26. // ------------------------------------------------------------------------------
  27. function getDeprecated(pragma) {
  28. const deprecated = {};
  29. // 0.12.0
  30. deprecated[`${pragma}.renderComponent`] = ['0.12.0', `${pragma}.render`];
  31. deprecated[`${pragma}.renderComponentToString`] = ['0.12.0', `${pragma}.renderToString`];
  32. deprecated[`${pragma}.renderComponentToStaticMarkup`] = ['0.12.0', `${pragma}.renderToStaticMarkup`];
  33. deprecated[`${pragma}.isValidComponent`] = ['0.12.0', `${pragma}.isValidElement`];
  34. deprecated[`${pragma}.PropTypes.component`] = ['0.12.0', `${pragma}.PropTypes.element`];
  35. deprecated[`${pragma}.PropTypes.renderable`] = ['0.12.0', `${pragma}.PropTypes.node`];
  36. deprecated[`${pragma}.isValidClass`] = ['0.12.0'];
  37. deprecated['this.transferPropsTo'] = ['0.12.0', 'spread operator ({...})'];
  38. // 0.13.0
  39. deprecated[`${pragma}.addons.classSet`] = ['0.13.0', 'the npm module classnames'];
  40. deprecated[`${pragma}.addons.cloneWithProps`] = ['0.13.0', `${pragma}.cloneElement`];
  41. // 0.14.0
  42. deprecated[`${pragma}.render`] = ['0.14.0', 'ReactDOM.render'];
  43. deprecated[`${pragma}.unmountComponentAtNode`] = ['0.14.0', 'ReactDOM.unmountComponentAtNode'];
  44. deprecated[`${pragma}.findDOMNode`] = ['0.14.0', 'ReactDOM.findDOMNode'];
  45. deprecated[`${pragma}.renderToString`] = ['0.14.0', 'ReactDOMServer.renderToString'];
  46. deprecated[`${pragma}.renderToStaticMarkup`] = ['0.14.0', 'ReactDOMServer.renderToStaticMarkup'];
  47. // 15.0.0
  48. deprecated[`${pragma}.addons.LinkedStateMixin`] = ['15.0.0'];
  49. deprecated['ReactPerf.printDOM'] = ['15.0.0', 'ReactPerf.printOperations'];
  50. deprecated['Perf.printDOM'] = ['15.0.0', 'Perf.printOperations'];
  51. deprecated['ReactPerf.getMeasurementsSummaryMap'] = ['15.0.0', 'ReactPerf.getWasted'];
  52. deprecated['Perf.getMeasurementsSummaryMap'] = ['15.0.0', 'Perf.getWasted'];
  53. // 15.5.0
  54. deprecated[`${pragma}.createClass`] = ['15.5.0', 'the npm module create-react-class'];
  55. deprecated[`${pragma}.addons.TestUtils`] = ['15.5.0', 'ReactDOM.TestUtils'];
  56. deprecated[`${pragma}.PropTypes`] = ['15.5.0', 'the npm module prop-types'];
  57. // 15.6.0
  58. deprecated[`${pragma}.DOM`] = ['15.6.0', 'the npm module react-dom-factories'];
  59. // 16.9.0
  60. // For now the following life-cycle methods are just legacy, not deprecated:
  61. // `componentWillMount`, `componentWillReceiveProps`, `componentWillUpdate`
  62. // https://github.com/yannickcr/eslint-plugin-react/pull/1750#issuecomment-425975934
  63. deprecated.componentWillMount = [
  64. '16.9.0',
  65. 'UNSAFE_componentWillMount',
  66. 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. '
  67. + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.',
  68. ];
  69. deprecated.componentWillReceiveProps = [
  70. '16.9.0',
  71. 'UNSAFE_componentWillReceiveProps',
  72. 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. '
  73. + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.',
  74. ];
  75. deprecated.componentWillUpdate = [
  76. '16.9.0',
  77. 'UNSAFE_componentWillUpdate',
  78. 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. '
  79. + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.',
  80. ];
  81. // 18.0.0
  82. // https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html#deprecations
  83. deprecated['ReactDOM.render'] = [
  84. '18.0.0',
  85. 'createRoot',
  86. 'https://reactjs.org/link/switch-to-createroot',
  87. ];
  88. deprecated['ReactDOM.hydrate'] = [
  89. '18.0.0',
  90. 'hydrateRoot',
  91. 'https://reactjs.org/link/switch-to-createroot',
  92. ];
  93. deprecated['ReactDOM.unmountComponentAtNode'] = [
  94. '18.0.0',
  95. 'root.unmount',
  96. 'https://reactjs.org/link/switch-to-createroot',
  97. ];
  98. deprecated['ReactDOMServer.renderToNodeStream'] = [
  99. '18.0.0',
  100. 'renderToPipeableStream',
  101. 'https://reactjs.org/docs/react-dom-server.html#rendertonodestream',
  102. ];
  103. return deprecated;
  104. }
  105. const messages = {
  106. deprecated: '{{oldMethod}} is deprecated since React {{version}}{{newMethod}}{{refs}}',
  107. };
  108. module.exports = {
  109. meta: {
  110. docs: {
  111. description: 'Disallow usage of deprecated methods',
  112. category: 'Best Practices',
  113. recommended: true,
  114. url: docsUrl('no-deprecated'),
  115. },
  116. messages,
  117. schema: [],
  118. },
  119. create(context) {
  120. const pragma = pragmaUtil.getFromContext(context);
  121. const deprecated = getDeprecated(pragma);
  122. function isDeprecated(method) {
  123. return (
  124. deprecated
  125. && deprecated[method]
  126. && deprecated[method][0]
  127. && testReactVersion(context, `>= ${deprecated[method][0]}`)
  128. );
  129. }
  130. function checkDeprecation(node, methodName, methodNode) {
  131. if (!isDeprecated(methodName)) {
  132. return;
  133. }
  134. const version = deprecated[methodName][0];
  135. const newMethod = deprecated[methodName][1];
  136. const refs = deprecated[methodName][2];
  137. report(context, messages.deprecated, 'deprecated', {
  138. node: methodNode || node,
  139. data: {
  140. oldMethod: methodName,
  141. version,
  142. newMethod: newMethod ? `, use ${newMethod} instead` : '',
  143. refs: refs ? `, see ${refs}` : '',
  144. },
  145. });
  146. }
  147. function getReactModuleName(node) {
  148. let moduleName = false;
  149. if (!node.init) {
  150. return false;
  151. }
  152. entries(MODULES).some((entry) => {
  153. const key = entry[0];
  154. const moduleNames = entry[1];
  155. if (
  156. node.init.arguments
  157. && node.init.arguments.length > 0
  158. && node.init.arguments[0]
  159. && key === node.init.arguments[0].value
  160. ) {
  161. moduleName = MODULES[key][0];
  162. } else {
  163. moduleName = moduleNames.find((name) => name === node.init.name);
  164. }
  165. return moduleName;
  166. });
  167. return moduleName;
  168. }
  169. /**
  170. * Returns life cycle methods if available
  171. * @param {ASTNode} node The AST node being checked.
  172. * @returns {Array} The array of methods.
  173. */
  174. function getLifeCycleMethods(node) {
  175. const properties = astUtil.getComponentProperties(node);
  176. return properties.map((property) => ({
  177. name: astUtil.getPropertyName(property),
  178. node: astUtil.getPropertyNameNode(property),
  179. }));
  180. }
  181. /**
  182. * Checks life cycle methods
  183. * @param {ASTNode} node The AST node being checked.
  184. */
  185. function checkLifeCycleMethods(node) {
  186. if (
  187. componentUtil.isES5Component(node, context)
  188. || componentUtil.isES6Component(node, context)
  189. ) {
  190. const methods = getLifeCycleMethods(node);
  191. methods.forEach((method) => checkDeprecation(node, method.name, method.node));
  192. }
  193. }
  194. // --------------------------------------------------------------------------
  195. // Public
  196. // --------------------------------------------------------------------------
  197. return {
  198. MemberExpression(node) {
  199. checkDeprecation(node, context.getSourceCode().getText(node));
  200. },
  201. ImportDeclaration(node) {
  202. const isReactImport = typeof MODULES[node.source.value] !== 'undefined';
  203. if (!isReactImport) {
  204. return;
  205. }
  206. node.specifiers.filter(((s) => s.imported)).forEach((specifier) => {
  207. checkDeprecation(node, `${MODULES[node.source.value][0]}.${specifier.imported.name}`, specifier);
  208. });
  209. },
  210. VariableDeclarator(node) {
  211. const reactModuleName = getReactModuleName(node);
  212. const isRequire = node.init && node.init.callee && node.init.callee.name === 'require';
  213. const isReactRequire = node.init
  214. && node.init.arguments
  215. && node.init.arguments.length
  216. && typeof MODULES[node.init.arguments[0].value] !== 'undefined';
  217. const isDestructuring = node.id && node.id.type === 'ObjectPattern';
  218. if (
  219. !(isDestructuring && reactModuleName)
  220. && !(isDestructuring && isRequire && isReactRequire)
  221. ) {
  222. return;
  223. }
  224. node.id.properties.filter((p) => p.type !== 'RestElement' && p.key).forEach((property) => {
  225. checkDeprecation(node, `${reactModuleName || pragma}.${property.key.name}`, property);
  226. });
  227. },
  228. ClassDeclaration: checkLifeCycleMethods,
  229. ClassExpression: checkLifeCycleMethods,
  230. ObjectExpression: checkLifeCycleMethods,
  231. };
  232. },
  233. };