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

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