[d565449] | 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 | /**
|
---|
| 9 | * Make the map from identifiers to each reference.
|
---|
| 10 | * @param {escope.Scope} scope The scope to get references.
|
---|
| 11 | * @param {Map<Identifier, escope.Reference>} [outReferenceMap] The map from identifier nodes to each reference object.
|
---|
| 12 | * @returns {Map<Identifier, escope.Reference>} `referenceMap`.
|
---|
| 13 | */
|
---|
| 14 | function createReferenceMap(scope, outReferenceMap = new Map()) {
|
---|
| 15 | for (const reference of scope.references) {
|
---|
| 16 | if (reference.resolved === null) {
|
---|
| 17 | continue;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | outReferenceMap.set(reference.identifier, reference);
|
---|
| 21 | }
|
---|
| 22 | for (const childScope of scope.childScopes) {
|
---|
| 23 | if (childScope.type !== "function") {
|
---|
| 24 | createReferenceMap(childScope, outReferenceMap);
|
---|
| 25 | }
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | return outReferenceMap;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * Get `reference.writeExpr` of a given reference.
|
---|
| 33 | * If it's the read reference of MemberExpression in LHS, returns RHS in order to address `a.b = await a`
|
---|
| 34 | * @param {escope.Reference} reference The reference to get.
|
---|
| 35 | * @returns {Expression|null} The `reference.writeExpr`.
|
---|
| 36 | */
|
---|
| 37 | function getWriteExpr(reference) {
|
---|
| 38 | if (reference.writeExpr) {
|
---|
| 39 | return reference.writeExpr;
|
---|
| 40 | }
|
---|
| 41 | let node = reference.identifier;
|
---|
| 42 |
|
---|
| 43 | while (node) {
|
---|
| 44 | const t = node.parent.type;
|
---|
| 45 |
|
---|
| 46 | if (t === "AssignmentExpression" && node.parent.left === node) {
|
---|
| 47 | return node.parent.right;
|
---|
| 48 | }
|
---|
| 49 | if (t === "MemberExpression" && node.parent.object === node) {
|
---|
| 50 | node = node.parent;
|
---|
| 51 | continue;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | break;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | return null;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * Checks if an expression is a variable that can only be observed within the given function.
|
---|
| 62 | * @param {Variable|null} variable The variable to check
|
---|
| 63 | * @param {boolean} isMemberAccess If `true` then this is a member access.
|
---|
| 64 | * @returns {boolean} `true` if the variable is local to the given function, and is never referenced in a closure.
|
---|
| 65 | */
|
---|
| 66 | function isLocalVariableWithoutEscape(variable, isMemberAccess) {
|
---|
| 67 | if (!variable) {
|
---|
| 68 | return false; // A global variable which was not defined.
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | // If the reference is a property access and the variable is a parameter, it handles the variable is not local.
|
---|
| 72 | if (isMemberAccess && variable.defs.some(d => d.type === "Parameter")) {
|
---|
| 73 | return false;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | const functionScope = variable.scope.variableScope;
|
---|
| 77 |
|
---|
| 78 | return variable.references.every(reference =>
|
---|
| 79 | reference.from.variableScope === functionScope);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
| 83 | * Represents segment information.
|
---|
| 84 | */
|
---|
| 85 | class SegmentInfo {
|
---|
| 86 | constructor() {
|
---|
| 87 | this.info = new WeakMap();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /**
|
---|
| 91 | * Initialize the segment information.
|
---|
| 92 | * @param {PathSegment} segment The segment to initialize.
|
---|
| 93 | * @returns {void}
|
---|
| 94 | */
|
---|
| 95 | initialize(segment) {
|
---|
| 96 | const outdatedReadVariables = new Set();
|
---|
| 97 | const freshReadVariables = new Set();
|
---|
| 98 |
|
---|
| 99 | for (const prevSegment of segment.prevSegments) {
|
---|
| 100 | const info = this.info.get(prevSegment);
|
---|
| 101 |
|
---|
| 102 | if (info) {
|
---|
| 103 | info.outdatedReadVariables.forEach(Set.prototype.add, outdatedReadVariables);
|
---|
| 104 | info.freshReadVariables.forEach(Set.prototype.add, freshReadVariables);
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | this.info.set(segment, { outdatedReadVariables, freshReadVariables });
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * Mark a given variable as read on given segments.
|
---|
| 113 | * @param {PathSegment[]} segments The segments that it read the variable on.
|
---|
| 114 | * @param {Variable} variable The variable to be read.
|
---|
| 115 | * @returns {void}
|
---|
| 116 | */
|
---|
| 117 | markAsRead(segments, variable) {
|
---|
| 118 | for (const segment of segments) {
|
---|
| 119 | const info = this.info.get(segment);
|
---|
| 120 |
|
---|
| 121 | if (info) {
|
---|
| 122 | info.freshReadVariables.add(variable);
|
---|
| 123 |
|
---|
| 124 | // If a variable is freshly read again, then it's no more out-dated.
|
---|
| 125 | info.outdatedReadVariables.delete(variable);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | /**
|
---|
| 131 | * Move `freshReadVariables` to `outdatedReadVariables`.
|
---|
| 132 | * @param {PathSegment[]} segments The segments to process.
|
---|
| 133 | * @returns {void}
|
---|
| 134 | */
|
---|
| 135 | makeOutdated(segments) {
|
---|
| 136 | for (const segment of segments) {
|
---|
| 137 | const info = this.info.get(segment);
|
---|
| 138 |
|
---|
| 139 | if (info) {
|
---|
| 140 | info.freshReadVariables.forEach(Set.prototype.add, info.outdatedReadVariables);
|
---|
| 141 | info.freshReadVariables.clear();
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /**
|
---|
| 147 | * Check if a given variable is outdated on the current segments.
|
---|
| 148 | * @param {PathSegment[]} segments The current segments.
|
---|
| 149 | * @param {Variable} variable The variable to check.
|
---|
| 150 | * @returns {boolean} `true` if the variable is outdated on the segments.
|
---|
| 151 | */
|
---|
| 152 | isOutdated(segments, variable) {
|
---|
| 153 | for (const segment of segments) {
|
---|
| 154 | const info = this.info.get(segment);
|
---|
| 155 |
|
---|
| 156 | if (info && info.outdatedReadVariables.has(variable)) {
|
---|
| 157 | return true;
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | return false;
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | //------------------------------------------------------------------------------
|
---|
| 165 | // Rule Definition
|
---|
| 166 | //------------------------------------------------------------------------------
|
---|
| 167 |
|
---|
| 168 | /** @type {import('../shared/types').Rule} */
|
---|
| 169 | module.exports = {
|
---|
| 170 | meta: {
|
---|
| 171 | type: "problem",
|
---|
| 172 |
|
---|
| 173 | docs: {
|
---|
| 174 | description: "Disallow assignments that can lead to race conditions due to usage of `await` or `yield`",
|
---|
| 175 | recommended: false,
|
---|
| 176 | url: "https://eslint.org/docs/latest/rules/require-atomic-updates"
|
---|
| 177 | },
|
---|
| 178 |
|
---|
| 179 | fixable: null,
|
---|
| 180 |
|
---|
| 181 | schema: [{
|
---|
| 182 | type: "object",
|
---|
| 183 | properties: {
|
---|
| 184 | allowProperties: {
|
---|
| 185 | type: "boolean",
|
---|
| 186 | default: false
|
---|
| 187 | }
|
---|
| 188 | },
|
---|
| 189 | additionalProperties: false
|
---|
| 190 | }],
|
---|
| 191 |
|
---|
| 192 | messages: {
|
---|
| 193 | nonAtomicUpdate: "Possible race condition: `{{value}}` might be reassigned based on an outdated value of `{{value}}`.",
|
---|
| 194 | nonAtomicObjectUpdate: "Possible race condition: `{{value}}` might be assigned based on an outdated state of `{{object}}`."
|
---|
| 195 | }
|
---|
| 196 | },
|
---|
| 197 |
|
---|
| 198 | create(context) {
|
---|
| 199 | const allowProperties = !!context.options[0] && context.options[0].allowProperties;
|
---|
| 200 |
|
---|
| 201 | const sourceCode = context.sourceCode;
|
---|
| 202 | const assignmentReferences = new Map();
|
---|
| 203 | const segmentInfo = new SegmentInfo();
|
---|
| 204 | let stack = null;
|
---|
| 205 |
|
---|
| 206 | return {
|
---|
| 207 | onCodePathStart(codePath, node) {
|
---|
| 208 | const scope = sourceCode.getScope(node);
|
---|
| 209 | const shouldVerify =
|
---|
| 210 | scope.type === "function" &&
|
---|
| 211 | (scope.block.async || scope.block.generator);
|
---|
| 212 |
|
---|
| 213 | stack = {
|
---|
| 214 | upper: stack,
|
---|
| 215 | codePath,
|
---|
| 216 | referenceMap: shouldVerify ? createReferenceMap(scope) : null,
|
---|
| 217 | currentSegments: new Set()
|
---|
| 218 | };
|
---|
| 219 | },
|
---|
| 220 | onCodePathEnd() {
|
---|
| 221 | stack = stack.upper;
|
---|
| 222 | },
|
---|
| 223 |
|
---|
| 224 | // Initialize the segment information.
|
---|
| 225 | onCodePathSegmentStart(segment) {
|
---|
| 226 | segmentInfo.initialize(segment);
|
---|
| 227 | stack.currentSegments.add(segment);
|
---|
| 228 | },
|
---|
| 229 |
|
---|
| 230 | onUnreachableCodePathSegmentStart(segment) {
|
---|
| 231 | stack.currentSegments.add(segment);
|
---|
| 232 | },
|
---|
| 233 |
|
---|
| 234 | onUnreachableCodePathSegmentEnd(segment) {
|
---|
| 235 | stack.currentSegments.delete(segment);
|
---|
| 236 | },
|
---|
| 237 |
|
---|
| 238 | onCodePathSegmentEnd(segment) {
|
---|
| 239 | stack.currentSegments.delete(segment);
|
---|
| 240 | },
|
---|
| 241 |
|
---|
| 242 |
|
---|
| 243 | // Handle references to prepare verification.
|
---|
| 244 | Identifier(node) {
|
---|
| 245 | const { referenceMap } = stack;
|
---|
| 246 | const reference = referenceMap && referenceMap.get(node);
|
---|
| 247 |
|
---|
| 248 | // Ignore if this is not a valid variable reference.
|
---|
| 249 | if (!reference) {
|
---|
| 250 | return;
|
---|
| 251 | }
|
---|
| 252 | const variable = reference.resolved;
|
---|
| 253 | const writeExpr = getWriteExpr(reference);
|
---|
| 254 | const isMemberAccess = reference.identifier.parent.type === "MemberExpression";
|
---|
| 255 |
|
---|
| 256 | // Add a fresh read variable.
|
---|
| 257 | if (reference.isRead() && !(writeExpr && writeExpr.parent.operator === "=")) {
|
---|
| 258 | segmentInfo.markAsRead(stack.currentSegments, variable);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | /*
|
---|
| 262 | * Register the variable to verify after ESLint traversed the `writeExpr` node
|
---|
| 263 | * if this reference is an assignment to a variable which is referred from other closure.
|
---|
| 264 | */
|
---|
| 265 | if (writeExpr &&
|
---|
| 266 | writeExpr.parent.right === writeExpr && // ← exclude variable declarations.
|
---|
| 267 | !isLocalVariableWithoutEscape(variable, isMemberAccess)
|
---|
| 268 | ) {
|
---|
| 269 | let refs = assignmentReferences.get(writeExpr);
|
---|
| 270 |
|
---|
| 271 | if (!refs) {
|
---|
| 272 | refs = [];
|
---|
| 273 | assignmentReferences.set(writeExpr, refs);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | refs.push(reference);
|
---|
| 277 | }
|
---|
| 278 | },
|
---|
| 279 |
|
---|
| 280 | /*
|
---|
| 281 | * Verify assignments.
|
---|
| 282 | * If the reference exists in `outdatedReadVariables` list, report it.
|
---|
| 283 | */
|
---|
| 284 | ":expression:exit"(node) {
|
---|
| 285 |
|
---|
| 286 | // referenceMap exists if this is in a resumable function scope.
|
---|
| 287 | if (!stack.referenceMap) {
|
---|
| 288 | return;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | // Mark the read variables on this code path as outdated.
|
---|
| 292 | if (node.type === "AwaitExpression" || node.type === "YieldExpression") {
|
---|
| 293 | segmentInfo.makeOutdated(stack.currentSegments);
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | // Verify.
|
---|
| 297 | const references = assignmentReferences.get(node);
|
---|
| 298 |
|
---|
| 299 | if (references) {
|
---|
| 300 | assignmentReferences.delete(node);
|
---|
| 301 |
|
---|
| 302 | for (const reference of references) {
|
---|
| 303 | const variable = reference.resolved;
|
---|
| 304 |
|
---|
| 305 | if (segmentInfo.isOutdated(stack.currentSegments, variable)) {
|
---|
| 306 | if (node.parent.left === reference.identifier) {
|
---|
| 307 | context.report({
|
---|
| 308 | node: node.parent,
|
---|
| 309 | messageId: "nonAtomicUpdate",
|
---|
| 310 | data: {
|
---|
| 311 | value: variable.name
|
---|
| 312 | }
|
---|
| 313 | });
|
---|
| 314 | } else if (!allowProperties) {
|
---|
| 315 | context.report({
|
---|
| 316 | node: node.parent,
|
---|
| 317 | messageId: "nonAtomicObjectUpdate",
|
---|
| 318 | data: {
|
---|
| 319 | value: sourceCode.getText(node.parent.left),
|
---|
| 320 | object: variable.name
|
---|
| 321 | }
|
---|
| 322 | });
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | }
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 | };
|
---|
| 330 | }
|
---|
| 331 | };
|
---|