@stateward/plugin-errors 1.2.2 → 1.3.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.
Files changed (46) hide show
  1. package/README.md +9 -8
  2. package/dist/catch-hides-unexpected.d.ts +4 -1
  3. package/dist/catch-hides-unexpected.js +12 -5
  4. package/dist/catch-misses-error.d.ts +1 -1
  5. package/dist/catch-misses-error.js +1 -1
  6. package/dist/finally-can-throw.js +3 -3
  7. package/dist/index.d.ts +3 -1
  8. package/dist/index.js +1 -0
  9. package/dist/inference/analysis.d.ts +109 -0
  10. package/dist/inference/analysis.js +170 -0
  11. package/dist/inference/builtins.d.ts +0 -2
  12. package/dist/inference/builtins.js +0 -33
  13. package/dist/inference/deps.js +2 -2
  14. package/dist/inference/needs.d.ts +16 -0
  15. package/dist/inference/needs.js +27 -0
  16. package/dist/inference/packages.d.ts +24 -32
  17. package/dist/inference/packages.js +156 -680
  18. package/dist/inference/project.d.ts +2 -2
  19. package/dist/inference/project.js +3 -2
  20. package/dist/inference/solve.d.ts +29 -4
  21. package/dist/inference/solve.js +34 -5
  22. package/dist/inference/summarize.d.ts +27 -15
  23. package/dist/inference/summarize.js +247 -130
  24. package/dist/inference/summary.d.ts +41 -5
  25. package/dist/inference/summary.js +84 -12
  26. package/dist/inference/unreachable.d.ts +5 -0
  27. package/dist/inference/unreachable.js +160 -0
  28. package/dist/must-handle-error.d.ts +2 -1
  29. package/dist/must-handle-error.js +4 -4
  30. package/dist/plugin.d.ts +12 -2
  31. package/dist/plugin.js +4 -2
  32. package/dist/react/tree.js +8 -4
  33. package/dist/render-throw-without-boundary.d.ts +13 -4
  34. package/dist/result-ignored.d.ts +4 -1
  35. package/dist/result-type.js +2 -2
  36. package/dist/result-unchecked.d.ts +4 -1
  37. package/dist/result-unchecked.js +139 -61
  38. package/dist/rules.meta.d.ts +26 -14
  39. package/dist/rules.meta.js +18 -14
  40. package/dist/syntax.d.ts +0 -13
  41. package/dist/syntax.js +0 -79
  42. package/dist/text.d.ts +5 -2
  43. package/dist/text.js +11 -5
  44. package/dist/throws-doc-mismatch.d.ts +1 -1
  45. package/dist/throws-doc-mismatch.js +7 -1
  46. package/package.json +2 -2
@@ -1,7 +1,9 @@
1
1
  import ts from "@stateward/core/typescript";
2
+ import { classIdOf, codeOnOneLine, declaredPackageOf, factKeyOf, isPlatformDeclaration, } from "@stateward/core";
2
3
  import { isFunctionBoundary, scanRunning, stripParentheses } from "../syntax.js";
3
- import { oneLine, shorten } from "../text.js";
4
- import { ANY_REJECTS, ARRAY_LENGTH, BUILTIN_ERRORS, DEFERRED_CALLERS, FETCH_ABORT, FETCH_REJECTS, MAPPING_METHODS, SYNC_CALLBACKS, THROWING_CONSTRUCTORS, THROWING_GLOBALS, REJECTING_MEMBERS, THROWING_MEMBERS, builtinId, isNonThrowingReactHook, } from "./builtins.js";
4
+ import { shorten, shortenStart } from "../text.js";
5
+ import { cannotRun } from "./unreachable.js";
6
+ import { ANY_REJECTS, ARRAY_LENGTH, BUILTIN_ERRORS, DEFERRED_CALLERS, FETCH_ABORT, FETCH_REJECTS, MAPPING_METHODS, SYNC_CALLBACKS, THROWING_CONSTRUCTORS, THROWING_GLOBALS, REJECTING_MEMBERS, THROWING_MEMBERS, isBuiltin, } from "./builtins.js";
5
7
  /** How many `const` names are followed to the error they hold (`const err = new X()`). */
6
8
  const HELD_DEPTH = 3;
7
9
  /** Why something adds `unknown`, as the hover and the messages show it. */
@@ -15,6 +17,7 @@ export const REASON = {
15
17
  promise: "a promise that is not followed",
16
18
  heldPromise: "promise held in a variable",
17
19
  accessor: "a getter or setter runs here",
20
+ untyped: "its type is `any`",
18
21
  };
19
22
  /** Why a callback handed to a call of the platform that is not in the tables adds `unknown`. */
20
23
  export function mayRunReason(callee) {
@@ -26,55 +29,28 @@ export const ANONYMOUS = "";
26
29
  const TEXT_MAX = 40;
27
30
  /** A class's `extends` chain is followed this deep at most. */
28
31
  const MAX_CLASS_DEPTH = 20;
29
- const TS_LIB = /\/typescript\/lib\/lib\.[^/]*\.d\.ts$/;
30
- /** A declaration of the platform: TypeScript's lib, or a global in a declaration file. */
31
- function isPlatform(node, file) {
32
- if (TS_LIB.test(file))
33
- return true;
34
- const sourceFile = node.getSourceFile();
35
- if (!sourceFile.isDeclarationFile)
36
- return false;
37
- const module = ts.findAncestor(node.parent, ts.isModuleDeclaration);
38
- const enclosing = module === undefined ? undefined : outermostModule(module);
39
- // `declare module "x"` is a package; `declare global` is the platform; a namespace is what
40
- // its file is.
41
- if (enclosing !== undefined && ts.isStringLiteral(enclosing.name))
42
- return false;
43
- if (enclosing?.name.text === "global")
44
- return true;
45
- return !ts.isExternalModule(sourceFile);
46
- }
47
- /** `axios`, `@tanstack/react-query`, `react` for `@types/react`. */
48
- function packageName(file) {
49
- const at = file.lastIndexOf("/node_modules/");
50
- const parts = (at < 0 ? file : file.slice(at + "/node_modules/".length)).split("/");
51
- const [first = "", second = ""] = parts;
52
- if (first === "@types")
53
- return second.includes("__") ? `@${second.replace("__", "/")}` : second;
54
- return first.startsWith("@") ? `${first}/${second}` : first;
55
- }
56
32
  /**
57
- * The package a declaration outside the project belongs to: by its node_modules folder, or, for a
58
- * package linked from elsewhere (a workspace package, whose real path has no node_modules), by
59
- * the name in its package.json.
33
+ * The id of the class a name refers to, and its declaration when the class is one the summaries
34
+ * describe (of the project or a package); a class of the platform has none.
60
35
  */
61
- export function packageNameOf(types, file) {
62
- return file.includes("/node_modules/")
63
- ? packageName(file)
64
- : (types.packageOf(file) ?? packageName(file));
65
- }
66
- /** The outermost of nested module declarations (`declare global { namespace X { … } }`). */
67
- function outermostModule(module) {
68
- const outer = ts.findAncestor(module.parent, ts.isModuleDeclaration);
69
- return outer === undefined ? module : outermostModule(outer);
70
- }
71
- /** The name of the `declare module "x"` a declaration sits in. */
72
- function ambientModuleName(node) {
73
- const module = ts.findAncestor(node.parent, ts.isModuleDeclaration);
74
- if (module === undefined)
36
+ function classIdAndDeclaration(types, node) {
37
+ const id = classIdOf(types, node);
38
+ if (id === undefined)
39
+ return undefined;
40
+ const name = ts.isPropertyAccessExpression(node)
41
+ ? node.name
42
+ : ts.isQualifiedName(node)
43
+ ? node.right
44
+ : node;
45
+ const symbol = types.symbolAt(name);
46
+ const declaration = symbol === undefined ? undefined : types.declarationOf(symbol);
47
+ if (symbol === undefined || declaration === undefined)
75
48
  return undefined;
76
- const outer = outermostModule(module);
77
- return ts.isStringLiteral(outer.name) ? outer.name.text : undefined;
49
+ // The platform's classes the analysis knows are its error classes; it describes none.
50
+ if (isBuiltin(id)) {
51
+ return BUILTIN_ERRORS.has(symbol.name) ? { id, symbol, declaration: undefined } : undefined;
52
+ }
53
+ return { id, symbol, declaration };
78
54
  }
79
55
  /**
80
56
  * `exports.load = function () {…}` (CommonJS, as a package's JavaScript writes it): the function
@@ -208,7 +184,14 @@ function calledName(callee) {
208
184
  return undefined;
209
185
  }
210
186
  function textOf(node, sourceFile) {
211
- return shorten(oneLine(node.getText(sourceFile)), TEXT_MAX);
187
+ return shorten(codeOnOneLine(node.getText(sourceFile)), TEXT_MAX);
188
+ }
189
+ /**
190
+ * The function a call or `new` names, as written, shortened from the start: each call of a chain
191
+ * keeps its own name (`…createQueryBuilder().delete`, `…delete().from`).
192
+ */
193
+ function calleeTextOf(callee, sourceFile) {
194
+ return shortenStart(codeOnOneLine(callee.getText(sourceFile)), TEXT_MAX);
212
195
  }
213
196
  function newScope(async, rejectParameters = new Set()) {
214
197
  return { async, returns: [], rejectParameters };
@@ -218,27 +201,17 @@ export function packageClassId(name, className) {
218
201
  return `package:${name}#${className}`;
219
202
  }
220
203
  /**
221
- * The id a class named in code has in the summaries (`builtin:TypeError`, `package:zod#ZodError`,
222
- * or the qualified id of a class of the project), without its facts; undefined when the name
223
- * resolves to nothing.
204
+ * The id `errors/throws` gives the error class a name refers to (core's `classIdOf`): undefined
205
+ * for a name of the platform that is not one of its error classes.
224
206
  */
225
- export function classIdOf(types, name) {
226
- const symbol = types.symbolAt(name);
227
- const declaration = symbol === undefined ? undefined : types.declarationOf(symbol);
228
- if (symbol === undefined || declaration === undefined)
229
- return undefined;
230
- if (types.isOwnFile(declaration.file))
231
- return symbol.id;
232
- if (isPlatform(declaration.node, declaration.file)) {
233
- return BUILTIN_ERRORS.has(symbol.name) ? builtinId(symbol.name) : undefined;
234
- }
235
- return packageClassId(ambientModuleName(declaration.node) ?? packageNameOf(types, declaration.file), symbol.name);
207
+ export function errorClassIdOf(types, name) {
208
+ return classIdAndDeclaration(types, name)?.id;
236
209
  }
237
210
  /**
238
- * The throws facts of the file (see summary.ts). With `packages`, a call into a package carries
239
- * its facts; without (a package's own source), it is unknown.
211
+ * The throws facts of the file (see summary.ts): a call into a package carries its facts
212
+ * (`packages`).
240
213
  */
241
- export function summarizeThrows(ctx, packages) {
214
+ export function summarizeThrows(ctx, quiet, packages) {
242
215
  const { sourceFile, types } = ctx;
243
216
  const classes = new Map();
244
217
  const spanOf = (node) => [node.getStart(sourceFile), node.getEnd()];
@@ -251,24 +224,12 @@ export function summarizeThrows(ctx, packages) {
251
224
  const held = new Map();
252
225
  // ---- classes --------------------------------------------------------------------------------
253
226
  const classRef = (node, depth = 0) => {
254
- const name = ts.isPropertyAccessExpression(node)
255
- ? node.name
256
- : ts.isQualifiedName(node)
257
- ? node.right
258
- : node;
259
- const symbol = types.symbolAt(name);
260
- const declaration = symbol === undefined ? undefined : types.declarationOf(symbol);
261
- if (symbol === undefined || declaration === undefined)
227
+ const found = classIdAndDeclaration(types, node);
228
+ if (found === undefined)
262
229
  return undefined;
263
- const own = types.isOwnFile(declaration.file);
264
- if (!own && isPlatform(declaration.node, declaration.file)) {
265
- return BUILTIN_ERRORS.has(symbol.name) ? builtinId(symbol.name) : undefined;
266
- }
267
- // A package's class is named by its package and name: its types, its source and the config
268
- // all name it so.
269
- const id = own
270
- ? symbol.id
271
- : packageClassId(ambientModuleName(declaration.node) ?? packageNameOf(types, declaration.file), symbol.name);
230
+ const { id, declaration, symbol } = found;
231
+ if (declaration === undefined)
232
+ return id;
272
233
  let classNode;
273
234
  if (ts.isClassDeclaration(declaration.node) || ts.isClassExpression(declaration.node)) {
274
235
  classNode = declaration.node;
@@ -283,20 +244,84 @@ export function summarizeThrows(ctx, packages) {
283
244
  return undefined;
284
245
  if (!classes.has(id)) {
285
246
  // Placeholder first: a class that (wrongly) extends itself stops here.
286
- classes.set(id, { id, name: symbol.name, parent: null, open: true });
247
+ classes.set(id, { id, name: symbol.name, parent: null, open: true, fields: [] });
287
248
  const base = classNode.heritageClauses
288
249
  ?.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword)
289
250
  ?.types.at(0)?.expression;
290
251
  const parent = base === undefined || depth >= MAX_CLASS_DEPTH ? undefined : classRef(base, depth + 1);
252
+ const open = base !== undefined && parent === undefined;
253
+ const written = base === undefined ? undefined : writtenName(base);
254
+ // A package's class whose parent is not followed: the `facts` key a hint names it by.
255
+ const key = open && !types.isOwnFile(declaration.file)
256
+ ? factKeyOf(types, declaration, node)
257
+ : undefined;
291
258
  classes.set(id, {
292
259
  id,
293
260
  name: symbol.name,
294
261
  parent: parent ?? null,
295
- open: base !== undefined && parent === undefined,
262
+ open,
263
+ fields: fieldsOf(classNode),
264
+ ...(open && written !== undefined ? { declared: written } : {}),
265
+ ...(key === undefined ? {} : { key }),
296
266
  });
297
267
  }
298
268
  return id;
299
269
  };
270
+ /**
271
+ * A package's classes a call gives whose parents are not read yet (named only by a facts line,
272
+ * or extending what its source does not resolve): the parent its declarations state, so
273
+ * `EntityNotFoundError → TypeORMError → Error` is known without a facts line. A class whose
274
+ * declarations do not say stays open, with the package's version for the hint.
275
+ */
276
+ const declaredParents = (packageName, ids, facts) => {
277
+ const prefix = packageClassId(packageName, "");
278
+ const queue = [...ids];
279
+ const seen = new Set();
280
+ for (let id = queue.shift(); id !== undefined; id = queue.shift()) {
281
+ if (seen.has(id) || !id.startsWith(prefix))
282
+ continue;
283
+ seen.add(id);
284
+ const known = classes.get(id);
285
+ if (known !== undefined && !known.open) {
286
+ if (known.parent !== null)
287
+ queue.push(known.parent);
288
+ continue;
289
+ }
290
+ const name = known?.name ?? id.slice(prefix.length);
291
+ const declaration = types.exportedClassOf(packageName, name);
292
+ const declaredName = declaration !== undefined &&
293
+ (ts.isClassDeclaration(declaration.node) || ts.isClassExpression(declaration.node))
294
+ ? declaration.node.name
295
+ : undefined;
296
+ if (declaredName !== undefined && classIdOf(types, declaredName) === id) {
297
+ classes.delete(id);
298
+ const read = classRef(declaredName) === id ? classes.get(id) : undefined;
299
+ if (read !== undefined && !read.open) {
300
+ if (read.parent !== null)
301
+ queue.push(read.parent);
302
+ continue;
303
+ }
304
+ }
305
+ const now = classes.get(id);
306
+ const declared = now?.declared ?? known?.declared;
307
+ // Named from its declaration when there is one; with none, no key can name it.
308
+ const key = now?.key ??
309
+ known?.key ??
310
+ (declaration === undefined || declaredName === undefined
311
+ ? undefined
312
+ : factKeyOf(types, declaration, declaredName));
313
+ classes.set(id, {
314
+ id,
315
+ name,
316
+ parent: null,
317
+ open: true,
318
+ fields: now?.fields ?? known?.fields ?? [],
319
+ ...(declared === undefined ? {} : { declared }),
320
+ ...(facts.version === "" ? {} : { version: facts.version }),
321
+ ...(key === undefined ? {} : { key }),
322
+ });
323
+ }
324
+ };
300
325
  // ---- leaves ---------------------------------------------------------------------------------
301
326
  const unknownLeaf = (reason, text, node) => ({
302
327
  k: "unknown",
@@ -305,38 +330,66 @@ export function summarizeThrows(ctx, packages) {
305
330
  at: spanOf(node),
306
331
  });
307
332
  const classLeaves = (ids, text, node) => ids.map((id) => ({ k: "class", id, text, at: spanOf(node) }));
333
+ /** The classes a call into the platform throws (`new URL(…)`, `JSON.parse(…)`). */
334
+ const platformLeaves = (ids, text, node) => ids.map((id) => ({ k: "class", id, text, at: spanOf(node), platform: true }));
308
335
  /**
309
336
  * A call into a package, by its facts: what it throws where it is called (`sync`) and what its
310
- * promise rejects with. Undefined for one of React's hooks that throw nothing.
337
+ * promise rejects with. Undefined for a call the facts say throws nothing (`QuietCall`).
338
+ *
339
+ * The config's or a pack's `facts` for the function are taken as they are; without a line it is
340
+ * unknown. A `@throws` on its declaration is read only when the declaration is the project's own
341
+ * (same package as this file): a package's doc comments are not its contract (0041).
311
342
  */
312
343
  const packageLeaves = (resolved, text, node) => {
313
- if (isNonThrowingReactHook(resolved.name, text))
344
+ const target = ts.isPropertyAccessExpression(node) ? node.expression : undefined;
345
+ const call = {
346
+ ...resolved,
347
+ through: node,
348
+ ...(target === undefined
349
+ ? {}
350
+ : {
351
+ receiverClass: (names) => types.genericBaseOf(target, resolved.name, names)?.name,
352
+ }),
353
+ };
354
+ if (quiet(call))
314
355
  return undefined;
315
- // A declaration whose JSDoc was read (a project's own `.d.ts`): what its `@throws` says.
316
- const tagged = taggedLeaves(resolved.node, text, node);
317
- if (tagged !== undefined)
318
- return tagged;
319
- if (packages === undefined) {
320
- const leaf = unknownLeaf(`package ${resolved.name}`, text, node);
321
- return { sync: [leaf], promise: [leaf] };
356
+ // A declaration of the project's own (a `.d.ts`, a `declare module`): its tag is the
357
+ // developer's statement, taken as it is.
358
+ if (types.packageOf(resolved.file) === types.packageOf(sourceFile.fileName)) {
359
+ const doc = taggedOf(resolved.node, text, node);
360
+ if (doc !== undefined) {
361
+ const leaves = [...classLeaves(doc.classes, `${text}(…)`, node), ...doc.leaves];
362
+ return doc.promise ? { sync: [], promise: leaves } : { sync: leaves, promise: [] };
363
+ }
322
364
  }
323
- const found = packages(resolved);
365
+ const found = packages(call);
324
366
  for (const fact of found.classes)
325
367
  if (!classes.has(fact.id))
326
368
  classes.set(fact.id, fact);
327
- const at = spanOf(node);
328
369
  const { facts } = found;
370
+ declaredParents(resolved.name, [...facts.throws.classes, ...facts.rejects.classes], facts);
371
+ const at = spanOf(node);
329
372
  const side = (part) => facts[part].classes.length > 0 || facts[part].unknown.length > 0
330
- ? [{ k: "package", facts, part, text, at }]
373
+ ? [
374
+ {
375
+ k: "package",
376
+ facts,
377
+ part,
378
+ text,
379
+ at,
380
+ ...(resolved.platform ? { platform: true } : {}),
381
+ },
382
+ ]
331
383
  : [];
332
384
  return { sync: side("throws"), promise: side("rejects") };
333
385
  };
334
386
  /**
335
- * What a declaration without a body says it throws with `@throws` (on any of its overloads), as
336
- * leaves at the call; undefined without a tag. On a function that returns a promise the tag
337
- * is what the promise rejects with.
387
+ * What a declaration without a body says it throws with `@throws` (on any of its overloads):
388
+ * its classes, and a leaf at the call for each entry that names no class it finds or has no
389
+ * type. Undefined without a tag. On a function that returns a promise the tag is what the
390
+ * promise rejects with.
338
391
  */
339
- const taggedLeaves = (declaration, text, at) => {
392
+ const taggedOf = (declaration, text, at) => {
340
393
  const signatures = ts.isFunctionDeclaration(declaration) || ts.isMethodDeclaration(declaration)
341
394
  ? signaturesOf(declaration)
342
395
  : [];
@@ -344,28 +397,35 @@ export function summarizeThrows(ctx, packages) {
344
397
  if (doc === null)
345
398
  return undefined;
346
399
  const span = spanOf(at);
347
- const leaves = [
348
- ...doc.entries.map((entry) => entry.id === null
349
- ? {
400
+ return {
401
+ promise: ts.isFunctionLike(declaration) && types.returnsPromiseLike(declaration),
402
+ classes: doc.entries.flatMap((entry) => (entry.id === null ? [] : [entry.id])),
403
+ leaves: [
404
+ ...doc.entries.flatMap((entry) => entry.id === null
405
+ ? [
406
+ {
407
+ k: "unknown",
408
+ reason: "`@throws` names a class that is not found",
409
+ text: entry.name,
410
+ at: span,
411
+ },
412
+ ]
413
+ : []),
414
+ ...doc.untyped.map(() => ({
350
415
  k: "unknown",
351
- reason: "`@throws` names a class that is not found",
352
- text: entry.name,
416
+ reason: "`@throws` without a type",
417
+ text: "@throws",
353
418
  at: span,
354
- }
355
- : { k: "class", id: entry.id, text: `${text}(…)`, at: span }),
356
- ...doc.untyped.map(() => ({
357
- k: "unknown",
358
- reason: "`@throws` without a type",
359
- text: "@throws",
360
- at: span,
361
- })),
362
- ];
363
- return ts.isFunctionLike(declaration) && types.returnsPromiseLike(declaration)
364
- ? { sync: [], promise: leaves }
365
- : { sync: leaves, promise: [] };
419
+ })),
420
+ ],
421
+ };
366
422
  };
367
423
  /** The leaf of a name that cannot be followed, by what it resolved to. */
368
424
  const unresolvedLeaf = (resolved, text, node) => {
425
+ // A callee typed `any` (an untyped `require`): no type states what it is, so no line can.
426
+ if ((resolved === undefined || resolved.kind === "other") && types.hasNoType(node)) {
427
+ return unknownLeaf(REASON.untyped, text, node);
428
+ }
369
429
  switch (resolved?.kind) {
370
430
  case undefined:
371
431
  return unknownLeaf(REASON.noDeclaration, text, node);
@@ -375,7 +435,7 @@ export function summarizeThrows(ctx, packages) {
375
435
  return unknownLeaf(REASON.callback, text, node);
376
436
  case "package":
377
437
  // Handled by `packageLeaves` where a call can be one.
378
- return isNonThrowingReactHook(resolved.name, text)
438
+ return quiet({ node: resolved.node, through: node })
379
439
  ? undefined
380
440
  : unknownLeaf(`package ${resolved.name}`, text, node);
381
441
  case "other":
@@ -630,7 +690,7 @@ export function summarizeThrows(ctx, packages) {
630
690
  return [];
631
691
  }
632
692
  const name = calledName(callee);
633
- const text = textOf(callee, sourceFile);
693
+ const text = calleeTextOf(callee, sourceFile);
634
694
  if (name === undefined) {
635
695
  expression(callee, walk);
636
696
  evaluateArguments(node, walk);
@@ -695,7 +755,7 @@ export function summarizeThrows(ctx, packages) {
695
755
  return [];
696
756
  case "any":
697
757
  elements(first, walk);
698
- return classLeaves([ANY_REJECTS], `${text}(…)`, node);
758
+ return platformLeaves([ANY_REJECTS], `${text}(…)`, node);
699
759
  case "reject":
700
760
  return first === undefined ? [] : thrown(first, walk);
701
761
  case "resolve":
@@ -726,11 +786,11 @@ export function summarizeThrows(ctx, packages) {
726
786
  }
727
787
  }
728
788
  if (key === "fetch") {
729
- return classLeaves(signalOf(args[1]) ? [...FETCH_REJECTS, FETCH_ABORT] : FETCH_REJECTS, `${text}(…)`, node);
789
+ return platformLeaves(signalOf(args[1]) ? [...FETCH_REJECTS, FETCH_ABORT] : FETCH_REJECTS, `${text}(…)`, node);
730
790
  }
731
791
  const rejects = REJECTING_MEMBERS.get(key);
732
792
  if (rejects !== undefined)
733
- return classLeaves(rejects, `${text}(…)`, node);
793
+ return platformLeaves(rejects, `${text}(…)`, node);
734
794
  const throws = resolved.owner === null
735
795
  ? (THROWING_GLOBALS.get(key) ??
736
796
  (key === "Array" && args.length === 1 ? ARRAY_LENGTH : []))
@@ -738,7 +798,7 @@ export function summarizeThrows(ctx, packages) {
738
798
  key === "Number.toString" && args.length === 0
739
799
  ? []
740
800
  : (THROWING_MEMBERS.get(key) ?? []);
741
- walk.sink.push(...classLeaves(throws, `${text}(…)`, node));
801
+ walk.sink.push(...platformLeaves(throws, `${text}(…)`, node));
742
802
  return mapping && MAPPING_METHODS.has(key) ? mapped : [];
743
803
  }
744
804
  if (receiver !== undefined)
@@ -803,7 +863,7 @@ export function summarizeThrows(ctx, packages) {
803
863
  const construct = (node, walk) => {
804
864
  const callee = stripParentheses(node.expression);
805
865
  const name = calledName(callee);
806
- const text = textOf(callee, sourceFile);
866
+ const text = calleeTextOf(callee, sourceFile);
807
867
  const args = node.arguments ?? [];
808
868
  if (name === undefined) {
809
869
  expression(callee, walk);
@@ -830,7 +890,7 @@ export function summarizeThrows(ctx, packages) {
830
890
  ? []
831
891
  : (THROWING_CONSTRUCTORS.get(resolved.name) ??
832
892
  (resolved.name === "Array" && args.length === 1 ? ARRAY_LENGTH : []));
833
- walk.sink.push(...classLeaves(throws, `new ${text}(…)`, node));
893
+ walk.sink.push(...platformLeaves(throws, `new ${text}(…)`, node));
834
894
  if (!DEFERRED_CALLERS.has(resolved.name)) {
835
895
  walk.sink.push(...mayRun(args, `new ${text}`, callee, walk));
836
896
  }
@@ -965,7 +1025,9 @@ export function summarizeThrows(ctx, packages) {
965
1025
  walk.sink.push(tryOf(node, walk.scope));
966
1026
  }
967
1027
  else if (ts.isThrowStatement(node)) {
968
- walk.sink.push(...thrown(node.expression, walk));
1028
+ // A `throw` no value can reach (an exhaustive switch's `default`) throws nothing.
1029
+ if (!cannotRun(types, node))
1030
+ walk.sink.push(...thrown(node.expression, walk));
969
1031
  }
970
1032
  else if (ts.isReturnStatement(node)) {
971
1033
  if (node.expression !== undefined) {
@@ -1246,7 +1308,7 @@ export function summarizeThrows(ctx, packages) {
1246
1308
  const id = ts.isTypeReferenceNode(type) ? classRef(type.typeName) : undefined;
1247
1309
  entries.push({
1248
1310
  id: id ?? null,
1249
- name: shorten(oneLine(type.getText(sourceFile)), TEXT_MAX),
1311
+ name: shorten(codeOnOneLine(type.getText(sourceFile)), TEXT_MAX),
1250
1312
  at: spanOf(type),
1251
1313
  });
1252
1314
  }
@@ -1390,7 +1452,10 @@ function resolveWith(types, name) {
1390
1452
  return undefined;
1391
1453
  const node = declaration.node;
1392
1454
  if (!types.isOwnFile(declaration.file)) {
1393
- if (isPlatform(node, declaration.file)) {
1455
+ // `declare module "x";` gives every import `any`: no type states anything, so no declaration.
1456
+ if (ts.isModuleDeclaration(node) && node.body === undefined)
1457
+ return undefined;
1458
+ if (isPlatformDeclaration(node, declaration.file)) {
1394
1459
  const parent = node.parent;
1395
1460
  const owner = (ts.isMethodSignature(node) || ts.isPropertySignature(node)) &&
1396
1461
  ts.isInterfaceDeclaration(parent)
@@ -1398,14 +1463,66 @@ function resolveWith(types, name) {
1398
1463
  : null;
1399
1464
  return { kind: "platform", owner, name: symbol.name };
1400
1465
  }
1466
+ // Outside the project and of no package stateward can name: as a name with no declaration.
1467
+ const packageName = declaredPackageOf(types, declaration);
1468
+ if (packageName === undefined)
1469
+ return undefined;
1401
1470
  return {
1402
1471
  kind: "package",
1403
- name: ambientModuleName(node) ?? packageNameOf(types, declaration.file),
1472
+ name: packageName,
1404
1473
  id: symbol.id,
1405
1474
  node,
1406
1475
  file: declaration.file,
1476
+ platform: types.isPlatformName(name),
1407
1477
  };
1408
1478
  }
1409
1479
  return ownKind(node, symbol.id);
1410
1480
  }
1481
+ /**
1482
+ * The instance properties a class declares itself, sorted: property declarations (not static),
1483
+ * parameter properties, and `this.x = …` statements of its constructor.
1484
+ */
1485
+ /** The name an `extends` expression ends in (`TypeORMError_1.TypeORMError`), else undefined. */
1486
+ function writtenName(base) {
1487
+ const value = stripParentheses(base);
1488
+ if (ts.isIdentifier(value))
1489
+ return value.text;
1490
+ return ts.isPropertyAccessExpression(value) ? value.name.text : undefined;
1491
+ }
1492
+ function fieldsOf(node) {
1493
+ const names = new Set();
1494
+ const isStatic = (member) => (ts.getCombinedModifierFlags(member) & ts.ModifierFlags.Static) !== 0;
1495
+ for (const member of node.members) {
1496
+ if (ts.isPropertyDeclaration(member) && !isStatic(member)) {
1497
+ const name = memberName(member.name);
1498
+ if (name !== undefined)
1499
+ names.add(name);
1500
+ }
1501
+ if (ts.isConstructorDeclaration(member)) {
1502
+ for (const parameter of member.parameters) {
1503
+ if (ts.isParameterPropertyDeclaration(parameter, member) &&
1504
+ ts.isIdentifier(parameter.name)) {
1505
+ names.add(parameter.name.text);
1506
+ }
1507
+ }
1508
+ for (const statement of member.body?.statements ?? []) {
1509
+ if (!ts.isExpressionStatement(statement))
1510
+ continue;
1511
+ const expression = statement.expression;
1512
+ if (ts.isBinaryExpression(expression) &&
1513
+ expression.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
1514
+ ts.isPropertyAccessExpression(expression.left) &&
1515
+ expression.left.expression.kind === ts.SyntaxKind.ThisKeyword) {
1516
+ names.add(expression.left.name.text);
1517
+ }
1518
+ }
1519
+ }
1520
+ }
1521
+ return [...names].sort();
1522
+ }
1523
+ function memberName(name) {
1524
+ if (ts.isIdentifier(name) || ts.isPrivateIdentifier(name))
1525
+ return name.text;
1526
+ return ts.isStringLiteral(name) || ts.isNumericLiteral(name) ? name.text : undefined;
1527
+ }
1411
1528
  //# sourceMappingURL=summarize.js.map
@@ -1,4 +1,7 @@
1
- import type { FactSource, Json } from "@stateward/core";
1
+ import type { FactKey, FactSide, FactSource, Json } from "@stateward/core";
2
+ /** Why a package's function is not (fully) known: fixed strings, shown as they are. */
3
+ export declare const UNKNOWN_REASONS: readonly ["its errors are not stated", "its `facts` line states no `rejects`, the kind its promise needs"];
4
+ export type UnknownReason = (typeof UNKNOWN_REASONS)[number];
2
5
  /**
3
6
  * The throws facts of one file: JSON, so the engine can cache it
4
7
  * under the file's dependency fingerprint. Every id is a qualified id (`src/api.ts#Api.get`) or,
@@ -17,6 +20,24 @@ export type ClassFact = {
17
20
  readonly name: string;
18
21
  readonly parent: string | null;
19
22
  readonly open: boolean;
23
+ /**
24
+ * The instance properties the class itself declares, sorted: property declarations, parameter
25
+ * properties, and `this.x = …` in its constructor. A reading pack decides what a field means
26
+ * (NestJS reads `statusCode`).
27
+ */
28
+ readonly fields: readonly string[];
29
+ /**
30
+ * With `open`: the name the declaration writes after `extends` when it does not resolve
31
+ * (`TypeORMError`), so a hint can name it.
32
+ */
33
+ readonly declared?: string;
34
+ /** With `open`, for a package's class: the package's version that runs, when it is known. */
35
+ readonly version?: string;
36
+ /**
37
+ * With `open`, for a package's class: the `facts` key that names it (core's `factKeyOf`, from its
38
+ * declaration); absent when its declaration is not found, so no line can name it.
39
+ */
40
+ readonly key?: FactKey;
20
41
  };
21
42
  /** Which part of a called function's result a call adds: what it throws, rejects, or both. */
22
43
  export type Part = "throws" | "rejects" | "both";
@@ -34,20 +55,31 @@ export type PackageFacts = {
34
55
  readonly name: string;
35
56
  /** "" when the package's version cannot be read. */
36
57
  readonly version: string;
37
- /** The function's path in the package's types: `remote`, `Client.get`. */
38
- readonly fn: string;
58
+ /**
59
+ * The `facts` key that names the function (core's `factKeyOf`): its module as the file imports
60
+ * it (`typeorm`, `node:path`), its name there and the version a line is checked against; or
61
+ * `unnamed`, the call as written and why no key can name it.
62
+ */
63
+ readonly key: FactKey;
64
+ /** What a line for the function states: `rejects` when the call gives a promise (`factSideOf`). */
65
+ readonly side: FactSide;
39
66
  readonly source: FactSource;
40
67
  readonly throws: Side;
41
68
  readonly rejects: Side;
42
69
  };
43
70
  /** Something that adds to what the code around it throws. */
44
71
  export type Leaf =
45
- /** `throw new X(…)`, or a built-in that throws X; `text` is the call that does, null for a `throw`. */
72
+ /**
73
+ * `throw new X(…)`, or a built-in that throws X; `text` is the call that does, null for a `throw`.
74
+ * `platform`: the call is into the platform (TypeScript's lib, Node's types, the config's
75
+ * `platform`).
76
+ */
46
77
  {
47
78
  readonly k: "class";
48
79
  readonly id: string;
49
80
  readonly text: string | null;
50
81
  readonly at: Span;
82
+ readonly platform?: true;
51
83
  }
52
84
  /** What cannot be followed, and why (`no declaration`, `callback of unknown origin`, …). */
53
85
  | {
@@ -75,13 +107,17 @@ export type Leaf =
75
107
  readonly text: string;
76
108
  readonly at: Span;
77
109
  }
78
- /** A call into a package: adds `part` of its facts (`throws` where it is called, `rejects` when awaited). */
110
+ /**
111
+ * A call into a package: adds `part` of its facts (`throws` where it is called, `rejects` when
112
+ * awaited). `platform`: the package is the platform's (a Node module, the config's `platform`).
113
+ */
79
114
  | {
80
115
  readonly k: "package";
81
116
  readonly facts: PackageFacts;
82
117
  readonly part: "throws" | "rejects";
83
118
  readonly text: string;
84
119
  readonly at: Span;
120
+ readonly platform?: true;
85
121
  }
86
122
  /**
87
123
  * `throw e` of the variable of the catch that starts at `of`: rethrows what reaches it there.