Code tables, tests and lists: duplicate-code-value, pinned-constant-assert, @sorted/@unique (list-invariant), no-positional-children #95
Loading…
Reference in a new issue
No description provided.
Delete branch "tables-tests-invariants"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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:
0x3C/0x3D), and the merge kept both lines.actor.children[2]: onezIndexmade PixiJS re-sort the actor's children, and every unselected villager lost its body.ENCODING_VERSION === 34broke main after merges (their issues #104/#105).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:1 | 2 | 2);caselabels of oneswitchwith one value (case 1:aftercase Op.Hello:).Tables and labels are read with the existing
typeofevaluator (json.rs), in the whole-program pass. Soas const,Object.freeze, spreads ofcodesOf([...])and imported tables all work. A table is a code table when one of these holds:(typeof X)[keyof typeof X],ValueOf<typeof X>);/** @unique */;Limits and sizes (
{ title: 80, body: 80 }) don't count. A deliberate alias is exempt when a comment on the member saysaliasor@deprecated, except in a@uniquetable.list-invariant(error) checks/** @sorted */and/** @unique */on a module-level[...] as constorObject.freeze([...])of strings or numbers. A type annotation is allowed, as in the adopter'sreadonly string[].@uniqueon a code table goes toduplicate-code-valueinstead.inline::tagged, the same as@json,@inlineand@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 anas consttable, or aconstof the test file. Assertions covered:expect(X).toBe/toEqual/toStrictEqual, alsoexpect.soft, a globalexpect, and chai's.to.equal;assert.equal/strictEqual/deepEqual/deepStrictEqual, also imported by name;assert(X === lit)andassert.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.childrenor.childNodes,.item(n)on them, andgetChildAt(n). Index 0 behind achildren.length === 1test of the same list is left alone (the only child). It is idiomatic because without typeschildrencan be a data tree's field, andel.children[0]is common in DOM code.json.rsgains four smallpub(crate)helpers on top of the existing reader:value_expr,literal_value,const_membersandnumber_of. The reader itself is unchanged, so the lane extending it for computed keys and references ({ [K.A]: … }) should giveduplicate-code-valuethose tables for free.Testing
cargo nextest run --workspace: 280/280 pass. That includes the new fixturescode_tables,pinned_constants,positional_childrenandsorted_lists(reduced, with positives and negatives, and checked through bothcheck_entryandcheck_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.git archiveinto /tmp:no-positional-childrenfires 7 times onfb9e22a5^(actor.children[0],[2],[3]) and not onfb9e22a5./** @sorted @unique */added to a copy,list-invariantreports'marchTogether'oncb282799^("between 'library' and 'pub'") and nothing oncb282799.duplicate-code-valuereports nothing.pinned-constant-assertreports 15 restated constants, all in tests:GENERATOR_VERSION,MIN_ENCODING_VERSION,MIN_PART_SIZE, …; their'HELLO is frozen'pins are exempt.no-positional-childrenreports 14 with--idiomatic, mostly tests and SVG-node reads.duplicate-code-valueon42622b07^: 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-dispatchandexhaustive-switchfor literal unions and code sets belong to another lane;negative-member-guard,code-range-compareandinline-member-setto a later one.no-duplicate-casealso reportscaselabels written the same way, so with the oxlint plugin those report twice.duplicate-code-valuecompares values (case 1:vscase Op.Hello:), andsafe-ts checkdoesn't run oxlint's rules.@sortedis ascending only; there is no descending form.@unique.Created with Claude Opus 5.5 in Claude Code.
67f3ce99040342ecef17