refactor(engine): link.js is TypeScript, the second converted engine module #2494
No reviewers
Labels
No labels
blocked-on-build
bug
device
device-request
enhancement
from-github
needs-datasheet
needs-info
needs-triage
ready-for-agent
ready-for-human
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
jaylouisw/packetforge!2494
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "migrate/link"
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?
What
Migration row two:
engine/link.jsbecomesengine/link.ts(139 → 243 lines). Every parameter,every return and every exported value is typed; the class's fields are declared where they used to
be assembled by assignment; the shapes on the boundary are written as interfaces because
engine/topology.jsandengine/simulator.jsare still JavaScript and there is no type to importfrom them.
LinkEndpoint— one end of a cable, asserialize()writes itLinkOptions— the sixth constructor argument, a shape filled in from a saved documentLabelProblem— the{ code, message }a refused label is answered withSerializedLink— the stored form, withlabel?/stateSinceMs?because both are written onlywhen they mean something
No
any, noas, no@ts-nocheck. The two values that are genuinely unknown at this boundary areunknownand narrowed in place — that is what the guards were always for:label, narrowed bylabelProblem()— the rule that makes a cable label a field that is saved orrefused, never accepted and dropped (#1513)
stateSinceMs, narrowed byNumber()+isFinite()— a timestamp that is not a number is still0 rather than NaN (#1323)
The one place a thrown
Errorcarries acodeis declared asError & { code?: string }on theconst, so a call site that catches it keeps reading.codeand nothing is cast.Importers
CommonJS does not resolve a
.tsfrom an extensionless relative require (trap 5, established in rowone), so both call sites spell the extension:
engine/index.jsrequire('./link')require('./link.ts')engine/topology.jsrequire('./link')require('./link.ts')grep -rn "require('./link')\|require('../engine/link')"now returns nothing.No converted module is left holding an extensionless require into JavaScript.
engine/link.tsrequires nothing at all, so there is no line in it that breaks the day a sibling converts.
Gates
npx tsc --noEmit--listFileslistsengine/link.ts(engine/**/*.tsalready covered it, sotsconfig.jsonis untouched)node scripts/check-no-any.tsnode scripts/syntax-check.jsnode test/canonical-urls.test.jsgit statusempty afterwardsSuites
Identical counts before and after, so the behaviour did not move:
Ledger row
docs/TYPESCRIPT.md, the Migrated so far table:Line count is
wc -lon the converted file, so it includes the annotations — the same convention asrow one (130 → 236).
Known, and left alone on purpose
Some prose still names
engine/link.js:NN—docs/SIMULATION.md,docs/UPSTREAM-DESIGN.md,docs/plans/*. Those citations now point at a path that does not exist, andscripts/check-doc-citations.jsskips a citation it cannot resolve rather than failing on it (it is a diagnostic, not a gate, and
still reports "no judged citation is more than 40 lines from the code it names"). Re-pointing them
means re-deriving line numbers in three documents, which does not belong in a diff whose point is
"the behaviour did not move" — it wants its own commit.
`engine/link.js` becomes `engine/link.ts` (139 -> 243 lines): every parameter, every return and every exported value is typed, the class's fields are declared where they used to be assembled by assignment, and the shapes on the boundary are written as interfaces - `LinkEndpoint`, `LinkOptions`, `LabelProblem`, `SerializedLink` - because `engine/topology.js` and `engine/simulator.js` are still JavaScript and there is nothing to import a type from. The two values that are genuinely unknown are now declared `unknown` and narrowed in place, which is what those guards were always for: - `label` - narrowed by `labelProblem()`, the rule that made a cable label a field that is saved or refused rather than accepted and dropped (#1513); - `stateSinceMs` - narrowed by `Number()` + `isFinite()`, so a timestamp that is not a number is still 0 rather than NaN (#1323). No `any`, no `as`, no `@ts-nocheck`: `node scripts/check-no-any.ts` scans the file and passes, and `tsc --noEmit --listFiles` lists `engine/link.ts` in the program (`engine/**/*.ts` already covers it, so `tsconfig.json` is untouched by this conversion). Both call sites spell the extension, because CommonJS does not resolve a `.ts` from an extensionless relative require (trap 5 of docs/TYPESCRIPT.md, established in row one): - `engine/index.js` `require('./link.ts')` - `engine/topology.js` `require('./link.ts')` `engine/link.ts` itself requires nothing, so no converted module is left holding an extensionless require into JavaScript - there is no line here that breaks the day a sibling converts. Behaviour is unchanged. The link suites pass before and after, with identical counts: link-state 69, link-parts 85, bidi-media 73, link-instant 37, link-roundtrip 36, topology-load-links 27, link-options 25, cable-length 22, cable-delete 17, link-state-drawing 13; canonical-urls 88 and `git status` empty after the run.