@evobug_com/safe-ts-skill 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-APACHE +201 -0
- package/LICENSE-MIT +21 -0
- package/README.md +15 -2
- package/install.mjs +62 -0
- package/package.json +35 -4
- package/safe-ts-migrate/SKILL.md +111 -0
- package/safe-ts-migrate/references/architecture.md +139 -0
- package/safe-ts-migrate/references/behaviour.md +86 -0
- package/safe-ts-migrate/references/census.md +114 -0
- package/safe-ts-migrate/references/escape-hatches.md +62 -0
- package/safe-ts-migrate/references/final-pass.md +44 -0
- package/safe-ts-migrate/references/order-and-gates.md +96 -0
- package/safe-ts-migrate/references/patterns-core.md +1074 -0
- package/safe-ts-migrate/references/patterns-react.md +230 -0
- package/safe-ts-migrate/references/patterns-vue.md +636 -0
- package/safe-ts-migrate/references/review-checklist.md +63 -0
- package/safe-ts-migrate/references/templates.md +235 -0
- package/safe-ts-migrate/references/tool-gaps.md +124 -0
- package/safe-ts-migrate/scripts/census.mjs +671 -0
- package/safe-ts-migrate/scripts/safe-ts.mjs +79 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Behaviour preservation
|
|
2
|
+
|
|
3
|
+
The type checker, the linter and `safe-ts check` can all pass while the app behaves differently:
|
|
4
|
+
a request body loses a key, a failure that was shown is now dropped, a dialog closes on an
|
|
5
|
+
error. The checks below find what they don't. Plan them in the setup step, record the "before"
|
|
6
|
+
then, and run them for every area.
|
|
7
|
+
|
|
8
|
+
## Contract checks: what goes on the wire
|
|
9
|
+
|
|
10
|
+
Moving from `null` to `undefined` changes what `JSON.stringify` writes: a field that was `null`
|
|
11
|
+
is now missing from the body. A server that tells the two apart (a missing key leaves a field
|
|
12
|
+
alone, `null` clears it) now ignores a clear.
|
|
13
|
+
|
|
14
|
+
- From the architecture document's list of fields that clear with `null`, write one test per
|
|
15
|
+
request builder: build the body for "clear this field" and assert the serialized text has
|
|
16
|
+
`"field":null`.
|
|
17
|
+
- Search for code that tests a hydrated value by value (`record[field] === undefined`,
|
|
18
|
+
`x === null`): after the conversion, `undefined` may mean "not loaded" where `null` meant
|
|
19
|
+
"none".
|
|
20
|
+
- Removing `?? undefined` bridges after a type change is safe where the type is precise, and not
|
|
21
|
+
where it is `any` (an editor library's attributes, a value read inside `unsafe`, `$el`):
|
|
22
|
+
`tsc` reports nothing there. Read each one.
|
|
23
|
+
|
|
24
|
+
## Network bodies, before and after
|
|
25
|
+
|
|
26
|
+
Record the requests the app sends for each write flow (method, URL, body), on the baseline build
|
|
27
|
+
and on the branch, against the same data, and compare them. A Playwright script that clicks
|
|
28
|
+
through the flows and logs `request.method()`, `request.url()` and `request.postData()` is
|
|
29
|
+
enough; so is a HAR export from the browser for a short list of flows. Any difference is either
|
|
30
|
+
intended (and written in the hand-off report) or a regression.
|
|
31
|
+
|
|
32
|
+
## Browser smoke
|
|
33
|
+
|
|
34
|
+
A list of every page and every write flow, run on the branch:
|
|
35
|
+
|
|
36
|
+
- each page loads, with no console errors and no framework warnings (Vue warns in development);
|
|
37
|
+
- each write flow sends what it sent before (above) and shows its result;
|
|
38
|
+
- each failure path still shows what it showed: block a request or make it answer 422 or 500
|
|
39
|
+
(route interception in Playwright), and check that inline messages are still inline;
|
|
40
|
+
- an enum value the build doesn't know, by rewriting one response: the page shows a fallback,
|
|
41
|
+
it doesn't crash;
|
|
42
|
+
- a slow response followed by a fast one (an id changed quickly): the newer answer stays.
|
|
43
|
+
|
|
44
|
+
Running the baseline build and the branch side by side against the same database, and comparing
|
|
45
|
+
the rendered text of each page (dates, numbers, lists), catches what no rule sees.
|
|
46
|
+
|
|
47
|
+
## Regressions a real migration produced
|
|
48
|
+
|
|
49
|
+
All of these passed every static gate:
|
|
50
|
+
|
|
51
|
+
- **A dialog closed and lost the typed input** when its submit handler started returning an `Err`
|
|
52
|
+
instead of throwing: the dialog closed on any return and stayed open only on a throw. Change
|
|
53
|
+
the caller's contract with the handler's.
|
|
54
|
+
- **A failure was dropped silently** because a store member started returning a `Result` and a
|
|
55
|
+
caller in another area kept its `try`/`catch`. The checker now reports a dropped store
|
|
56
|
+
`Result` (`must-use-result`) and, with the idiomatic layer, the dead `try` (`catch-on-result`);
|
|
57
|
+
it still doesn't follow a store passed as a parameter or kept in a `ref`. Search every caller
|
|
58
|
+
of every changed member.
|
|
59
|
+
- **A toast replaced an inline message**: three dialogs showed the server's 422 sentence inline by
|
|
60
|
+
testing the HTTP library's error class; when the store began to reject with its own error
|
|
61
|
+
type, the test stopped matching and a generic toast appeared. Match on the new error kinds.
|
|
62
|
+
- **A reload blanked a list**: a list rendered only in the `Ready` slot disappeared during every
|
|
63
|
+
refresh (`run()` sets `Loading`). Keep the last answer on screen
|
|
64
|
+
([patterns-vue.md](patterns-vue.md#a-refresh-that-blanks-the-list)).
|
|
65
|
+
- **A cross-module refresh stopped**: a page refreshed through another store's `$onAction` hook;
|
|
66
|
+
once the call moved to a plain request function, the hook never fired. Use an explicit event
|
|
67
|
+
(a counter in a small store).
|
|
68
|
+
- **A load started in setup read element refs before they existed**: moving a `load` from
|
|
69
|
+
`onMounted` to setup made it read a `useElement` before mount, the task went back to `Idle`,
|
|
70
|
+
and the page stayed on its loading mark.
|
|
71
|
+
- **An autofix changed a number another language reads**: `unicorn/numeric-separators-style`
|
|
72
|
+
wrote `47_474` in a constant that a server-side regex parsed as `47`. The preset no longer
|
|
73
|
+
turns that rule on; if the project's own config does, check where a constant is read before
|
|
74
|
+
accepting its fix.
|
|
75
|
+
- **A startup check validated data another module decorates at import time**, and a legacy file
|
|
76
|
+
used a prelude name without an import (`ReferenceError` at runtime, in a setup that kept legacy
|
|
77
|
+
files away from the transform): both passed `tsc`, the checker and the Node tests, and only
|
|
78
|
+
the browser showed them. Don't `exclude` legacy files from the plugin.
|
|
79
|
+
- **An unknown server value crashed a page**: a table indexed by a state the build didn't know
|
|
80
|
+
gave `undefined`, and a property read on it threw. Look up wire keys with
|
|
81
|
+
`getOwn(table, key, fallback)` ([patterns-core.md](patterns-core.md#a-table-keyed-by-server-data)).
|
|
82
|
+
- **An `Open<K>` sent back as the object**: `JSON.stringify` writes `{"tag":…,"value":…}`
|
|
83
|
+
where the server expects the string. Send `.value`; the request-body comparison catches it.
|
|
84
|
+
- **Base64 of non-ASCII text changes with `toBase64`**: it encodes UTF-8 where `btoa` wrote
|
|
85
|
+
Latin-1 bytes. Compare the encoded values a flow sends before and after
|
|
86
|
+
([patterns-core.md](patterns-core.md#base64)).
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# The census
|
|
2
|
+
|
|
3
|
+
`scripts/census.mjs` (in this skill) counts two things per rule, per pattern and per directory:
|
|
4
|
+
|
|
5
|
+
1. what `safe-ts check --json --idiomatic` reports, errors and warnings, with the idiomatic
|
|
6
|
+
rules on whether or not package.json turns them on yet: several of them (`prefer-is-over-tag`,
|
|
7
|
+
`prefer-q`, `partial-type-guard`, `key-presence-union`, `no-swallowed-error`,
|
|
8
|
+
`json-null-in-app`, `cast-in-test`, ...) report the hand-rolled forms a checker-driven
|
|
9
|
+
migration leaves behind;
|
|
10
|
+
2. the hand-rolled patterns no rule reports, found by regular expressions over the source
|
|
11
|
+
text.
|
|
12
|
+
|
|
13
|
+
It also lists the escape hatches and how often the code already uses each SAFE-TS feature.
|
|
14
|
+
|
|
15
|
+
## Running it
|
|
16
|
+
|
|
17
|
+
From the project root (the folder with `package.json` and `tsconfig.json`):
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
node .claude/skills/safe-ts-migrate/scripts/census.mjs # the report
|
|
21
|
+
node .claude/skills/safe-ts-migrate/scripts/census.mjs src app # only these folders
|
|
22
|
+
node .claude/skills/safe-ts-migrate/scripts/census.mjs --depth 3 # finer directory grouping
|
|
23
|
+
node .claude/skills/safe-ts-migrate/scripts/census.mjs --show trio-files # every place of one pattern
|
|
24
|
+
node .claude/skills/safe-ts-migrate/scripts/census.mjs --show no-unchecked-cast # every finding of one rule
|
|
25
|
+
node .claude/skills/safe-ts-migrate/scripts/census.mjs --json > census.json
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
- Files: `.ts`, `.tsx`, `.mts`, `.cts`, `.vue`, `.jsx`, from `git ls-files` (so `.gitignore`
|
|
29
|
+
applies), without `.d.ts` files. Outside git, `node_modules`, `dist`, `build` and the like
|
|
30
|
+
are skipped.
|
|
31
|
+
- The checker: the `safe-ts` binary from the project's `node_modules` (`SAFE_TS_BIN=<path>`
|
|
32
|
+
overrides it; `--no-check` skips it, `--no-idiomatic` leaves the idiomatic rules out). Every
|
|
33
|
+
scanned file is passed as an entry, so tests, scripts and files nothing imports are counted
|
|
34
|
+
like the patterns are (`check` follows imports, tsconfig aliases included, from its entries).
|
|
35
|
+
`--entry src/main.ts` (repeatable) checks from given entries instead.
|
|
36
|
+
- Files marked `// @safe-ts legacy` report nothing. Run the first census **before** marking
|
|
37
|
+
anything, so the counts cover the whole code base.
|
|
38
|
+
|
|
39
|
+
## Reading it
|
|
40
|
+
|
|
41
|
+
- **Checker findings by rule**, errors and warnings (`(warn)`), with the directories that have
|
|
42
|
+
the most and, for the rules a migration meets most, the catalogue entry to read. Start with
|
|
43
|
+
the **root causes**: one origin (a `throw` in a helper, a `JSON.parse`) behind many
|
|
44
|
+
`unhandled-failure` findings. Fixing it there clears them all. `safe-ts check src/main.ts
|
|
45
|
+
--summary` gives the same table by rule and directory without the patterns.
|
|
46
|
+
- **Patterns** are a map of where to look, not findings. Each line names the catalogue entry with
|
|
47
|
+
the target form. Read every place before changing it: `=== ''` is often a real empty-input
|
|
48
|
+
check, and `-1` is sometimes a real number.
|
|
49
|
+
- **Escape hatches** are the budget's starting point ([escape-hatches.md](escape-hatches.md)).
|
|
50
|
+
- **Features in use**: a migration that ends with `useTask`, `AsyncState`, `Enum`, `match` or
|
|
51
|
+
`<Match>` near zero has converted the code, not adopted the architecture
|
|
52
|
+
([final-pass.md](final-pass.md)).
|
|
53
|
+
- **Per directory**: findings and the most frequent patterns. This is the input for the areas
|
|
54
|
+
and their order ([order-and-gates.md](order-and-gates.md)).
|
|
55
|
+
|
|
56
|
+
## The patterns
|
|
57
|
+
|
|
58
|
+
| Pattern | Finds | Target |
|
|
59
|
+
|---|---|---|
|
|
60
|
+
| `loading-flag` | `const loading = ref(false)`, `useState(false)`, `let busy = false` (names like loading, busy, pending, saving, submitting) | `useTask` |
|
|
61
|
+
| `error-state` | an error kept in its own state: `ref('')`, `ref(null)`, `ref<E>()`, `useState('')` named error, failed, problem | `useTask`'s `Failed`, or a `Result` |
|
|
62
|
+
| `trio-files` | files with a loading flag and an error state | `useTask` + `<Match>` |
|
|
63
|
+
| `empty-string-sentinel` | `=== ''`, `!== ''`, `return ''` | `Option<string>`, `Result<void, E>` |
|
|
64
|
+
| `minus-one-sentinel` | `=== -1`, `return -1`, `= -1` | `Option<T>`, `indexIn` |
|
|
65
|
+
| `zero-sentinel` | an id or index that starts at `0` | `Option<T>` |
|
|
66
|
+
| `string-if-chain` | one value compared with 3 or more string literals in a file | `Enum` + `match` |
|
|
67
|
+
| `boolean-state`, `boolean-flag-files` | boolean refs/states; files with 3 or more | one `Enum` |
|
|
68
|
+
| `raw-fetch` | `fetch(`, `axios(`/`axios.get(`, `new XMLHttpRequest` | a request function returning a `Result` |
|
|
69
|
+
| `try-catch`, `promise-catch` | `try {`, `.catch(` | a `Result`; `unsafe` for code that throws |
|
|
70
|
+
| `as-cast`, `double-cast` | `x as T` (not `as const`), `as unknown as`, `as never`, `as any` | `validate<T>`, an annotation, `satisfies` |
|
|
71
|
+
| `json-parse`, `raw-storage` | `JSON.parse(`, `localStorage.getItem(` and friends | `parseJson<T>`, the browser pack's `getItem` / `setItem` |
|
|
72
|
+
| `unsafe`, `unsafe-async`, `unexplained-unsafe`, `panic`, `void-call` | escape hatches; `unsafe(` without a comment on its line or the line above | [escape-hatches.md](escape-hatches.md) |
|
|
73
|
+
| `trust`, `legacy-null`, `safe-ts-ignore` | `trust<T>(...)`, `LegacyNull`, `// safe-ts-ignore` (and `-file`, `<!-- -->`) comments | [escape-hatches.md](escape-hatches.md) |
|
|
74
|
+
| `module-let` | a `let` at the top of a `.ts` module | a store, a context |
|
|
75
|
+
|
|
76
|
+
Comment lines are skipped (except for `safe-ts-ignore`); strings are not, so a pattern inside a
|
|
77
|
+
string literal counts.
|
|
78
|
+
|
|
79
|
+
The forms the idiomatic rules see are counted as their findings, not as patterns:
|
|
80
|
+
|
|
81
|
+
| Rule (idiomatic) | Finds | Target |
|
|
82
|
+
|---|---|---|
|
|
83
|
+
| `prefer-is-over-tag` | `x.tag === '...'`, `switch (x.tag)`, in code and templates | `is`, `variant`, `match`, `<Match>` |
|
|
84
|
+
| `prefer-q`, `prefer-map-err` | `if (isErr(x)) return x`, an error re-wrapped by hand | `q(x)`, `q(x, map)`, `mapErr` |
|
|
85
|
+
| `key-presence-union` | `'key' in x` telling a project union's members apart | a tagged `Enum` |
|
|
86
|
+
| `partial-type-guard` | a `value is T` guard that tests part of `T` | `validate<T>` |
|
|
87
|
+
| `no-swallowed-error` | `unwrapOr(x, undefined)`, `.catch(() => {})`, helpers around them | the `Result` itself |
|
|
88
|
+
| `json-null-in-app` | `JsonNull` as app state (`Ref<T \| JsonNull>`, `Option<T \| JsonNull>`) | `Option`, an `Enum`, `AsyncState<Option<T>, E>` |
|
|
89
|
+
| `catch-on-result` | `.catch` / `try` around calls that return a `Result` | the `Result` checked |
|
|
90
|
+
| `latest-wins-counter`, `unused-task-signal` | a hand-written "latest run wins" ticket; a task that ignores its `signal` | `useTask`, the `signal` passed on |
|
|
91
|
+
| `cast-in-test` | `as unknown as T`, `as never` in tests where `no-unchecked-cast` is off for them (otherwise that rule reports the cast) | `fixture`, `testDouble`, `unchecked` |
|
|
92
|
+
| `panic-on-loading` | `panic` under a loading flag | a state the page shows |
|
|
93
|
+
|
|
94
|
+
`--show <rule>` lists every finding of a rule, like `--show <pattern>` for a pattern.
|
|
95
|
+
|
|
96
|
+
## The census as a gate
|
|
97
|
+
|
|
98
|
+
A baseline records, for each gated pattern and each rule, the count in every file. The gate
|
|
99
|
+
fails when any file's count rises, and names the file:
|
|
100
|
+
|
|
101
|
+
```sh
|
|
102
|
+
node scripts/census.mjs --write-baseline safe-ts-census.json # once, and after a reviewed change
|
|
103
|
+
node scripts/census.mjs --baseline safe-ts-census.json # in CI: exit 1 if a count rose
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
This is the ratchet: the counts may only fall, so a new `loading` flag, `as` cast, `trust` or
|
|
107
|
+
`safe-ts-ignore` in any file fails the build, while the old ones are burned down area by area.
|
|
108
|
+
It holds rules too, warnings included, so a rule you aren't ready to make an error stays at
|
|
109
|
+
its level in package.json and can't grow: ratchet it rather than setting it `"off"`. A legacy
|
|
110
|
+
file reports nothing, so its findings count from the change that removes its marker: remove it
|
|
111
|
+
in the change that makes the file pass. Rewrite the baseline only in
|
|
112
|
+
a commit that says why (a decided exception, a moved file). Commit the baseline next to the
|
|
113
|
+
architecture document. For CI, copy `census.mjs` and `safe-ts.mjs` into the project's
|
|
114
|
+
`scripts/` folder (they import each other by relative path), or commit the skill folder.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Escape hatches: policy and budget
|
|
2
|
+
|
|
3
|
+
Every escape hatch is allowed somewhere, and each one is visible and counted. The policy says
|
|
4
|
+
where; the budget says how many; the hand-off report says how many are left. `safe-ts check`
|
|
5
|
+
counts most of them after each run:
|
|
6
|
+
|
|
7
|
+
```text
|
|
8
|
+
escape hatches: 2 unsafe() block(s), 0 panic() call(s), 1 trust() call(s), 3 LegacyNull use(s), 0 @cache mark(s), 1 safe-ts-ignore comment(s), 0 legacy file(s), 1 std file(s) (src/protocol/trusted.ts)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The census counts those and the rest (`unsafeAsync`, `void` calls, casts, `unsafe` without a
|
|
12
|
+
reason comment), per file, and its baseline keeps each count from rising. Treat a rising count
|
|
13
|
+
as a review question, the way Rust projects treat a new `unsafe` block.
|
|
14
|
+
|
|
15
|
+
## When each is allowed
|
|
16
|
+
|
|
17
|
+
| Escape hatch | Allowed for | Not for |
|
|
18
|
+
|---|---|---|
|
|
19
|
+
| `unsafe(() => ...)`, `unsafeAsync(...)` | a call into code that throws or rejects and has no safe version (std, a feature pack): a built-in, a library, legacy code | your own functions (make them return a `Result`); a call std or a pack has a safe version of (`toBase64`, the browser pack's `fetch`, `getItem`, IndexedDB, the Node pack's `parseArgs`) |
|
|
20
|
+
| `unsafeAsync(fn, Panic)` | a call that returns an `Err` and may also throw by mistake | turning bugs into user-visible failures |
|
|
21
|
+
| `panic(message)` | an invariant nothing can handle: a table built at load time with a duplicate name | data from a request, a store not loaded yet, a missing element |
|
|
22
|
+
| `defineSanitizer(fn)` | the one sanitizer the project vouches for, in one module, configured and reviewed | a function that doesn't sanitize; one per component |
|
|
23
|
+
| `trust<T>(value)` | a value no check fits that another part of the same program made (a worker of the same build), listed in the architecture document's boundaries | data from the network, storage, a file, a message from another origin: `validate<T>` |
|
|
24
|
+
| `LegacyNull` | strict code whose legacy callers still pass `null`, until they are migrated | a `null` that means something on the wire (`JsonNull`); the end state |
|
|
25
|
+
| `// safe-ts-ignore <rule>: <reason>` | one line in a migrated file that is right as it is, or that the rule gets wrong (a package's type that tells `null` from `undefined`) | a file that isn't migrated yet (the legacy marker); a finding the code should fix |
|
|
26
|
+
| `// safe-ts-ignore-file <rule>: <reason>` | a generated file the project can't change | hand-written code |
|
|
27
|
+
| `"off"` / `"warn"` in package.json `"rules"` / `"overrides"` | a directory whose code the rule doesn't fit (generated types), decided in the architecture document | silencing an area's findings to pass a gate: the census baseline ratchets them instead |
|
|
28
|
+
| `void f()` | a `Result` or promise whose failure really doesn't matter (best-effort telemetry) | anything the user started, or any write |
|
|
29
|
+
| `// @safe-ts legacy` | a file that waits its turn in the burn-down | the end state; a file that "doesn't fit" |
|
|
30
|
+
| `// @safe-ts std` | low-level helpers like the std library itself, reviewed line by line | application code |
|
|
31
|
+
| `/** @cache */` | a memo that can be thrown away and computed again | state |
|
|
32
|
+
| `safeTs({ exclude })` | vendored or generated code the build should leave alone entirely | the project's own code waiting its turn (it loses its prelude imports): the legacy marker |
|
|
33
|
+
| `as T`, `x!` | nowhere on outside data. `x[i]!` under a bound the checker accepts; `as const` | data from a request, storage, a message, the DOM; a brand (`as Html`, `as SafeUrl` are `brand-constructor` errors) |
|
|
34
|
+
| An overload over a loose implementation, a `value is T` guard | the patterns in "Typing without casts", with a body short enough to see that it does what its type says (`partial-type-guard` accepts a guard that tests every required field) | a way around `validate<T>` |
|
|
35
|
+
| A catch-all `catch` | a harness boundary that runs independent units of work and reports each failure | anywhere below it |
|
|
36
|
+
|
|
37
|
+
## The reason
|
|
38
|
+
|
|
39
|
+
Every `unsafe`, `unsafeAsync`, `panic`, `trust`, `LegacyNull`, `void`, `@cache` and cast carries
|
|
40
|
+
a comment on its line or the line above that says why it is safe here, in terms a reviewer can
|
|
41
|
+
check:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
// The pattern comes from the admin's saved search: a bad one is shown as "invalid filter".
|
|
45
|
+
const filter = unsafe(() => new RegExp(saved.pattern));
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
A `safe-ts-ignore` has its reason built in, after the colon, and the checker enforces it: the
|
|
49
|
+
`safe-ts-ignore` rule (an error in the preset) reports one without a rule or a reason, with a
|
|
50
|
+
rule that doesn't exist, or one that silences nothing (the finding was fixed or moved). In a
|
|
51
|
+
Vue template it is an HTML comment before the element:
|
|
52
|
+
`<!-- safe-ts-ignore vue-unsafe-url: the server checks every link it sends -->`.
|
|
53
|
+
|
|
54
|
+
The census's `unexplained-unsafe` pattern counts the `unsafe` calls without a comment; the
|
|
55
|
+
baseline gate keeps it from growing.
|
|
56
|
+
|
|
57
|
+
## The budget
|
|
58
|
+
|
|
59
|
+
The architecture document sets, per area: the legacy files allowed at each milestone (zero at the
|
|
60
|
+
end), the `LegacyNull` uses allowed (zero once the area's callers are migrated), and the number
|
|
61
|
+
of each other hatch the area may keep. The hand-off report of each area gives the counts before
|
|
62
|
+
and after, from the census, and lists each hatch that remains with its reason.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# The final pass: use every feature
|
|
2
|
+
|
|
3
|
+
When the gates pass and the markers are gone, run the census again and read its last two
|
|
4
|
+
sections: the patterns that remain, and how often each SAFE-TS feature is used. A migration that
|
|
5
|
+
aimed at "the checker passes" ends with converted code: no `null`, no `throw`, `Result`s at the
|
|
6
|
+
boundaries, and the old state machines still hand-written around them. This pass looks for the
|
|
7
|
+
places where a feature would replace hand-rolled code, area by area.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
node scripts/census.mjs --show trio-files
|
|
11
|
+
node scripts/census.mjs --show loading-flag
|
|
12
|
+
node scripts/census.mjs --show string-if-chain
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
| Look for | Census (pattern or rule) | Replace with |
|
|
16
|
+
|---|---|---|
|
|
17
|
+
| loading / error / data state, a lone busy flag, an error string | `trio-files`, `loading-flag`, `error-state`, `vue-split-state` | `useTask` + `<Match :value>` (or `match` in a `computed`) |
|
|
18
|
+
| hand-written `AsyncState` assignments, latest-wins counters, tasks that ignore their signal | `latest-wins-counter`, `unused-task-signal`, a search for `AsyncState.` outside `useTask` | `useTask`, in the component or the setup store |
|
|
19
|
+
| an answer or error the user dismisses by hand | a search for `dismissed`, `clearError` | `useTask`'s `reset()` |
|
|
20
|
+
| a blank `Idle` after a bug | `<Match>` without `#Idle` / `#_` | a slot that reads the task's `panic` |
|
|
21
|
+
| string unions in if-chains, boolean sets, unions told apart by keys | `string-if-chain`, `boolean-flag-files`, `key-presence-union` | `Enum` + `constructors` + `match` |
|
|
22
|
+
| raw tag reads | `prefer-is-over-tag` | `is`, `variant`, `match`, `<Match>` |
|
|
23
|
+
| `if (isErr(x)) return x`, hand re-wraps of an error | `prefer-q`, `prefer-map-err` | `q(x)`, `q(x, map)`, `mapErr` |
|
|
24
|
+
| `.catch` or `try` around calls that return a `Result` | `catch-on-result`, `promise-catch`, `try-catch` | the `Result` checked |
|
|
25
|
+
| `''`, `-1`, `0` sentinels | `empty-string-sentinel`, `minus-one-sentinel`, `zero-sentinel`, `sentinel-error` | `Option<T>`, `indexIn`, an `Enum` variant |
|
|
26
|
+
| `unwrapOr(x, undefined)` and helpers around it | `no-swallowed-error` | the `Result`; `Result<Option<T>, E>` |
|
|
27
|
+
| `JsonNull` as app state, every `undefined` written as `null` | `json-null-in-app`, `undefined-to-null` | `Option`, an `Enum`, `AsyncState<Option<T>, E>`; `jsonNull` only where a body needs it |
|
|
28
|
+
| `JSON.parse`, direct storage, casts and guards on outside data | `json-parse`, `raw-storage`, `as-cast`, `partial-type-guard` | `parseJson<T>`, `validate<T>`, the browser pack |
|
|
29
|
+
| `btoa` / `atob` of text | `unhandled-failure` | `toBase64`, `fromBase64` |
|
|
30
|
+
| `ref<HTMLElement \| null>(null)` | (checker: `no-null`) | `useElement` / `useOptionalElement` / `useElements` |
|
|
31
|
+
| `addEventListener` / `setInterval` in components | (checker: `listener-cleanup`) | `useListener`, `useInterval` (Vue); `listen` elsewhere |
|
|
32
|
+
| tables indexed by wire data | `no-object-dictionary`, a search for `[` on `Record` constants | `getOwn(table, key, fallback)`; for a server field from a list, `Open<K>` + `lookup` |
|
|
33
|
+
| `null` in request bodies, `undefined` turned into `null` on the way out | `json-null-in-app`, `undefined-to-null`, a search for `jsonNull` | `Patch<T>` with `keep` / `clear` |
|
|
34
|
+
| markup and URLs passed around as `string` | `vue-v-html`, `vue-unsafe-url`, `Html/SafeUrl` in the features | `defineSanitizer`, props typed `Html` / `SafeUrl` |
|
|
35
|
+
| numbers and strings tested for truth | `strict-truthiness` | `> 0`, `!== ""`, `!== undefined` |
|
|
36
|
+
| casts in tests | `no-unchecked-cast` in test files (`cast-in-test` where that is off for tests) | `fixture`, `testDouble`, `unchecked`, `expectPresent` |
|
|
37
|
+
| bridges left from the migration | `legacy-null`, `trust`, `safe-ts-ignore` | gone, or each one in the exception list with its reason |
|
|
38
|
+
|
|
39
|
+
For each area, the hand-off report of this pass gives the feature counts before and after, and
|
|
40
|
+
for each remaining hand-rolled pattern either the change or the reason it stays (a line in the
|
|
41
|
+
architecture document's exception list).
|
|
42
|
+
|
|
43
|
+
The pass is done when every remaining pattern is an exception someone decided, not one nobody
|
|
44
|
+
looked at.
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# Order of work and gates
|
|
2
|
+
|
|
3
|
+
## The order
|
|
4
|
+
|
|
5
|
+
1. **Setup** (one change): packages, tsconfig flags, oxlint preset, Vite plugin (or the Bun
|
|
6
|
+
plugin), the loader for Node processes, `"idiomatic": true`, `"requestFunctions"`, the gate
|
|
7
|
+
scripts and CI job ([templates.md](templates.md)).
|
|
8
|
+
Then the first census, then legacy markers on every file that doesn't pass yet, then the
|
|
9
|
+
census baseline. From here on the build is green and every gate runs.
|
|
10
|
+
2. **The shared foundation**: what every other area imports.
|
|
11
|
+
- The transport and the request functions (`Promise<Result<T, RequestError>>`, with a signal,
|
|
12
|
+
a time limit and `validate<T>`), and the error types.
|
|
13
|
+
- The absence conversion at the boundary, and the outgoing-`null` contract.
|
|
14
|
+
- The stores: their state, their actions returning `Result`, async state through `useTask`.
|
|
15
|
+
- Shared types and the sanctioned helpers from the architecture document.
|
|
16
|
+
Land this before anything that depends on it, so the areas don't each invent their own
|
|
17
|
+
bridge (a local `orNone`, a `UiLegacyNull`, a `must()`). The bridges that exist are std's:
|
|
18
|
+
`LegacyNull` for strict code with legacy callers that pass `null`, `trust<T>` for a named
|
|
19
|
+
trusted boundary ([patterns-core.md](patterns-core.md#strict-code-with-legacy-callers-that-pass-null)).
|
|
20
|
+
3. **Utilities and the component library**: the code the feature areas render with. Decide the
|
|
21
|
+
one form for optional props under `exactOptionalPropertyTypes` here, once, before 80
|
|
22
|
+
components each pick their own: the conditional spread of a one-key literal, or `x?: Option<T>`
|
|
23
|
+
for props a template always passes ([patterns-core.md](patterns-core.md#an-optional-key)).
|
|
24
|
+
4. **Feature areas, in parallel by dependency**: an area starts when what it imports is done.
|
|
25
|
+
Each area is one change with its own gates and hand-off report.
|
|
26
|
+
5. **Integration**: merge the areas, run every gate on the whole tree, the browser smoke on every
|
|
27
|
+
page, and the cross-area search for changed signatures (below).
|
|
28
|
+
6. **Removal of the legacy path**: bridges and adapters from the architecture document, the last
|
|
29
|
+
legacy markers, and the exceptions that had an end date.
|
|
30
|
+
7. **The final pass** ([final-pass.md](final-pass.md)) and the review
|
|
31
|
+
([review-checklist.md](review-checklist.md)).
|
|
32
|
+
|
|
33
|
+
### Legacy markers are scaffolding
|
|
34
|
+
|
|
35
|
+
`// @safe-ts legacy` (first lines of a `.ts` file; in a `.vue` file, the first line inside either
|
|
36
|
+
`<script>` block, or `<!-- @safe-ts legacy -->` at the top) lets the build stay green while a
|
|
37
|
+
file waits its turn. A marker anywhere else in a component marks nothing and is reported
|
|
38
|
+
(`vue-sfc`). It is never the end state:
|
|
39
|
+
|
|
40
|
+
- The census counts the marked files; the baseline holds the count, so no new file gets one.
|
|
41
|
+
The count per week or per release is the burn-down.
|
|
42
|
+
- Each area ends with no markers left in it.
|
|
43
|
+
- Strict code calling into a legacy file is still checked, through every import the checker
|
|
44
|
+
follows (relative paths, tsconfig `paths` aliases, workspace packages). An import it can't
|
|
45
|
+
resolve is named in a `could not resolve` warning: fix those first, the quarantine doesn't
|
|
46
|
+
reach behind them.
|
|
47
|
+
- The oxlint preset's non-SAFE-TS rules (`no-empty`, `unicorn/prefer-at`, ...) still report in
|
|
48
|
+
legacy files. The marker silences only `safe-ts/*` rules.
|
|
49
|
+
- The Vite plugin and the Node loader give a legacy file only the rewrites it needs to run
|
|
50
|
+
(prelude imports, `q`, `match`, validators), so its shipped code doesn't change until it is
|
|
51
|
+
migrated. Don't put legacy files in `safeTs({ exclude })`: an excluded file gets no prelude
|
|
52
|
+
imports either.
|
|
53
|
+
- A marker is for a file that waits its turn. One line in a migrated file that must stay as it
|
|
54
|
+
is takes `// safe-ts-ignore <rule>: <reason>` instead ([escape-hatches.md](escape-hatches.md)).
|
|
55
|
+
|
|
56
|
+
### Parallel builders
|
|
57
|
+
|
|
58
|
+
When several agents migrate areas at once:
|
|
59
|
+
|
|
60
|
+
- Each gets this skill, the architecture document, and the list of the foundation's exported
|
|
61
|
+
signatures.
|
|
62
|
+
- An area that changes an exported signature (a function or store member that now returns a
|
|
63
|
+
`Result`, a type from `T | null` to `Option<T>`) lists each changed name in its hand-off
|
|
64
|
+
report. The integrator searches every caller in every area (`rg -n '\bname\('`) and reads each
|
|
65
|
+
one. The checker reports a `Result` dropped through a store member, a returned object or a
|
|
66
|
+
factory, and (idiomatic) a `try` or `.catch` left around one, but not through a parameter, a
|
|
67
|
+
class field, a `ref` or a `Map`; and a caller's changed behaviour (a dialog that closed on a
|
|
68
|
+
throw) is no rule's business.
|
|
69
|
+
- Builders don't edit the foundation; a missing piece goes back to it as its own change.
|
|
70
|
+
|
|
71
|
+
## Gates per step
|
|
72
|
+
|
|
73
|
+
Every change passes all of these before it merges. Fixing a gate by weakening it (a new legacy
|
|
74
|
+
marker, a rule turned off, an `as`, a `void`, a baseline rewrite) is not a fix unless the
|
|
75
|
+
architecture document's exception list gains a line for it.
|
|
76
|
+
|
|
77
|
+
| Gate | Command (adapt to the project) | Passes when |
|
|
78
|
+
|---|---|---|
|
|
79
|
+
| Types | `tsc --noEmit`, or `vue-tsc --noEmit` for Vue, with `noUncheckedIndexedAccess` and `exactOptionalPropertyTypes` on | no errors |
|
|
80
|
+
| Lint | `oxlint --deny-warnings` (add `--type-aware` if the project uses it) with the SAFE-TS preset | no warnings or errors |
|
|
81
|
+
| SAFE-TS | `safe-ts check <entries> --deny-warnings`: the app's entry points, plus the tests, scripts and workers nothing imports | exit 0, no `could not resolve` warnings (legacy files report nothing) |
|
|
82
|
+
| Architecture | `node scripts/census.mjs --baseline safe-ts-census.json` | no count rose in any file |
|
|
83
|
+
| Tests | the project's runner; Node processes with `--import @evobug_com/safe-ts-transform/register` | pass |
|
|
84
|
+
| Build | `vite build` with `safeTs({ check: "error" })` (fails on errors; warnings are the SAFE-TS gate's) | succeeds, and its last line says `safe-ts: N files checked, ...` |
|
|
85
|
+
| Behaviour | the checks for the touched area in [behaviour.md](behaviour.md) | same bodies, smoke list clean |
|
|
86
|
+
|
|
87
|
+
An area is **done** when, in its directories, the gates pass, no file is legacy, and the census's
|
|
88
|
+
architecture patterns and the idiomatic rules' findings are at zero, or each remaining one is
|
|
89
|
+
an entry in the exception list. The hand-off report ([templates.md](templates.md)) says which.
|
|
90
|
+
|
|
91
|
+
Run the SAFE-TS gate in your own loop the way CI runs it, with `--deny-warnings`: a plain
|
|
92
|
+
`safe-ts check` exits 0 with every idiomatic finding still there. `--max-warnings <n>`, with `n`
|
|
93
|
+
the area's current count, lets a burn-down land in steps without letting the count rise.
|
|
94
|
+
|
|
95
|
+
Before a gate's fix text or a project rule names a replacement, check the replacement against
|
|
96
|
+
this skill's catalogue: a gate that sends people to the wrong form is followed faithfully.
|