@cplieger/deadset-ts 8.0.0-dev.14 → 8.0.0-dev.16

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 (149) hide show
  1. package/dist/src/alias-chain.js +2 -2
  2. package/dist/src/analysis.js +43 -38
  3. package/dist/src/commands/read.js +6 -7
  4. package/dist/src/conformance-record.js +1 -1
  5. package/dist/src/consumers.js +4 -2
  6. package/dist/src/contract/config.generated.js +1 -1
  7. package/dist/src/contract/contract.generated.js +120 -2
  8. package/dist/src/dependencies.js +81 -49
  9. package/dist/src/entries/build-scripts.js +222 -0
  10. package/dist/src/{configuration-files.js → entries/configuration-files.js} +20 -58
  11. package/dist/src/{convention-rows.js → entries/convention-rows.js} +120 -27
  12. package/dist/src/entries/conventions.js +739 -0
  13. package/dist/src/{html-entries.js → entries/html-entries.js} +3 -3
  14. package/dist/src/{mdx-entries.js → entries/mdx-entries.js} +2 -2
  15. package/dist/src/entries/run-directories.js +77 -0
  16. package/dist/src/entries/sources.js +144 -0
  17. package/dist/src/entries/static-module.js +669 -0
  18. package/dist/src/entries/test-runners.js +684 -0
  19. package/dist/src/{workflow-steps.js → entries/workflow-steps.js} +18 -8
  20. package/dist/src/extensions.js +37 -0
  21. package/dist/src/facts.js +38 -0
  22. package/dist/src/file-facts.js +1 -1
  23. package/dist/src/findings/emitters.js +5 -8
  24. package/dist/src/findings/self-check.js +36 -20
  25. package/dist/src/findings-pass.js +5 -6
  26. package/dist/src/framework-lifecycle.js +1 -1
  27. package/dist/src/fs/manifest.js +15 -20
  28. package/dist/src/generated-file.js +172 -29
  29. package/dist/src/graph.js +2 -5
  30. package/dist/src/index.js +1 -1
  31. package/dist/src/injection-container.js +1 -1
  32. package/dist/src/inventory/commonjs.js +507 -0
  33. package/dist/src/inventory/enumerate.js +167 -0
  34. package/dist/src/inventory/syntax.js +236 -0
  35. package/dist/src/inventory.js +305 -705
  36. package/dist/src/javascript-file.js +26 -11
  37. package/dist/src/load/classify.js +28 -9
  38. package/dist/src/load/diagnostics.js +8 -1
  39. package/dist/src/load/discover.js +16 -8
  40. package/dist/src/load/package-scope.js +56 -0
  41. package/dist/src/load/plan.js +2 -2
  42. package/dist/src/load/synthesize.js +5 -5
  43. package/dist/src/load/workspace-manifests.js +1 -1
  44. package/dist/src/matrix.js +6 -13
  45. package/dist/src/outside-tests.js +1 -1
  46. package/dist/src/paths.js +16 -0
  47. package/dist/src/platform-calls.js +78 -0
  48. package/dist/src/platform-symbols.js +140 -0
  49. package/dist/src/position.js +41 -0
  50. package/dist/src/ref.js +11 -20
  51. package/dist/src/references.js +281 -50
  52. package/dist/src/resolve.js +1 -1
  53. package/dist/src/{configured-declarations.js → roots/configured-declarations.js} +4 -4
  54. package/dist/src/{entry-point-gaps.js → roots/entry-point-gaps.js} +5 -8
  55. package/dist/src/{manifest.js → roots/manifest.js} +4 -4
  56. package/dist/src/roots/published-reach.js +412 -0
  57. package/dist/src/roots/root-set.js +489 -0
  58. package/dist/src/roots/unseen-loads.js +208 -0
  59. package/dist/src/serialization-contract.js +1 -1
  60. package/dist/src/session.js +3 -0
  61. package/dist/src/source-files.js +5 -9
  62. package/dist/src/suppress-inline.js +33 -81
  63. package/dist/src/suppress.js +30 -32
  64. package/dist/src/test-support.js +4 -2
  65. package/dist/src/verbs/analyze.js +13 -1
  66. package/dist/src/verbs/describe.js +5 -3
  67. package/jsr.json +1 -1
  68. package/package.json +3 -3
  69. package/src/alias-chain.ts +2 -2
  70. package/src/analysis.ts +77 -53
  71. package/src/automatic-types.ts +1 -1
  72. package/src/commands/read.ts +6 -8
  73. package/src/conformance-record.ts +1 -1
  74. package/src/consumers.ts +5 -1
  75. package/src/contract/config.generated.ts +1 -1
  76. package/src/contract/contract.generated.ts +139 -2
  77. package/src/dependencies.ts +98 -50
  78. package/src/entries/build-scripts.ts +292 -0
  79. package/src/{configuration-files.ts → entries/configuration-files.ts} +23 -75
  80. package/src/{convention-rows.ts → entries/convention-rows.ts} +167 -28
  81. package/src/{conventions.ts → entries/conventions.ts} +347 -522
  82. package/src/{html-entries.ts → entries/html-entries.ts} +5 -5
  83. package/src/{mdx-entries.ts → entries/mdx-entries.ts} +4 -4
  84. package/src/entries/run-directories.ts +108 -0
  85. package/src/entries/sources.ts +266 -0
  86. package/src/entries/static-module.ts +857 -0
  87. package/src/entries/test-runners.ts +868 -0
  88. package/src/{workflow-steps.ts → entries/workflow-steps.ts} +29 -11
  89. package/src/exempt.ts +3 -2
  90. package/src/extensions.ts +45 -0
  91. package/src/facts.ts +44 -0
  92. package/src/file-facts.ts +2 -2
  93. package/src/findings/emitters.ts +14 -10
  94. package/src/findings/self-check.ts +41 -22
  95. package/src/findings/visibility-narrowing.ts +1 -1
  96. package/src/findings-pass.ts +5 -12
  97. package/src/framework-lifecycle.ts +1 -1
  98. package/src/fs/manifest.ts +25 -21
  99. package/src/generated-file.ts +240 -36
  100. package/src/graph.ts +3 -6
  101. package/src/index.ts +1 -1
  102. package/src/injection-container.ts +1 -1
  103. package/src/inventory/commonjs.ts +696 -0
  104. package/src/inventory/enumerate.ts +203 -0
  105. package/src/inventory/syntax.ts +311 -0
  106. package/src/inventory.ts +424 -878
  107. package/src/javascript-file.ts +29 -12
  108. package/src/load/classify.ts +49 -13
  109. package/src/load/diagnostics.ts +11 -1
  110. package/src/load/discover.ts +28 -12
  111. package/src/load/package-scope.ts +74 -0
  112. package/src/load/plan.ts +11 -2
  113. package/src/load/synthesize.ts +6 -6
  114. package/src/load/test-globals.ts +1 -1
  115. package/src/load/workspace-manifests.ts +1 -1
  116. package/src/load/workspace-run.ts +1 -1
  117. package/src/matrix.ts +7 -14
  118. package/src/outside-tests.ts +1 -1
  119. package/src/paths.ts +18 -0
  120. package/src/platform-calls.ts +119 -0
  121. package/src/platform-symbols.ts +205 -0
  122. package/src/position.ts +42 -0
  123. package/src/ref.ts +11 -20
  124. package/src/references.ts +383 -73
  125. package/src/report.ts +1 -1
  126. package/src/resolve.ts +1 -1
  127. package/src/{configured-declarations.ts → roots/configured-declarations.ts} +7 -7
  128. package/src/{entry-point-gaps.ts → roots/entry-point-gaps.ts} +5 -8
  129. package/src/{manifest.ts → roots/manifest.ts} +5 -5
  130. package/src/roots/published-reach.ts +481 -0
  131. package/src/{roots.ts → roots/root-set.ts} +203 -630
  132. package/src/roots/unseen-loads.ts +307 -0
  133. package/src/serialization-contract.ts +1 -1
  134. package/src/session.ts +17 -1
  135. package/src/source-files.ts +5 -10
  136. package/src/suppress-inline.ts +53 -89
  137. package/src/suppress.ts +48 -33
  138. package/src/test-support.ts +8 -3
  139. package/src/verbs/analyze.ts +17 -4
  140. package/src/verbs/describe.ts +5 -2
  141. package/src/verbs/explain.ts +1 -1
  142. package/dist/src/build-scripts.js +0 -418
  143. package/dist/src/conventions.js +0 -907
  144. package/dist/src/entry-points.js +0 -513
  145. package/dist/src/roots.js +0 -865
  146. package/dist/src/unseen-loads.js +0 -100
  147. package/src/build-scripts.ts +0 -536
  148. package/src/entry-points.ts +0 -661
  149. package/src/unseen-loads.ts +0 -134
package/src/report.ts CHANGED
@@ -15,7 +15,7 @@ import type {
15
15
  FindingPosition,
16
16
  PositionedSymbol,
17
17
  } from "./finding.ts";
18
- import type { AppliedConvention } from "./conventions.ts";
18
+ import type { AppliedConvention } from "./entries/conventions.ts";
19
19
  import type { PassResult } from "./findings-pass.ts";
20
20
  import type { StaleSuppression } from "./findings/self-check.ts";
21
21
  import type { TestFileRule } from "./references.ts";
package/src/resolve.ts CHANGED
@@ -9,7 +9,7 @@ import {
9
9
  type ResolvedConfig,
10
10
  type Source,
11
11
  } from "./config.ts";
12
- import { CONVENTION_ROWS } from "./convention-rows.ts";
12
+ import { CONVENTION_ROWS } from "./entries/convention-rows.ts";
13
13
  import { refuseOverBound } from "./fs/document.ts";
14
14
  import {
15
15
  dotted,
@@ -17,13 +17,13 @@ import {
17
17
  type Node,
18
18
  } from "@typescript/native/ast";
19
19
  import { SymbolFlags, type NodeHandle, type Symbol as TSSymbol } from "@typescript/native/sync";
20
- import type { AliasChains } from "./alias-chain.ts";
21
- import { resolvedTargets } from "./calls.ts";
22
- import type { DeclarationEntry, ResolvedConfig } from "./config.ts";
23
- import { declaredComponent, type Inventory } from "./inventory.ts";
24
- import { must, Unanswerable } from "./query.ts";
25
- import { nameComponent } from "./ref.ts";
26
- import type { ProjectView } from "./session.ts";
20
+ import type { AliasChains } from "../alias-chain.ts";
21
+ import { resolvedTargets } from "../calls.ts";
22
+ import type { DeclarationEntry, ResolvedConfig } from "../config.ts";
23
+ import { declaredComponent, type Inventory } from "../inventory.ts";
24
+ import { must, Unanswerable } from "../query.ts";
25
+ import { nameComponent } from "../ref.ts";
26
+ import type { ProjectView } from "../session.ts";
27
27
 
28
28
  /** The meanings a name of the global scope is looked up under: any of them. */
29
29
  const ANY_MEANING: SymbolFlags = SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace;
@@ -1,14 +1,11 @@
1
1
  /**
2
- * Every entry-point convention this analyzer declines, each with the tool it belongs
3
- * to and why. A file only a declined convention enters is reported as never
4
- * imported, so a project that relies on one names the file in `ts.entry_files` or
5
- * `roots.patterns`, and this list is what tells it to.
6
- *
7
- * Every row carries a reason, because the list is read where the rows are shown
8
- * one at a time: beside a finding, or in a report's declared gaps.
2
+ * Every entry-point convention this analyzer declines, each with its tool and why. A file only
3
+ * a declined convention enters is reported as never imported, so a project relying on one names
4
+ * the file in `ts.entry_files` or `roots.patterns`, and this list tells it to. Every row carries
5
+ * a reason, since rows are shown one at a time, beside a finding or in a report's declared gaps.
9
6
  */
10
7
 
11
- import { bytewise } from "./text/compare.ts";
8
+ import { bytewise } from "../text/compare.ts";
12
9
 
13
10
  /** One declined entry-point convention. */
14
11
  export interface DeclinedConvention {
@@ -5,11 +5,11 @@
5
5
  * directory's manifest, {@link packageEntries} those of the packages below the target root.
6
6
  */
7
7
 
8
- import type { Host } from "./host.ts";
9
- import { isAbsolutePath, joinPath, relativePath, resolvePath } from "./paths.ts";
10
- import { isRecord } from "./json-document.ts";
11
- import { MANIFEST, manifestAt, readJSON } from "./fs/manifest.ts";
12
- import { pathsNamed, readCommands } from "./commands/read.ts";
8
+ import type { Host } from "../host.ts";
9
+ import { isAbsolutePath, joinPath, relativePath, resolvePath } from "../paths.ts";
10
+ import { isRecord } from "../json-document.ts";
11
+ import { MANIFEST, manifestAt, readJSON } from "../fs/manifest.ts";
12
+ import { pathsNamed, readCommands } from "../commands/read.ts";
13
13
 
14
14
  /** The three members that name an entry point beside `exports` and `bin`. */
15
15
  const LEGACY_MEMBERS = ["main", "module", "types"] as const;
@@ -0,0 +1,481 @@
1
+ /**
2
+ * The published reach of a library: what a consumer outside the target can name, from the
3
+ * files a published manifest entry or a bundle hands it and the declarations a release tag
4
+ * marks, through the types each published declaration names.
5
+ */
6
+
7
+ import {
8
+ isArrowFunction,
9
+ isAssertionExpression,
10
+ isExpressionWithTypeArguments,
11
+ isFunctionLikeDeclaration,
12
+ isIdentifier,
13
+ isMethodDeclaration,
14
+ isObjectLiteralExpression,
15
+ isParenthesizedExpression,
16
+ isPropertyAssignment,
17
+ isReturnStatement,
18
+ isSatisfiesExpression,
19
+ isPropertyAccessExpression,
20
+ isQualifiedName,
21
+ isTypeQueryNode,
22
+ isTypeReferenceNode,
23
+ isPropertyDeclaration,
24
+ isVariableDeclaration,
25
+ isVariableStatement,
26
+ SyntaxKind,
27
+ type Expression,
28
+ type Node,
29
+ type ObjectLiteralExpression,
30
+ type SourceFile,
31
+ } from "@typescript/native/ast";
32
+ import { aliasChains } from "../alias-chain.ts";
33
+ import { isGlobalBlock } from "../augmentations.ts";
34
+ import { nodeKey, type Inventory, type InventorySymbol } from "../inventory.ts";
35
+ import { UNANSWERED } from "../query.ts";
36
+ import type { ProjectView } from "../session.ts";
37
+ import type { Symbol as TSSymbol, Type } from "@typescript/native/sync";
38
+
39
+ /** A release tag of a documentation comment: `@` and its name, between white space or the ends. */
40
+ const RELEASE_TAG = /(?:^|\s)@(?:public|beta|alpha)(?=\s|$)/u;
41
+
42
+ /**
43
+ * Whether the documentation comment of the statement that starts at `start`, the last
44
+ * `/** *\/` comment before it with only white space between the two, holds a release tag.
45
+ */
46
+ function releaseTagIn(text: string, from: number, start: number): boolean {
47
+ const leading = text.slice(from, start);
48
+ const open = leading.lastIndexOf("/**");
49
+ const close = open < 0 ? -1 : leading.indexOf("*/", open + 3);
50
+ if (close < 0 || leading.slice(close + 2).trim() !== "") {
51
+ return false;
52
+ }
53
+ return RELEASE_TAG.test(leading.slice(open + 3, close));
54
+ }
55
+
56
+ /** The exported top-level declarations of the program's files a release tag marks. */
57
+ export function releaseTagged(
58
+ held: Inventory,
59
+ files: { readonly byPath: ReadonlyMap<string, SourceFile> },
60
+ ): readonly string[] {
61
+ const exported = new Set(held.symbols.filter((symbol) => symbol.exported).map((one) => one.id));
62
+ const found: string[] = [];
63
+ for (const file of files.byPath.values()) {
64
+ for (const statement of file.statements) {
65
+ if (!releaseTagIn(file.text, statement.pos, statement.getStart())) {
66
+ continue;
67
+ }
68
+ const nodes: readonly Node[] = isVariableStatement(statement)
69
+ ? statement.declarationList.declarations
70
+ : [statement];
71
+ for (const node of nodes) {
72
+ const id = held.declarations.get(nodeKey(file, node));
73
+ if (id !== undefined && exported.has(id)) {
74
+ found.push(id);
75
+ }
76
+ }
77
+ }
78
+ }
79
+ return found;
80
+ }
81
+
82
+ /** The kinds of container whose export table, not its members' visibility, says what it publishes. */
83
+ const EXPORTING_CONTAINERS: ReadonlySet<string> = new Set(["file", "namespace"]);
84
+
85
+ /**
86
+ * Every declaration a consumer of the library can name, a published file itself excluded:
87
+ * what the published files export, with a namespace's exported members and any other
88
+ * declaration's public and protected ones; a declaration a published one's type names; an
89
+ * exported declaration a release tag marks; each declaration a `declare global` block holds
90
+ * in a published file or one holding a published declaration; and, as `guessed`, each
91
+ * declaration spelled as a type name the checker leaves unanswered.
92
+ */
93
+ export function publishedAPI<Brand>(
94
+ project: ProjectView<Brand>,
95
+ published: readonly SourceFile[],
96
+ exportsOf: ReadonlyMap<string, readonly string[]>,
97
+ held: Inventory,
98
+ byPath: ReadonlyMap<string, SourceFile>,
99
+ tagged: readonly string[],
100
+ ): { readonly ids: readonly string[]; readonly guessed: readonly string[] } {
101
+ const byId = new Map(held.symbols.map((symbol) => [symbol.id, symbol]));
102
+ const children = childrenOf(held);
103
+ const nodes = declarationNodes(held, byPath);
104
+ const chains = aliasChains(project, held);
105
+ const reached = new Set<string>();
106
+ const entered = new Set<string>();
107
+ const guessed = new Set<string>();
108
+ const pending: string[] = [];
109
+ const reach = (id: string): void => {
110
+ const file = byId.get(id)?.kind === "file";
111
+ if (!(file ? entered : reached).has(id)) {
112
+ (file ? entered : reached).add(id);
113
+ pending.push(id);
114
+ }
115
+ };
116
+ for (const id of tagged) {
117
+ reach(id);
118
+ }
119
+ for (const file of published) {
120
+ for (const id of exportsOf.get(file.fileName) ?? []) {
121
+ reach(id);
122
+ }
123
+ }
124
+ const files = new Set(held.symbols.filter((one) => one.kind === "file").map((one) => one.id));
125
+ const blocks = held.symbols.filter((one) => isGlobalBlock(one, held, files));
126
+ const fileOf = (id: string): string | undefined => {
127
+ let at = byId.get(id);
128
+ while (at !== undefined && at.kind !== "file") {
129
+ at = byId.get(at.parent);
130
+ }
131
+ return at?.id;
132
+ };
133
+ const publishedFiles = published.flatMap(
134
+ (file) => held.declarations.get(nodeKey(file, file)) ?? [],
135
+ );
136
+ const globalsReached = (): boolean => {
137
+ const typed = new Set([...publishedFiles, ...[...reached].flatMap((id) => fileOf(id) ?? [])]);
138
+ const before = pending.length;
139
+ for (const block of blocks.filter((one) => typed.has(one.parent))) {
140
+ reach(block.id);
141
+ for (const member of children.get(block.id) ?? []) {
142
+ if (member.kind !== "type-parameter") {
143
+ reach(member.id);
144
+ }
145
+ }
146
+ }
147
+ return pending.length > before;
148
+ };
149
+ do {
150
+ while (pending.length > 0) {
151
+ const round: string[] = [];
152
+ for (let next = pending.shift(); next !== undefined; next = pending.shift()) {
153
+ round.push(next);
154
+ const exporting = EXPORTING_CONTAINERS.has(byId.get(next)?.kind ?? "");
155
+ for (const member of children.get(next) ?? []) {
156
+ const reachable = exporting
157
+ ? member.exported
158
+ : member.visibility !== "private" && member.visibility !== "private-name";
159
+ if (member.kind !== "type-parameter" && reachable) {
160
+ reach(member.id);
161
+ }
162
+ }
163
+ }
164
+ const named = round.flatMap((id) => {
165
+ const symbol = byId.get(id);
166
+ return symbol === undefined || symbol.kind === "file" ? [] : nodes.typeNamesOf(symbol);
167
+ });
168
+ const inferred = inferredTypeSymbols(
169
+ project,
170
+ round.flatMap((id) => {
171
+ const symbol = byId.get(id);
172
+ return symbol === undefined ? [] : nodes.nodesOf(symbol);
173
+ }),
174
+ );
175
+ for (const symbol of inferred.types) {
176
+ for (const target of chains.chainOf(symbol)) {
177
+ reach(target.id);
178
+ }
179
+ }
180
+ for (const symbol of inferred.members) {
181
+ for (const target of chains.chainOf(symbol)) {
182
+ const member = byId.get(target.id);
183
+ if (member?.visibility === "public" || member?.visibility === "protected") {
184
+ reach(target.id);
185
+ }
186
+ }
187
+ }
188
+ project.symbolsAt(named.map((node) => project.handle(node))).forEach((symbol, index) => {
189
+ if (symbol === UNANSWERED) {
190
+ for (const id of held.byName.get(named[index]?.getText() ?? "") ?? []) {
191
+ guessed.add(id);
192
+ reach(id);
193
+ }
194
+ } else if (symbol !== undefined) {
195
+ for (const target of chains.chainOf(symbol)) {
196
+ reach(target.id);
197
+ }
198
+ }
199
+ });
200
+ }
201
+ } while (globalsReached());
202
+ return { ids: [...reached], guessed: [...guessed] };
203
+ }
204
+
205
+ /** The type names declarations of the inventory write, each file walked once, when first asked. */
206
+ function declarationNodes(
207
+ held: Inventory,
208
+ byPath: ReadonlyMap<string, SourceFile>,
209
+ ): {
210
+ typeNamesOf(symbol: InventorySymbol): readonly Node[];
211
+ nodesOf(symbol: InventorySymbol): readonly Node[];
212
+ } {
213
+ const walked = new Map<string, { byId: Map<string, Node[]>; nodes: Set<Node> }>();
214
+ const nodesIn = (path: string): { byId: Map<string, Node[]>; nodes: Set<Node> } => {
215
+ const known = walked.get(path);
216
+ if (known !== undefined) {
217
+ return known;
218
+ }
219
+ const found = { byId: new Map<string, Node[]>(), nodes: new Set<Node>() };
220
+ const file = byPath.get(path);
221
+ if (file !== undefined) {
222
+ const visit = (node: Node): void => {
223
+ const id = held.declarations.get(nodeKey(file, node));
224
+ if (id !== undefined) {
225
+ found.nodes.add(node);
226
+ found.byId.set(id, [...(found.byId.get(id) ?? []), node]);
227
+ }
228
+ node.forEachChild(visit);
229
+ };
230
+ visit(file);
231
+ }
232
+ walked.set(path, found);
233
+ return found;
234
+ };
235
+ return {
236
+ typeNamesOf: (symbol) => {
237
+ const { byId, nodes } = nodesIn(symbol.position.path);
238
+ return (byId.get(symbol.id) ?? []).flatMap((declaration) => typeNamesOf(declaration, nodes));
239
+ },
240
+ nodesOf: (symbol) => nodesIn(symbol.position.path).byId.get(symbol.id) ?? [],
241
+ };
242
+ }
243
+
244
+ /** How deep the type arguments and constituents of an inferred type are followed. */
245
+ const INFERRED_DEPTH = 4;
246
+
247
+ /**
248
+ * The values one function body returns, each through the type assertions around it: the
249
+ * value a caller receives carries the operand's type whatever type the assertion or the
250
+ * signature names. A nested function's returns are its own.
251
+ */
252
+ function returnedValues(body: Node): readonly Expression[] {
253
+ const found: Expression[] = [];
254
+ const add = (value: Expression): void => {
255
+ for (let at: Expression | undefined = value; at !== undefined;) {
256
+ found.push(at);
257
+ at =
258
+ isAssertionExpression(at) || isSatisfiesExpression(at) || isParenthesizedExpression(at)
259
+ ? at.expression
260
+ : undefined;
261
+ }
262
+ };
263
+ if (!isFunctionLikeDeclaration(body.parent) || body.parent.body !== body) {
264
+ return found;
265
+ }
266
+ if (isArrowFunction(body.parent) && body.kind !== SyntaxKind.Block) {
267
+ add(body as Expression);
268
+ return found;
269
+ }
270
+ const visit = (node: Node): void => {
271
+ if (isFunctionLikeDeclaration(node)) {
272
+ return;
273
+ }
274
+ if (isReturnStatement(node) && node.expression !== undefined) {
275
+ add(node.expression);
276
+ }
277
+ node.forEachChild(visit);
278
+ };
279
+ body.forEachChild(visit);
280
+ return found;
281
+ }
282
+
283
+ /** The object literal one returned value is, written there or as the initializer of a variable `body` declares. */
284
+ function returnedLiteral(value: Expression, body: Node): ObjectLiteralExpression | undefined {
285
+ if (isObjectLiteralExpression(value)) {
286
+ return value;
287
+ }
288
+ if (!isIdentifier(value)) {
289
+ return undefined;
290
+ }
291
+ let found: ObjectLiteralExpression | undefined;
292
+ const visit = (node: Node): void => {
293
+ if (
294
+ isVariableDeclaration(node) &&
295
+ isIdentifier(node.name) &&
296
+ node.name.text === value.text &&
297
+ node.initializer !== undefined
298
+ ) {
299
+ let at: Expression = node.initializer;
300
+ while (
301
+ isAssertionExpression(at) ||
302
+ isSatisfiesExpression(at) ||
303
+ isParenthesizedExpression(at)
304
+ ) {
305
+ at = at.expression;
306
+ }
307
+ found = isObjectLiteralExpression(at) ? at : found;
308
+ }
309
+ if (!isFunctionLikeDeclaration(node)) {
310
+ node.forEachChild(visit);
311
+ }
312
+ };
313
+ body.forEachChild(visit);
314
+ return found;
315
+ }
316
+
317
+ /**
318
+ * The values one function body returns, and those each method of an object literal it
319
+ * returns returns, to a fixed depth: a caller holding the literal calls its methods, and
320
+ * receives what they return.
321
+ */
322
+ function returnedThroughLiterals(body: Node, depth = 0): readonly Expression[] {
323
+ const values = returnedValues(body);
324
+ if (depth >= INFERRED_DEPTH) {
325
+ return values;
326
+ }
327
+ const found = [...values];
328
+ for (const value of values) {
329
+ for (const property of returnedLiteral(value, body)?.properties ?? []) {
330
+ const fn = isMethodDeclaration(property)
331
+ ? property
332
+ : isPropertyAssignment(property) && isFunctionLikeDeclaration(property.initializer)
333
+ ? property.initializer
334
+ : undefined;
335
+ if (fn?.body !== undefined) {
336
+ found.push(...returnedThroughLiterals(fn.body, depth + 1));
337
+ }
338
+ }
339
+ }
340
+ return found;
341
+ }
342
+
343
+ /**
344
+ * The declarations of the types the compiler infers where a declaration writes none, and
345
+ * of the values a function returns: the type of a variable or a property with no
346
+ * annotation, the result of a function or a method with no result annotation, and the type
347
+ * of each value its body returns, each with the types its type arguments and its union or
348
+ * intersection constituents name, to a fixed depth; and as `members` the properties of each
349
+ * returned value's type. A question the checker leaves unanswered names nothing.
350
+ */
351
+ function inferredTypeSymbols<Brand>(
352
+ project: ProjectView<Brand>,
353
+ declarations: readonly Node[],
354
+ ): { readonly types: readonly TSSymbol[]; readonly members: readonly TSSymbol[] } {
355
+ const { queries } = project;
356
+ const valued: Node[] = declarations.flatMap((node) =>
357
+ (isVariableDeclaration(node) || isPropertyDeclaration(node)) &&
358
+ node.type === undefined &&
359
+ !isFunctionLikeDeclaration(node.initializer ?? node)
360
+ ? [node.name]
361
+ : [],
362
+ );
363
+ const returned = declarations.flatMap((node) => {
364
+ const signature = isFunctionLikeDeclaration(node)
365
+ ? node
366
+ : isVariableDeclaration(node) &&
367
+ node.initializer !== undefined &&
368
+ isFunctionLikeDeclaration(node.initializer)
369
+ ? node.initializer
370
+ : undefined;
371
+ return signature?.body === undefined ? [] : returnedThroughLiterals(signature.body);
372
+ });
373
+ const types: Type[] = [];
374
+ const returnedTypes: Type[] = [];
375
+ queries.typesAt([...valued, ...returned]).forEach((type, at) => {
376
+ if (type !== UNANSWERED && type !== undefined) {
377
+ (at < valued.length ? types : returnedTypes).push(type);
378
+ }
379
+ });
380
+ types.push(...returnedTypes);
381
+ for (const node of declarations) {
382
+ const signature = isFunctionLikeDeclaration(node)
383
+ ? node
384
+ : isVariableDeclaration(node) &&
385
+ node.type === undefined &&
386
+ node.initializer !== undefined &&
387
+ isFunctionLikeDeclaration(node.initializer)
388
+ ? node.initializer
389
+ : undefined;
390
+ if (signature === undefined || signature.type !== undefined || signature.body === undefined) {
391
+ continue;
392
+ }
393
+ const typed = queries.signatureOf(signature);
394
+ const result =
395
+ typed === UNANSWERED || typed === undefined ? undefined : queries.returnTypeOf(typed);
396
+ if (result !== UNANSWERED && result !== undefined) {
397
+ types.push(result);
398
+ }
399
+ }
400
+ const found = new Map<number, TSSymbol>();
401
+ const seen = new Set<Type>();
402
+ const visit = (type: Type, depth: number): void => {
403
+ if (depth > INFERRED_DEPTH || seen.has(type)) {
404
+ return;
405
+ }
406
+ seen.add(type);
407
+ const symbol = queries.symbolOfType(type);
408
+ if (symbol !== UNANSWERED && symbol !== undefined) {
409
+ found.set(symbol.id, symbol);
410
+ }
411
+ const parts = queries.constituents(type);
412
+ const args = queries.typeArguments(type);
413
+ for (const next of [
414
+ ...(parts === UNANSWERED || (parts.length === 1 && parts[0] === type) ? [] : parts),
415
+ ...(args === UNANSWERED ? [] : args),
416
+ ]) {
417
+ visit(next, depth + 1);
418
+ }
419
+ };
420
+ types.forEach((type) => {
421
+ visit(type, 0);
422
+ });
423
+ // A returned value carries each property of its type, one an anonymous type declares or
424
+ // an intersection merges included, which no type name reaches.
425
+ const members = new Map<number, TSSymbol>();
426
+ for (const type of returnedTypes) {
427
+ const parts = queries.constituents(type);
428
+ for (const one of [type, ...(parts === UNANSWERED ? [] : parts)]) {
429
+ const properties = queries.propertiesOf(one);
430
+ for (const property of properties === UNANSWERED ? [] : properties) {
431
+ members.set(property.id, property);
432
+ }
433
+ }
434
+ }
435
+ return { types: [...found.values()], members: [...members.values()] };
436
+ }
437
+
438
+ /**
439
+ * The names of types one declaration's own syntax writes: in a type reference, a
440
+ * heritage clause or a type query. A function body is not read, and neither is a
441
+ * declaration nested in this one, which `nested` holds and which is read on its own.
442
+ */
443
+ function typeNamesOf(declaration: Node, nested: ReadonlySet<Node>): readonly Node[] {
444
+ const found: Node[] = [];
445
+ const visit = (node: Node): void => {
446
+ if (node !== declaration && nested.has(node)) {
447
+ return;
448
+ }
449
+ if (isTypeReferenceNode(node)) {
450
+ found.push(isQualifiedName(node.typeName) ? node.typeName.right : node.typeName);
451
+ } else if (isExpressionWithTypeArguments(node)) {
452
+ found.push(
453
+ isPropertyAccessExpression(node.expression) ? node.expression.name : node.expression,
454
+ );
455
+ } else if (isTypeQueryNode(node)) {
456
+ found.push(isQualifiedName(node.exprName) ? node.exprName.right : node.exprName);
457
+ }
458
+ const body = isFunctionLikeDeclaration(node) ? node.body : undefined;
459
+ node.forEachChild((child) => {
460
+ if (child !== body) {
461
+ visit(child);
462
+ }
463
+ });
464
+ };
465
+ visit(declaration);
466
+ return found;
467
+ }
468
+
469
+ /** The members of one declaration, indexed by the container that holds them. */
470
+ function childrenOf(held: Inventory): ReadonlyMap<string, readonly InventorySymbol[]> {
471
+ const byParent = new Map<string, InventorySymbol[]>();
472
+ for (const symbol of held.symbols) {
473
+ const siblings = byParent.get(symbol.parent);
474
+ if (siblings === undefined) {
475
+ byParent.set(symbol.parent, [symbol]);
476
+ } else {
477
+ siblings.push(symbol);
478
+ }
479
+ }
480
+ return byParent;
481
+ }