Skip to content

code-range-compare ​

<, <=, > or >= with a constant of a code object of whole numbers: afterJob >= Job.MOVE && afterJob <= Job.IGNITE, u.job < Job.DEPOSIT. A code object is an as const or frozen object of literals, such as a codesOf(...) table. Not reported for one declared ordered with /** @ordered */, nor for one with a fraction among its values ({ ms: 650, reach: 1.5 }), which holds measures rather than codes.

Why: the codes of such a table are its names' places in a list, and a new name is appended at the end. A range over them reads as "these members", but it says "these codes": a name added later gets the next code, outside the range or past it, and the range takes it in or leaves it out without a word. An adopter's save reader accepted the jobs a unit keeps as afterJob >= Job.MOVE && afterJob <= Job.IGNITE plus a few names. Farms and mines were added after IGNITE; a farmer who left his field for the toilet kept FARM, and the reader refused the save. The message names the members the range takes in today. Guide: Enums.

❌ Rejected: a job appended later is out of the range

✕ Rejectedcode-range-compare
ts
export const Job = Object.freeze({ NONE: 0, MOVE: 1, GATHER: 2, IGNITE: 3, MINE: 4 });
export type Job = (typeof Job)[keyof typeof Job];

export function resumable(afterJob: Job): boolean {
  return afterJob >= Job.MOVE && afterJob <= Job.IGNITE;
}
text
`afterJob >= Job.MOVE && afterJob <= Job.IGNITE` picks members of `Job` by their codes: it takes in
Job.MOVE, Job.GATHER and Job.IGNITE. A name added to `Job` gets the next code, and this compare
leaves it out without a word

✅ Accepted: the members, named once

✓ Accepted0 diagnostics
ts
export const Job = Object.freeze({ NONE: 0, MOVE: 1, GATHER: 2, IGNITE: 3, MINE: 4 });
export type Job = (typeof Job)[keyof typeof Job];

/** Jobs a unit away from them keeps, for the reader, the writer and the checks. */
export const RESUMABLE: ReadonlySet<Job> = new Set([Job.MOVE, Job.GATHER, Job.IGNITE, Job.MINE]);

export function resumable(afterJob: Job): boolean {
  return RESUMABLE.has(afterJob);
}

✅ Accepted: a set whose order is what it means

✓ Accepted0 diagnostics
ts
/** Life stages, in the order a unit lives them. @ordered */
export const Stage = Object.freeze({ BABY: 0, CHILD: 1, ADULT: 2, ELDER: 3 });
export type Stage = (typeof Stage)[keyof typeof Stage];

export function grown(stage: Stage): boolean {
  return stage >= Stage.ADULT;
}

@ordered ​

The tag goes in the comment right before the module-level const of the code object (exported or not), with other text and tags if you like, as @sorted and @unique do. It says the order of the names is part of what they mean (stages, levels, severities), so a range over them is the point. New names then go where they belong in that order, not at the end. The tag is read from the file that declares the object, also when the compare is in another file.

What counts ​

  • Reported: an order compare of a value with a constant read from a code object of numbers (Job.MOVE, codes.job.MOVE); a range x >= A && x <= B is reported once.
  • Not reported: an object marked @ordered; compares with number literals (x >= 2), and of two constants; tables of strings; tables with a value that isn't a whole number (Object.freeze({ px: 7, ms: 650, reach: 1.5 })): codes are places in a list, so a fraction says the table holds tuning values, and the compare with one is a threshold.
  • Still reported: a table of whole numbers, whatever else it holds. The rule can't tell Object.freeze({ ms: 300, maxRange: 12 }) (two thresholds) from a table of codes written by hand with gaps ({ NONE: 0, MOVE: 1, MINE: 3 }, a member removed from a save format). Keep a threshold in a const of its own (const SHAKE_MS = 300), or mark the table /** @ordered */.

✅ Accepted: a table of measures

✓ Accepted0 diagnostics
ts
export const SHAKE = Object.freeze({ px: 7, ms: 650, reach: 1.5, max: 12 });

export function settled(elapsed: number, strength: number): boolean {
  return elapsed > SHAKE.ms || strength >= SHAKE.max;
}

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra