no-access-state-in-setstate.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * @fileoverview Prevent usage of this.state within setState
  3. * @author Rolf Erik Lekang, Jørgen Aaberg
  4. */
  5. 'use strict';
  6. const docsUrl = require('../util/docsUrl');
  7. const Components = require('../util/Components');
  8. // ------------------------------------------------------------------------------
  9. // Rule Definition
  10. // ------------------------------------------------------------------------------
  11. module.exports = {
  12. meta: {
  13. docs: {
  14. description: 'Reports when this.state is accessed within setState',
  15. category: 'Possible Errors',
  16. recommended: false,
  17. url: docsUrl('no-access-state-in-setstate')
  18. },
  19. messages: {
  20. useCallback: 'Use callback in setState when referencing the previous state.'
  21. }
  22. },
  23. create: Components.detect((context, components, utils) => {
  24. function isSetStateCall(node) {
  25. return node.type === 'CallExpression'
  26. && node.callee.property
  27. && node.callee.property.name === 'setState'
  28. && node.callee.object.type === 'ThisExpression';
  29. }
  30. function isFirstArgumentInSetStateCall(current, node) {
  31. if (!isSetStateCall(current)) {
  32. return false;
  33. }
  34. while (node && node.parent !== current) {
  35. node = node.parent;
  36. }
  37. return current.arguments[0] === node;
  38. }
  39. function isClassComponent() {
  40. return !!(utils.getParentES6Component() || utils.getParentES5Component());
  41. }
  42. // The methods array contains all methods or functions that are using this.state
  43. // or that are calling another method or function using this.state
  44. const methods = [];
  45. // The vars array contains all variables that contains this.state
  46. const vars = [];
  47. return {
  48. CallExpression(node) {
  49. if (!isClassComponent()) {
  50. return;
  51. }
  52. // Appends all the methods that are calling another
  53. // method containing this.state to the methods array
  54. methods.forEach((method) => {
  55. if (node.callee.name === method.methodName) {
  56. let current = node.parent;
  57. while (current.type !== 'Program') {
  58. if (current.type === 'MethodDefinition') {
  59. methods.push({
  60. methodName: current.key.name,
  61. node: method.node
  62. });
  63. break;
  64. }
  65. current = current.parent;
  66. }
  67. }
  68. });
  69. // Finding all CallExpressions that is inside a setState
  70. // to further check if they contains this.state
  71. let current = node.parent;
  72. while (current.type !== 'Program') {
  73. if (isFirstArgumentInSetStateCall(current, node)) {
  74. const methodName = node.callee.name;
  75. methods.forEach((method) => {
  76. if (method.methodName === methodName) {
  77. context.report({
  78. node: method.node,
  79. messageId: 'useCallback'
  80. });
  81. }
  82. });
  83. break;
  84. }
  85. current = current.parent;
  86. }
  87. },
  88. MemberExpression(node) {
  89. if (
  90. node.property.name === 'state'
  91. && node.object.type === 'ThisExpression'
  92. && isClassComponent()
  93. ) {
  94. let current = node;
  95. while (current.type !== 'Program') {
  96. // Reporting if this.state is directly within this.setState
  97. if (isFirstArgumentInSetStateCall(current, node)) {
  98. context.report({
  99. node,
  100. messageId: 'useCallback'
  101. });
  102. break;
  103. }
  104. // Storing all functions and methods that contains this.state
  105. if (current.type === 'MethodDefinition') {
  106. methods.push({
  107. methodName: current.key.name,
  108. node
  109. });
  110. break;
  111. } else if (current.type === 'FunctionExpression' && current.parent.key) {
  112. methods.push({
  113. methodName: current.parent.key.name,
  114. node
  115. });
  116. break;
  117. }
  118. // Storing all variables containg this.state
  119. if (current.type === 'VariableDeclarator') {
  120. vars.push({
  121. node,
  122. scope: context.getScope(),
  123. variableName: current.id.name
  124. });
  125. break;
  126. }
  127. current = current.parent;
  128. }
  129. }
  130. },
  131. Identifier(node) {
  132. // Checks if the identifier is a variable within an object
  133. let current = node;
  134. while (current.parent.type === 'BinaryExpression') {
  135. current = current.parent;
  136. }
  137. if (
  138. current.parent.value === current
  139. || current.parent.object === current
  140. ) {
  141. while (current.type !== 'Program') {
  142. if (isFirstArgumentInSetStateCall(current, node)) {
  143. vars
  144. .filter((v) => v.scope === context.getScope() && v.variableName === node.name)
  145. .forEach((v) => {
  146. context.report({
  147. node: v.node,
  148. messageId: 'useCallback'
  149. });
  150. });
  151. }
  152. current = current.parent;
  153. }
  154. }
  155. },
  156. ObjectPattern(node) {
  157. const isDerivedFromThis = node.parent.init && node.parent.init.type === 'ThisExpression';
  158. node.properties.forEach((property) => {
  159. if (property && property.key && property.key.name === 'state' && isDerivedFromThis) {
  160. vars.push({
  161. node: property.key,
  162. scope: context.getScope(),
  163. variableName: property.key.name
  164. });
  165. }
  166. });
  167. }
  168. };
  169. })
  170. };