Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

100 wiersze
2.7 KiB

  1. /**
  2. * @fileoverview Prevent usage of UNSAFE_ methods
  3. * @author Sergei Startsev
  4. */
  5. 'use strict';
  6. const Components = require('../util/Components');
  7. const astUtil = require('../util/ast');
  8. const docsUrl = require('../util/docsUrl');
  9. const versionUtil = require('../util/version');
  10. // ------------------------------------------------------------------------------
  11. // Rule Definition
  12. // ------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. description: 'Prevent usage of UNSAFE_ methods',
  17. category: 'Best Practices',
  18. recommended: false,
  19. url: docsUrl('no-unsafe')
  20. },
  21. schema: []
  22. },
  23. create: Components.detect((context, components, utils) => {
  24. const isApplicable = versionUtil.testReactVersion(context, '16.3.0');
  25. if (!isApplicable) {
  26. return {};
  27. }
  28. /**
  29. * Returns a list of unsafe methods
  30. * @returns {Array} A list of unsafe methods
  31. */
  32. function getUnsafeMethods() {
  33. return [
  34. 'UNSAFE_componentWillMount',
  35. 'UNSAFE_componentWillReceiveProps',
  36. 'UNSAFE_componentWillUpdate'
  37. ];
  38. }
  39. /**
  40. * Checks if a passed method is unsafe
  41. * @param {string} method Life cycle method
  42. * @returns {boolean} Returns true for unsafe methods, otherwise returns false
  43. */
  44. function isUnsafe(method) {
  45. const unsafeMethods = getUnsafeMethods();
  46. return unsafeMethods.indexOf(method) !== -1;
  47. }
  48. /**
  49. * Reports the error for an unsafe method
  50. * @param {ASTNode} node The AST node being checked
  51. * @param {string} method Life cycle method
  52. */
  53. function checkUnsafe(node, method) {
  54. if (!isUnsafe(method)) {
  55. return;
  56. }
  57. context.report({
  58. node: node,
  59. message: `${method} is unsafe for use in async rendering, see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html`
  60. });
  61. }
  62. /**
  63. * Returns life cycle methods if available
  64. * @param {ASTNode} node The AST node being checked.
  65. * @returns {Array} The array of methods.
  66. */
  67. function getLifeCycleMethods(node) {
  68. const properties = astUtil.getComponentProperties(node);
  69. return properties.map(property => astUtil.getPropertyName(property));
  70. }
  71. /**
  72. * Checks life cycle methods
  73. * @param {ASTNode} node The AST node being checked.
  74. */
  75. function checkLifeCycleMethods(node) {
  76. if (utils.isES5Component(node) || utils.isES6Component(node)) {
  77. const methods = getLifeCycleMethods(node);
  78. methods.forEach(method => checkUnsafe(node, method));
  79. }
  80. }
  81. return {
  82. ClassDeclaration: checkLifeCycleMethods,
  83. ClassExpression: checkLifeCycleMethods,
  84. ObjectExpression: checkLifeCycleMethods
  85. };
  86. })
  87. };