@stateward/plugin-errors 1.2.2 → 1.3.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.
Files changed (54) 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 +7 -4
  6. package/dist/finally-can-throw.js +3 -3
  7. package/dist/hover.js +6 -9
  8. package/dist/index.d.ts +3 -1
  9. package/dist/index.js +1 -0
  10. package/dist/inference/analysis.d.ts +109 -0
  11. package/dist/inference/analysis.js +170 -0
  12. package/dist/inference/builtins.d.ts +0 -2
  13. package/dist/inference/builtins.js +0 -33
  14. package/dist/inference/deps.d.ts +13 -2
  15. package/dist/inference/deps.js +24 -13
  16. package/dist/inference/needs.d.ts +16 -0
  17. package/dist/inference/needs.js +27 -0
  18. package/dist/inference/packages.d.ts +24 -32
  19. package/dist/inference/packages.js +156 -680
  20. package/dist/inference/project.d.ts +4 -4
  21. package/dist/inference/project.js +6 -17
  22. package/dist/inference/solve.d.ts +29 -4
  23. package/dist/inference/solve.js +42 -12
  24. package/dist/inference/summarize.d.ts +27 -15
  25. package/dist/inference/summarize.js +247 -130
  26. package/dist/inference/summary.d.ts +41 -5
  27. package/dist/inference/summary.js +84 -12
  28. package/dist/inference/unreachable.d.ts +5 -0
  29. package/dist/inference/unreachable.js +160 -0
  30. package/dist/inlay.d.ts +1 -5
  31. package/dist/inlay.js +1 -12
  32. package/dist/must-handle-error.d.ts +4 -3
  33. package/dist/must-handle-error.js +10 -13
  34. package/dist/plugin.d.ts +12 -2
  35. package/dist/plugin.js +4 -2
  36. package/dist/react/boundaries.d.ts +9 -7
  37. package/dist/react/boundaries.js +16 -24
  38. package/dist/react/tree.js +8 -4
  39. package/dist/refactors.js +1 -1
  40. package/dist/render-throw-without-boundary.d.ts +13 -4
  41. package/dist/render-throw-without-boundary.js +4 -2
  42. package/dist/result-ignored.d.ts +4 -1
  43. package/dist/result-type.js +2 -2
  44. package/dist/result-unchecked.d.ts +4 -1
  45. package/dist/result-unchecked.js +139 -61
  46. package/dist/rules.meta.d.ts +26 -14
  47. package/dist/rules.meta.js +18 -14
  48. package/dist/syntax.d.ts +0 -13
  49. package/dist/syntax.js +0 -79
  50. package/dist/text.d.ts +5 -2
  51. package/dist/text.js +11 -5
  52. package/dist/throws-doc-mismatch.d.ts +2 -2
  53. package/dist/throws-doc-mismatch.js +13 -5
  54. package/package.json +2 -2
@@ -1,8 +1,8 @@
1
- import type { FileContext } from "@stateward/core";
1
+ import type { AnalysisContext } from "@stateward/core";
2
2
  import type { ThrowsSummary } from "./summary.js";
3
3
  /**
4
- * `summarizeThrows` with the facts of the packages the file calls (packages.ts), computed once
5
- * per file when its rules run one after the other.
4
+ * `summarizeThrows` with the facts of the packages the file calls (packages.ts): the file side
5
+ * of the `errors/throws` analysis, whose summaries the pack's rules read (`analysisSummaries`).
6
6
  */
7
- export declare function summarizeOnce(ctx: FileContext): ThrowsSummary;
7
+ export declare function summarizeWithPackages(ctx: AnalysisContext): ThrowsSummary;
8
8
  //# sourceMappingURL=project.d.ts.map
@@ -1,22 +1,11 @@
1
- import { packageResolver } from "./packages.js";
1
+ import { packageResolver, quietCalls } from "./packages.js";
2
2
  import { summarizeThrows } from "./summarize.js";
3
3
  /**
4
- * The last file's summary, by program: `catch-misses-error`, `throws-doc-mismatch` and the React
5
- * rules all summarize the same facts, and the engine runs a file's rules one after the other.
6
- * Only the last file is kept: kept for every file, the summaries stayed in memory for the whole
7
- * run next to the engine's own copies.
4
+ * `summarizeThrows` with the facts of the packages the file calls (packages.ts): the file side
5
+ * of the `errors/throws` analysis, whose summaries the pack's rules read (`analysisSummaries`).
8
6
  */
9
- const last = new WeakMap();
10
- /**
11
- * `summarizeThrows` with the facts of the packages the file calls (packages.ts), computed once
12
- * per file when its rules run one after the other.
13
- */
14
- export function summarizeOnce(ctx) {
15
- const known = last.get(ctx.types);
16
- if (known?.sourceFile === ctx.sourceFile)
17
- return known.summary;
18
- const summary = summarizeThrows(ctx, packageResolver(ctx));
19
- last.set(ctx.types, { sourceFile: ctx.sourceFile, summary });
20
- return summary;
7
+ export function summarizeWithPackages(ctx) {
8
+ const quiet = quietCalls(ctx);
9
+ return summarizeThrows(ctx, quiet.test, packageResolver(ctx));
21
10
  }
22
11
  //# sourceMappingURL=project.js.map
@@ -1,7 +1,12 @@
1
- import type { Json, Location } from "@stateward/core";
1
+ import type { FactNeed, Json, Location, UnnamedFact } from "@stateward/core";
2
2
  import type { ClassFact, Effect, FunctionFact, Handler, PackageFacts } from "./summary.js";
3
- /** `zod 3.23.8`, or `zod` when its version is not known. */
4
- export declare function packageLabel(facts: Pick<PackageFacts, "name" | "version">): string;
3
+ /**
4
+ * `zod 3.23.8`, or `zod` when its version is not known: the module the file imports the function
5
+ * from, with that module's version, when a `facts` key names it there; else its package. A method
6
+ * a class inherits from another package (`wrap.on` of an `Emitter`) is named by the module it is
7
+ * imported from, with that module's version, as the `facts` line to add is.
8
+ */
9
+ export declare function packageLabel(facts: Pick<PackageFacts, "name" | "key" | "version">): string;
5
10
  /** Something that cannot be followed, where it is and why: shown as `api.get: no declaration`. */
6
11
  export type Source = {
7
12
  readonly file: string;
@@ -9,8 +14,28 @@ export type Source = {
9
14
  readonly end: number;
10
15
  readonly text: string;
11
16
  readonly reason: string;
12
- /** The package whose facts this unknown comes from, when it does. */
17
+ /** The package whose facts this unknown comes from, when it does: the module of its key. */
13
18
  readonly package?: string;
19
+ /** What a `facts` line or an import does for it (`UnknownFact`). */
20
+ readonly fact?: UnknownFact;
21
+ };
22
+ /**
23
+ * What decides an unknown: `unstated`, a package call no `facts` line states, which `need`
24
+ * decides; `withheld`, a call whose line is for other versions than the project runs (`need`
25
+ * is the line for the version that runs); `unnamed`, a package call no `facts` key can name;
26
+ * `untyped`, a call whose callee is typed `any`, which an import with types would state.
27
+ */
28
+ export type UnknownFact = {
29
+ readonly kind: "unstated";
30
+ readonly need: FactNeed;
31
+ } | {
32
+ readonly kind: "withheld";
33
+ readonly need: FactNeed;
34
+ } | {
35
+ readonly kind: "unnamed";
36
+ readonly unnamed: UnnamedFact;
37
+ } | {
38
+ readonly kind: "untyped";
14
39
  };
15
40
  /**
16
41
  * A set of errors: known classes by id (true when the class surely reaches here, false when it
@@ -1,10 +1,36 @@
1
- import { createPropagationMemo, propagate } from "@stateward/core";
1
+ import { createPropagationMemo, propagate, summaryHash } from "@stateward/core";
2
2
  import { builtinParent, isBuiltin } from "./builtins.js";
3
3
  import { ANONYMOUS, REASON } from "./summarize.js";
4
- import { readSummary } from "./summary.js";
5
- /** `zod 3.23.8`, or `zod` when its version is not known. */
4
+ import { UNKNOWN_REASONS, readSummary } from "./summary.js";
5
+ /** Whether a package call's reason is one of the fixed ones (`UNKNOWN_REASONS`). */
6
+ function isFixedReason(reason) {
7
+ return UNKNOWN_REASONS.some((one) => one === reason);
8
+ }
9
+ /**
10
+ * `zod 3.23.8`, or `zod` when its version is not known: the module the file imports the function
11
+ * from, with that module's version, when a `facts` key names it there; else its package. A method
12
+ * a class inherits from another package (`wrap.on` of an `Emitter`) is named by the module it is
13
+ * imported from, with that module's version, as the `facts` line to add is.
14
+ */
6
15
  export function packageLabel(facts) {
7
- return facts.version === "" ? facts.name : `${facts.name} ${facts.version}`;
16
+ const { key } = facts;
17
+ const name = key.kind === "named" ? key.module : facts.name;
18
+ const version = key.kind === "named" ? (key.version ?? facts.version) : facts.version;
19
+ return version === "" ? name : `${name} ${version}`;
20
+ }
21
+ /** What decides an unknown of a package call, by its key and whether a line states it. */
22
+ function packageFact(facts, reason) {
23
+ const { key } = facts;
24
+ if (key.kind === "unnamed") {
25
+ return { kind: "unnamed", unnamed: { name: key.name, reason: key.reason } };
26
+ }
27
+ const need = {
28
+ module: key.module,
29
+ name: key.name,
30
+ ...(key.version === undefined ? {} : { version: key.version }),
31
+ states: [facts.side],
32
+ };
33
+ return { kind: isFixedReason(reason) ? "unstated" : "withheld", need };
8
34
  }
9
35
  /** At most this many unknown sources are kept per set; `more` says there were others. */
10
36
  const MAX_SOURCES = 50;
@@ -131,6 +157,7 @@ export function evaluate(effects, context, caught, into) {
131
157
  ...locationOf(file, effect.at),
132
158
  text: effect.text,
133
159
  reason: effect.reason,
160
+ ...(effect.reason === REASON.untyped ? { fact: { kind: "untyped" } } : {}),
134
161
  });
135
162
  break;
136
163
  case "call": {
@@ -161,8 +188,10 @@ export function evaluate(effects, context, caught, into) {
161
188
  addSource(into, {
162
189
  ...locationOf(file, effect.at),
163
190
  text: effect.text,
164
- reason: `${label}: ${reason}`,
165
- package: facts.name,
191
+ // A fixed reason names the package it is about; a facts line left out says it itself.
192
+ reason: isFixedReason(reason) ? `${label}: ${reason}` : reason,
193
+ package: facts.key.kind === "named" ? facts.key.module : facts.name,
194
+ fact: packageFact(facts, reason),
166
195
  });
167
196
  }
168
197
  break;
@@ -387,9 +416,10 @@ const memo = createPropagationMemo();
387
416
  /** One analysis per summaries map: the engine hands out the same map until they change. */
388
417
  const analyses = new WeakMap();
389
418
  /**
390
- * The summaries of the last analysis, by their JSON text. Each rule of the pack (and the hover)
391
- * receives its own parsed copy of the same facts; mapping equal texts to one object lets the
392
- * second rule, and the next full pass, find the facts unchanged by identity.
419
+ * The summaries of the last analysis, by the engine's hash of their JSON text (`summaryHash`), or
420
+ * the text itself for a value the engine did not hand out (a test's). A summary the next full
421
+ * pass computes again from an unchanged file is a new object with the same hash: mapping it to
422
+ * the last one lets the solve find the facts unchanged by identity.
393
423
  */
394
424
  let canonical = new Map();
395
425
  let last;
@@ -405,9 +435,9 @@ export function analyse(summaries) {
405
435
  const parsed = readSummary(value);
406
436
  if (parsed === undefined || typeof value !== "object" || value === null)
407
437
  continue;
408
- const text = textOf(value);
409
- const summary = canonical.get(text) ?? next.get(text) ?? parsed;
410
- next.set(text, summary);
438
+ const key = summaryHash(value) ?? textOf(value);
439
+ const summary = canonical.get(key) ?? next.get(key) ?? parsed;
440
+ next.set(key, summary);
411
441
  files.push(file);
412
442
  read.push(summary);
413
443
  }
@@ -1,5 +1,5 @@
1
1
  import ts from "@stateward/core/typescript";
2
- import type { FileContext, TypeQueries } from "@stateward/core";
2
+ import type { AnalysisContext, TypeQueries } from "@stateward/core";
3
3
  import type { ClassFact, PackageFacts, ThrowsSummary } from "./summary.js";
4
4
  /** Why something adds `unknown`, as the hover and the messages show it. */
5
5
  export declare const REASON: {
@@ -12,25 +12,38 @@ export declare const REASON: {
12
12
  readonly promise: "a promise that is not followed";
13
13
  readonly heldPromise: "promise held in a variable";
14
14
  readonly accessor: "a getter or setter runs here";
15
+ readonly untyped: "its type is `any`";
15
16
  };
16
17
  /** Why a callback handed to a call of the platform that is not in the tables adds `unknown`. */
17
18
  export declare function mayRunReason(callee: string): string;
18
19
  /** The id of a function without a name; the analysis names it by its file and offset. */
19
20
  export declare const ANONYMOUS = "";
20
- /**
21
- * The package a declaration outside the project belongs to: by its node_modules folder, or, for a
22
- * package linked from elsewhere (a workspace package, whose real path has no node_modules), by
23
- * the name in its package.json.
24
- */
25
- export declare function packageNameOf(types: TypeQueries, file: string): string;
26
21
  /** A package's declaration that a call reaches, for its facts (packages.ts). */
27
22
  export type PackageCall = {
28
23
  readonly name: string;
29
24
  readonly id: string;
30
25
  readonly node: ts.Node;
31
26
  readonly file: string;
27
+ /**
28
+ * What the call is made through (`repo.save`, `join`): the module the file imports it from, and
29
+ * whether it is called or constructed, decide its `facts` line.
30
+ */
31
+ readonly through: ts.Expression;
32
+ /**
33
+ * For a method called on a value (`repo.save()`): the nearest of `names`, classes or interfaces
34
+ * of the call's package, that the value's type is or extends; undefined when it is none of them,
35
+ * or its type is not read. Absent for a call with no receiver.
36
+ */
37
+ readonly receiverClass?: (names: ReadonlySet<string>) => string | undefined;
32
38
  };
33
- /** Finds what a call into a package throws; its classes, when it read some. */
39
+ /**
40
+ * Whether the facts say a call into a package throws and rejects with nothing (`throws: []`), as
41
+ * the React pack says of React's hooks: the call then adds nothing, not even a package effect.
42
+ */
43
+ export type QuietCall = (call: Pick<PackageCall, "node"> & {
44
+ readonly through?: ts.Node | undefined;
45
+ }) => boolean;
46
+ /** Finds what a call into a package throws, from its `facts` line; the classes the line states. */
34
47
  export type PackageResolver = (call: PackageCall) => {
35
48
  readonly facts: PackageFacts;
36
49
  readonly classes: readonly ClassFact[];
@@ -38,14 +51,13 @@ export type PackageResolver = (call: PackageCall) => {
38
51
  /** `package:zod#ZodError`: a package's class, by its package and name, the same from its types and its source. */
39
52
  export declare function packageClassId(name: string, className: string): string;
40
53
  /**
41
- * The id a class named in code has in the summaries (`builtin:TypeError`, `package:zod#ZodError`,
42
- * or the qualified id of a class of the project), without its facts; undefined when the name
43
- * resolves to nothing.
54
+ * The id `errors/throws` gives the error class a name refers to (core's `classIdOf`): undefined
55
+ * for a name of the platform that is not one of its error classes.
44
56
  */
45
- export declare function classIdOf(types: TypeQueries, name: ts.Node): string | undefined;
57
+ export declare function errorClassIdOf(types: TypeQueries, name: ts.Node): string | undefined;
46
58
  /**
47
- * The throws facts of the file (see summary.ts). With `packages`, a call into a package carries
48
- * its facts; without (a package's own source), it is unknown.
59
+ * The throws facts of the file (see summary.ts): a call into a package carries its facts
60
+ * (`packages`).
49
61
  */
50
- export declare function summarizeThrows(ctx: FileContext, packages?: PackageResolver): ThrowsSummary;
62
+ export declare function summarizeThrows(ctx: Pick<AnalysisContext, "sourceFile" | "types">, quiet: QuietCall, packages: PackageResolver): ThrowsSummary;
51
63
  //# sourceMappingURL=summarize.d.ts.map