@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
package/src/report.ts CHANGED
@@ -18,7 +18,7 @@ import type {
18
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
- import type { TestFileRule } from "./references.ts";
21
+ import type { TestFileRule } from "./tests/file-class.ts";
22
22
  import { bytewise } from "./text/compare.ts";
23
23
  import { LANGUAGE, PRODUCT } from "./product.ts";
24
24
 
@@ -204,6 +204,9 @@ function modulesNamed<Brand>(
204
204
  project: ProjectView<Brand>,
205
205
  specifiers: ReadonlySet<string>,
206
206
  ): ReadonlyMap<string, TSSymbol> {
207
+ if (specifiers.size === 0) {
208
+ return new Map();
209
+ }
207
210
  const written = new Map<string, Node>();
208
211
  for (const file of project.ownSourceFiles()) {
209
212
  const visit = (node: Node): void => {
@@ -235,7 +235,9 @@ function declarationNodes(
235
235
  return {
236
236
  typeNamesOf: (symbol) => {
237
237
  const { byId, nodes } = nodesIn(symbol.position.path);
238
- return (byId.get(symbol.id) ?? []).flatMap((declaration) => typeNamesOf(declaration, nodes));
238
+ return (byId.get(symbol.id) ?? []).flatMap((declaration) =>
239
+ typeNamesOf(declaration, (nested) => nodes.has(nested)),
240
+ );
239
241
  },
240
242
  nodesOf: (symbol) => nodesIn(symbol.position.path).byId.get(symbol.id) ?? [],
241
243
  };
@@ -438,12 +440,15 @@ function inferredTypeSymbols<Brand>(
438
440
  /**
439
441
  * The names of types one declaration's own syntax writes: in a type reference, a
440
442
  * 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.
443
+ * declaration nested in this one that `readAlone` says is read on its own.
442
444
  */
443
- function typeNamesOf(declaration: Node, nested: ReadonlySet<Node>): readonly Node[] {
445
+ export function typeNamesOf(
446
+ declaration: Node,
447
+ readAlone: (nested: Node) => boolean,
448
+ ): readonly Node[] {
444
449
  const found: Node[] = [];
445
450
  const visit = (node: Node): void => {
446
- if (node !== declaration && nested.has(node)) {
451
+ if (node !== declaration && readAlone(node)) {
447
452
  return;
448
453
  }
449
454
  if (isTypeReferenceNode(node)) {
@@ -621,7 +621,7 @@ export function rootSet<Brand>(
621
621
  for (const id of skippedRoots(project, held, options.skips)) {
622
622
  add(id, "type-error", "");
623
623
  }
624
- for (const id of mergedGlobalInterfaces(project, held, files)) {
624
+ for (const id of mergedGlobalInterfaces(project, held, files).kept) {
625
625
  add(id, "global-augmentation", "");
626
626
  }
627
627
 
@@ -19,9 +19,18 @@ import {
19
19
  type Node,
20
20
  type SourceFile,
21
21
  } from "@typescript/native/ast";
22
+ import { amdLoadOf, amdUnresolved } from "../amd.ts";
22
23
  import { membersNamed, textsOf, writtenValue, type Value } from "../entries/static-module.ts";
23
24
  import { writtenName } from "../platform-calls.ts";
24
- import { dirnamePath, relativePath, resolvePath } from "../paths.ts";
25
+ import {
26
+ cannotStartPackageName,
27
+ dirnamePath,
28
+ hasURLScheme,
29
+ isAbsolutePath,
30
+ relativePath,
31
+ resolvePath,
32
+ startsRelativePath,
33
+ } from "../paths.ts";
25
34
  import type { Position } from "../position.ts";
26
35
  import { unwrapped } from "../unwrap.ts";
27
36
  import { globExpression } from "../text/glob.ts";
@@ -54,7 +63,7 @@ function leadingLiteral(node: Node): string | undefined {
54
63
  * it, each of which would name every sibling.
55
64
  */
56
65
  function computedLoadDirectory(dir: string, prefix: string): string | undefined {
57
- if (!(prefix.startsWith("./") || prefix.startsWith("../")) || !prefix.endsWith("/")) {
66
+ if (!startsRelativePath(prefix) || !prefix.endsWith("/")) {
58
67
  return undefined;
59
68
  }
60
69
  const target = resolvePath(dir, prefix);
@@ -74,11 +83,13 @@ interface Loadable {
74
83
  }
75
84
 
76
85
  /** One load by a computed path whose files the analysis resolves. */
77
- interface ComputedLoad {
86
+ export interface ComputedLoad {
78
87
  readonly call: CallExpression;
79
88
  readonly from: string;
80
89
  /** The name a `require()` call is written with; {@link callsPlatform} owns which one loads. */
81
90
  readonly require: Identifier | undefined;
91
+ /** Whether the call is an AMD `define` or `require` (references-ts.37). */
92
+ readonly amd: boolean;
82
93
  readonly files: readonly Loadable[];
83
94
  /** Whether the load reads each file as text or as a URL, which references no declaration of it. */
84
95
  readonly asset: boolean;
@@ -136,7 +147,7 @@ function globbed(
136
147
  const dir = dirnamePath(file.fileName);
137
148
  const root = (): string | undefined => rootOf(file.fileName);
138
149
  const against = (at: string): string | undefined => {
139
- if (!at.startsWith("/")) {
150
+ if (!isAbsolutePath(at)) {
140
151
  return resolvePath(dir, at);
141
152
  }
142
153
  const top = root();
@@ -149,9 +160,9 @@ function globbed(
149
160
  const negated = written.startsWith("!");
150
161
  const pattern = negated ? written.slice(1) : written;
151
162
  let path: string | undefined;
152
- if (pattern.startsWith("/")) {
163
+ if (isAbsolutePath(pattern)) {
153
164
  path = against(pattern);
154
- } else if (pattern.startsWith("./") || pattern.startsWith("../")) {
165
+ } else if (startsRelativePath(pattern)) {
155
166
  path = from === undefined ? undefined : resolvePath(from, pattern);
156
167
  } else if (pattern.startsWith("**")) {
157
168
  path = pattern;
@@ -173,7 +184,8 @@ function globbed(
173
184
  * file itself: an `import()` or `require()` whose path starts with a relative literal naming
174
185
  * a directory below the file's loads every candidate below it, as a bundler's context module
175
186
  * reads it, and an `import.meta.glob` every candidate its globs match ({@link globbed}). A
176
- * call of a computed path that names no such directory and no package is an unresolved load.
187
+ * call of a computed path that names no such directory and no package is an unresolved load,
188
+ * and so is an AMD call one of whose dependencies names no path ({@link amdUnresolved}).
177
189
  */
178
190
  export function computedLoadsOf(
179
191
  file: SourceFile,
@@ -196,6 +208,7 @@ export function computedLoadsOf(
196
208
  call,
197
209
  from,
198
210
  require: undefined,
211
+ amd: false,
199
212
  files: files ?? [],
200
213
  resolved: files !== undefined,
201
214
  asset,
@@ -203,6 +216,23 @@ export function computedLoadsOf(
203
216
  },
204
217
  ];
205
218
  }
219
+ const amd = amdLoadOf(call);
220
+ if (amd !== undefined) {
221
+ return amdUnresolved(amd)
222
+ ? [
223
+ {
224
+ call,
225
+ from,
226
+ require: amd.callee,
227
+ amd: true,
228
+ files: [],
229
+ resolved: false,
230
+ asset: false,
231
+ eager: false,
232
+ },
233
+ ]
234
+ : [];
235
+ }
206
236
  const argument = unwrapped(first);
207
237
  const require =
208
238
  call.expression.kind === SyntaxKind.ImportKeyword ? undefined : writtenName(call.expression);
@@ -219,7 +249,16 @@ export function computedLoadsOf(
219
249
  ? []
220
250
  : others.filter((candidate) => relativePath(target, candidate.file.fileName) !== undefined);
221
251
  return [
222
- { call, from, require, files, resolved: target !== undefined, asset: false, eager: false },
252
+ {
253
+ call,
254
+ from,
255
+ require,
256
+ amd: false,
257
+ files,
258
+ resolved: target !== undefined,
259
+ asset: false,
260
+ eager: false,
261
+ },
223
262
  ];
224
263
  });
225
264
  }
@@ -300,8 +339,7 @@ function namesPackage(prefix: string | undefined): boolean {
300
339
  return (
301
340
  prefix !== undefined &&
302
341
  prefix !== "" &&
303
- !prefix.startsWith(".") &&
304
- !prefix.startsWith("/") &&
305
- !/^[A-Za-z][A-Za-z\d+.-]*:/u.test(prefix)
342
+ !cannotStartPackageName(prefix) &&
343
+ !hasURLScheme(prefix)
306
344
  );
307
345
  }
@@ -1220,7 +1220,8 @@ export interface Decoded {
1220
1220
 
1221
1221
  /**
1222
1222
  * The data members the decodes and copies of `files` fill, as {@link Decoded} names them.
1223
- * A generic declared outside the analysis, or only ambiently, fills the type that a type
1223
+ * A generic declared outside the analysis, or only ambiently, or one that reads a value
1224
+ * {@link readTypes} finds untyped as its own type parameter, fills the type that a type
1224
1225
  * parameter of its own no parameter names binds, where the awaited result carries that
1225
1226
  * type through its data members rather than only naming it, as a store's methods do: no
1226
1227
  * argument hands the value over, so it fills it where the analysis cannot see, and may
@@ -1282,6 +1283,10 @@ function decodedOf<Brand>(
1282
1283
  for (const file of files) {
1283
1284
  file.forEachChild(visit);
1284
1285
  }
1286
+ const read = readTypes(project, untyped);
1287
+ // A generic of the analysis that reads an untyped value as its own type parameter is a
1288
+ // client of what made the value, and fills what that parameter binds as an outside one does.
1289
+ const clients = new Set(read.made.flatMap((type) => (type.isTypeParameter() ? [type.id] : [])));
1285
1290
  const filled: { readonly carried: Type; readonly types: readonly Type[] }[] = [];
1286
1291
  const resolved = queries.resolvedSignatures(calls).map(must);
1287
1292
  for (const signature of resolved) {
@@ -1296,7 +1301,8 @@ function decodedOf<Brand>(
1296
1301
  if (
1297
1302
  declaration === undefined ||
1298
1303
  (inAnalysis(declaration.path) &&
1299
- (node === undefined || (node.flags & NodeFlags.Ambient) === 0))
1304
+ (node === undefined || (node.flags & NodeFlags.Ambient) === 0) &&
1305
+ !generic.some((id) => clients.has(id)))
1300
1306
  ) {
1301
1307
  continue;
1302
1308
  }
@@ -1318,7 +1324,6 @@ function decodedOf<Brand>(
1318
1324
  filled.push({ carried: awaited(project, given), types });
1319
1325
  }
1320
1326
  }
1321
- const read = readTypes(project, untyped);
1322
1327
  const copies = copiedNames(project, spreads, calls);
1323
1328
  if (
1324
1329
  filled.length === 0 &&
package/src/stores.ts CHANGED
@@ -12,7 +12,9 @@ import {
12
12
  import { nodeKey, type Inventory } from "./inventory.ts";
13
13
  import type { Reference } from "./references.ts";
14
14
  import type { ProjectView } from "./session.ts";
15
- import type { Exemption, Mode } from "./sweep.ts";
15
+ import type { Exemption } from "./exempt.ts";
16
+ import type { Mode } from "./tests/file-class.ts";
17
+ import { classedAsTest, madeByTestCode, withheldBy } from "./tests/file-class.ts";
16
18
 
17
19
  /** One run's stores and reads, over every configuration. */
18
20
  export interface Stores {
@@ -39,6 +41,8 @@ export interface Stores {
39
41
  /** What one configuration contributes to the run's stores. */
40
42
  interface ConfigurationStores {
41
43
  readonly references: readonly Reference[];
44
+ /** The configuration's test-support files, whose references are test code's. */
45
+ readonly support: ReadonlySet<string>;
42
46
  readonly accessors: readonly string[];
43
47
  }
44
48
 
@@ -71,12 +75,20 @@ export function storesOf(
71
75
  ): Stores {
72
76
  return {
73
77
  references: per.flatMap((one) =>
74
- one.references.filter((reference) => !mode.production || !reference.test),
78
+ one.references.filter((reference) => !withheldBy(mode, reference, one.support)),
75
79
  ),
76
80
  readInTests: new Set(
77
81
  per.flatMap((one) =>
78
82
  one.references
79
- .filter((reference) => reference.test && reference.use !== "write")
83
+ .filter(
84
+ (reference) =>
85
+ reference.use !== "write" &&
86
+ classedAsTest(
87
+ madeByTestCode(reference, one.support),
88
+ reference.consumer !== undefined,
89
+ mode,
90
+ ),
91
+ )
80
92
  .map((reference) => reference.to),
81
93
  ),
82
94
  ),
@@ -23,10 +23,9 @@ import {
23
23
  import { parenthesesRemoved } from "./unwrap.ts";
24
24
  import type { Symbol as TSSymbol, Type } from "@typescript/native/sync";
25
25
  import type { AliasChains } from "./alias-chain.ts";
26
- import { nodeKey, type Inventory, type InventorySymbol } from "./inventory.ts";
27
- import { renderPosition } from "./position.ts";
28
- import { isAnswered, type PropertyTables } from "./query.ts";
29
- import type { Reference } from "./references.ts";
26
+ import type { InventorySymbol } from "./inventory.ts";
27
+ import { isAnswered, UNANSWERED, type PropertyTables } from "./query.ts";
28
+ import type { Read, ReadRule } from "./references/read-rule.ts";
30
29
  import type { ProjectView } from "./session.ts";
31
30
 
32
31
  /** One element access whose key is a name, and how the access uses the member it selects. */
@@ -193,104 +192,105 @@ function wellKnownName(name: Node | undefined): string | undefined {
193
192
  }
194
193
 
195
194
  /**
196
- * The references the iterations of one project's own files make to the iteration members
197
- * of the values they iterate: a `for…of` statement and a spread element read
198
- * `[Symbol.iterator]`, and a `for await…of` statement reads `[Symbol.asyncIterator]` and,
199
- * which it falls back to, `[Symbol.iterator]`. Each is made by the declaration holding it.
195
+ * The reads the iterations of a side's files make of the iteration members of the values they
196
+ * iterate: a `for…of` statement and a spread element read `[Symbol.iterator]`, and a
197
+ * `for await…of` statement reads `[Symbol.asyncIterator]` and, which it falls back to,
198
+ * `[Symbol.iterator]`. Each is made by the declaration holding it. A value whose type or
199
+ * members the checker leaves unanswered holds the members those names could spell.
200
200
  */
201
- export function iterationReferences<Brand>(
202
- project: ProjectView<Brand>,
203
- held: Inventory,
204
- chains: AliasChains,
205
- root: string,
206
- testFiles: ReadonlySet<string>,
207
- ): readonly Reference[] {
208
- const queries = project.queries;
209
- const ownPaths = project.ownPaths();
210
- const iterated: {
211
- readonly file: SourceFile;
212
- readonly from: string;
213
- readonly operand: Expression;
214
- readonly protocols: readonly Protocol[];
215
- }[] = [];
216
- for (const file of project.ownSourceFiles()) {
217
- const fileId = held.declarations.get(nodeKey(file, file));
218
- if (fileId === undefined) {
219
- continue;
220
- }
221
- const visit = (node: Node, from: string): void => {
222
- const holder = held.declarations.get(nodeKey(file, node)) ?? from;
223
- if (isForOfStatement(node)) {
224
- const protocols: readonly Protocol[] =
225
- node.awaitModifier === undefined ? ["iterator"] : ["asyncIterator", "iterator"];
226
- iterated.push({ file, from: holder, operand: node.expression, protocols });
227
- } else if (isSpreadElement(node)) {
228
- iterated.push({ file, from: holder, operand: node.expression, protocols: ["iterator"] });
229
- }
230
- node.forEachChild((child) => {
231
- visit(child, holder);
232
- });
233
- };
234
- file.forEachChild((child) => {
235
- visit(child, fileId);
236
- });
237
- }
238
- if (iterated.length === 0) {
239
- return [];
240
- }
241
- const types = queries.typesAt(iterated.map((one) => one.operand));
242
- const members = new Map<
243
- number,
244
- readonly { readonly name: string; readonly ids: readonly string[] }[]
245
- >();
246
- const membersOf = (
247
- type: Type,
248
- ): readonly { readonly name: string; readonly ids: readonly string[] }[] => {
249
- const known = members.get(type.id);
250
- if (known !== undefined) {
251
- return known;
252
- }
253
- const keyed: { name: string; ids: readonly string[] }[] = [];
254
- const properties = queries.propertiesOf(type);
255
- for (const property of isAnswered(properties) ? properties : []) {
256
- for (const handle of property.declarations) {
257
- const node = ownPaths.has(handle.path) ? project.declarationAt(handle)?.node : undefined;
258
- const name = wellKnownName((node as { readonly name?: Node } | undefined)?.name);
259
- if (name !== undefined) {
260
- keyed.push({ name, ids: chains.declarationsOf(property) });
261
- break;
262
- }
263
- }
264
- }
265
- members.set(type.id, keyed);
266
- return keyed;
267
- };
268
- const found: Reference[] = [];
269
- iterated.forEach((one, index) => {
270
- const type = types[index];
271
- if (type === undefined || !isAnswered(type)) {
272
- return;
273
- }
274
- const parts =
275
- type.isUnionType() || type.isIntersectionType() ? queries.constituents(type) : [type];
276
- const position = renderPosition(one.file, root, one.operand.getStart());
277
- for (const part of isAnswered(parts) ? parts : []) {
278
- for (const member of membersOf(part)) {
279
- if (!(one.protocols as readonly string[]).includes(member.name)) {
280
- continue;
201
+ export const iteration: ReadRule = {
202
+ start() {
203
+ const iterated: {
204
+ readonly file: SourceFile;
205
+ readonly from: string;
206
+ readonly operand: Expression;
207
+ readonly protocols: readonly Protocol[];
208
+ }[] = [];
209
+ return {
210
+ visit(node, { file, from }) {
211
+ if (isForOfStatement(node)) {
212
+ const protocols: readonly Protocol[] =
213
+ node.awaitModifier === undefined ? ["iterator"] : ["asyncIterator", "iterator"];
214
+ iterated.push({ file, from, operand: node.expression, protocols });
215
+ } else if (isSpreadElement(node)) {
216
+ iterated.push({ file, from, operand: node.expression, protocols: ["iterator"] });
281
217
  }
282
- for (const to of member.ids) {
283
- found.push({
284
- from: one.from,
285
- to,
286
- position,
287
- use: "read",
288
- resolution: "keyed",
289
- test: testFiles.has(position.path),
290
- });
218
+ },
219
+ reads({ project, chains }) {
220
+ const found: Read[] = [];
221
+ if (iterated.length === 0) {
222
+ return found;
291
223
  }
292
- }
293
- }
294
- });
295
- return found;
296
- }
224
+ const queries = project.queries;
225
+ const ownPaths = project.ownPaths();
226
+ const types = queries.typesAt(iterated.map((one) => one.operand));
227
+ const members = new Map<
228
+ number,
229
+ readonly { readonly name: string; readonly ids: readonly string[] }[] | typeof UNANSWERED
230
+ >();
231
+ const membersOf = (type: Type) => {
232
+ const known = members.get(type.id);
233
+ if (known !== undefined) {
234
+ return known;
235
+ }
236
+ const properties = queries.propertiesOf(type);
237
+ const keyed: { name: string; ids: readonly string[] }[] = [];
238
+ for (const property of isAnswered(properties) ? properties : []) {
239
+ for (const handle of property.declarations) {
240
+ const node = ownPaths.has(handle.path)
241
+ ? project.declarationAt(handle)?.node
242
+ : undefined;
243
+ const name = wellKnownName((node as { readonly name?: Node } | undefined)?.name);
244
+ if (name !== undefined) {
245
+ keyed.push({ name, ids: chains.declarationsOf(property) });
246
+ break;
247
+ }
248
+ }
249
+ }
250
+ const result = isAnswered(properties) ? keyed : UNANSWERED;
251
+ members.set(type.id, result);
252
+ return result;
253
+ };
254
+ // A member a well-known symbol keys is named by the symbol's computed name.
255
+ const spelled = (protocols: readonly Protocol[]): readonly string[] =>
256
+ protocols.map((protocol) => `Symbol.${protocol}`);
257
+ iterated.forEach((one, index) => {
258
+ const type = types[index];
259
+ if (type === UNANSWERED) {
260
+ found.push({ kind: "held", from: one.from, names: spelled(one.protocols) });
261
+ return;
262
+ }
263
+ if (type === undefined) {
264
+ return;
265
+ }
266
+ const parts =
267
+ type.isUnionType() || type.isIntersectionType() ? queries.constituents(type) : [type];
268
+ for (const part of isAnswered(parts) ? parts : [type]) {
269
+ const keyed = membersOf(part);
270
+ if (keyed === UNANSWERED) {
271
+ found.push({ kind: "held", from: one.from, names: spelled(one.protocols) });
272
+ continue;
273
+ }
274
+ for (const member of keyed) {
275
+ if (!(one.protocols as readonly string[]).includes(member.name)) {
276
+ continue;
277
+ }
278
+ for (const to of member.ids) {
279
+ found.push({
280
+ kind: "named",
281
+ from: one.from,
282
+ to,
283
+ file: one.file,
284
+ at: one.operand,
285
+ use: "read",
286
+ via: "keyed",
287
+ });
288
+ }
289
+ }
290
+ }
291
+ });
292
+ return found;
293
+ },
294
+ };
295
+ },
296
+ };
@@ -0,0 +1,135 @@
1
+ /**
2
+ * The file classes the run counts references by. A test file is one a `ts.test_files`
3
+ * pattern or a test runner's row names (the test-runner entry source decides which), a test
4
+ * file no project holds, or a consumer's file a pattern names below the consumer's root.
5
+ * A test-support file is test code as well, decided once the references are known
6
+ * (`support.ts`), so a reference carries only whether a test file made it and the counters
7
+ * read the support class beside it.
8
+ */
9
+
10
+ import type { SourceFile } from "@typescript/native/ast";
11
+ import type { Edge } from "../liveness/graph.ts";
12
+ import { renderPosition } from "../position.ts";
13
+ import type { Reference } from "../references.ts";
14
+ import type { ProjectView } from "../session.ts";
15
+ import { bytewise } from "../text/compare.ts";
16
+ import { globExpression } from "../text/glob.ts";
17
+
18
+ /**
19
+ * The run's reference mode, decided once by the composition and read by every stage
20
+ * that counts references.
21
+ */
22
+ export interface Mode {
23
+ /**
24
+ * Counts no reference a test file made, under either relation. Every reference a root
25
+ * in a test file makes is such a reference, so nothing only a test reaches is live.
26
+ */
27
+ readonly production: boolean;
28
+ /**
29
+ * Classes a reference a loaded consumer's test file made as a production one rather than a
30
+ * test one, `analysis.consumer_tests` at `production`: a classification, not a filter, so it
31
+ * decides the split a candidate reports as well as what a production sweep counts.
32
+ */
33
+ readonly consumerTestsProduction?: boolean;
34
+ }
35
+
36
+ /** One rule that classified files as test files, and how many files it matched. */
37
+ export interface TestFileRule {
38
+ /** The rule's name, as a report's own rule list spells one. */
39
+ readonly rule: string;
40
+ readonly matched: number;
41
+ }
42
+
43
+ /** Whether one file of a side is a test file. */
44
+ export type TestFile = (file: SourceFile) => boolean;
45
+
46
+ /** The test files of the target: each file the test-runner source names, by its file name. */
47
+ export function targetTestFile(named: ReadonlySet<string>): TestFile {
48
+ return (file) => named.has(file.fileName);
49
+ }
50
+
51
+ /** The test files a pattern names, each by its path below `root`: a consumer's, below the consumer's root. */
52
+ export function patternTestFile(patterns: readonly string[], root: string): TestFile {
53
+ const expressions = patterns.map(globExpression);
54
+ return (file) => {
55
+ const path = renderPosition(file, root, 0).path;
56
+ return expressions.some((expression) => expression.test(path));
57
+ };
58
+ }
59
+
60
+ /** The own files of a project that are test files, by their path below `root`, ascending. */
61
+ export function testFilePaths<Brand>(
62
+ project: ProjectView<Brand>,
63
+ root: string,
64
+ testFile: TestFile,
65
+ ): readonly string[] {
66
+ return project
67
+ .ownSourceFiles()
68
+ .filter(testFile)
69
+ .map((file) => renderPosition(file, root, 0).path)
70
+ .sort(bytewise);
71
+ }
72
+
73
+ /** Whether test code made one reference: a test file, or a test-support file of the target. */
74
+ export function madeByTestCode(reference: Reference, support: ReadonlySet<string>): boolean {
75
+ return (
76
+ reference.test || (reference.consumer === undefined && support.has(reference.position.path))
77
+ );
78
+ }
79
+
80
+ /** Whether the mode classes a reference test code made as a test one: any but a loaded consumer's it classes as production. */
81
+ export function classedAsTest(test: boolean, consumer: boolean, mode: Mode): boolean {
82
+ return test && !(consumer && mode.consumerTestsProduction === true);
83
+ }
84
+
85
+ /** Whether a production mode counts no reference of this one: test code made it, as the mode classes it. */
86
+ export function withheldBy(
87
+ mode: Mode,
88
+ reference: Reference,
89
+ support: ReadonlySet<string>,
90
+ ): boolean {
91
+ return (
92
+ mode.production &&
93
+ classedAsTest(madeByTestCode(reference, support), reference.consumer !== undefined, mode)
94
+ );
95
+ }
96
+
97
+ /** Whether a production mode follows no edge like this one: {@link withheldBy} for a graph edge. */
98
+ export function edgeWithheldBy(mode: Mode, edge: Edge): boolean {
99
+ return mode.production && classedAsTest(edge.test, edge.consumer, mode);
100
+ }
101
+
102
+ /** The rule a file the analysis classified as test-support code is counted under. */
103
+ const TEST_SUPPORT_RULE = "test-support";
104
+
105
+ /**
106
+ * The name one test-file rule takes. A rule's name is a word list the report's own
107
+ * vocabulary spells, which cannot carry a pattern, so a rule is named by the place of
108
+ * its pattern in the configured list.
109
+ */
110
+ function patternRule(index: number): string {
111
+ return `test-file-pattern-${String(index + 1)}`;
112
+ }
113
+
114
+ /**
115
+ * The rules over the test files of several projects at once, ordered by rule: a file two
116
+ * projects hold is one file, so each rule counts the distinct paths it matches.
117
+ */
118
+ export function testFileRulesOf(
119
+ testFiles: readonly string[],
120
+ paths: readonly string[],
121
+ supportFiles: readonly string[] = [],
122
+ ): readonly TestFileRule[] {
123
+ const distinct = [...new Set(paths)];
124
+ const support = new Set(supportFiles).size;
125
+ return [
126
+ ...testFiles.map((pattern, index) => {
127
+ const expression = globExpression(pattern);
128
+ return {
129
+ rule: patternRule(index),
130
+ matched: distinct.filter((path) => expression.test(path)).length,
131
+ };
132
+ }),
133
+ ...(support === 0 ? [] : [{ rule: TEST_SUPPORT_RULE, matched: support }]),
134
+ ].sort((a, b) => bytewise(a.rule, b.rule));
135
+ }
@@ -19,19 +19,25 @@ import {
19
19
  type Node,
20
20
  type SourceFile,
21
21
  } from "@typescript/native/ast";
22
- import { RESOLVED_EXTENSIONS, type ParseFile } from "./entries/configuration-files.ts";
23
- import { emittedFrom } from "./load/emit-map.ts";
24
- import type { Host } from "./host.ts";
25
- import type { Inventory } from "./inventory.ts";
26
- import { namesImported } from "./module-declarations.ts";
27
- import { dirnamePath, joinPath, normalizePath, queriedModulePath } from "./paths.ts";
28
- import { renderPosition } from "./position.ts";
29
- import { nameComponent } from "./ref.ts";
30
- import type { Reference } from "./references.ts";
22
+ import { RESOLVED_EXTENSIONS, type ParseFile } from "../entries/configuration-files.ts";
23
+ import { emittedFrom } from "../load/emit-map.ts";
24
+ import type { Host } from "../host.ts";
25
+ import type { Inventory } from "../inventory.ts";
26
+ import { namesImported } from "../import-bindings.ts";
27
+ import {
28
+ dirnamePath,
29
+ joinPath,
30
+ normalizePath,
31
+ queriedModulePath,
32
+ startsRelativePath,
33
+ } from "../paths.ts";
34
+ import { renderPosition } from "../position.ts";
35
+ import { nameComponent } from "../ref.ts";
36
+ import type { Reference } from "../references.ts";
31
37
 
32
38
  /** The paths below the target root one relative specifier may name, written path first. */
33
39
  function candidates(dir: string, specifier: string): readonly string[] {
34
- if (!specifier.startsWith("./") && !specifier.startsWith("../")) {
40
+ if (!startsRelativePath(specifier)) {
35
41
  return [];
36
42
  }
37
43
  const path = normalizePath(dir === "" ? specifier : `${dir}/${specifier}`);