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

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 (161) hide show
  1. package/dist/src/alias-chain.js +0 -5
  2. package/dist/src/amd.js +81 -0
  3. package/dist/src/analysis.js +82 -91
  4. package/dist/src/augmentations.js +24 -25
  5. package/dist/src/bindings.js +382 -0
  6. package/dist/src/calls.js +279 -43
  7. package/dist/src/commands/tools.js +5 -9
  8. package/dist/src/component-files.js +3 -2
  9. package/dist/src/conformance-record.js +1 -1
  10. package/dist/src/consumers.js +6 -9
  11. package/dist/src/contextual-uses.js +1 -17
  12. package/dist/src/contract/config.generated.js +4 -1
  13. package/dist/src/contract/contract.generated.js +2 -2
  14. package/dist/src/contract/report.generated.js +6 -2
  15. package/dist/src/dependencies.js +25 -29
  16. package/dist/src/destructuring.js +1 -19
  17. package/dist/src/element-access.js +179 -164
  18. package/dist/src/entries/build-scripts.js +2 -2
  19. package/dist/src/entries/configuration-files.js +3 -7
  20. package/dist/src/entries/conventions.js +2 -2
  21. package/dist/src/entries/html-entries.js +7 -10
  22. package/dist/src/entries/mdx-entries.js +2 -2
  23. package/dist/src/entries/static-module.js +2 -1
  24. package/dist/src/exempt.js +0 -26
  25. package/dist/src/exposure.js +400 -0
  26. package/dist/src/facts.js +4 -4
  27. package/dist/src/findings/completion.js +44 -21
  28. package/dist/src/findings/emitters.js +22 -33
  29. package/dist/src/{implementations.js → findings/implementations.js} +3 -3
  30. package/dist/src/{in-operands.js → findings/in-operands.js} +2 -2
  31. package/dist/src/findings/intra-function.js +3 -3
  32. package/dist/src/findings/reads-and-writes.js +71 -15
  33. package/dist/src/findings/unused-declarations.js +2 -22
  34. package/dist/src/findings/visibility-narrowing.js +14 -20
  35. package/dist/src/framework-lifecycle.js +41 -14
  36. package/dist/src/import-bindings.js +73 -28
  37. package/dist/src/index/sites.js +557 -0
  38. package/dist/src/indexed-access.js +523 -478
  39. package/dist/src/interface-satisfaction.js +1 -1
  40. package/dist/src/inventory.js +18 -0
  41. package/dist/src/keyof-comparisons.js +124 -103
  42. package/dist/src/{components.js → liveness/components.js} +1 -1
  43. package/dist/src/{graph.js → liveness/graph.js} +45 -20
  44. package/dist/src/liveness/matrix.js +99 -0
  45. package/dist/src/liveness/sweep.js +558 -0
  46. package/dist/src/load/expected-module.js +2 -2
  47. package/dist/src/load/specifiers.js +2 -4
  48. package/dist/src/load/synthesize.js +5 -0
  49. package/dist/src/load/workspace-resolution.js +2 -2
  50. package/dist/src/module-declarations.js +70 -85
  51. package/dist/src/overrides.js +134 -112
  52. package/dist/src/paths.js +46 -7
  53. package/dist/src/platform-calls.js +2 -2
  54. package/dist/src/platform-symbols.js +13 -19
  55. package/dist/src/query.js +46 -0
  56. package/dist/src/references/read-rule.js +1 -0
  57. package/dist/src/references/read-rules.js +48 -0
  58. package/dist/src/references/sink.js +237 -0
  59. package/dist/src/references/sources.js +450 -0
  60. package/dist/src/references.js +110 -1285
  61. package/dist/src/roots/configured-declarations.js +3 -0
  62. package/dist/src/roots/published-reach.js +4 -4
  63. package/dist/src/roots/root-set.js +1 -1
  64. package/dist/src/roots/unseen-loads.js +38 -10
  65. package/dist/src/serialization-contract.js +8 -3
  66. package/dist/src/stores.js +4 -2
  67. package/dist/src/symbol-keys.js +89 -83
  68. package/dist/src/tests/file-class.js +76 -0
  69. package/dist/src/{outside-tests.js → tests/outside.js} +7 -7
  70. package/dist/src/{test-support.js → tests/support.js} +2 -11
  71. package/dist/src/type-parameter-reads.js +178 -139
  72. package/dist/src/unimplemented-callees.js +262 -242
  73. package/dist/src/verbs/analyze.js +3 -3
  74. package/dist/src/verbs/explain.js +75 -132
  75. package/dist/src/verbs/print-retained.js +3 -3
  76. package/jsr.json +1 -1
  77. package/package.json +2 -2
  78. package/src/alias-chain.ts +0 -7
  79. package/src/amd.ts +130 -0
  80. package/src/analysis.ts +125 -128
  81. package/src/augmentations.ts +42 -28
  82. package/src/bindings.ts +460 -0
  83. package/src/calls.ts +338 -46
  84. package/src/commands/tools.ts +5 -11
  85. package/src/component-files.ts +4 -2
  86. package/src/conformance-record.ts +1 -1
  87. package/src/consumers.ts +8 -11
  88. package/src/contextual-uses.ts +2 -22
  89. package/src/contract/config.generated.ts +4 -1
  90. package/src/contract/contract.generated.ts +2 -2
  91. package/src/contract/report.generated.ts +6 -2
  92. package/src/dependencies.ts +27 -28
  93. package/src/destructuring.ts +1 -31
  94. package/src/element-access.ts +188 -182
  95. package/src/entries/build-scripts.ts +2 -2
  96. package/src/entries/configuration-files.ts +11 -8
  97. package/src/entries/conventions.ts +2 -1
  98. package/src/entries/html-entries.ts +15 -14
  99. package/src/entries/mdx-entries.ts +2 -2
  100. package/src/entries/static-module.ts +2 -1
  101. package/src/exempt.ts +31 -46
  102. package/src/exposure.ts +492 -0
  103. package/src/facts.ts +5 -3
  104. package/src/finding.ts +1 -1
  105. package/src/findings/boundary.ts +9 -0
  106. package/src/findings/completion.ts +48 -23
  107. package/src/findings/emitter.ts +1 -1
  108. package/src/findings/emitters.ts +23 -41
  109. package/src/{implementations.ts → findings/implementations.ts} +4 -4
  110. package/src/{in-operands.ts → findings/in-operands.ts} +3 -3
  111. package/src/findings/interfaces.ts +3 -3
  112. package/src/findings/intra-function.ts +3 -3
  113. package/src/findings/reads-and-writes.ts +99 -26
  114. package/src/findings/unused-declarations.ts +4 -28
  115. package/src/findings/visibility-narrowing.ts +25 -26
  116. package/src/framework-lifecycle.ts +42 -14
  117. package/src/import-bindings.ts +98 -29
  118. package/src/index/sites.ts +781 -0
  119. package/src/indexed-access.ts +570 -527
  120. package/src/interface-satisfaction.ts +2 -2
  121. package/src/inventory.ts +19 -0
  122. package/src/keyof-comparisons.ts +131 -116
  123. package/src/{components.ts → liveness/components.ts} +3 -3
  124. package/src/{graph.ts → liveness/graph.ts} +93 -27
  125. package/src/liveness/matrix.ts +162 -0
  126. package/src/liveness/sweep.ts +855 -0
  127. package/src/load/expected-module.ts +8 -2
  128. package/src/load/specifiers.ts +8 -5
  129. package/src/load/synthesize.ts +6 -0
  130. package/src/load/workspace-resolution.ts +9 -2
  131. package/src/module-declarations.ts +74 -98
  132. package/src/overrides.ts +162 -134
  133. package/src/paths.ts +54 -8
  134. package/src/platform-calls.ts +3 -3
  135. package/src/platform-symbols.ts +11 -27
  136. package/src/query.ts +81 -0
  137. package/src/references/read-rule.ts +60 -0
  138. package/src/references/read-rules.ts +55 -0
  139. package/src/references/sink.ts +314 -0
  140. package/src/references/sources.ts +632 -0
  141. package/src/references.ts +175 -1733
  142. package/src/report.ts +1 -1
  143. package/src/roots/configured-declarations.ts +3 -0
  144. package/src/roots/published-reach.ts +9 -4
  145. package/src/roots/root-set.ts +1 -1
  146. package/src/roots/unseen-loads.ts +49 -11
  147. package/src/serialization-contract.ts +8 -3
  148. package/src/stores.ts +15 -3
  149. package/src/symbol-keys.ts +102 -102
  150. package/src/tests/file-class.ts +135 -0
  151. package/src/{outside-tests.ts → tests/outside.ts} +16 -10
  152. package/src/{test-support.ts → tests/support.ts} +9 -20
  153. package/src/type-parameter-reads.ts +187 -158
  154. package/src/unimplemented-callees.ts +297 -280
  155. package/src/verbs/analyze.ts +3 -2
  156. package/src/verbs/explain.ts +83 -158
  157. package/src/verbs/print-retained.ts +3 -3
  158. package/dist/src/matrix.js +0 -273
  159. package/dist/src/sweep.js +0 -222
  160. package/src/matrix.ts +0 -463
  161. package/src/sweep.ts +0 -361
@@ -0,0 +1,460 @@
1
+ /**
2
+ * The binding identities of the facts document, `bindings.6` and `bindings.7`: the consumer
3
+ * records a generator row's output names, read from the syntax its plugin writes, and the
4
+ * fallback records of the rows that bind a file only as a whole. The run publishes no state,
5
+ * so every record is an identity (`bindings.10`).
6
+ */
7
+
8
+ import {
9
+ isAsExpression,
10
+ isEnumDeclaration,
11
+ isIdentifier,
12
+ isInterfaceDeclaration,
13
+ isIntersectionTypeNode,
14
+ isJSDocText,
15
+ isLiteralTypeNode,
16
+ isMethodSignatureDeclaration,
17
+ isObjectLiteralExpression,
18
+ isParenthesizedTypeNode,
19
+ isPropertyAssignment,
20
+ isPropertySignatureDeclaration,
21
+ isSatisfiesExpression,
22
+ isStringLiteralLikeNode,
23
+ isTypeAliasDeclaration,
24
+ isTypeLiteralNode,
25
+ isTypeReferenceNode,
26
+ isUnionTypeNode,
27
+ isVariableStatement,
28
+ type Node,
29
+ type SourceFile,
30
+ } from "@typescript/native/ast";
31
+ import { GENERATORS } from "./contract/contract.generated.ts";
32
+ import type { GeneratedFact } from "./generated-file.ts";
33
+ import { componentOf, nameNodeOf } from "./inventory.ts";
34
+ import { nameComponent, spell } from "./ref.ts";
35
+ import { bytewise } from "./text/compare.ts";
36
+
37
+ /** One binding record of the facts document, as `facts.schema.json` spells it. */
38
+ export interface BindingRecord {
39
+ readonly role: "consumer" | "fallback";
40
+ readonly symbol: string;
41
+ readonly schema_file: string;
42
+ readonly schema?: string;
43
+ readonly generated_file: string;
44
+ readonly scope?: "all" | "types";
45
+ readonly state?: "live";
46
+ }
47
+
48
+ /** A consumer one output names: the generated declaration's fragment and the schema declaration's dotted name. */
49
+ interface Named {
50
+ readonly fragment: string;
51
+ readonly name: string;
52
+ }
53
+
54
+ /** The `@generated from <kind> <rest>` tags of one node's own documentation, in order. */
55
+ function generatedTags(node: Node): readonly { readonly kind: string; readonly rest: string }[] {
56
+ const docs = (node as { readonly jsDoc?: readonly Node[] }).jsDoc ?? [];
57
+ return docs.flatMap((doc) =>
58
+ ((doc as { readonly tags?: readonly Node[] }).tags ?? []).flatMap((tag) => {
59
+ const { tagName, comment } = tag as {
60
+ readonly tagName?: Node;
61
+ readonly comment?: readonly Node[];
62
+ };
63
+ if (tagName === undefined || !isIdentifier(tagName) || tagName.text !== "generated") {
64
+ return [];
65
+ }
66
+ const text = (comment ?? [])
67
+ .map((part) => (isJSDocText(part) ? part.text : ""))
68
+ .join("")
69
+ .trim();
70
+ const found = /^from (\w+(?: value)?):? (.+)$/u.exec(text);
71
+ return found === null ? [] : [{ kind: found[1] ?? "", rest: (found[2] ?? "").trim() }];
72
+ }),
73
+ );
74
+ }
75
+
76
+ /** The tag of one kind a node carries, its text after the kind. */
77
+ function tagged(node: Node, kind: string): string | undefined {
78
+ return generatedTags(node).find((one) => one.kind === kind)?.rest;
79
+ }
80
+
81
+ /**
82
+ * The name a field or an enum value tag declares: the word before ` = <number>`, read
83
+ * before an optional bracketed option list (`= 7 [json_name = "sync"];`).
84
+ */
85
+ function declaredName(rest: string): string | undefined {
86
+ return /(\w+) = \d+(?: \[.*\])?;$/u.exec(rest)?.[1];
87
+ }
88
+
89
+ /** The spelled component of one declaration's name, or undefined for a name the grammar cannot spell. */
90
+ function spelledName(node: Node): string | undefined {
91
+ const component = componentOf(nameNodeOf(node));
92
+ return component === undefined ? undefined : spell(component);
93
+ }
94
+
95
+ /** The object type literals one type is or holds as a direct constituent of a union or an intersection. */
96
+ function literalsOf(type: Node | undefined): readonly Node[] {
97
+ if (type === undefined) {
98
+ return [];
99
+ }
100
+ if (isParenthesizedTypeNode(type)) {
101
+ return literalsOf(type.type);
102
+ }
103
+ if (isUnionTypeNode(type) || isIntersectionTypeNode(type)) {
104
+ return type.types.flatMap((one) => literalsOf(one));
105
+ }
106
+ return isTypeLiteralNode(type) ? [type] : [];
107
+ }
108
+
109
+ /** The string literal type one type literal declares for its `case` property, the oneof discriminant. */
110
+ function caseOf(literal: Node): string | undefined {
111
+ if (!isTypeLiteralNode(literal)) {
112
+ return undefined;
113
+ }
114
+ for (const member of literal.members) {
115
+ if (
116
+ isPropertySignatureDeclaration(member) &&
117
+ spelledName(member) === "case" &&
118
+ isLiteralTypeNode(member.type) &&
119
+ isStringLiteralLikeNode(member.type.literal)
120
+ ) {
121
+ return member.type.literal.text;
122
+ }
123
+ }
124
+ return undefined;
125
+ }
126
+
127
+ /** The fields, enum values and oneof constituents of one tagged message or enum, below its fragment. */
128
+ function membersOf(members: readonly Node[], fragment: string, name: string): readonly Named[] {
129
+ return members.flatMap((member): Named[] => {
130
+ const spelled = spelledName(member);
131
+ if (spelled === undefined) {
132
+ return [];
133
+ }
134
+ const at = `${fragment}.${spelled}`;
135
+ const field = tagged(member, "field") ?? tagged(member, "enum value");
136
+ const declared = field === undefined ? undefined : declaredName(field);
137
+ if (declared !== undefined) {
138
+ return [{ fragment: at, name: `${name}.${declared}` }];
139
+ }
140
+ if (tagged(member, "oneof") === undefined || !isPropertySignatureDeclaration(member)) {
141
+ return [];
142
+ }
143
+ return literalsOf(member.type).flatMap((constituent): Named[] => {
144
+ const discriminant = caseOf(constituent);
145
+ return discriminant === undefined
146
+ ? []
147
+ : [
148
+ {
149
+ fragment: `${at}.'${discriminant.replaceAll("\\", "\\\\").replaceAll("'", "\\'")}':case`,
150
+ name: `${name}.${discriminant}`,
151
+ },
152
+ ];
153
+ });
154
+ });
155
+ }
156
+
157
+ /** The full name `@generated from <kind> <pkg.Name>` gives, without a trailing comment. */
158
+ function fullName(rest: string): string {
159
+ return rest.split(/\s/u)[0] ?? rest;
160
+ }
161
+
162
+ /** The consumers a protoc-gen-es file of row `protoc-gen-es-2` names, as `references-ts.28` reads them. */
163
+ function protobufEsConsumers(file: SourceFile): readonly Named[] {
164
+ const found: Named[] = [];
165
+ const typed = new Map<string, string>();
166
+ const constants: { readonly fragment: string; readonly type: Node; readonly node: Node }[] = [];
167
+ for (const statement of file.statements) {
168
+ if (isTypeAliasDeclaration(statement) || isInterfaceDeclaration(statement)) {
169
+ const fragment = spelledName(statement);
170
+ const message = tagged(statement, "message");
171
+ if (fragment === undefined || message === undefined) {
172
+ continue;
173
+ }
174
+ const name = fullName(message);
175
+ typed.set(statement.name.text, name);
176
+ found.push({ fragment, name });
177
+ const members = isInterfaceDeclaration(statement)
178
+ ? statement.members
179
+ : literalsOf(statement.type).flatMap((one) => (isTypeLiteralNode(one) ? one.members : []));
180
+ found.push(...membersOf(members, fragment, name));
181
+ } else if (isEnumDeclaration(statement)) {
182
+ const fragment = spelledName(statement);
183
+ const enumName = tagged(statement, "enum");
184
+ if (fragment === undefined || enumName === undefined) {
185
+ continue;
186
+ }
187
+ const name = fullName(enumName);
188
+ typed.set(statement.name.text, name);
189
+ found.push({ fragment, name }, ...membersOf(statement.members, fragment, name));
190
+ } else if (isVariableStatement(statement)) {
191
+ for (const declaration of statement.declarationList.declarations) {
192
+ const fragment = spelledName(declaration);
193
+ if (fragment === undefined) {
194
+ continue;
195
+ }
196
+ const tags = [...generatedTags(statement), ...generatedTags(declaration)];
197
+ const enumName = tags.find((one) => one.kind === "enum")?.rest;
198
+ const service = tags.find((one) => one.kind === "service")?.rest;
199
+ const initializer = declaration.initializer;
200
+ const object = initializer === undefined ? undefined : asConst(initializer);
201
+ if (enumName !== undefined && object !== undefined && isObjectLiteralExpression(object)) {
202
+ const name = fullName(enumName);
203
+ typed.set(fragment, name);
204
+ found.push({ fragment, name }, ...membersOf(object.properties, fragment, name));
205
+ } else if (service !== undefined) {
206
+ found.push({ fragment, name: fullName(service) });
207
+ const [methods] = typeArguments(declaration.type);
208
+ for (const literal of literalsOf(methods)) {
209
+ for (const member of isTypeLiteralNode(literal) ? literal.members : []) {
210
+ const spelled = spelledName(member);
211
+ const rpc = tagged(member, "rpc");
212
+ if (spelled !== undefined && rpc !== undefined) {
213
+ found.push({ fragment: `${fragment}.${spelled}`, name: fullName(rpc) });
214
+ }
215
+ }
216
+ }
217
+ } else if (declaration.type !== undefined) {
218
+ constants.push({ fragment, type: declaration.type, node: declaration });
219
+ }
220
+ }
221
+ }
222
+ }
223
+ for (const constant of constants) {
224
+ const type = constant.type;
225
+ if (!isTypeReferenceNode(type) || !isIdentifier(type.typeName)) {
226
+ continue;
227
+ }
228
+ if (type.typeName.text !== "GenMessage" && type.typeName.text !== "GenEnum") {
229
+ continue;
230
+ }
231
+ const [argument] = typeArguments(type);
232
+ const named =
233
+ argument !== undefined && isTypeReferenceNode(argument) && isIdentifier(argument.typeName)
234
+ ? typed.get(argument.typeName.text)
235
+ : undefined;
236
+ if (named !== undefined) {
237
+ found.push({ fragment: constant.fragment, name: named });
238
+ }
239
+ }
240
+ return found;
241
+ }
242
+
243
+ /** The expression an `as const` or `satisfies` clause holds, or the expression itself. */
244
+ function asConst(node: Node): Node {
245
+ return isAsExpression(node) || isSatisfiesExpression(node) ? asConst(node.expression) : node;
246
+ }
247
+
248
+ /** The type arguments one type reference writes. */
249
+ function typeArguments(type: Node | undefined): readonly Node[] {
250
+ return type !== undefined && isTypeReferenceNode(type) ? (type.typeArguments ?? []) : [];
251
+ }
252
+
253
+ /** A ts-proto service path literal, split into its service and its method. */
254
+ const SERVICE_PATH = /^\/((?:\w+\.)*\w+)\/(\w+)$/u;
255
+
256
+ /**
257
+ * The service and RPC consumers a ts-proto file of row `ts-proto-2` names, as
258
+ * `references-ts.31` reads them: the methods of the interface a `<Svc>ServiceName`
259
+ * constant names, and a definition object whose members write a service `path`, with each
260
+ * member the consumer of its RPC and the object, and the `<Svc>Client` that the same
261
+ * file declares beside it, consumers of the service.
262
+ */
263
+ function tsProtoConsumers(file: SourceFile): readonly Named[] {
264
+ const found: Named[] = [];
265
+ const interfaces = new Map<string, readonly Node[]>();
266
+ const clients = new Set<string>();
267
+ for (const statement of file.statements) {
268
+ if (isInterfaceDeclaration(statement)) {
269
+ interfaces.set(statement.name.text, statement.members);
270
+ }
271
+ if (isVariableStatement(statement)) {
272
+ for (const one of statement.declarationList.declarations) {
273
+ if (isIdentifier(one.name)) {
274
+ clients.add(one.name.text);
275
+ }
276
+ }
277
+ }
278
+ }
279
+ for (const statement of file.statements) {
280
+ if (!isVariableStatement(statement)) {
281
+ continue;
282
+ }
283
+ for (const declaration of statement.declarationList.declarations) {
284
+ const { name, initializer } = declaration;
285
+ if (!isIdentifier(name) || initializer === undefined) {
286
+ continue;
287
+ }
288
+ if (name.text.endsWith("ServiceName") && isStringLiteralLikeNode(initializer)) {
289
+ const service = name.text.slice(0, -"ServiceName".length);
290
+ for (const member of interfaces.get(service) ?? []) {
291
+ const spelled = spelledName(member);
292
+ if (spelled !== undefined && isMethodSignatureDeclaration(member)) {
293
+ found.push({
294
+ fragment: `${nameComponent(service)}.${spelled}`,
295
+ name: `${initializer.text}.${spelled}`,
296
+ });
297
+ }
298
+ }
299
+ continue;
300
+ }
301
+ const object = asConst(initializer);
302
+ if (!isObjectLiteralExpression(object)) {
303
+ continue;
304
+ }
305
+ let service: string | undefined;
306
+ for (const property of object.properties) {
307
+ const spelled = spelledName(property);
308
+ const value = isPropertyAssignment(property) ? asConst(property.initializer) : undefined;
309
+ const path =
310
+ value !== undefined && isObjectLiteralExpression(value)
311
+ ? value.properties.find(
312
+ (one) =>
313
+ isPropertyAssignment(one) &&
314
+ spelledName(one) === "path" &&
315
+ isStringLiteralLikeNode(one.initializer),
316
+ )
317
+ : undefined;
318
+ const matched =
319
+ path !== undefined &&
320
+ isPropertyAssignment(path) &&
321
+ isStringLiteralLikeNode(path.initializer)
322
+ ? SERVICE_PATH.exec(path.initializer.text)
323
+ : null;
324
+ if (spelled === undefined || matched === null) {
325
+ continue;
326
+ }
327
+ service = matched[1];
328
+ const rpc = `${matched[1] ?? ""}.${matched[2] ?? ""}`;
329
+ found.push({ fragment: `${nameComponent(name.text)}.${spelled}`, name: rpc });
330
+ const client = `${name.text.replace(/Service$/u, "")}Client`;
331
+ if (interfaces.get(client)?.some((member) => spelledName(member) === spelled) === true) {
332
+ found.push({ fragment: `${nameComponent(client)}.${spelled}`, name: rpc });
333
+ }
334
+ }
335
+ if (service !== undefined) {
336
+ found.push({ fragment: nameComponent(name.text), name: service });
337
+ const client = `${name.text.replace(/Service$/u, "")}Client`;
338
+ if (interfaces.has(client) || clients.has(client)) {
339
+ found.push({ fragment: nameComponent(client), name: service });
340
+ }
341
+ }
342
+ }
343
+ }
344
+ return found;
345
+ }
346
+
347
+ /** The package `// @generated from file <path> (package <pkg>, ...)` or `protobufPackage` names, `.` for none. */
348
+ function packageOf(file: SourceFile): string {
349
+ const header = /^\/\/ @generated from file \S+ \(package ([\w.]+)/mu.exec(file.text)?.[1];
350
+ if (header !== undefined) {
351
+ return header;
352
+ }
353
+ for (const statement of file.statements) {
354
+ if (!isVariableStatement(statement)) {
355
+ continue;
356
+ }
357
+ for (const one of statement.declarationList.declarations) {
358
+ if (
359
+ isIdentifier(one.name) &&
360
+ one.name.text === "protobufPackage" &&
361
+ one.initializer !== undefined &&
362
+ isStringLiteralLikeNode(one.initializer) &&
363
+ one.initializer.text !== ""
364
+ ) {
365
+ return one.initializer.text;
366
+ }
367
+ }
368
+ }
369
+ return ".";
370
+ }
371
+
372
+ /** The `proto://` reference of a schema declaration a full name written in package `pkg` names. */
373
+ function schemaRef(pkg: string, name: string): string {
374
+ const local = pkg !== "." && name.startsWith(`${pkg}.`) ? name.slice(pkg.length + 1) : name;
375
+ return `proto://${pkg}#${local}`;
376
+ }
377
+
378
+ /** The consumer reader of each row whose output the analyzer decodes. */
379
+ const CONSUMERS: Readonly<Record<string, (file: SourceFile) => readonly Named[]>> = {
380
+ "protoc-gen-es-2": protobufEsConsumers,
381
+ "ts-proto-2": tsProtoConsumers,
382
+ };
383
+
384
+ /** The scope of the fallback record each fallback cell publishes. */
385
+ const FALLBACK_SCOPE: Readonly<Record<string, "all" | "types">> = {
386
+ fallback: "all",
387
+ "rpc-types-fallback": "types",
388
+ };
389
+
390
+ /**
391
+ * The binding records one project's generated files publish, each file read once:
392
+ * `files` by path below the target root, `moduleRefs` each file's own reference.
393
+ */
394
+ export function bindingsOf(
395
+ files: ReadonlyMap<string, SourceFile>,
396
+ generated: ReadonlyMap<string, GeneratedFact>,
397
+ moduleRefs: ReadonlyMap<string, string>,
398
+ ): readonly BindingRecord[] {
399
+ const records: BindingRecord[] = [];
400
+ for (const [path, fact] of generated) {
401
+ const file = files.get(path);
402
+ const moduleRef = moduleRefs.get(path);
403
+ const generator = fact.generator;
404
+ if (file === undefined || moduleRef === undefined || generator === undefined) {
405
+ continue;
406
+ }
407
+ const [schemaFile] = generator.schemaFiles;
408
+ const cell = GENERATORS.find((row) => row.id === generator.row)?.cell;
409
+ const scope = cell === undefined ? undefined : FALLBACK_SCOPE[cell];
410
+ if (scope !== undefined) {
411
+ for (const one of generator.schemaFiles) {
412
+ records.push({
413
+ role: "fallback",
414
+ symbol: moduleRef,
415
+ schema_file: one,
416
+ generated_file: path,
417
+ scope,
418
+ state: "live",
419
+ });
420
+ }
421
+ }
422
+ const read = CONSUMERS[generator.row];
423
+ if (read === undefined || schemaFile === undefined) {
424
+ continue;
425
+ }
426
+ const pkg = packageOf(file);
427
+ const seen = new Set<string>();
428
+ for (const named of read(file)) {
429
+ const symbol = `${moduleRef}${named.fragment}`;
430
+ if (seen.has(symbol)) {
431
+ continue;
432
+ }
433
+ seen.add(symbol);
434
+ records.push({
435
+ role: "consumer",
436
+ symbol,
437
+ schema_file: schemaFile,
438
+ schema: schemaRef(pkg, named.name),
439
+ generated_file: path,
440
+ });
441
+ }
442
+ }
443
+ return records;
444
+ }
445
+
446
+ /**
447
+ * The records of a run's projects, each once, in the order the facts schema states: by
448
+ * schema file, schema, role and symbol. A generated file two projects hold publishes its
449
+ * records in each.
450
+ */
451
+ export function distinctBindings(records: readonly BindingRecord[]): readonly BindingRecord[] {
452
+ const byKey = new Map(records.map((one) => [JSON.stringify(one), one]));
453
+ return [...byKey.values()].sort(
454
+ (a, b) =>
455
+ bytewise(a.schema_file, b.schema_file) ||
456
+ bytewise(a.schema ?? "", b.schema ?? "") ||
457
+ bytewise(a.role, b.role) ||
458
+ bytewise(a.symbol, b.symbol),
459
+ );
460
+ }