Code tables, tests and lists: duplicate-code-value, pinned-constant-assert, @sorted/@unique (list-invariant), no-positional-children #95

Merged
sionzee merged 1 commit from tables-tests-invariants into main 2026-11-20 16:34:55 +01:00
Owner

Problem

An adopter audited ~50 old bug fixes in their game (TypeScript, Vue, PixiJS). Four kinds of bug came from code that looked fine and that nothing checked:

  • Codes numbered by hand collided. Parallel branches each added a member to a code table and picked the same next number (job 16/17, encodings 37–39, opcodes 0x3C/0x3D), and the merge kept both lines.
  • A child picked by position. actor.children[2]: one zIndex made PixiJS re-sort the actor's children, and every unselected villager lost its body.
  • Tests that restated constants. ENCODING_VERSION === 34 broke main after merges (their issues #104/#105).
  • A list that had to stay sorted (ENCODING_FEATURES) said so only in a comment, and an entry landed out of order.

Solution

Four rules, each wired end to end: the rule list and levels, the oxlint plugin (index.mjs, recommended.json), a rule page with checked ✅/❌ examples, the rule tables in README and docs, the sidebar, the guides and CHANGELOG.

  • duplicate-code-value (error). It reports:

    • two members of a code table with one value;
    • a literal union naming a member twice (1 | 2 | 2);
    • two case labels of one switch with one value (case 1: after case Op.Hello:).

    Tables and labels are read with the existing typeof evaluator (json.rs), in the whole-program pass. So as const, Object.freeze, spreads of codesOf([...]) and imported tables all work. A table is a code table when one of these holds:

    • the file takes the union of its values ((typeof X)[keyof typeof X], ValueOf<typeof X>);
    • it is marked /** @unique */;
    • its values are whole numbers under capitalised names, either close together or flags from 1.

    Limits and sizes ({ title: 80, body: 80 }) don't count. A deliberate alias is exempt when a comment on the member says alias or @deprecated, except in a @unique table.

  • list-invariant (error) checks /** @sorted */ and /** @unique */ on a module-level [...] as const or Object.freeze([...]) of strings or numbers. A type annotation is allowed, as in the adopter's readonly string[].

    • It reports the first element out of order and the place it belongs ("between 'library' and 'pub'"), and the second of two equal elements.
    • It also reports a tag it can't verify, such as on a mutable array.
    • @unique on a code table goes to duplicate-code-value instead.
    • Tags are found with inline::tagged, the same as @json, @inline and @cache.
  • pinned-constant-assert (warn) reports, in test files, an equality assertion between a literal and a constant whose value is a literal. The constant can be imported, a member of an as const table, or a const of the test file. Assertions covered:

    • expect(X).toBe/toEqual/toStrictEqual, also expect.soft, a global expect, and chai's .to.equal;
    • assert.equal/strictEqual/deepEqual/deepStrictEqual, also imported by name;
    • assert(X === lit) and assert.ok(X === lit).

    An assertion with a message is exempt. The adopter's own repo showed why: assert.equal(Op.Hello, 0x01, 'HELLO is frozen') is a deliberate wire-compat pin.

  • no-positional-children (idiomatic, warn) reports a literal index into .children or .childNodes, .item(n) on them, and getChildAt(n). Index 0 behind a children.length === 1 test of the same list is left alone (the only child). It is idiomatic because without types children can be a data tree's field, and el.children[0] is common in DOM code.

json.rs gains four small pub(crate) helpers on top of the existing reader: value_expr, literal_value, const_members and number_of. The reader itself is unchanged, so the lane extending it for computed keys and references ({ [K.A]: … }) should give duplicate-code-value those tables for free.

Testing

  • cargo nextest run --workspace: 280/280 pass. That includes the new fixtures code_tables, pinned_constants, positional_children and sorted_lists (reduced, with positives and negatives, and checked through both check_entry and check_file), plus the docs and skill examples.
  • node scripts/docs-typecheck.mjs: 621 documentation examples compile.
  • npm run test:std: 367/367 pass.
  • npm run build -w docs: the site builds.
  • npm run check: the demo's error count is unchanged, and no new rule fires there.
  • cargo clippy -p safe_ts_core: no warnings in the new code.
  • Against the adopter's repo, read-only, through git archive into /tmp:
    • no-positional-children fires 7 times on fb9e22a5^ (actor.children[0], [2], [3]) and not on fb9e22a5.
    • With /** @sorted @unique */ added to a copy, list-invariant reports 'marchTogether' on cb282799^ ("between 'library' and 'pub'") and nothing on cb282799.
    • On their HEAD (699 files): duplicate-code-value reports nothing. pinned-constant-assert reports 15 restated constants, all in tests: GENERATOR_VERSION, MIN_ENCODING_VERSION, MIN_PART_SIZE, …; their 'HELLO is frozen' pins are exempt. no-positional-children reports 14 with --idiomatic, mostly tests and SVG-node reads.
    • duplicate-code-value on 42622b07^: no revision in their history ever kept a duplicate code (a script over every table in about 3000 commits found none). The collisions were fixed by hand during merges, so the firing side rests on the reduced fixtures.

Left out

  • exhaustive-dispatch and exhaustive-switch for literal unions and code sets belong to another lane; negative-member-guard, code-range-compare and inline-member-set to a later one.
  • oxlint's own no-duplicate-case also reports case labels written the same way, so with the oxlint plugin those report twice. duplicate-code-value compares values (case 1: vs case Op.Hello:), and safe-ts check doesn't run oxlint's rules.
  • @sorted is ascending only; there is no descending form.
  • A table typed only in another file (the union type declared elsewhere) counts only through the number heuristic or @unique.

Created with Claude Opus 5.5 in Claude Code.

## Problem An adopter audited ~50 old bug fixes in their game (TypeScript, Vue, PixiJS). Four kinds of bug came from code that looked fine and that nothing checked: - **Codes numbered by hand collided.** Parallel branches each added a member to a code table and picked the same next number (job 16/17, encodings 37–39, opcodes `0x3C`/`0x3D`), and the merge kept both lines. - **A child picked by position.** `actor.children[2]`: one `zIndex` made PixiJS re-sort the actor's children, and every unselected villager lost its body. - **Tests that restated constants.** `ENCODING_VERSION === 34` broke main after merges (their issues #104/#105). - **A list that had to stay sorted** (`ENCODING_FEATURES`) said so only in a comment, and an entry landed out of order. ## Solution Four rules, each wired end to end: the rule list and levels, the oxlint plugin (`index.mjs`, `recommended.json`), a rule page with checked ✅/❌ examples, the rule tables in README and docs, the sidebar, the guides and CHANGELOG. - **`duplicate-code-value`** (error). It reports: - two members of a code table with one value; - a literal union naming a member twice (`1 | 2 | 2`); - two `case` labels of one `switch` with one value (`case 1:` after `case Op.Hello:`). Tables and labels are read with the existing `typeof` evaluator (`json.rs`), in the whole-program pass. So `as const`, `Object.freeze`, spreads of `codesOf([...])` and imported tables all work. A table is a code table when one of these holds: - the file takes the union of its values (`(typeof X)[keyof typeof X]`, `ValueOf<typeof X>`); - it is marked `/** @unique */`; - its values are whole numbers under capitalised names, either close together or flags from 1. Limits and sizes (`{ title: 80, body: 80 }`) don't count. A deliberate alias is exempt when a comment on the member says `alias` or `@deprecated`, except in a `@unique` table. - **`list-invariant`** (error) checks `/** @sorted */` and `/** @unique */` on a module-level `[...] as const` or `Object.freeze([...])` of strings or numbers. A type annotation is allowed, as in the adopter's `readonly string[]`. - It reports the first element out of order and the place it belongs ("between 'library' and 'pub'"), and the second of two equal elements. - It also reports a tag it can't verify, such as on a mutable array. - `@unique` on a code table goes to `duplicate-code-value` instead. - Tags are found with `inline::tagged`, the same as `@json`, `@inline` and `@cache`. - **`pinned-constant-assert`** (warn) reports, in test files, an equality assertion between a literal and a constant whose value is a literal. The constant can be imported, a member of an `as const` table, or a `const` of the test file. Assertions covered: - `expect(X).toBe/toEqual/toStrictEqual`, also `expect.soft`, a global `expect`, and chai's `.to.equal`; - `assert.equal/strictEqual/deepEqual/deepStrictEqual`, also imported by name; - `assert(X === lit)` and `assert.ok(X === lit)`. An assertion with a message is exempt. The adopter's own repo showed why: `assert.equal(Op.Hello, 0x01, 'HELLO is frozen')` is a deliberate wire-compat pin. - **`no-positional-children`** (idiomatic, warn) reports a literal index into `.children` or `.childNodes`, `.item(n)` on them, and `getChildAt(n)`. Index 0 behind a `children.length === 1` test of the same list is left alone (the only child). It is idiomatic because without types `children` can be a data tree's field, and `el.children[0]` is common in DOM code. `json.rs` gains four small `pub(crate)` helpers on top of the existing reader: `value_expr`, `literal_value`, `const_members` and `number_of`. The reader itself is unchanged, so the lane extending it for computed keys and references (`{ [K.A]: … }`) should give `duplicate-code-value` those tables for free. ## Testing - `cargo nextest run --workspace`: 280/280 pass. That includes the new fixtures `code_tables`, `pinned_constants`, `positional_children` and `sorted_lists` (reduced, with positives and negatives, and checked through both `check_entry` and `check_file`), plus the docs and skill examples. - `node scripts/docs-typecheck.mjs`: 621 documentation examples compile. - `npm run test:std`: 367/367 pass. - `npm run build -w docs`: the site builds. - `npm run check`: the demo's error count is unchanged, and no new rule fires there. - `cargo clippy -p safe_ts_core`: no warnings in the new code. - Against the adopter's repo, read-only, through `git archive` into /tmp: - `no-positional-children` fires 7 times on `fb9e22a5^` (`actor.children[0]`, `[2]`, `[3]`) and not on `fb9e22a5`. - With `/** @sorted @unique */` added to a copy, `list-invariant` reports `'marchTogether'` on `cb282799^` ("between 'library' and 'pub'") and nothing on `cb282799`. - On their HEAD (699 files): `duplicate-code-value` reports nothing. `pinned-constant-assert` reports 15 restated constants, all in tests: `GENERATOR_VERSION`, `MIN_ENCODING_VERSION`, `MIN_PART_SIZE`, …; their `'HELLO is frozen'` pins are exempt. `no-positional-children` reports 14 with `--idiomatic`, mostly tests and SVG-node reads. - `duplicate-code-value` on `42622b07^`: no revision in their history ever kept a duplicate code (a script over every table in about 3000 commits found none). The collisions were fixed by hand during merges, so the firing side rests on the reduced fixtures. ## Left out - `exhaustive-dispatch` and `exhaustive-switch` for literal unions and code sets belong to another lane; `negative-member-guard`, `code-range-compare` and `inline-member-set` to a later one. - oxlint's own `no-duplicate-case` also reports `case` labels written the same way, so with the oxlint plugin those report twice. `duplicate-code-value` compares values (`case 1:` vs `case Op.Hello:`), and `safe-ts check` doesn't run oxlint's rules. - `@sorted` is ascending only; there is no descending form. - A table typed only in another file (the union type declared elsewhere) counts only through the number heuristic or `@unique`. Created with Claude Opus 5.5 in Claude Code.
- duplicate-code-value (error): two members of a code table with one value (as const,
  Object.freeze, spreads of codesOf, read across files as typeof reads them), a literal
  union naming a member twice, two case labels of a switch with one value. Aliases said so
  in a comment are exempt; /** @unique */ marks a table explicitly.
- list-invariant (error): /** @sorted */ and /** @unique */ on a module-level frozen list of
  strings or numbers, checked; the first element out of order is reported with its place.
- pinned-constant-assert (warn): a test asserting that a constant has its declared literal
  value. An assertion with a message (a pin on purpose) is exempt.
- no-positional-children (idiomatic): children[2], childNodes[0], getChildAt(1); the only
  child behind a length === 1 test is left alone.

Rule pages, rule tables, sidebar, the enums, testing and JSON guides, CHANGELOG.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee force-pushed tables-tests-invariants from 67f3ce9904
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m59s
to 0342ecef17
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m32s
2026-11-20 16:34:14 +01:00
Compare
sionzee deleted branch tables-tests-invariants 2026-11-20 16:34:55 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!95
No description provided.