unbalanced-update
A change and the change that undoes it, written as a pair in one function (a flag set and cleared, a count taken and given back, an entry added and removed, a value changed and given back what it held), and a way out between the two that leaves with the first and without the second: an await that rejects, a call that throws, a return, a q(...) that returns the error, a throw, and in a loop's body a continue or a break. On by default, as a warning.
Why: the pair says the function means to bracket its work, and a way out that skips the undo leaves the world as nobody meant it: a busy flag stuck on (the button stays disabled for good), a count that never returns to zero (whoever waits on it waits for ever), an id that stays in the set of pending ones, a lock nobody drops. The path that forgets is the one you rarely run: the request that fails, the early return added later. Rust's answer is a guard whose Drop undoes the change on every way out; in TypeScript it is try / finally, or no hand-written flag at all.
✅ Accepted: in a component, useTask holds the task's state itself (Idle, Loading, Ready, Failed), so there is no flag to forget
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { onMounted } from "vue";
import { Match } from "@evobug_com/safe-ts-vue/match";
import { useTask } from "@evobug_com/safe-ts-vue/task";
import { fetchOrders } from "./api";
const { state, run } = useTask((signal) => fetchOrders(signal));
onMounted(() => {
run().catch((reason: unknown) => console.error(reason));
});
</script>
<template>
<Match :value="state">
<template #Loading>Loading…</template>
<template #Failed="{ error }">{{ error.message }}</template>
<template #Ready="{ value }">
<p v-for="order in value" :key="order.id">{{ order.total }}</p>
</template>
<template #_></template>
</Match>
</template>Elsewhere (a store, a class, a server's state) a flag by hand is fine when it is cleared on every way out.
❌ Rejected: if load() rejects, busy stays true
✕ Rejectedunbalanced-update
ts
import type { Mut } from "@evobug_com/safe-ts-std/mut";
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class Offline extends PropagateError {}
async function load(): Promise<string> {
await Promise.resolve();
if (Date.now() < 0) throw new Offline("offline");
return "text";
}
interface Panel {
busy: boolean;
text: string;
}
export async function reload(panel: Mut<Panel>): Promise<void> {
panel.busy = true;
panel.text = await load();
panel.busy = false;
}✅ Accepted: the flag is cleared in a finally
✓ Accepted0 diagnostics
ts
import type { Mut } from "@evobug_com/safe-ts-std/mut";
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class Offline extends PropagateError {}
async function load(): Promise<string> {
await Promise.resolve();
if (Date.now() < 0) throw new Offline("offline");
return "text";
}
interface Panel {
busy: boolean;
text: string;
}
export async function reload(panel: Mut<Panel>): Promise<void> {
panel.busy = true;
try {
panel.text = await load();
} finally {
panel.busy = false;
}
}The same pair made by the functions a function calls counts: enter(mut(room)) and leave(mut(room)) are a pair when the whole program shows that enter always adds one to room.users and leave takes one off.
❌ Rejected: the visit called off on the way leaves its user counted
✕ Rejectedunbalanced-update
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
interface Room {
users: number;
}
function enter(room: Mut<Room>): void {
room.users += 1;
}
function leave(room: Mut<Room>): void {
room.users -= 1;
}
export function visit(room: Mut<Room>, calledOff: boolean): string {
enter(mut(room));
if (calledOff) return "called off";
leave(mut(room));
return "done";
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
interface Room {
users: number;
}
function enter(room: Mut<Room>): void {
room.users += 1;
}
function leave(room: Mut<Room>): void {
room.users -= 1;
}
export function visit(room: Mut<Room>, calledOff: boolean): string {
enter(room);
if (calledOff) return "called off";
leave(room);
return "done";
}✓ Accepted0 diagnostics
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
interface Room {
users: number;
}
function enter(room: Mut<Room>): void {
room.users += 1;
}
function leave(room: Mut<Room>): void {
room.users -= 1;
}
export function visit(room: Mut<Room>, calledOff: boolean): string {
enter(mut(room));
try {
if (calledOff) return "called off";
return "done";
} finally {
leave(mut(room));
}
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
interface Room {
users: number;
}
function enter(room: Mut<Room>): void {
room.users += 1;
}
function leave(room: Mut<Room>): void {
room.users -= 1;
}
export function visit(room: Mut<Room>, calledOff: boolean): string {
enter(room);
try {
if (calledOff) return "called off";
return "done";
} finally {
leave(room);
}
}The quick fix Undo it in a finally writes this when the undo is a statement of the same block as the change and nothing declared between them is used after it.
What counts
- A pair is two statements of one statement list (a block, the function's body, a loop's body: one round of it), the second after the first, each a change the code shows undoes the other: a boolean place set to
trueand then tofalse(or the reverse);x += nandx -= n(++and--) with the same step;set.add(k)andset.delete(k),map.set(k, v)andmap.delete(k)with the same key (item.idanditem.idare one key, aconstread once stands for its value);pushandpop; a place given a new value and then what aconstsaved of it before (const old = view.mode; view.mode = "edit"; ...; view.mode = old). A change in a branch or a loop whose undo is outside it isn't half of a pair:if (ok) count += 1;is a decision, not a bracket. - Through calls: a call is the change its callee always makes to what it is given, once, on every path that completes (
room.users += 1as a statement of its body, with nothing else writingusers), with the caller's arguments put in (setMode(lamp, Mode.OFF)setslamp.modetoMode.OFFwhensetModedoeslamp.mode = mode). Which functions those are is a summary of each function, worked out once per run by the whole-program pass (the file's own analysis and the editor's ask the same pass). By the loan rules a call changes only what it is lent (mut(room), a parameter declaredMut<...>) and its receiver; a function with no body here, or a method a subclass may override, makes no change the rule can count on (seedecided-testfor every form of call). A callee's summary describes its own changes; a call the callee makes may change anything it is given, so a change made there is no half of a pair. - A call between the two halves is the work the pair brackets, whatever it may change: a function with no body here, a package's call, a function value, a method that may be overridden, a place handed on. The pair stays open across it; what closes it is the undo, written or made by a callee that always makes it. Only the function's own write ends what the rule knows of the pair: a value it can't read (
panel.busy = isBusy()), a write it doesn't follow (an element,delete), a new value for what holds the place (panel = next). - Places are what outlives the call: a parameter's object,
this, module state, a binding of an enclosing function. The function's own locals end with it and aren't followed. - A way out is a
return, aq(...)that returns the error, athrow, acontinueor abreakbetween two halves in a loop's body (the round it ends leaves the change done; the finding says which line), or anawaitoutside atry(or inside one that only has afinally) whose call can fail, asunreleased-resourcedecides it: a function of the project that propagates a failure, a library's member a pack marks as failing, something the checker can't look up. Anawaitof a function declared to return a promise of aResult, of a value the code uses as aResult(q(await f()),isErr(r)), of a pack's safe version or ofPromise.resolve(...)can't reject, and isn't one. A call (or anew) between the two whose callee can throw where it is, asunreleased-resourcedecides it (a function of the project that throws or propagates aPropagateError, through aThrowingslot too; a built-in that throws,JSON.parse(text); a library's member a pack marks as throwing; a callback the call runs right away that does,list.forEach((x) => check(x))), is one too, whoever catches it: a call that can't fail, std's, or one the checker can't resolve isn't. std'sqis known by its import, under whatever name (q as ask). - A flag and a
returnthe code writes (or acontinueorbreak): setting a flag and clearing it only after a written earlyreturnis often what the function decides there (hide the old error; show a new one only on failure), so a flag is reported on areturnonly when the function tested the flag before setting it, as a lock:if (this.busy) return; this.busy = true;. Aq(...)that returns the error is the failure leaving, not an outcome the author chose for it, as a rejection is: both are reported for every pair. In a checked projectpanel.text = q(await load());between the two is the stuck flag's usual spelling. - Test files aren't asked: a test that fails on the way out is reported by its runner, and what it leaves behind is its fixture's.
How it relates to the other rules
unreleased-resourceis the same question about a resource the packs name (a timer, a handle, an observer); this rule asks it of state the function changes itself.vue-split-statereports aloadingref set around anawaitin a component, whatever the way out; this rule reports the way out, anywhere.stale-across-awaitis about what another call changes during theawait; this rule about what the function's own way out leaves.
What the rule can't see
- Pairs split across functions:
begin()sets the flag andend()clears it, and the caller calls both. Each function alone has no pair; whether every caller callsend()on every way out is out of this rule's sight by construction. - A pair the code doesn't show: an acquire and a release with nothing in common the code shows (
lock(name)andunlock(name)that work on a table elsewhere), a library's "callupdate()after changingitems". Those would need a declaration of the pair. - A change a callee makes only on some paths, or together with other writes of the same place: no summary says it always happens, so it is no half of a pair. The same for a change made below the callee (
entercallsbump, which adds the user).