@evobug_com/safe-ts-skill 0.4.0 → 0.5.1
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/install.mjs +31 -7
- package/package.json +1 -1
- package/safe-ts-migrate/SKILL.md +80 -13
- package/safe-ts-migrate/references/architecture.md +10 -3
- package/safe-ts-migrate/references/census.md +31 -9
- package/safe-ts-migrate/references/escape-hatches.md +19 -9
- package/safe-ts-migrate/references/final-pass.md +7 -5
- package/safe-ts-migrate/references/fix-the-design.md +87 -0
- package/safe-ts-migrate/references/order-and-gates.md +9 -4
- package/safe-ts-migrate/references/patterns-core.md +1240 -85
- package/safe-ts-migrate/references/patterns-react.md +52 -16
- package/safe-ts-migrate/references/patterns-vue.md +98 -21
- package/safe-ts-migrate/references/review-checklist.md +7 -0
- package/safe-ts-migrate/references/templates.md +46 -21
- package/safe-ts-migrate/references/tool-gaps.md +284 -14
- package/safe-ts-migrate/scripts/census.mjs +343 -144
|
@@ -21,13 +21,21 @@ no longer needed.
|
|
|
21
21
|
(Adopting SAFE-TS, "Rule levels").
|
|
22
22
|
- **`check` reads what its entries reach.** It follows relative imports, tsconfig.json `paths`,
|
|
23
23
|
workspace packages, `new Worker(new URL(...))` and `import()`. A file nothing imports (a
|
|
24
|
-
script, a test, a worker whose path is built at runtime) is checked only as an entry
|
|
25
|
-
|
|
24
|
+
script, a test, a worker whose path is built at runtime) is checked only as an entry. A
|
|
25
|
+
directory is an entry for each of its source files (`safe-ts check src scripts`), so passing
|
|
26
|
+
the directories is the simplest gate. With file entries, `check` names the source files under
|
|
27
|
+
each entry's directory that the run didn't reach (`warning: 2 source files under the entries'
|
|
28
|
+
directories are not checked: ...`), and they count as warnings, so `--deny-warnings` fails on
|
|
29
|
+
them. It doesn't look outside those directories: a `tests/` next to `src/` is only checked
|
|
30
|
+
when it is an entry too. Build output, other packages (a directory with its own
|
|
31
|
+
package.json) and tool configs (`vite.config.ts`) aren't counted.
|
|
26
32
|
- **Vite-only aliases** (a `resolve.alias` entry without a tsconfig `paths` entry) are seen by
|
|
27
33
|
the Vite plugin only, and only those with a string `find`. `safe-ts check`, the editor and
|
|
28
34
|
the oxlint plugin name each import they can't resolve (`warning: could not resolve "@x/y"`):
|
|
29
35
|
add the alias to tsconfig.json `paths`, which is the source of truth (Adopting SAFE-TS, "How
|
|
30
36
|
imports are followed"). Behind an unresolved import, the legacy quarantine doesn't reach.
|
|
37
|
+
Each one counts as a warning: the run ends `⚠ 1 unresolved import`, and `--deny-warnings`
|
|
38
|
+
fails on it.
|
|
31
39
|
|
|
32
40
|
## The oxlint plugin
|
|
33
41
|
|
|
@@ -68,6 +76,9 @@ no longer needed.
|
|
|
68
76
|
- **`safe-ts emit` doesn't write `.vue` files**: type-check components with `vue-tsc`.
|
|
69
77
|
- **Vite loads `vite.config.ts` without the plugin**: what it imports can't use the prelude or
|
|
70
78
|
`q(...)`, unless it goes through `loadTransformed` (Adopting SAFE-TS, "The Vite config").
|
|
79
|
+
`needs-transform` reports such uses there (in what `vitest.config.*` imports too), and the
|
|
80
|
+
quick fixes and `migrate throw-to-result` in those files write the plain
|
|
81
|
+
`if (isErr(x)) return x` instead of `q`.
|
|
71
82
|
|
|
72
83
|
## The codemods
|
|
73
84
|
|
|
@@ -81,25 +92,237 @@ no longer needed.
|
|
|
81
92
|
event types and slot props keep theirs. Convert those by hand where the architecture says so.
|
|
82
93
|
- **`throw-to-result` converts top-level functions only**, and only when every caller can take
|
|
83
94
|
a `Result`; the report's "Needs a decision" lists the rest.
|
|
95
|
+
- **Applying quick fixes in bulk: `safe-ts fix <rule> <dir>`**, one rule per run, never
|
|
96
|
+
`oxlint --fix-suggestions` (it applies the first suggestion of every rule at once, and lets
|
|
97
|
+
bad fixes in). Read the diff it prints before `--write`, and run `safe-ts check` and `tsc`
|
|
98
|
+
after. A site with several fixes is skipped and listed: decide those by hand, or pick one
|
|
99
|
+
kind with `--fix "<start of its title>"` when the same answer is right at every site listed
|
|
100
|
+
(Adopting SAFE-TS, "Applying one rule's quick fixes").
|
|
84
101
|
|
|
85
102
|
## Rules and the checker
|
|
86
103
|
|
|
87
|
-
- **Calls a
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
104
|
+
- **Calls a failure isn't followed through without a declaration**: a method reached through
|
|
105
|
+
an interface, an abstract method or a stored callback propagates only what its type declares
|
|
106
|
+
with `Throws<R, E>`; what is put there is checked against it (`failure-contract`). Not
|
|
107
|
+
checked: a value of an inferred type or from a package put into such a slot, a slot reached
|
|
108
|
+
through a `Map` lookup or a computed key, a class used where an unrelated class is expected
|
|
109
|
+
(rule page `failure-contract`, "What the rule can't see").
|
|
110
|
+
- **Calls a `Result` isn't followed through**: a return type written as an alias the checker
|
|
111
|
+
can't open (a conditional or mapped type), a function without a declared type whose `Result`
|
|
112
|
+
comes from something other than `err(...)` or a followed call, a method reached through a value typed with an
|
|
113
|
+
interface or a union (`this.deps.store.save()` with `store: Store`, an interface), a generic
|
|
114
|
+
type, an array element, a `ref`, `reactive()`, a `Map` or an object literal written inside a
|
|
115
|
+
function (rule page `must-use-result`, "What the rule can't see"). A receiver whose class is
|
|
116
|
+
written down one hop away is followed: a field declared with the class (`this.repo.save()`),
|
|
117
|
+
a property of a declared type (`deps.repo.save()`), a function declared to return it
|
|
118
|
+
(`makeRepo().save()`, `(await openRepo()).save()`), a member chain into a module-level
|
|
119
|
+
object or a `readonly` field holding an object literal (`api.users.save()`,
|
|
120
|
+
`this.api.save()`), and
|
|
121
|
+
getters and setters. Store members, returned objects,
|
|
122
|
+
factories and copies (`const save = store.save`, a class field `handler = save`) are followed,
|
|
123
|
+
and a function passed by name counts as the arrow that calls it (`list.forEach(save)` drops
|
|
124
|
+
each `Result`, `.then(save)` in a dropped chain too). A value kept in a `const` and read once
|
|
125
|
+
in the next statement is the value written there (`const p = save(x); await p;` drops the
|
|
126
|
+
`Result` like `await save(x);`); one read twice, or read later, is a binding of its own.
|
|
127
|
+
Search every caller of every changed member anyway.
|
|
128
|
+
- **A rejection is a `throw`**: `Promise.reject(x)` and a `new Promise` executor's `reject(x)`
|
|
129
|
+
are `no-throw` unless `x` is `new` of a `PropagateError` subclass, and handing `reject` on by
|
|
130
|
+
name (`stream.on("error", reject)`, `pending.set(id, { resolve, reject })`) too. Reject with
|
|
131
|
+
a tracked error made there (`(cause) => reject(new StreamFailed(String(cause)))`), or resolve
|
|
132
|
+
with a `Result` (rule page `no-throw`).
|
|
92
133
|
- **`validate<T>` refuses library types**: a package's types, global types from `@types/...`
|
|
93
134
|
and `declare module "..."` blocks. Namespace members (generated `Backend.Models.User`), types
|
|
94
135
|
behind a tsconfig alias and workspace packages are fine (JSON guide, "What isn't supported").
|
|
136
|
+
- **Sinks are seen where the receiver's class is written down.** `el.innerHTML = x` is checked
|
|
137
|
+
when `el` is declared `HTMLElement`, comes from `document.createElement("div")`, `document`'s
|
|
138
|
+
methods or `as HTMLElement` (and `innerHTML`, `insertAdjacentHTML`, `srcdoc` by name on any
|
|
139
|
+
receiver), and through a field, an `Option` checked, an `instanceof` test or an item of a typed
|
|
140
|
+
list; `setAttribute`, `href` and `src` on an element from a template ref, or typed with an
|
|
141
|
+
interface of the project's own, aren't. Markup accumulated in a `let` (`html += ...`) is fine
|
|
142
|
+
when every part added is.
|
|
143
|
+
`path-sink` reports only in code a handler the runtime calls with outside data reaches (a
|
|
144
|
+
request, a socket's data, a message), from `safe-ts check`; there a path from
|
|
145
|
+
the server's own configuration is reported too when it travels as a `string` (type it
|
|
146
|
+
`FromOperator`). A framework whose handlers no pack describes, a call through a value whose type
|
|
147
|
+
isn't written (a route picked by `flatMap(...).find(...)`: type it), a handler bound, and a port
|
|
148
|
+
typed with a type of the project's own leave what they run silent. A call through a field
|
|
149
|
+
typed with a function type runs every function put into that type.
|
|
150
|
+
- **An `any` nobody wrote is seen in two ways, neither complete.** `no-hidden-any` reports lib's
|
|
151
|
+
and Node's from a list (`MessageEvent` without a type argument or with one that claims what
|
|
152
|
+
arrives, `Function`, `history.state`, `event.data` in an unannotated `"message"` listener on a
|
|
153
|
+
DOM target, `workerData`, a `worker_threads` `"message"` value), and std's strict-lib in
|
|
154
|
+
tsconfig.json `"types"` makes `JSON.parse`, `response.json()`, `Array.isArray`, `new Map()`,
|
|
155
|
+
`new Array(n)`, `Object.create`, `Object.values` / `Object.entries` of an interface,
|
|
156
|
+
`Object.getPrototypeOf`, a descriptor's `value`, a `replace` callback's captures, a
|
|
157
|
+
`JSON.stringify` replacer's values and a rejection reason `unknown`, so `tsc` refuses an
|
|
158
|
+
unchecked use (`check` warns when the tsconfig doesn't load it, and `--deny-warnings` fails
|
|
159
|
+
on that warning). The repository's `tests/lib-any/allowed.txt` lists lib's other `any` with
|
|
160
|
+
why each is left. Not seen: a library's own `any` (an untyped package, a `.d.ts` that says
|
|
161
|
+
`any`), Node's other ones (`process.on("message")`, `require`, a stream's chunks), a listener on a receiver
|
|
162
|
+
whose type the checker can't see, and `event.data` after `instanceof MessageEvent`. Annotate
|
|
163
|
+
such a value `unknown` where it comes in, or wrap the call in `unsafe(...)`, which gives
|
|
164
|
+
`unknown` for a function returning `any`; then `validate<T>` (rule page `no-hidden-any`,
|
|
165
|
+
"What the rule can't see").
|
|
166
|
+
- **Type predicates and overloads are reported, not verified.** `no-type-predicate` reports
|
|
167
|
+
every hand-written `x is T` and assertion signature; it never decides that a claim is true.
|
|
168
|
+
Its quick fix removes the claim where the body is one test TypeScript narrows by, and
|
|
169
|
+
TypeScript then infers the predicate. Where the claim was wider than the test (`value is
|
|
170
|
+
Map<string, Patch>` over `value instanceof Map`, `Array.isArray` on a `readonly` array that
|
|
171
|
+
TypeScript doesn't narrow), the code that used it stops compiling: write the real check
|
|
172
|
+
there, don't put the annotation back. `no-overload` compares the overloads with the
|
|
173
|
+
implementation as written text: a union behind an alias doesn't count as its members.
|
|
174
|
+
Neither sees a `declare function` in a `.ts` file implemented elsewhere, nor what a
|
|
175
|
+
`trust<T>(value)` or a `// @safe-ts std` module claims (rule pages, "What the rule can't
|
|
176
|
+
see").
|
|
177
|
+
- **The language's built-ins go by what is written.** A built-in's method is reported only on a
|
|
178
|
+
receiver whose type the code says, and an argument only when it comes from data (a parameter,
|
|
179
|
+
a field, parsed text): a count the function computes itself, a typed array's length, a date
|
|
180
|
+
made from a number and a `DataView` read guarded by a helper aren't reported. Checks the
|
|
181
|
+
checker sees (`if (digits < 0 || digits > 100) return`, `Number.isNaN(date.getTime())`,
|
|
182
|
+
`list.length === 0`) make the call acceptable; a check in a helper it calls doesn't. Don't
|
|
183
|
+
move a check into a helper to make a finding go away, and don't add one only to please it
|
|
184
|
+
where the value can't be out of range: say so in the hand-off report.
|
|
95
185
|
- **`JSON.stringify` of a value it can't prove JSON-safe** is reported (`unhandled-failure`): a
|
|
96
186
|
`bigint`, a `toJSON`, a type that refers to itself, an `async` callback's result. Give the
|
|
97
187
|
value a type that is plain data, or `unsafe(...)` with a reason.
|
|
98
|
-
- **`stable-shape` and three or more conditional spreads** in one literal (eight shapes)
|
|
99
|
-
|
|
188
|
+
- **`stable-shape` and three or more conditional spreads** in one literal (eight shapes) are
|
|
189
|
+
reported whatever the type declares, by design: the message counts the shapes and names the
|
|
190
|
+
fix. Declare the fields `Option<T>` and always write them
|
|
191
|
+
([patterns-core.md](patterns-core.md#an-optional-key)); assigning them after the literal makes
|
|
192
|
+
as many shapes and is reported too.
|
|
100
193
|
- **Heuristics by name**: `panic-on-loading` goes by the names in the condition (`hydrated`,
|
|
101
|
-
`isLoading`, a `Loading` variant),
|
|
102
|
-
|
|
194
|
+
`isLoading`, a `Loading` variant), `unbounded-poll` by the delay's name (`sleep`, `tick`, ...)
|
|
195
|
+
and a limit's (`deadline`, `attempts`, ...), and the census's patterns are regular
|
|
196
|
+
expressions. All are a map for review, not a proof. `pending-without-close` takes any
|
|
197
|
+
`close` / `error` / `exit` listener in the class as the cleanup, without checking that it
|
|
198
|
+
settles the pending calls.
|
|
199
|
+
- **`cycle-init-read` on an existing project** usually finds several reads at once, including
|
|
200
|
+
ones that work with today's entry and break when a test imports one module on its own. Move
|
|
201
|
+
the read into a function the entry (or the test's setup) calls, or move the shared constants
|
|
202
|
+
into a module that imports nothing back; don't reorder imports to make one entry pass (rule
|
|
203
|
+
page `cycle-init-read`).
|
|
204
|
+
- **Seven rules are reported by `safe-ts check` only**, not by the editor, the lint plugin or
|
|
205
|
+
the Vite plugin, because they need the whole program: `constructor-calls-override` (the
|
|
206
|
+
subclasses may be in other files), `no-optional-field` (whether a type is JSON or props depends
|
|
207
|
+
on where it is used), `inline-member-set`, `path-sink` (what a handler of outside data calls),
|
|
208
|
+
`unused-code` (who imports an export), `unknown-event` and `unmatched-event` (whether any file
|
|
209
|
+
dispatches or listens for a name). A
|
|
210
|
+
gate runs `check`; an editor without those findings isn't a clean result.
|
|
211
|
+
- **`unused-code` judges exports by what the run sees.** Checking entries by name, a file of the
|
|
212
|
+
same package the run doesn't reach (a test, a script) still keeps what it imports alive; code
|
|
213
|
+
in another repository, a name looked up at run time (`window[name]`) and templates other than
|
|
214
|
+
Vue's are not seen: name such code as an entry, or check the directories. Code nothing refers
|
|
215
|
+
to at all and isn't exported, and code after `return`, are oxlint's (`no-unused-vars`,
|
|
216
|
+
`no-unreachable`, which the SAFE-TS preset names), not the checker's.
|
|
217
|
+
- **Event names are matched on emitters the source writes down** (a module-level instance, a
|
|
218
|
+
class extending `EventEmitter` / `EventTarget`, a field holding one, the DOM). An emitter passed
|
|
219
|
+
around as `EventEmitter` has no identity: its names only keep other sites' names from being
|
|
220
|
+
reported, and an emitter handed to code the checker can't see is never reported. Node's own
|
|
221
|
+
emitters (`process`, a server made by `createServer`, a socket, a stream) are checked against
|
|
222
|
+
@types/node's event maps when the source names them. `@types/node`'s typed
|
|
223
|
+
`EventEmitter<Events>` types the arguments, but lets any name through.
|
|
224
|
+
- **`no-method-signature` closes method bivariance for interfaces and type literals,
|
|
225
|
+
`override-parameter` for overrides of the program's classes.** A class used as a type still
|
|
226
|
+
accepts another, unrelated class whose method takes less; an override of a package's class
|
|
227
|
+
isn't compared (its declarations aren't read); and the platform's types are declared with
|
|
228
|
+
methods (`Dog[]` is accepted as `Animal[]`). `override-parameter` compares written types and
|
|
229
|
+
reports only what it can show narrower: a package's class no pack describes, compared with a
|
|
230
|
+
class of another name, is left alone. Pass
|
|
231
|
+
objects around as an interface with property members, and take read-only views
|
|
232
|
+
(`readonly T[]`, `ReadonlyMap`) where the code doesn't change the collection (rule page
|
|
233
|
+
`no-method-signature`, "What the property form closes, and what it leaves"). After
|
|
234
|
+
`safe-ts fix no-method-signature --write`, `tsc` reports each assignment that only compiled
|
|
235
|
+
through the bivariance: fix the types, don't drop `readonly` to silence it.
|
|
236
|
+
- **`implicit-propagation` is checked at the definition, not where the method runs.** A
|
|
237
|
+
`toString`, `toJSON`, `[Symbol.iterator]`, `then`, ... that can propagate is refused in a class
|
|
238
|
+
or object literal; one assigned later, put on a prototype, made by `Object.defineProperty` or
|
|
239
|
+
inherited from a package's class isn't seen, nor a `Proxy` handler that isn't written at
|
|
240
|
+
`new Proxy(...)`. A failure that isn't a `PropagateError` (a built-in, a library) is reported
|
|
241
|
+
where it is called inside the method, as anywhere.
|
|
242
|
+
- **`unbounded-spread` has no types**: a project method named `push`, `concat` or `append` that
|
|
243
|
+
takes `...items` is recognised only where the receiver is declared with the project's type
|
|
244
|
+
(`router: Router`, `this.router`, `new Router()`); on a receiver whose type is inferred
|
|
245
|
+
(`getRouter().push(...xs)`), or a member the type inherits, it is taken for the built-in.
|
|
246
|
+
Annotate the receiver, or give the method a list parameter. A list bounded by the code around
|
|
247
|
+
it (`values.slice(0, n)`, three pushes) counts as unknown: write the bound as a tuple type, or
|
|
248
|
+
use `maxOf` / a loop, which cost nothing for a short list. A bounded list can still be empty:
|
|
249
|
+
`Math.max(...xs.slice(0, 3))` isn't reported and is `-Infinity` for `[]`.
|
|
250
|
+
- **The test rules take a loop as running.** `test-without-assertion` counts a loop or a
|
|
251
|
+
`forEach` whose body asserts as asserting, so a test over a table that turns out empty passes.
|
|
252
|
+
A helper in a source file without assertions that only calls another helper is taken not to
|
|
253
|
+
assert (move the check into the helper, or into the test). A call it can't follow counts as an
|
|
254
|
+
assertion only by its name (`check…`, `assert…`, `expect…`, `verify…`, ...). An assertion
|
|
255
|
+
inside a helper called in a `try` isn't seen by `swallowed-assertion`.
|
|
256
|
+
- **`use-after-consume` follows a value in one function.** A value used up inside a function it
|
|
257
|
+
is handed to (`drain(it)` then `[...it]`) or inside a callback isn't seen, nor one whose kind
|
|
258
|
+
the code doesn't show (an imported generator function's result, a field, a reassigned copy). Keep the use
|
|
259
|
+
and the operation in one function, or give the value its type (`it: IterableIterator<T>`).
|
|
260
|
+
`must-use-result` doesn't count an early `return` before the code that checks a kept `Result`
|
|
261
|
+
as dropping it (`if (signal.aborted) return;` first is fine).
|
|
262
|
+
- **The flow rules see one function.** `stale-across-await` doesn't know who calls a method: one
|
|
263
|
+
only ever awaited in sequence is reported like one run twice at once (say so in a
|
|
264
|
+
`safe-ts-ignore` with the reason, or read again after the `await`); state reached through an
|
|
265
|
+
alias (`const cart = this.cart`) isn't followed. `promise-never-settles` trusts whoever is handed
|
|
266
|
+
`resolve`. `unreleased-resource` takes a resource returned, stored, passed on or captured by a
|
|
267
|
+
callback as released by its new holder, and an `await` of a promise held in a property or a
|
|
268
|
+
parameter as one that may reject (an awaited call is judged by whether it can fail, a call by
|
|
269
|
+
whether it can throw; one it can't resolve doesn't); one acquired at module level or through a
|
|
270
|
+
field isn't followed.
|
|
271
|
+
`unbalanced-update` sees a pair written in one function, or made by callees that always make
|
|
272
|
+
the change once on every path; `begin()` / `end()` split between functions, a pair with
|
|
273
|
+
nothing in common the code shows, and a call between it can't resolve (taken not to throw) are
|
|
274
|
+
out of its sight,
|
|
275
|
+
and a flag left set on a written `return` counts only where the function tested it first.
|
|
276
|
+
`q-undeclared-error` compares the error `q` passes on with the declared one only where both are
|
|
277
|
+
written: not a pack's safe version (its declarations aren't read), a callback typed by its
|
|
278
|
+
context, a type parameter, a generic or an object type written in place.
|
|
279
|
+
`decided-test` knows literals in places given in the function itself (or by a callee that
|
|
280
|
+
always leaves them); a callee that reads state its caller already cleared isn't reported.
|
|
281
|
+
- **What happened during an `await` is known for the platform's objects only.** `late-listener`
|
|
282
|
+
reads which events happen once from the packs: a project's own emitter (a class extending
|
|
283
|
+
`EventEmitter`) has no such data, and there is no way to declare it; for a one-time event of
|
|
284
|
+
your own, keep a promise (`closed: Promise<void>`), which still works when awaited late. A
|
|
285
|
+
receiver whose class the source doesn't write down (what a library or a platform method
|
|
286
|
+
returns, a library callback's parameters) isn't known either: declare it
|
|
287
|
+
(`const put: IDBRequest<IDBValidKey> = store.put(...)`). `superseded-task-write` follows a task
|
|
288
|
+
callback written in place and the function it calls, not a function that one calls in turn.
|
|
289
|
+
- **An answer nobody reads is known for the language's built-ins, a few of the platform's, and
|
|
290
|
+
the program's own functions.** `unused-answer` measured the built-ins on Node; of the
|
|
291
|
+
browser's and Node's members only those whose answer tells what they did (`encodeInto`,
|
|
292
|
+
`readSync`, `confirm`) are listed, so `params.get(key);` passes. A dropped answer that isn't a
|
|
293
|
+
statement (an arrow whose body is the call) isn't seen, and `Set#add` answers with the set, not
|
|
294
|
+
whether the value was new: to count additions compare `set.size` before and after.
|
|
295
|
+
`misplaced-argument` compares names with the program's own functions' parameters only
|
|
296
|
+
(`Math.atan2(x, y)` passes), and needs two or more arguments shifted alike, or a swapped pair.
|
|
297
|
+
- **`regex-last-index` knows a regex's flags only where it is made.** A regex passed in (a
|
|
298
|
+
parameter without a default, a call's result) or made with flags in a variable isn't followed,
|
|
299
|
+
and a `lastIndex` set by a function the code calls isn't seen. Prefer `matchAll` for walking
|
|
300
|
+
matches and a regex without `g` for `test`.
|
|
301
|
+
- **The sentinel rules see the code, not the invariants around it.** `not-found-index` reports
|
|
302
|
+
`list.splice(list.indexOf(x), 1)` even where `x` is known to be in `list` (it came from it in
|
|
303
|
+
a caller): on an existing codebase most of its warnings are that kind. Add the check rather
|
|
304
|
+
than an ignore: a second removal of the same `x` takes another element. Test files aren't
|
|
305
|
+
checked. `divide-by-length` looks only at a collection the function made by selecting
|
|
306
|
+
(`filter`, a `push` under a condition); a parameter's length isn't checked, since a caller
|
|
307
|
+
usually guarantees it isn't empty: give such a function a property test with
|
|
308
|
+
`arbitrary<T>()`, which tries the empty list. `sparse-array-write` reports
|
|
309
|
+
only arrays made `[]` and filled by position, and writes past `a.length`; other index writes
|
|
310
|
+
it can't follow only stop `a[i]!` from being proved on that array in the file: read it with
|
|
311
|
+
`?? fallback`, or loop with `for...of`.
|
|
312
|
+
- **A promise is seen where it is made, not where it is kept.** `promise-as-value` and
|
|
313
|
+
`floating-promise` know a call returns a promise when its function is `async` or declares
|
|
314
|
+
`Promise<...>` (or a `const` holds such a call); one stored in a `let`, a field or an array
|
|
315
|
+
isn't followed. A method named like a built-in (`filter`, `forEach`) is the built-in's when
|
|
316
|
+
the receiver's written type is an array, a `Map`, an iterator (not a class of yours or a
|
|
317
|
+
package's `Readable`), and when no type is written for it: annotate such a receiver. std's
|
|
318
|
+
types can't refuse a promise in generic code (a type parameter may be one), and explicit type
|
|
319
|
+
arguments that are type parameters (`isErr<T, E>(r)`) fail to compile with a message about
|
|
320
|
+
`NotAPromise<...>` that doesn't name the cause: drop them, and type the parameter instead
|
|
321
|
+
where they widened one (`r: Result<Option<T>, E>`).
|
|
322
|
+
- **`constructor-calls-override` takes the program as complete**: a subclass in a file the
|
|
323
|
+
check doesn't reach, or in another program importing a published class, isn't seen, and a
|
|
324
|
+
call inside a callback is taken to run later. A class meant to be extended by others calls
|
|
325
|
+
no overridable method while constructing.
|
|
103
326
|
- **The idiomatic layer reports nothing until package.json turns it on**
|
|
104
327
|
(`"safe-ts": { "idiomatic": true }`), whatever the oxlint preset lists. `safe-ts check
|
|
105
328
|
--idiomatic` shows it for one run.
|
|
@@ -115,9 +338,56 @@ no longer needed.
|
|
|
115
338
|
- **Brands and `Open`/`Patch` aren't in the prelude**: import them from the module that makes
|
|
116
339
|
them (`@evobug_com/safe-ts-std/html`, `/url`, `/regexp`, `/ascii`, `/open`, `/patch`).
|
|
117
340
|
- **`defineSanitizer` can't check that the function sanitizes**: it is where the project vouches
|
|
118
|
-
for it, like `trust`. Review its module like an escape hatch
|
|
119
|
-
|
|
120
|
-
|
|
341
|
+
for it, like `trust`. Review its module like an escape hatch: `check` counts every call
|
|
342
|
+
(`defineSanitizer() call(s)`, however it is imported or re-exported) and the baseline holds
|
|
343
|
+
the count.
|
|
344
|
+
- **`no-unchecked-declare` can't see members a library's namespace gets in `declare global`**
|
|
345
|
+
(`namespace PixiMixins { interface Container { x: T } }`) or what an `extends` clause adds:
|
|
346
|
+
those are counted, not reported. Write added members optional (`x?: Option<T>`) anyway.
|
|
347
|
+
- **`strict-truthiness` is silent where the type isn't known** (a result that is neither
|
|
348
|
+
declared nor plain, a package's value, `unknown`; `undeclared-return-type`, with the idiomatic
|
|
349
|
+
rules on, lists the functions to declare), and `x || fallback` as a value is `prefer-nullish-default`'s. So is
|
|
350
|
+
`compare-misses-undefined`. It follows a test of the value written the same way, in the same
|
|
351
|
+
test, in an `if` around it or in an `if … return` before it; a check in another function
|
|
352
|
+
(`assertSelected(game)`) or a reassignment (`id ??= ""`) isn't followed: write the
|
|
353
|
+
`undefined` half where the comparison is.
|
|
354
|
+
- **`exhaustive-dispatch` and `string-dispatch` are silent where the type isn't known**, and read
|
|
355
|
+
only `===` / `==` with a literal or a constant: a chain of `x !== A`, `[A, B].includes(x)` or
|
|
356
|
+
comparisons with variables isn't a dispatch to them. A chain with one test and an `else` isn't
|
|
357
|
+
either, by design: grep the census's `string-if-chain` places for the rest.
|
|
358
|
+
- **`match` on a union of strings** works in code, not in `<Match>` (Vue, React), which takes an
|
|
359
|
+
enum only: use `match` in a `computed`. Numbers aren't matched: a `switch` with
|
|
360
|
+
`unreachable`, or a `Record<Code, T>` table. A `match` whose value's type the build can't find
|
|
361
|
+
(a result neither declared nor plain, a package's value) stays a runtime call in TypeScript: `safe-ts explain`
|
|
362
|
+
lists it; declare the type to get the `switch`.
|
|
363
|
+
- **`Mut<T>` and what it can't follow.** `no-param-mutation` follows a method of your own class
|
|
364
|
+
that changes its object (`c.bump()` on a borrowed `c`), a part handed out by a function or
|
|
365
|
+
method whose every `return` gives back the same path, and a borrowed value a class keeps and
|
|
366
|
+
changes later, and the objects a borrowed list or map holds (a loop's variable, `find`, `at`,
|
|
367
|
+
`get`, a `filter`ed or spread copy's elements); `mutating-getter` refuses a getter that changes
|
|
368
|
+
its object. Not followed: a method declared in an interface (no body), a subclass's override
|
|
369
|
+
that changes the object where its base doesn't, a `map` whose callback returns its argument,
|
|
370
|
+
a borrowed value kept outside the object's own fields (a `Mut` parameter's collection, a
|
|
371
|
+
closure kept for later). `aliased-mut` reports two
|
|
372
|
+
loans alive at once on the paths the code shows (branches, loops, lists, fields, dropped
|
|
373
|
+
promises, listeners, intervals, a parameter's loan that outlives the call, on a `return` or an exception), where the callee
|
|
374
|
+
writes the loan after an `await`; not who calls a function, a callback the
|
|
375
|
+
checker doesn't know runs again (a framework's ticker), a promise awaited only through a flag,
|
|
376
|
+
which parts of one object two loans touch, or a call dropped without `void` (`floating-promise`
|
|
377
|
+
first).
|
|
378
|
+
`covariant-alias` needs a written type on both sides. `missing-mut` and `aliased-mut`
|
|
379
|
+
follow only calls the checker can follow; TypeScript refuses the missing loans it doesn't
|
|
380
|
+
see, and `aliased-mut` takes a member whose type it can't find to be a copy. A key chosen at
|
|
381
|
+
run time is written through the plain view (`const target: T = obj; target[key] = value`).
|
|
382
|
+
A method lends its own object as `mut<C>(this)`: `mut(this)` doesn't type.
|
|
383
|
+
The type itself lets a loan through: a function in a writable property replaced by one that
|
|
384
|
+
needs a narrower argument (`{ onEvent: (a: Animal) => string }` lent as
|
|
385
|
+
`Mut<{ onEvent: (d: Dog) => string }>`); `covariant-alias` reports the lend where both types
|
|
386
|
+
are written (not a package's, a generic or an inferred one). Make a handler property `readonly`
|
|
387
|
+
where nothing should replace it. A write through a union to a property its members type differently is
|
|
388
|
+
`union-tag-write`'s (written types only), and a `readonly` value given a writable place is
|
|
389
|
+
`readonly-dropped`'s (a type the program declares as an object; not a package's, a mapped or a
|
|
390
|
+
computed one).
|
|
121
391
|
- **`Open<K>` is an object at runtime** (`{ tag, value }`): send `.value` to the server.
|
|
122
392
|
`json-type` refuses an `Open` whose `K` isn't string literals.
|
|
123
393
|
- **`toBase64` encodes text as UTF-8**, where `btoa` wrote characters U+0080 to U+00FF as one
|