@cplieger/deadset-ts 6.1.1-dev.1 → 7.0.0-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/src/analysis.js +39 -16
  2. package/dist/src/conformance-record.js +2 -2
  3. package/dist/src/contextual-uses.js +27 -3
  4. package/dist/src/convention-rows.js +42 -0
  5. package/dist/src/conventions.js +48 -0
  6. package/dist/src/dependencies.js +109 -38
  7. package/dist/src/findings/completion.js +6 -7
  8. package/dist/src/findings/dependencies-and-module-machinery.js +6 -26
  9. package/dist/src/html-entries.js +29 -14
  10. package/dist/src/indexed-access.js +371 -81
  11. package/dist/src/intra-function-parts.js +57 -11
  12. package/dist/src/kinds.js +6 -6
  13. package/dist/src/references.js +60 -21
  14. package/dist/src/roots.js +96 -6
  15. package/dist/src/session.js +6 -1
  16. package/dist/src/source-files.js +1 -0
  17. package/dist/src/sweep.js +29 -17
  18. package/dist/src/symbol-keys.js +114 -0
  19. package/dist/src/test-joins.js +101 -0
  20. package/dist/src/version.js +1 -1
  21. package/dist/src/workflow-steps.js +21 -15
  22. package/dist/src/workspace-programs.js +1 -1
  23. package/jsr.json +1 -1
  24. package/package.json +1 -1
  25. package/src/analysis.ts +45 -17
  26. package/src/conformance-record.ts +2 -2
  27. package/src/contextual-uses.ts +36 -4
  28. package/src/convention-rows.ts +57 -0
  29. package/src/conventions.ts +59 -0
  30. package/src/dependencies.ts +134 -46
  31. package/src/finding.ts +2 -0
  32. package/src/findings/completion.ts +7 -7
  33. package/src/findings/dependencies-and-module-machinery.ts +9 -28
  34. package/src/html-entries.ts +33 -15
  35. package/src/indexed-access.ts +468 -102
  36. package/src/intra-function-parts.ts +84 -12
  37. package/src/kinds.ts +7 -6
  38. package/src/references.ts +86 -27
  39. package/src/roots.ts +129 -6
  40. package/src/session.ts +7 -1
  41. package/src/source-files.ts +3 -0
  42. package/src/sweep.ts +29 -17
  43. package/src/symbol-keys.ts +172 -0
  44. package/src/test-joins.ts +120 -0
  45. package/src/version.ts +1 -1
  46. package/src/workflow-steps.ts +23 -16
  47. package/src/workspace-programs.ts +1 -1
@@ -1,34 +1,46 @@
1
1
  /**
2
- * The members an indexed-access type names. `T["a" | "b"]` reads the members `a` and `b`
3
- * of `T` as a property access by either name would. Where the index is a type parameter
4
- * a call fixes, the call names the members the literal type argument names, written or
5
- * inferred, which is read off the parameter the type parameter annotates whole. The type
6
- * query of a type-query alias is no reference.
2
+ * The members an indexed-access type names: `T["a" | "b"]` reads `a` and `b` of `T`. An
3
+ * index naming a type parameter is read once the reference or the call holding it fixes
4
+ * that parameter, and a call of a method of a generic receiver fixes the receiver's by its
5
+ * type arguments. The type query of a type-query alias is no reference.
7
6
  */
8
7
 
9
8
  import {
10
9
  isArrowFunction,
11
10
  isCallExpression,
11
+ isClassDeclaration,
12
+ isClassExpression,
12
13
  isFunctionDeclaration,
13
14
  isFunctionExpression,
14
15
  isIdentifier,
16
+ isNonNullExpression,
17
+ isParenthesizedExpression,
18
+ isNamedImports,
19
+ isNamespaceImport,
20
+ isImportDeclaration,
15
21
  isIndexedAccessTypeNode,
22
+ isInterfaceDeclaration,
16
23
  isLiteralTypeNode,
17
24
  isMethodDeclaration,
25
+ isMethodSignatureDeclaration,
18
26
  isNewExpression,
19
27
  isNoSubstitutionTemplateLiteral,
20
28
  isNumericLiteral,
21
29
  isParenthesizedTypeNode,
22
30
  isPropertyAccessExpression,
23
31
  isStringLiteral,
32
+ isTypeAliasDeclaration,
24
33
  isTypeReferenceNode,
25
34
  isUnionTypeNode,
26
- SyntaxKind,
35
+ isVariableDeclaration,
36
+ type CallExpression,
37
+ type NewExpression,
27
38
  type Node,
28
39
  type SourceFile,
29
40
  type TypeNode,
41
+ type TypeReferenceNode,
30
42
  } from "@typescript/native/unstable/ast";
31
- import type { Type } from "@typescript/native/unstable/sync";
43
+ import { SymbolFlags, type Symbol as TSSymbol, type Type } from "@typescript/native/unstable/sync";
32
44
  import type { AliasChains } from "./alias-chain.ts";
33
45
  import { nodeKey, type Inventory } from "./inventory.ts";
34
46
  import { renderPosition } from "./position.ts";
@@ -37,22 +49,32 @@ import type { Reference } from "./references.ts";
37
49
  import type { ProjectView } from "./session.ts";
38
50
  import { typeQueryAliases } from "./type-query-alias.ts";
39
51
 
40
- /** A function whose signature declares type parameters, by the kinds of node that can. */
52
+ /** A declaration that declares type parameters, by the kinds of node that can. */
41
53
  type Generic = Node & {
42
54
  readonly typeParameters?: readonly { readonly name: Node }[];
43
- readonly parameters: readonly { readonly name: Node; readonly type?: TypeNode }[];
55
+ readonly parameters?: readonly { readonly name: Node; readonly type?: TypeNode }[];
44
56
  };
45
57
 
46
58
  function isGeneric(node: Node): node is Generic {
47
59
  return (
48
60
  (isFunctionDeclaration(node) ||
49
61
  isMethodDeclaration(node) ||
62
+ isMethodSignatureDeclaration(node) ||
50
63
  isArrowFunction(node) ||
51
- isFunctionExpression(node)) &&
64
+ isFunctionExpression(node) ||
65
+ isTypeAliasDeclaration(node) ||
66
+ isInterfaceDeclaration(node) ||
67
+ isClassDeclaration(node) ||
68
+ isClassExpression(node)) &&
52
69
  (node as { readonly typeParameters?: readonly unknown[] }).typeParameters !== undefined
53
70
  );
54
71
  }
55
72
 
73
+ /** Whether one generic is a receiver's: a class or an interface whose methods it parameterizes. */
74
+ function isReceiver(node: Node): boolean {
75
+ return isInterfaceDeclaration(node) || isClassDeclaration(node) || isClassExpression(node);
76
+ }
77
+
56
78
  function unwrappedType(node: TypeNode): TypeNode {
57
79
  return isParenthesizedTypeNode(node) ? unwrappedType(node.type) : node;
58
80
  }
@@ -92,25 +114,150 @@ function literalTypeNames(
92
114
  return names;
93
115
  }
94
116
 
95
- /** One indexed-access type whose index is a type parameter its function declares. */
96
- interface ByParameter {
97
- readonly object: TypeNode;
98
- /** The index of the parameter the type parameter annotates whole. */
99
- readonly parameter: number;
117
+ /**
118
+ * One side of an indexed access inside a generic, as a site of the generic fixes it: the
119
+ * type parameter of that position, a type the generic writes, or a type parameter of the
120
+ * receiver whose method the generic is.
121
+ */
122
+ type Side =
123
+ | { readonly kind: "parameter"; readonly index: number }
124
+ | { readonly kind: "written"; readonly node: TypeNode }
125
+ | { readonly kind: "receiver"; readonly index: number };
126
+
127
+ /** An indexed access inside one generic whose index names a type parameter. */
128
+ interface Carried {
129
+ readonly object: Side;
130
+ readonly index: Side;
100
131
  }
101
132
 
102
- /** An indexed access to resolve: the object type, the names, and where the reference is made. */
133
+ /** A side a site has fixed: a type node it writes, a type the compiler answers, or a type parameter it passes on. */
134
+ type Fixed =
135
+ | { readonly kind: "node"; readonly node: TypeNode }
136
+ | { readonly kind: "type"; readonly type: Type }
137
+ | { readonly kind: "passed"; readonly generic: Generic; readonly index: number };
138
+
139
+ /** An indexed access to resolve: the object type, the index, and where the reference is made. */
103
140
  interface Access {
104
141
  readonly file: SourceFile;
105
142
  readonly from: string;
106
143
  readonly at: Node;
107
144
  readonly object: TypeNode | Type;
108
- readonly names: readonly string[];
145
+ /** The names a literal index spells, or the index to read as a type. */
146
+ readonly index: readonly string[] | TypeNode | Type;
147
+ }
148
+
149
+ /** A reference or a call that may fix a generic's type parameters. */
150
+ interface Site {
151
+ readonly file: SourceFile;
152
+ readonly from: string;
153
+ readonly node: TypeReferenceNode | CallExpression | NewExpression;
154
+ readonly name: string;
155
+ readonly enclosing: readonly Generic[];
156
+ }
157
+
158
+ /** The type a fixed side stands for, or none where the site passes it on. */
159
+ function concrete(fixed: Fixed): TypeNode | Type | undefined {
160
+ return fixed.kind === "node" ? fixed.node : fixed.kind === "type" ? fixed.type : undefined;
161
+ }
162
+
163
+ /** The position of the type parameter `name` among the ones a generic declares, or -1. */
164
+ function parameterAt(generic: Generic, name: string): number {
165
+ return (generic.typeParameters ?? []).findIndex(
166
+ (parameter) => isIdentifier(parameter.name) && parameter.name.text === name,
167
+ );
168
+ }
169
+
170
+ /** The type parameter one type node names whole, among the enclosing generics, innermost first. */
171
+ function namedParameter(
172
+ node: TypeNode,
173
+ enclosing: readonly Generic[],
174
+ ): { readonly generic: Generic; readonly index: number } | undefined {
175
+ const type = unwrappedType(node);
176
+ if (
177
+ !isTypeReferenceNode(type) ||
178
+ !isIdentifier(type.typeName) ||
179
+ type.typeArguments !== undefined
180
+ ) {
181
+ return undefined;
182
+ }
183
+ const spelled = type.typeName.text;
184
+ for (const generic of [...enclosing].reverse()) {
185
+ const index = parameterAt(generic, spelled);
186
+ if (index >= 0) {
187
+ return { generic, index };
188
+ }
189
+ }
190
+ return undefined;
191
+ }
192
+
193
+ /** The name a site writes to reach one generic: its own, or its variable's. */
194
+ function calledName(generic: Generic): string | undefined {
195
+ const own = (generic as { readonly name?: Node }).name;
196
+ if (own !== undefined && isIdentifier(own)) {
197
+ return own.text;
198
+ }
199
+ const parent = generic.parent as Node | undefined;
200
+ return parent !== undefined && isVariableDeclaration(parent) && isIdentifier(parent.name)
201
+ ? parent.name.text
202
+ : undefined;
203
+ }
204
+
205
+ /** The generic a declaration a symbol names is: the function a variable is initialized with included. */
206
+ function genericOf(node: Node): Generic | undefined {
207
+ if (isGeneric(node)) {
208
+ return node;
209
+ }
210
+ if (
211
+ isVariableDeclaration(node) &&
212
+ node.initializer !== undefined &&
213
+ isGeneric(node.initializer)
214
+ ) {
215
+ return node.initializer;
216
+ }
217
+ return undefined;
218
+ }
219
+
220
+ /**
221
+ * Each indexed access inside one generic whose index names a type parameter of it, read
222
+ * from its own text: the object side is a type parameter of the same generic, one of the
223
+ * receiver it is a method of, or a type it writes.
224
+ */
225
+ function carriedIn(generic: Generic): readonly Carried[] {
226
+ const found: Carried[] = [];
227
+ const receiver = generic.parent as Node | undefined;
228
+ const outer =
229
+ receiver !== undefined && isReceiver(receiver) && isGeneric(receiver) ? receiver : undefined;
230
+ const visit = (node: Node): void => {
231
+ if (isIndexedAccessTypeNode(node)) {
232
+ const index = namedParameter(node.indexType, [generic]);
233
+ if (index !== undefined) {
234
+ const own = namedParameter(node.objectType, [generic]);
235
+ const fromReceiver =
236
+ own === undefined && outer !== undefined
237
+ ? namedParameter(node.objectType, [outer])
238
+ : undefined;
239
+ const object: Side =
240
+ own !== undefined
241
+ ? { kind: "parameter", index: own.index }
242
+ : fromReceiver !== undefined
243
+ ? { kind: "receiver", index: fromReceiver.index }
244
+ : { kind: "written", node: node.objectType };
245
+ found.push({ object, index: { kind: "parameter", index: index.index } });
246
+ }
247
+ }
248
+ if (node !== generic && isGeneric(node)) {
249
+ return;
250
+ }
251
+ node.forEachChild(visit);
252
+ };
253
+ visit(generic);
254
+ return found;
109
255
  }
110
256
 
111
257
  /**
112
258
  * The references the indexed-access types of one project's own files make to the members
113
- * they name, each made by the declaration that holds the type or the call, or by the file.
259
+ * they name, each made by the declaration that holds the type, the reference or the call,
260
+ * or by the file.
114
261
  */
115
262
  export function indexedAccessReferences<Brand>(
116
263
  project: ProjectView<Brand>,
@@ -121,9 +268,27 @@ export function indexedAccessReferences<Brand>(
121
268
  ): readonly Reference[] {
122
269
  const queries = project.queries;
123
270
  const accesses: Access[] = [];
124
- const generics = new Map<string, ByParameter[]>();
125
- const calls: { file: SourceFile; from: string; call: Node; name: string }[] = [];
271
+ const carried = new Map<Generic, Carried[]>();
126
272
  const names = new Set<string>();
273
+ const sites: Site[] = [];
274
+ /** Calls passing a literal argument whose callee a package binding heads, which a dependency's generic may fix. */
275
+ const literalCalls: Site[] = [];
276
+
277
+ const carry = (generic: Generic, one: Carried): boolean => {
278
+ const known = carried.get(generic) ?? [];
279
+ const same = known.some(
280
+ (other) => JSON.stringify(sideKey(other)) === JSON.stringify(sideKey(one)),
281
+ );
282
+ if (same) {
283
+ return false;
284
+ }
285
+ carried.set(generic, [...known, one]);
286
+ const name = calledName(generic);
287
+ if (name !== undefined) {
288
+ names.add(name);
289
+ }
290
+ return true;
291
+ };
127
292
 
128
293
  for (const file of project.ownSourceFiles()) {
129
294
  const fileId = held.declarations.get(nodeKey(file, file));
@@ -131,18 +296,27 @@ export function indexedAccessReferences<Brand>(
131
296
  continue;
132
297
  }
133
298
  const aliasTypes = new Set(typeQueryAliases(file).map((alias) => alias.type));
299
+ const imported = packageBindings(file);
134
300
  const visit = (node: Node, from: string, enclosing: readonly Generic[]): void => {
135
301
  const own = held.declarations.get(nodeKey(file, node));
136
302
  const holder = own ?? from;
137
303
  const inside = isGeneric(node) ? [...enclosing, node] : enclosing;
304
+ if (isGeneric(node)) {
305
+ carriedIn(node).forEach((one) => carry(node, one));
306
+ }
138
307
  if (isIndexedAccessTypeNode(node) && !aliasTypes.has(node)) {
139
308
  const literal = literalNames(node.indexType);
140
309
  if (literal !== undefined) {
141
- accesses.push({ file, from: holder, at: node, object: node.objectType, names: literal });
142
- } else {
143
- parameterIndex(node, inside, file, generics, names);
310
+ accesses.push({ file, from: holder, at: node, object: node.objectType, index: literal });
144
311
  }
145
312
  }
313
+ if (
314
+ isTypeReferenceNode(node) &&
315
+ node.typeArguments !== undefined &&
316
+ isIdentifier(node.typeName)
317
+ ) {
318
+ sites.push({ file, from: holder, node, name: node.typeName.text, enclosing: inside });
319
+ }
146
320
  if (isCallExpression(node) || isNewExpression(node)) {
147
321
  const callee = node.expression;
148
322
  const name = isIdentifier(callee)
@@ -151,7 +325,15 @@ export function indexedAccessReferences<Brand>(
151
325
  ? callee.name.text
152
326
  : undefined;
153
327
  if (name !== undefined) {
154
- calls.push({ file, from: holder, call: node, name });
328
+ const site = { file, from: holder, node, name, enclosing: inside };
329
+ sites.push(site);
330
+ const literal = (node.arguments ?? []).some(
331
+ (argument) => isStringLiteral(argument) || isNumericLiteral(argument),
332
+ );
333
+ const head = rootName(callee);
334
+ if (literal && isCallExpression(node) && head !== undefined && imported.has(head)) {
335
+ literalCalls.push(site);
336
+ }
155
337
  }
156
338
  }
157
339
  node.forEachChild((child) => {
@@ -163,50 +345,220 @@ export function indexedAccessReferences<Brand>(
163
345
  });
164
346
  }
165
347
 
166
- for (const { file, from, call, name } of calls) {
167
- if (!names.has(name)) {
168
- continue;
169
- }
170
- const resolved = queries.resolvedSignature(call);
171
- if (resolved === UNANSWERED || resolved?.declaration === undefined) {
172
- continue;
348
+ function partsOf(type: Type): readonly Type[] {
349
+ const parts = queries.constituents(type);
350
+ return parts === UNANSWERED ? [type] : parts;
351
+ }
352
+
353
+ /** The generics one symbol declares, through the alias it may be. */
354
+ const genericsOf = (symbol: TSSymbol): readonly Generic[] => {
355
+ const target = (symbol.flags & SymbolFlags.Alias) === 0 ? symbol : queries.aliased(symbol);
356
+ if (target === UNANSWERED) {
357
+ return [];
173
358
  }
174
- const declaration = project.declarationAt(resolved.declaration)?.node;
175
- const byParameter =
176
- declaration === undefined
177
- ? undefined
178
- : generics.get(nodeKey(declaration.getSourceFile(), declaration));
179
- if (byParameter === undefined) {
180
- continue;
359
+ return target.declarations.flatMap((handle) => {
360
+ const node = project.declarationAt(handle)?.node;
361
+ const generic = node === undefined ? undefined : genericOf(node);
362
+ return generic === undefined ? [] : [generic];
363
+ });
364
+ };
365
+
366
+ /** What one site fixes for one side of a carried access. */
367
+ const fixedAt = (site: Site, side: Side, generic: Generic): Fixed | undefined => {
368
+ if (side.kind === "written") {
369
+ return { kind: "node", node: side.node };
181
370
  }
182
- for (const { object, parameter } of byParameter) {
183
- const fixed = queries.typeAtPosition(resolved, parameter);
184
- const literal =
185
- fixed === UNANSWERED ? undefined : literalTypeNames(fixed, (type) => partsOf(type));
186
- if (literal !== undefined) {
187
- accesses.push({ file, from, at: call, object, names: literal });
371
+ const { node } = site;
372
+ if (side.kind === "receiver") {
373
+ const callee = isCallExpression(node) ? node.expression : undefined;
374
+ if (callee === undefined || !isPropertyAccessExpression(callee)) {
375
+ return undefined;
376
+ }
377
+ const receiver = queries.typeAt(callee.expression);
378
+ if (receiver === UNANSWERED || receiver === undefined) {
379
+ return undefined;
188
380
  }
381
+ const args = queries.typeArguments(receiver);
382
+ const type = args === UNANSWERED ? undefined : args[side.index];
383
+ return type === undefined ? undefined : { kind: "type", type };
189
384
  }
190
- }
385
+ const written = node.typeArguments?.[side.index];
386
+ if (written !== undefined) {
387
+ const passed = namedParameter(written, site.enclosing);
388
+ return passed === undefined
389
+ ? { kind: "node", node: written }
390
+ : { kind: "passed", generic: passed.generic, index: passed.index };
391
+ }
392
+ if (isTypeReferenceNode(node)) {
393
+ return undefined;
394
+ }
395
+ const at = (generic.parameters ?? []).findIndex((parameter) => {
396
+ const type = parameter.type === undefined ? undefined : unwrappedType(parameter.type);
397
+ return (
398
+ type !== undefined &&
399
+ isTypeReferenceNode(type) &&
400
+ isIdentifier(type.typeName) &&
401
+ parameterAt(generic, type.typeName.text) === side.index
402
+ );
403
+ });
404
+ if (at < 0) {
405
+ return undefined;
406
+ }
407
+ const resolved = queries.resolvedSignature(node);
408
+ if (resolved === UNANSWERED || resolved === undefined) {
409
+ return undefined;
410
+ }
411
+ const type = queries.typeAtPosition(resolved, at);
412
+ return type === UNANSWERED ? undefined : { kind: "type", type };
413
+ };
191
414
 
192
- function partsOf(type: Type): readonly Type[] {
193
- const parts = queries.constituents(type);
194
- return parts === UNANSWERED ? [type] : parts;
415
+ /** Reads one carried access at one site: an access where both sides are fixed, a carry where one is passed on. */
416
+ const readAt = (site: Site, generic: Generic, one: Carried): boolean => {
417
+ const object = fixedAt(site, one.object, generic);
418
+ const index = object === undefined ? undefined : fixedAt(site, one.index, generic);
419
+ if (object === undefined || index === undefined) {
420
+ return false;
421
+ }
422
+ const passed = [object, index].find((fixed) => fixed.kind === "passed");
423
+ if (passed !== undefined) {
424
+ const side = (fixed: Fixed): Side | undefined =>
425
+ fixed.kind === "passed"
426
+ ? fixed.generic === passed.generic
427
+ ? { kind: "parameter", index: fixed.index }
428
+ : undefined
429
+ : fixed.kind === "node"
430
+ ? { kind: "written", node: fixed.node }
431
+ : undefined;
432
+ const objectSide = side(object);
433
+ const indexSide = side(index);
434
+ return objectSide !== undefined && indexSide !== undefined
435
+ ? carry(passed.generic, { object: objectSide, index: indexSide })
436
+ : false;
437
+ }
438
+ const objectAt = concrete(object);
439
+ const indexAt = concrete(index);
440
+ if (objectAt === undefined || indexAt === undefined) {
441
+ return false;
442
+ }
443
+ accesses.push({
444
+ file: site.file,
445
+ from: site.from,
446
+ at: site.node,
447
+ object: objectAt,
448
+ index: "kind" in indexAt ? (literalNames(indexAt) ?? indexAt) : indexAt,
449
+ });
450
+ return false;
451
+ };
452
+
453
+ // A site passing a type parameter on carries the access to its own generic, so the
454
+ // sites are read again until no generic gains an access.
455
+ const resolvedGenerics = new Map<Site, readonly Generic[]>();
456
+ const read = new Set<string>();
457
+ for (let changed = true; changed;) {
458
+ changed = false;
459
+ const pending = sites.filter((site) => names.has(site.name) && !resolvedGenerics.has(site));
460
+ const typeSites = pending.filter((site) => isTypeReferenceNode(site.node));
461
+ const symbols =
462
+ typeSites.length === 0
463
+ ? []
464
+ : queries.symbolsAt(typeSites.map((site) => (site.node as TypeReferenceNode).typeName));
465
+ typeSites.forEach((site, at) => {
466
+ const symbol = symbols[at];
467
+ resolvedGenerics.set(
468
+ site,
469
+ symbol === UNANSWERED || symbol === undefined ? [] : genericsOf(symbol),
470
+ );
471
+ });
472
+ for (const site of pending.filter((one) => !isTypeReferenceNode(one.node))) {
473
+ const resolved = queries.resolvedSignature(site.node);
474
+ const declaration =
475
+ resolved === UNANSWERED || resolved?.declaration === undefined
476
+ ? undefined
477
+ : project.declarationAt(resolved.declaration)?.node;
478
+ const generic = declaration === undefined ? undefined : genericOf(declaration);
479
+ resolvedGenerics.set(site, generic === undefined ? [] : [generic]);
480
+ }
481
+ sites.forEach((site, siteAt) => {
482
+ for (const generic of resolvedGenerics.get(site) ?? []) {
483
+ (carried.get(generic) ?? []).forEach((one, at) => {
484
+ const key = `${String(siteAt)}:${String(at)}:${nodeKey(generic.getSourceFile(), generic)}`;
485
+ if (read.has(key)) {
486
+ return;
487
+ }
488
+ read.add(key);
489
+ changed = readAt(site, generic, one) || changed;
490
+ });
491
+ }
492
+ });
195
493
  }
196
494
 
197
- const objectNodes = accesses.flatMap((one) => ("kind" in one.object ? [one.object] : []));
495
+ // A generic function or method declared outside the project's own files carries what
496
+ // its own text indexes by its type parameters and its receiver's.
497
+ const outside = literalCalls.filter((site) => !resolvedGenerics.has(site));
498
+ const methods =
499
+ outside.length === 0
500
+ ? []
501
+ : queries.symbolsAt(
502
+ outside.map((site) => {
503
+ const callee = (site.node as CallExpression).expression;
504
+ return isPropertyAccessExpression(callee) ? callee.name : callee;
505
+ }),
506
+ );
507
+ const own = project.ownPaths();
508
+ outside.forEach((site, at) => {
509
+ const named = methods[at];
510
+ const symbol =
511
+ named === UNANSWERED || named === undefined || (named.flags & SymbolFlags.Alias) === 0
512
+ ? named
513
+ : queries.aliased(named);
514
+ if (
515
+ symbol === UNANSWERED ||
516
+ symbol === undefined ||
517
+ (symbol.flags & (SymbolFlags.Function | SymbolFlags.Method)) === 0
518
+ ) {
519
+ return;
520
+ }
521
+ for (const handle of symbol.declarations) {
522
+ if (own.has(handle.path)) {
523
+ continue;
524
+ }
525
+ const node = project.declarationAt(handle)?.node;
526
+ const generic = node === undefined ? undefined : genericOf(node);
527
+ if (generic === undefined) {
528
+ continue;
529
+ }
530
+ for (const one of carriedIn(generic)) {
531
+ readAt(site, generic, one);
532
+ }
533
+ }
534
+ });
535
+
536
+ const typeNodes = accesses.flatMap((one) => [
537
+ ...("kind" in one.object ? [one.object] : []),
538
+ ...(!Array.isArray(one.index) && "kind" in one.index ? [one.index] : []),
539
+ ]);
198
540
  const typeOf = new Map<Node, Type>();
199
- queries.typesAt(objectNodes).forEach((answer, index) => {
200
- const node = objectNodes[index];
541
+ queries.typesAt(typeNodes).forEach((answer, index) => {
542
+ const node = typeNodes[index];
201
543
  if (answer !== UNANSWERED && answer !== undefined && node !== undefined) {
202
544
  typeOf.set(node, answer);
203
545
  }
204
546
  });
547
+ const asType = (side: TypeNode | Type): Type | undefined =>
548
+ "kind" in side ? typeOf.get(side) : side;
205
549
 
206
550
  const found: Reference[] = [];
207
551
  for (const access of accesses) {
208
- const type = "kind" in access.object ? typeOf.get(access.object) : access.object;
209
- if (type === undefined) {
552
+ const type = asType(access.object);
553
+ const indexType = Array.isArray(access.index)
554
+ ? undefined
555
+ : asType(access.index as TypeNode | Type);
556
+ const accessed = Array.isArray(access.index)
557
+ ? (access.index as readonly string[])
558
+ : indexType === undefined
559
+ ? undefined
560
+ : literalTypeNames(indexType, partsOf);
561
+ if (type === undefined || accessed === undefined) {
210
562
  continue;
211
563
  }
212
564
  const position = renderPosition(access.file, root, access.at.getStart());
@@ -216,7 +568,7 @@ export function indexedAccessReferences<Brand>(
216
568
  continue;
217
569
  }
218
570
  for (const property of properties) {
219
- if (!access.names.includes(property.escapedName)) {
571
+ if (!accessed.includes(property.escapedName)) {
220
572
  continue;
221
573
  }
222
574
  for (const to of chains.declarationsOf(property)) {
@@ -236,60 +588,74 @@ export function indexedAccessReferences<Brand>(
236
588
  }
237
589
 
238
590
  /**
239
- * Records an indexed-access type whose index names a type parameter of an enclosing
240
- * function that a parameter of that function is annotated with whole, so a call's
241
- * argument for that parameter fixes the index.
591
+ * The local names one file binds to a package: each name an import declaration with a
592
+ * bare specifier binds, and each variable initialized by a call or a construction whose
593
+ * callee such a name heads, read in source order.
242
594
  */
243
- function parameterIndex(
244
- node: Node & { readonly objectType: TypeNode; readonly indexType: TypeNode },
245
- enclosing: readonly Generic[],
246
- file: SourceFile,
247
- generics: Map<string, ByParameter[]>,
248
- names: Set<string>,
249
- ): void {
250
- const index = unwrappedType(node.indexType);
251
- if (!isTypeReferenceNode(index) || !isIdentifier(index.typeName)) {
252
- return;
253
- }
254
- const spelled = index.typeName.text;
255
- for (const generic of [...enclosing].reverse()) {
256
- const declares = (generic.typeParameters ?? []).some(
257
- (parameter) => isIdentifier(parameter.name) && parameter.name.text === spelled,
258
- );
259
- if (!declares) {
595
+ function packageBindings(file: SourceFile): ReadonlySet<string> {
596
+ const names = new Set<string>();
597
+ for (const statement of file.statements) {
598
+ if (
599
+ !isImportDeclaration(statement) ||
600
+ !isStringLiteral(statement.moduleSpecifier) ||
601
+ /^[./]/u.test(statement.moduleSpecifier.text)
602
+ ) {
260
603
  continue;
261
604
  }
262
- const at = generic.parameters.findIndex((parameter) => {
263
- const type = parameter.type === undefined ? undefined : unwrappedType(parameter.type);
264
- return (
265
- type !== undefined &&
266
- isTypeReferenceNode(type) &&
267
- isIdentifier(type.typeName) &&
268
- type.typeName.text === spelled
269
- );
270
- });
271
- if (at >= 0) {
272
- const key = nodeKey(file, generic);
273
- generics.set(key, [...(generics.get(key) ?? []), { object: node.objectType, parameter: at }]);
274
- const name = calledName(generic);
275
- if (name !== undefined) {
276
- names.add(name);
277
- }
605
+ const clause = statement.importClause;
606
+ if (clause?.name !== undefined) {
607
+ names.add(clause.name.text);
608
+ }
609
+ const bindings = clause?.namedBindings;
610
+ if (bindings !== undefined && isNamespaceImport(bindings)) {
611
+ names.add(bindings.name.text);
612
+ } else if (bindings !== undefined && isNamedImports(bindings)) {
613
+ bindings.elements.forEach((element) => names.add(element.name.text));
278
614
  }
279
- return;
280
615
  }
616
+ if (names.size === 0) {
617
+ return names;
618
+ }
619
+ const visit = (node: Node): void => {
620
+ if (
621
+ isVariableDeclaration(node) &&
622
+ isIdentifier(node.name) &&
623
+ node.initializer !== undefined &&
624
+ (isCallExpression(node.initializer) || isNewExpression(node.initializer))
625
+ ) {
626
+ const head = rootName(node.initializer.expression);
627
+ if (head !== undefined && names.has(head)) {
628
+ names.add(node.name.text);
629
+ }
630
+ }
631
+ node.forEachChild(visit);
632
+ };
633
+ visit(file);
634
+ return names;
281
635
  }
282
636
 
283
- /** The name a call writes to call one generic function: its own, or its variable's. */
284
- function calledName(generic: Generic): string | undefined {
285
- const own = (generic as { readonly name?: Node }).name;
286
- if (own !== undefined && isIdentifier(own)) {
287
- return own.text;
637
+ /** The identifier an expression is headed by, through property accesses and calls. */
638
+ function rootName(expression: Node): string | undefined {
639
+ let at: Node = expression;
640
+ for (;;) {
641
+ if (isIdentifier(at)) {
642
+ return at.text;
643
+ }
644
+ if (isPropertyAccessExpression(at) || isCallExpression(at) || isNewExpression(at)) {
645
+ at = at.expression;
646
+ continue;
647
+ }
648
+ if (isParenthesizedExpression(at) || isNonNullExpression(at)) {
649
+ at = at.expression;
650
+ continue;
651
+ }
652
+ return undefined;
288
653
  }
289
- const parent = generic.parent as { readonly kind: SyntaxKind; readonly name?: Node };
290
- return parent.kind === SyntaxKind.VariableDeclaration &&
291
- parent.name !== undefined &&
292
- isIdentifier(parent.name)
293
- ? parent.name.text
294
- : undefined;
654
+ }
655
+
656
+ /** What makes two carried accesses one: the kind and position of each side, or its written node. */
657
+ function sideKey(one: Carried): readonly unknown[] {
658
+ const key = (side: Side): unknown =>
659
+ side.kind === "written" ? ["written", side.node.pos, side.node.end] : [side.kind, side.index];
660
+ return [key(one.object), key(one.index)];
295
661
  }