require-atomic-updates.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * @fileoverview disallow assignments that can lead to race conditions due to usage of `await` or `yield`
  3. * @author Teddy Katz
  4. * @author Toru Nagashima
  5. */
  6. "use strict";
  7. /**
  8. * Make the map from identifiers to each reference.
  9. * @param {escope.Scope} scope The scope to get references.
  10. * @param {Map<Identifier, escope.Reference>} [outReferenceMap] The map from identifier nodes to each reference object.
  11. * @returns {Map<Identifier, escope.Reference>} `referenceMap`.
  12. */
  13. function createReferenceMap(scope, outReferenceMap = new Map()) {
  14. for (const reference of scope.references) {
  15. outReferenceMap.set(reference.identifier, reference);
  16. }
  17. for (const childScope of scope.childScopes) {
  18. if (childScope.type !== "function") {
  19. createReferenceMap(childScope, outReferenceMap);
  20. }
  21. }
  22. return outReferenceMap;
  23. }
  24. /**
  25. * Get `reference.writeExpr` of a given reference.
  26. * If it's the read reference of MemberExpression in LHS, returns RHS in order to address `a.b = await a`
  27. * @param {escope.Reference} reference The reference to get.
  28. * @returns {Expression|null} The `reference.writeExpr`.
  29. */
  30. function getWriteExpr(reference) {
  31. if (reference.writeExpr) {
  32. return reference.writeExpr;
  33. }
  34. let node = reference.identifier;
  35. while (node) {
  36. const t = node.parent.type;
  37. if (t === "AssignmentExpression" && node.parent.left === node) {
  38. return node.parent.right;
  39. }
  40. if (t === "MemberExpression" && node.parent.object === node) {
  41. node = node.parent;
  42. continue;
  43. }
  44. break;
  45. }
  46. return null;
  47. }
  48. /**
  49. * Checks if an expression is a variable that can only be observed within the given function.
  50. * @param {Variable|null} variable The variable to check
  51. * @param {boolean} isMemberAccess If `true` then this is a member access.
  52. * @returns {boolean} `true` if the variable is local to the given function, and is never referenced in a closure.
  53. */
  54. function isLocalVariableWithoutEscape(variable, isMemberAccess) {
  55. if (!variable) {
  56. return false; // A global variable which was not defined.
  57. }
  58. // If the reference is a property access and the variable is a parameter, it handles the variable is not local.
  59. if (isMemberAccess && variable.defs.some(d => d.type === "Parameter")) {
  60. return false;
  61. }
  62. const functionScope = variable.scope.variableScope;
  63. return variable.references.every(reference =>
  64. reference.from.variableScope === functionScope);
  65. }
  66. class SegmentInfo {
  67. constructor() {
  68. this.info = new WeakMap();
  69. }
  70. /**
  71. * Initialize the segment information.
  72. * @param {PathSegment} segment The segment to initialize.
  73. * @returns {void}
  74. */
  75. initialize(segment) {
  76. const outdatedReadVariableNames = new Set();
  77. const freshReadVariableNames = new Set();
  78. for (const prevSegment of segment.prevSegments) {
  79. const info = this.info.get(prevSegment);
  80. if (info) {
  81. info.outdatedReadVariableNames.forEach(Set.prototype.add, outdatedReadVariableNames);
  82. info.freshReadVariableNames.forEach(Set.prototype.add, freshReadVariableNames);
  83. }
  84. }
  85. this.info.set(segment, { outdatedReadVariableNames, freshReadVariableNames });
  86. }
  87. /**
  88. * Mark a given variable as read on given segments.
  89. * @param {PathSegment[]} segments The segments that it read the variable on.
  90. * @param {string} variableName The variable name to be read.
  91. * @returns {void}
  92. */
  93. markAsRead(segments, variableName) {
  94. for (const segment of segments) {
  95. const info = this.info.get(segment);
  96. if (info) {
  97. info.freshReadVariableNames.add(variableName);
  98. }
  99. }
  100. }
  101. /**
  102. * Move `freshReadVariableNames` to `outdatedReadVariableNames`.
  103. * @param {PathSegment[]} segments The segments to process.
  104. * @returns {void}
  105. */
  106. makeOutdated(segments) {
  107. for (const segment of segments) {
  108. const info = this.info.get(segment);
  109. if (info) {
  110. info.freshReadVariableNames.forEach(Set.prototype.add, info.outdatedReadVariableNames);
  111. info.freshReadVariableNames.clear();
  112. }
  113. }
  114. }
  115. /**
  116. * Check if a given variable is outdated on the current segments.
  117. * @param {PathSegment[]} segments The current segments.
  118. * @param {string} variableName The variable name to check.
  119. * @returns {boolean} `true` if the variable is outdated on the segments.
  120. */
  121. isOutdated(segments, variableName) {
  122. for (const segment of segments) {
  123. const info = this.info.get(segment);
  124. if (info && info.outdatedReadVariableNames.has(variableName)) {
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. }
  131. //------------------------------------------------------------------------------
  132. // Rule Definition
  133. //------------------------------------------------------------------------------
  134. module.exports = {
  135. meta: {
  136. type: "problem",
  137. docs: {
  138. description: "disallow assignments that can lead to race conditions due to usage of `await` or `yield`",
  139. category: "Possible Errors",
  140. recommended: false,
  141. url: "https://eslint.org/docs/rules/require-atomic-updates"
  142. },
  143. fixable: null,
  144. schema: [],
  145. messages: {
  146. nonAtomicUpdate: "Possible race condition: `{{value}}` might be reassigned based on an outdated value of `{{value}}`."
  147. }
  148. },
  149. create(context) {
  150. const sourceCode = context.getSourceCode();
  151. const assignmentReferences = new Map();
  152. const segmentInfo = new SegmentInfo();
  153. let stack = null;
  154. return {
  155. onCodePathStart(codePath) {
  156. const scope = context.getScope();
  157. const shouldVerify =
  158. scope.type === "function" &&
  159. (scope.block.async || scope.block.generator);
  160. stack = {
  161. upper: stack,
  162. codePath,
  163. referenceMap: shouldVerify ? createReferenceMap(scope) : null
  164. };
  165. },
  166. onCodePathEnd() {
  167. stack = stack.upper;
  168. },
  169. // Initialize the segment information.
  170. onCodePathSegmentStart(segment) {
  171. segmentInfo.initialize(segment);
  172. },
  173. // Handle references to prepare verification.
  174. Identifier(node) {
  175. const { codePath, referenceMap } = stack;
  176. const reference = referenceMap && referenceMap.get(node);
  177. // Ignore if this is not a valid variable reference.
  178. if (!reference) {
  179. return;
  180. }
  181. const name = reference.identifier.name;
  182. const variable = reference.resolved;
  183. const writeExpr = getWriteExpr(reference);
  184. const isMemberAccess = reference.identifier.parent.type === "MemberExpression";
  185. // Add a fresh read variable.
  186. if (reference.isRead() && !(writeExpr && writeExpr.parent.operator === "=")) {
  187. segmentInfo.markAsRead(codePath.currentSegments, name);
  188. }
  189. /*
  190. * Register the variable to verify after ESLint traversed the `writeExpr` node
  191. * if this reference is an assignment to a variable which is referred from other closure.
  192. */
  193. if (writeExpr &&
  194. writeExpr.parent.right === writeExpr && // ← exclude variable declarations.
  195. !isLocalVariableWithoutEscape(variable, isMemberAccess)
  196. ) {
  197. let refs = assignmentReferences.get(writeExpr);
  198. if (!refs) {
  199. refs = [];
  200. assignmentReferences.set(writeExpr, refs);
  201. }
  202. refs.push(reference);
  203. }
  204. },
  205. /*
  206. * Verify assignments.
  207. * If the reference exists in `outdatedReadVariableNames` list, report it.
  208. */
  209. ":expression:exit"(node) {
  210. const { codePath, referenceMap } = stack;
  211. // referenceMap exists if this is in a resumable function scope.
  212. if (!referenceMap) {
  213. return;
  214. }
  215. // Mark the read variables on this code path as outdated.
  216. if (node.type === "AwaitExpression" || node.type === "YieldExpression") {
  217. segmentInfo.makeOutdated(codePath.currentSegments);
  218. }
  219. // Verify.
  220. const references = assignmentReferences.get(node);
  221. if (references) {
  222. assignmentReferences.delete(node);
  223. for (const reference of references) {
  224. const name = reference.identifier.name;
  225. if (segmentInfo.isOutdated(codePath.currentSegments, name)) {
  226. context.report({
  227. node: node.parent,
  228. messageId: "nonAtomicUpdate",
  229. data: {
  230. value: sourceCode.getText(node.parent.left)
  231. }
  232. });
  233. }
  234. }
  235. }
  236. }
  237. };
  238. }
  239. };