require-optimization.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * @fileoverview Enforce React components to have a shouldComponentUpdate method
  3. * @author Evgueni Naverniouk
  4. */
  5. 'use strict';
  6. const has = require('has');
  7. const Components = require('../util/Components');
  8. const docsUrl = require('../util/docsUrl');
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. description: 'Enforce React components to have a shouldComponentUpdate method',
  13. category: 'Best Practices',
  14. recommended: false,
  15. url: docsUrl('require-optimization')
  16. },
  17. schema: [{
  18. type: 'object',
  19. properties: {
  20. allowDecorators: {
  21. type: 'array',
  22. items: {
  23. type: 'string'
  24. }
  25. }
  26. },
  27. additionalProperties: false
  28. }]
  29. },
  30. create: Components.detect((context, components, utils) => {
  31. const MISSING_MESSAGE = 'Component is not optimized. Please add a shouldComponentUpdate method.';
  32. const configuration = context.options[0] || {};
  33. const allowDecorators = configuration.allowDecorators || [];
  34. /**
  35. * Checks to see if our component is decorated by PureRenderMixin via reactMixin
  36. * @param {ASTNode} node The AST node being checked.
  37. * @returns {Boolean} True if node is decorated with a PureRenderMixin, false if not.
  38. */
  39. const hasPureRenderDecorator = function (node) {
  40. if (node.decorators && node.decorators.length) {
  41. for (let i = 0, l = node.decorators.length; i < l; i++) {
  42. if (
  43. node.decorators[i].expression &&
  44. node.decorators[i].expression.callee &&
  45. node.decorators[i].expression.callee.object &&
  46. node.decorators[i].expression.callee.object.name === 'reactMixin' &&
  47. node.decorators[i].expression.callee.property &&
  48. node.decorators[i].expression.callee.property.name === 'decorate' &&
  49. node.decorators[i].expression.arguments &&
  50. node.decorators[i].expression.arguments.length &&
  51. node.decorators[i].expression.arguments[0].name === 'PureRenderMixin'
  52. ) {
  53. return true;
  54. }
  55. }
  56. }
  57. return false;
  58. };
  59. /**
  60. * Checks to see if our component is custom decorated
  61. * @param {ASTNode} node The AST node being checked.
  62. * @returns {Boolean} True if node is decorated name with a custom decorated, false if not.
  63. */
  64. const hasCustomDecorator = function (node) {
  65. const allowLength = allowDecorators.length;
  66. if (allowLength && node.decorators && node.decorators.length) {
  67. for (let i = 0; i < allowLength; i++) {
  68. for (let j = 0, l = node.decorators.length; j < l; j++) {
  69. if (
  70. node.decorators[j].expression &&
  71. node.decorators[j].expression.name === allowDecorators[i]
  72. ) {
  73. return true;
  74. }
  75. }
  76. }
  77. }
  78. return false;
  79. };
  80. /**
  81. * Checks if we are declaring a shouldComponentUpdate method
  82. * @param {ASTNode} node The AST node being checked.
  83. * @returns {Boolean} True if we are declaring a shouldComponentUpdate method, false if not.
  84. */
  85. const isSCUDeclarеd = function (node) {
  86. return Boolean(
  87. node &&
  88. node.name === 'shouldComponentUpdate'
  89. );
  90. };
  91. /**
  92. * Checks if we are declaring a PureRenderMixin mixin
  93. * @param {ASTNode} node The AST node being checked.
  94. * @returns {Boolean} True if we are declaring a PureRenderMixin method, false if not.
  95. */
  96. const isPureRenderDeclared = function (node) {
  97. let hasPR = false;
  98. if (node.value && node.value.elements) {
  99. for (let i = 0, l = node.value.elements.length; i < l; i++) {
  100. if (node.value.elements[i] && node.value.elements[i].name === 'PureRenderMixin') {
  101. hasPR = true;
  102. break;
  103. }
  104. }
  105. }
  106. return Boolean(
  107. node &&
  108. node.key.name === 'mixins' &&
  109. hasPR
  110. );
  111. };
  112. /**
  113. * Mark shouldComponentUpdate as declared
  114. * @param {ASTNode} node The AST node being checked.
  115. */
  116. const markSCUAsDeclared = function (node) {
  117. components.set(node, {
  118. hasSCU: true
  119. });
  120. };
  121. /**
  122. * Reports missing optimization for a given component
  123. * @param {Object} component The component to process
  124. */
  125. const reportMissingOptimization = function (component) {
  126. context.report({
  127. node: component.node,
  128. message: MISSING_MESSAGE,
  129. data: {
  130. component: component.name
  131. }
  132. });
  133. };
  134. /**
  135. * Checks if we are declaring function in class
  136. * @returns {Boolean} True if we are declaring function in class, false if not.
  137. */
  138. const isFunctionInClass = function () {
  139. let blockNode;
  140. let scope = context.getScope();
  141. while (scope) {
  142. blockNode = scope.block;
  143. if (blockNode && blockNode.type === 'ClassDeclaration') {
  144. return true;
  145. }
  146. scope = scope.upper;
  147. }
  148. return false;
  149. };
  150. return {
  151. ArrowFunctionExpression: function (node) {
  152. // Stateless Functional Components cannot be optimized (yet)
  153. markSCUAsDeclared(node);
  154. },
  155. ClassDeclaration: function (node) {
  156. if (!(hasPureRenderDecorator(node) || hasCustomDecorator(node) || utils.isPureComponent(node))) {
  157. return;
  158. }
  159. markSCUAsDeclared(node);
  160. },
  161. FunctionDeclaration: function (node) {
  162. // Skip if the function is declared in the class
  163. if (isFunctionInClass()) {
  164. return;
  165. }
  166. // Stateless Functional Components cannot be optimized (yet)
  167. markSCUAsDeclared(node);
  168. },
  169. FunctionExpression: function (node) {
  170. // Skip if the function is declared in the class
  171. if (isFunctionInClass()) {
  172. return;
  173. }
  174. // Stateless Functional Components cannot be optimized (yet)
  175. markSCUAsDeclared(node);
  176. },
  177. MethodDefinition: function (node) {
  178. if (!isSCUDeclarеd(node.key)) {
  179. return;
  180. }
  181. markSCUAsDeclared(node);
  182. },
  183. ObjectExpression: function (node) {
  184. // Search for the shouldComponentUpdate declaration
  185. for (let i = 0, l = node.properties.length; i < l; i++) {
  186. if (
  187. !node.properties[i].key || (
  188. !isSCUDeclarеd(node.properties[i].key) &&
  189. !isPureRenderDeclared(node.properties[i])
  190. )
  191. ) {
  192. continue;
  193. }
  194. markSCUAsDeclared(node);
  195. }
  196. },
  197. 'Program:exit': function () {
  198. const list = components.list();
  199. // Report missing shouldComponentUpdate for all components
  200. for (const component in list) {
  201. if (!has(list, component) || list[component].hasSCU) {
  202. continue;
  203. }
  204. reportMissingOptimization(list[component]);
  205. }
  206. }
  207. };
  208. })
  209. };