refactor(engine): link.js is TypeScript, the second converted engine module #2494

Merged
jaylouisw merged 2 commits from migrate/link into main 2026-11-24 00:16:05 +00:00
Owner

What

Migration row two: 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; the shapes on the boundary are written as interfaces because
engine/topology.js and engine/simulator.js are still JavaScript and there is no type to import
from them.

  • LinkEndpoint — one end of a cable, as serialize() writes it
  • LinkOptions — the sixth constructor argument, a shape filled in from a saved document
  • LabelProblem — the { code, message } a refused label is answered with
  • SerializedLink — the stored form, with label?/stateSinceMs? because both are written only
    when they mean something

No any, no as, no @ts-nocheck. The two values that are genuinely unknown at this boundary are
unknown and narrowed in place — that is what the guards were always for:

  • label, narrowed by labelProblem() — the rule that makes a cable label a field that is saved or
    refused, never accepted and dropped (#1513)
  • stateSinceMs, narrowed by Number() + isFinite() — a timestamp that is not a number is still
    0 rather than NaN (#1323)

The one place a thrown Error carries a code is declared as Error & { code?: string } on the
const, so a call site that catches it keeps reading .code and nothing is cast.

Importers

CommonJS does not resolve a .ts from an extensionless relative require (trap 5, established in row
one), so both call sites spell the extension:

File Was Now
engine/index.js require('./link') require('./link.ts')
engine/topology.js require('./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.ts
requires nothing at all, so there is no line in it that breaks the day a sibling converts.

Gates

Gate Result
npx tsc --noEmit clean; --listFiles lists engine/link.ts (engine/**/*.ts already covered it, so tsconfig.json is untouched)
node scripts/check-no-any.ts 0 occurrences (6 TypeScript files scanned)
node scripts/syntax-check.js 553 files (547 JavaScript, 6 TypeScript): all OK
node test/canonical-urls.test.js 88 passed, 0 failed — and git status empty afterwards

Suites

Identical counts before and after, so the behaviour did not move:

Suite Passed
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

Ledger row

docs/TYPESCRIPT.md, the Migrated so far table:

| `engine/link.ts` | 243 | `628fc3f5` (#2494) |

Line count is wc -l on the converted file, so it includes the annotations — the same convention as
row 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, and scripts/check-doc-citations.js
skips 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.

## What Migration row two: `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; the shapes on the boundary are written as interfaces because `engine/topology.js` and `engine/simulator.js` are still JavaScript and there is no type to import from them. - `LinkEndpoint` — one end of a cable, as `serialize()` writes it - `LinkOptions` — the sixth constructor argument, a shape filled in from a saved document - `LabelProblem` — the `{ code, message }` a refused label is answered with - `SerializedLink` — the stored form, with `label?`/`stateSinceMs?` because both are written only when they mean something No `any`, no `as`, no `@ts-nocheck`. The two values that are genuinely unknown at this boundary are `unknown` and narrowed in place — that is what the guards were always for: - `label`, narrowed by `labelProblem()` — the rule that makes a cable label a field that is saved or refused, never accepted and dropped (#1513) - `stateSinceMs`, narrowed by `Number()` + `isFinite()` — a timestamp that is not a number is still 0 rather than NaN (#1323) The one place a thrown `Error` carries a `code` is declared as `Error & { code?: string }` on the `const`, so a call site that catches it keeps reading `.code` and nothing is cast. ## Importers CommonJS does not resolve a `.ts` from an extensionless relative require (trap 5, established in row one), so both call sites spell the extension: | File | Was | Now | |---|---|---| | `engine/index.js` | `require('./link')` | `require('./link.ts')` | | `engine/topology.js` | `require('./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.ts` requires nothing at all, so there is no line in it that breaks the day a sibling converts. ## Gates | Gate | Result | |---|---| | `npx tsc --noEmit` | clean; `--listFiles` lists `engine/link.ts` (`engine/**/*.ts` already covered it, so `tsconfig.json` is untouched) | | `node scripts/check-no-any.ts` | 0 occurrences (6 TypeScript files scanned) | | `node scripts/syntax-check.js` | 553 files (547 JavaScript, 6 TypeScript): all OK | | `node test/canonical-urls.test.js` | 88 passed, 0 failed — and `git status` empty afterwards | ## Suites Identical counts before and after, so the behaviour did not move: | Suite | Passed | |---|---| | 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 | ## Ledger row `docs/TYPESCRIPT.md`, the *Migrated so far* table: ``` | `engine/link.ts` | 243 | `628fc3f5` (#2494) | ``` Line count is `wc -l` on the converted file, so it includes the annotations — the same convention as row 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, and `scripts/check-doc-citations.js` skips 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.
refactor(engine): link.js is TypeScript, the second converted engine module
All checks were successful
CI / Typecheck (pull_request) Successful in 20s
CI / Server smoke and Docker build (pull_request) Successful in 46s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 8s
CI / Test (Node 22) (pull_request) Successful in 52s
CI / Test (Node 24) (pull_request) Successful in 55s
CI / Arm auto-merge (pull_request) Successful in 3s
628fc3f55f
`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.
forgejo-actions scheduled this pull request to auto merge when all checks succeed 2026-11-24 00:03:35 +00:00
docs(typescript): link.ts joins the migrated ledger
All checks were successful
CI / Typecheck (pull_request) Successful in 25s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 12s
CI / Server smoke and Docker build (pull_request) Successful in 50s
CI / Test (Node 22) (pull_request) Successful in 56s
CI / Test (Node 24) (pull_request) Successful in 1m2s
CI / Arm auto-merge (pull_request) Successful in 6s
8c9e4a2370
Row two of the `Migrated so far` table: `engine/link.ts`, 243 lines (`wc -l`, so the
annotations are counted, the same convention as row one), converted in 628fc3f5 as #2494.

The paragraph under the table says what the 104 net lines are, in the voice row one used:
`LinkOptions`, `SerializedLink`, and the two `unknown`s a saved document can put anything
into, both narrowed by guards that predate the conversion.
jaylouisw deleted branch migrate/link 2026-11-24 00:16:07 +00:00
Sign in to join this conversation.
No description provided.