no-deprecated.js 8.2 KB

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