Skip to content
This repository has been archived by the owner on Feb 21, 2020. It is now read-only.

Latest commit

 

History

History
39 lines (34 loc) · 1.22 KB

require-atomic-updates.md

File metadata and controls

39 lines (34 loc) · 1.22 KB

This rule aims to report assignments to variables or properties where all of the following are true:

  • A variable or property is reassigned to a new value which is based on its old value.
  • A yield or await expression interrupts the assignment after the old value is read, and before the new value is set.
  • The rule cannot easily verify that the assignment is safe (e.g. if an assigned variable is local and would not be readable from anywhere else while the function is paused).

Examples of incorrect code for this rule:

//Bad:
let result;
async function foo() {
  result += await somethingElse;
  result = result + await somethingElse;
  result = result + doSomething(await somethingElse);
}
function* bar() {
  result += yield;
  result = result + (yield somethingElse);
  result = result + doSomething(yield somethingElse);
}

//Good:
let result;
async function foo() {
  result = await somethingElse + result;
  let tmp = await somethingElse;
  result += tmp;
  let localVariable = 0;
  localVariable += await somethingElse;
}
function* bar() {
  result += yield;
  result = (yield somethingElse) + result;
  result = doSomething(yield somethingElse, result);
}

Source