fallow-type-aware 3.41.0 → 3.43.0

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.
package/src/protocol.mjs CHANGED
@@ -4,6 +4,7 @@ import path from "node:path";
4
4
  import { BACKEND } from "./backend.mjs";
5
5
  import {
6
6
  ANALYSIS_OPERATION,
7
+ CALL_EVIDENCE_OPERATION,
7
8
  MUTANT_TYPECHECK_OPERATION,
8
9
  QUERY_OPERATIONS as MANIFEST_QUERY_OPERATIONS,
9
10
  WIRE_PROTOCOL_VERSION,
@@ -457,6 +458,94 @@ const parseMutantTypecheckRequest = (value) => {
457
458
  return { operation: MUTANT_TYPECHECK_OPERATION, root, files };
458
459
  };
459
460
 
461
+ const CALL_EVIDENCE_REQUEST_KEYS = new Set([
462
+ "protocol_version",
463
+ "operation",
464
+ "root",
465
+ "defaults",
466
+ "classes",
467
+ "files",
468
+ ]);
469
+ const DEFAULT_CANDIDATE_KEYS = new Set(["id", "arguments"]);
470
+ const ARGUMENT_KEYS = new Set(["path", "call_start", "index"]);
471
+ const CLASS_CANDIDATE_KEYS = new Set(["id", "path", "class", "methods"]);
472
+ const MAX_DEFAULT_CANDIDATES = 50_000;
473
+ const MAX_CANDIDATE_ARGUMENTS = 200_000;
474
+ const MAX_CLASS_CANDIDATES = 20_000;
475
+ const MAX_CLASS_METHODS = 2_000;
476
+ const MAX_PROJECT_FILES = 100_000;
477
+
478
+ const parseArgument = (value, field, root) => {
479
+ requireObject(value, field);
480
+ requireExactKeys(value, ARGUMENT_KEYS, field);
481
+ const { candidatePath } = parseCandidatePath(value.path, `${field}.path`, root);
482
+ return {
483
+ path: candidatePath,
484
+ call_start: requireInteger(value.call_start, `${field}.call_start`, 0),
485
+ index: requireInteger(value.index, `${field}.index`, 0),
486
+ };
487
+ };
488
+
489
+ const requireUniqueId = (ids, id, field) => {
490
+ if (ids.has(id)) throw new Error(`duplicate ${field} id ${id}`);
491
+ ids.add(id);
492
+ return id;
493
+ };
494
+
495
+ const parseCallEvidenceRequest = (value) => {
496
+ requireExactKeys(value, CALL_EVIDENCE_REQUEST_KEYS, "request");
497
+ const root = parseRoot(value.root);
498
+ const defaultIds = new Set();
499
+ let argumentCount = 0;
500
+ const defaults = requireBoundedArray(value.defaults, "defaults", MAX_DEFAULT_CANDIDATES).map(
501
+ (candidate, index) => {
502
+ const field = `defaults[${index}]`;
503
+ requireObject(candidate, field);
504
+ requireExactKeys(candidate, DEFAULT_CANDIDATE_KEYS, field);
505
+ const args = requireBoundedArray(
506
+ candidate.arguments,
507
+ `${field}.arguments`,
508
+ MAX_CANDIDATE_ARGUMENTS,
509
+ );
510
+ argumentCount += args.length;
511
+ if (argumentCount > MAX_CANDIDATE_ARGUMENTS) {
512
+ throw new Error(`arguments exceed the ${MAX_CANDIDATE_ARGUMENTS} item limit`);
513
+ }
514
+ return {
515
+ id: requireUniqueId(defaultIds, requireInteger(candidate.id, `${field}.id`, 0), "default"),
516
+ arguments: args.map((argument, at) =>
517
+ parseArgument(argument, `${field}.arguments[${at}]`, root),
518
+ ),
519
+ };
520
+ },
521
+ );
522
+ const classIds = new Set();
523
+ const classes = requireBoundedArray(value.classes, "classes", MAX_CLASS_CANDIDATES).map(
524
+ (candidate, index) => {
525
+ const field = `classes[${index}]`;
526
+ requireObject(candidate, field);
527
+ requireExactKeys(candidate, CLASS_CANDIDATE_KEYS, field);
528
+ const { candidatePath } = parseCandidatePath(candidate.path, `${field}.path`, root);
529
+ return {
530
+ id: requireUniqueId(classIds, requireInteger(candidate.id, `${field}.id`, 0), "class"),
531
+ path: candidatePath,
532
+ class: requireString(candidate.class, `${field}.class`),
533
+ methods: [
534
+ ...new Set(
535
+ requireBoundedArray(candidate.methods, `${field}.methods`, MAX_CLASS_METHODS).map(
536
+ (method, at) => requireString(method, `${field}.methods[${at}]`),
537
+ ),
538
+ ),
539
+ ],
540
+ };
541
+ },
542
+ );
543
+ const files = requireBoundedArray(value.files, "files", MAX_PROJECT_FILES).map(
544
+ (file, index) => parseCandidatePath(file, `files[${index}]`, root).candidatePath,
545
+ );
546
+ return { operation: CALL_EVIDENCE_OPERATION, root, defaults, classes, files };
547
+ };
548
+
460
549
  export const parseRequest = (value) => {
461
550
  requireObject(value, "request");
462
551
  if (
@@ -465,6 +554,12 @@ export const parseRequest = (value) => {
465
554
  ) {
466
555
  return parseMutantTypecheckRequest(value);
467
556
  }
557
+ if (
558
+ value.protocol_version === WIRE_PROTOCOL_VERSION &&
559
+ value.operation === CALL_EVIDENCE_OPERATION
560
+ ) {
561
+ return parseCallEvidenceRequest(value);
562
+ }
468
563
  if (value.protocol_version === WIRE_PROTOCOL_VERSION) {
469
564
  return parseBatchRequest(value);
470
565
  }
@@ -494,6 +589,17 @@ export const createSemanticResponse = ({
494
589
  elapsed_ms: Math.max(0, Math.round(elapsedMs)),
495
590
  });
496
591
 
592
+ export const createCallEvidenceResponse = ({ defaults, methods, elapsedMs }) => ({
593
+ protocol_version: WIRE_PROTOCOL_VERSION,
594
+ operation: CALL_EVIDENCE_OPERATION,
595
+ sidecar_version: SIDECAR_VERSION,
596
+ backend: BACKEND.family,
597
+ backend_version: BACKEND.version,
598
+ defaults,
599
+ methods,
600
+ elapsed_ms: Math.max(0, Math.round(elapsedMs)),
601
+ });
602
+
497
603
  export const createMutantTypecheckResponse = ({ results, elapsedMs }) => ({
498
604
  protocol_version: WIRE_PROTOCOL_VERSION,
499
605
  operation: MUTANT_TYPECHECK_OPERATION,
@@ -1,4 +1,5 @@
1
- import { ast } from "./backend.mjs";
1
+ import { ast, projectCompilerOptions } from "./backend.mjs";
2
+ import { canonicalFileIdentity } from "./file-identity.mjs";
2
3
 
3
4
  const {
4
5
  isExportDeclaration,
@@ -17,17 +18,27 @@ const JAVASCRIPT_FILE = /\.[cm]?jsx?$/iu;
17
18
  // The scan keeps every identifier of these files.
18
19
  export const isJavaScriptFile = (sourceFile) => JAVASCRIPT_FILE.test(sourceFile.fileName);
19
20
 
20
- const importNames = ({ importClause }) => {
21
+ // An alias edge says that the local name of an import or export specifier
22
+ // refers to the imported name. A `from` of null means that the local name is
23
+ // always kept: a default import, a namespace import or export, or an
24
+ // `import x =` declaration.
25
+ const ALWAYS = null;
26
+ const edge = (from, to) => ({ from: from?.text ?? ALWAYS, to: to?.text });
27
+ const specifierEdge = (element) => edge(element.propertyName ?? element.name, element.name);
28
+
29
+ const importEdges = ({ importClause }) => {
21
30
  const bindings = importClause?.namedBindings;
22
- if (!bindings) return [importClause?.name];
23
- if (isNamespaceImport(bindings)) return [importClause.name, bindings.name];
24
- return [importClause.name, ...bindings.elements.map((element) => element.name)];
31
+ const defaultEdge = importClause?.name ? [{ from: ALWAYS, to: importClause.name.text }] : [];
32
+ if (!bindings) return defaultEdge;
33
+ if (isNamespaceImport(bindings))
34
+ return [...defaultEdge, { from: ALWAYS, to: bindings.name.text }];
35
+ return [...defaultEdge, ...bindings.elements.map(specifierEdge)];
25
36
  };
26
37
 
27
- const exportNames = ({ exportClause }) => {
38
+ const exportEdges = ({ exportClause }) => {
28
39
  if (!exportClause) return [];
29
- if (isNamedExports(exportClause)) return exportClause.elements.map((element) => element.name);
30
- return [exportClause.name];
40
+ if (isNamedExports(exportClause)) return exportClause.elements.map(specifierEdge);
41
+ return [{ from: ALWAYS, to: exportClause.name.text }];
31
42
  };
32
43
 
33
44
  /** Returns the statements of a `namespace` or `declare module` body, also when nested. */
@@ -36,14 +47,29 @@ export const moduleBodyStatements = (body) => {
36
47
  return body && isModuleBlock(body) ? body.statements : [];
37
48
  };
38
49
 
39
- const statementAliasNames = (statement) => {
40
- if (isImportDeclaration(statement)) return importNames(statement);
50
+ // A JavaScript file can make an alias with `exports.run =`, `exports["run"] =`
51
+ // or `module.exports.run =`, also inside a comma sequence, a condition or a
52
+ // wrapper function. The text match finds each such name, and the scan keeps
53
+ // it, because a TypeScript importer of `run` can reach a target through it.
54
+ const COMMON_JS_EXPORT =
55
+ /\b(?:module\s*\.\s*)?exports\s*(?:\.\s*([A-Za-z_$][\w$]*)|\[\s*(["'`])([^"'`]+)\2\s*\])/gu;
56
+
57
+ const commonJsExportEdges = (sourceFile) =>
58
+ isJavaScriptFile(sourceFile)
59
+ ? [...(sourceFile.text ?? "").matchAll(COMMON_JS_EXPORT)].map((match) => ({
60
+ from: ALWAYS,
61
+ to: match[1] ?? match[3],
62
+ }))
63
+ : [];
64
+
65
+ const statementAliasEdges = (statement) => {
66
+ if (isImportDeclaration(statement)) return importEdges(statement);
41
67
  if (isImportEqualsDeclaration(statement) || isNamespaceExportDeclaration(statement)) {
42
- return [statement.name];
68
+ return [{ from: ALWAYS, to: statement.name?.text }];
43
69
  }
44
- if (isExportDeclaration(statement)) return exportNames(statement);
70
+ if (isExportDeclaration(statement)) return exportEdges(statement);
45
71
  if (isModuleDeclaration(statement)) {
46
- return moduleBodyStatements(statement.body).flatMap(statementAliasNames);
72
+ return moduleBodyStatements(statement.body).flatMap(statementAliasEdges);
47
73
  }
48
74
  return [];
49
75
  };
@@ -66,19 +92,68 @@ export const ownSourceFiles = (project) =>
66
92
  .map((fileName) => project.program.getSourceFile(fileName))
67
93
  .filter((sourceFile) => isOwnSourceFile(project, sourceFile));
68
94
 
69
- const programAliasNames = (project) =>
70
- ownSourceFiles(project)
71
- .flatMap((sourceFile) => sourceFile.statements.flatMap(statementAliasNames))
72
- .map((name) => name?.text)
73
- .filter((text) => typeof text === "string");
95
+ // Alias edges come from every program file outside node_modules, also from a
96
+ // linked workspace package that the program marks as an external library.
97
+ // With `preserveSymlinks`, such a package keeps a node_modules path, so only
98
+ // then does a node_modules path need its real path, which costs a system call.
99
+ const aliasSourceFiles = (project) => {
100
+ const linked = Boolean(projectCompilerOptions(project)?.preserveSymlinks);
101
+ return project.program
102
+ .getSourceFileNames()
103
+ .filter(
104
+ (fileName) =>
105
+ !NODE_MODULES_SEGMENT.test(fileName) ||
106
+ (linked && !NODE_MODULES_SEGMENT.test(canonicalFileIdentity(fileName))),
107
+ )
108
+ .map((fileName) => project.program.getSourceFile(fileName))
109
+ .filter((sourceFile) => sourceFile && !project.program.isSourceFileDefaultLibrary(sourceFile));
110
+ };
111
+
112
+ const programAliasEdges = (project) =>
113
+ aliasSourceFiles(project)
114
+ .flatMap((sourceFile) => [
115
+ ...sourceFile.statements.flatMap(statementAliasEdges),
116
+ ...commonJsExportEdges(sourceFile),
117
+ ])
118
+ .filter(({ to }) => typeof to === "string");
119
+
120
+ /** Adds the local name of each edge whose imported name is in `names`, until no name is added. */
121
+ const closeOverAliases = (names, edges) => {
122
+ let pending = edges.filter(({ from }) => from !== ALWAYS);
123
+ let added = true;
124
+ while (added) {
125
+ added = false;
126
+ const next = [];
127
+ for (const aliasEdge of pending) {
128
+ if (!names.has(aliasEdge.from)) {
129
+ next.push(aliasEdge);
130
+ } else if (!names.has(aliasEdge.to)) {
131
+ names.add(aliasEdge.to);
132
+ added = true;
133
+ }
134
+ }
135
+ pending = next;
136
+ }
137
+ return names;
138
+ };
74
139
 
75
140
  /**
76
141
  * Returns the identifier names that can reach a symbol-use target.
77
142
  *
78
143
  * An identifier resolves to a declaration with its own name, or to an alias
79
- * that an import or export declares under that name. So the scan needs only
80
- * the target names and the names that the alias declarations of the project
81
- * files declare, project declaration files included.
144
+ * that an import or export declares under that name. An alias can reach a
145
+ * target only when the name that it imports or re-exports can. So the scan
146
+ * needs the target names, `default`, and the local name of each alias whose
147
+ * imported name is one of these names, followed through re-export chains. The
148
+ * local names of default imports, namespace imports and exports, and
149
+ * `import x =` declarations are always kept.
82
150
  */
83
151
  export const referenceNames = (projects, targetNames) =>
84
- new Set([...targetNames, "default", ...projects.flatMap(programAliasNames)]);
152
+ referenceNamesFromEdges(projects.flatMap(programAliasEdges), targetNames);
153
+
154
+ /** The reference names for alias edges `{ from, to }`, where `from: null` always keeps `to`. */
155
+ export const referenceNamesFromEdges = (edges, targetNames) => {
156
+ const names = new Set([...targetNames, "default"]);
157
+ edges.filter(({ from }) => from === ALWAYS).forEach(({ to }) => names.add(to));
158
+ return closeOverAliases(names, edges);
159
+ };
@@ -1,6 +1,6 @@
1
1
  import path from "node:path";
2
2
 
3
- import { sync, ast } from "./backend.mjs";
3
+ import { ast, isInferredProject, projectConfigFileName, sync } from "./backend.mjs";
4
4
 
5
5
  import { canonicalFileIdentity } from "./file-identity.mjs";
6
6
 
@@ -307,11 +307,40 @@ export const isProjectSource = (project, sourceFile) =>
307
307
  !project.program.isSourceFileDefaultLibrary(sourceFile) &&
308
308
  !project.program.isSourceFileFromExternalLibrary(sourceFile);
309
309
 
310
- export const projectSourceFiles = (project) =>
311
- project.program
310
+ // Each `getSourceFile` call transfers a file from the checker. A file below a
311
+ // node_modules directory of the project and a declaration file are never
312
+ // project sources, so their names skip the transfer. The node_modules test
313
+ // uses the path relative to the tsconfig directory, because the project itself
314
+ // can live below node_modules, and it keeps each root file that `include`
315
+ // names. TypeScript matches declaration suffixes case-sensitively.
316
+ // `isProjectSource` still checks every remaining file.
317
+ const NODE_MODULES_PATH = /(?:^|[\\/])node_modules[\\/]/u;
318
+ const DECLARATION_FILE_NAME = /\.d\.(?:[cm]?ts|[^./\\]+\.ts)$/u;
319
+ const sourceFilesByProgram = new WeakMap();
320
+
321
+ const sourceNameFilter = (project) => {
322
+ const configFileName = projectConfigFileName(project);
323
+ const configDirectory = isInferredProject(project) ? null : path.dirname(configFileName);
324
+ const rootFiles = new Set(project.rootFiles ?? []);
325
+ const belowNodeModules = (fileName) =>
326
+ configDirectory !== null &&
327
+ !rootFiles.has(fileName) &&
328
+ NODE_MODULES_PATH.test(path.relative(configDirectory, fileName));
329
+ return (fileName) => !DECLARATION_FILE_NAME.test(fileName) && !belowNodeModules(fileName);
330
+ };
331
+
332
+ /** The project source files of a program, read one time for each program. */
333
+ export const projectSourceFiles = (project) => {
334
+ const cached = sourceFilesByProgram.get(project.program);
335
+ if (cached) return cached;
336
+ const sourceFiles = project.program
312
337
  .getSourceFileNames()
338
+ .filter(sourceNameFilter(project))
313
339
  .map((fileName) => project.program.getSourceFile(fileName))
314
340
  .filter((sourceFile) => isProjectSource(project, sourceFile));
341
+ sourceFilesByProgram.set(project.program, sourceFiles);
342
+ return sourceFiles;
343
+ };
315
344
 
316
345
  const projectExportIndexes = new WeakMap();
317
346
 
package/src/semantic.mjs CHANGED
@@ -2,9 +2,10 @@ import { createHash } from "node:crypto";
2
2
  import { existsSync } from "node:fs";
3
3
  import path from "node:path";
4
4
 
5
+ import { accessorPairExists, countsAsAccessorUse } from "./accessor-direction.mjs";
5
6
  import { ast, createApi as createBackendApi, heritageTypeName, sync } from "./backend.mjs";
6
7
 
7
- import { canonicalFileIdentity } from "./file-identity.mjs";
8
+ import { canonicalFileIdentity, resetFileIdentityCache } from "./file-identity.mjs";
8
9
  import { packagePathTargets, projectPackage } from "./project-package.mjs";
9
10
  import { projectResult, projectState } from "./project-state.mjs";
10
11
  import {
@@ -65,7 +66,6 @@ const {
65
66
  isExportDeclaration,
66
67
  isExportSpecifier,
67
68
  isFunctionBody,
68
- isGetAccessorDeclaration,
69
69
  isIdentifier,
70
70
  isImportDeclaration,
71
71
  isImportSpecifier,
@@ -73,7 +73,6 @@ const {
73
73
  isNamespaceImport,
74
74
  isPropertyAccessExpression,
75
75
  isPrivateIdentifier,
76
- isSetAccessorDeclaration,
77
76
  isStringLiteralLikeNode,
78
77
  isTypeNode,
79
78
  isTypeQueryNode,
@@ -589,6 +588,7 @@ const recordSymbolUse = (root, state, project, entry, node, namespace) => {
589
588
  const moduleEdge = moduleEdgeDeclaration(node);
590
589
  if (moduleEdge) return;
591
590
  if (!isExactExportReference(project, entry, node)) return;
591
+ if (!countsAsAccessorUse(entry.resolved.declaration, node)) return;
592
592
  const evidence = state.evidenceByQuery.get(entry.query.id);
593
593
  state.totalByQuery.set(entry.query.id, state.totalByQuery.get(entry.query.id) + 1);
594
594
  state.directFilesByQuery.get(entry.query.id).add(sourceFileIdentity(node.getSourceFile()));
@@ -666,6 +666,7 @@ const surfaceEntriesForNode = (state, project, node, namespace) => {
666
666
 
667
667
  const scanSymbolUseFile = (root, state, project, sourceFile, names) => {
668
668
  const projectEntries = classMemberEntrySetForProject(state, project);
669
+ const fileNames = isJavaScriptFile(sourceFile) ? undefined : names;
669
670
  const nodes = [];
670
671
  visit(sourceFile, (node) => {
671
672
  recordDynamicImportUncertainty(state, project, node, moduleIdentityForSpecifier);
@@ -673,7 +674,7 @@ const scanSymbolUseFile = (root, state, project, sourceFile, names) => {
673
674
  recordStringDispatchedMemberAccess(state, projectEntries, node);
674
675
  recordComputedMemberAccess(state, project, projectEntries, node);
675
676
  }
676
- if (isIdentifierReference(node, isJavaScriptFile(sourceFile) ? undefined : names)) {
677
+ if (isIdentifierReference(node, fileNames)) {
677
678
  nodes.push(node);
678
679
  return;
679
680
  }
@@ -843,22 +844,6 @@ const hasDecorator = (node) =>
843
844
  Boolean(node?.decorators?.length) ||
844
845
  Boolean(node?.modifiers?.some((modifier) => isDecorator(modifier)));
845
846
 
846
- const accessorPairExists = (declaration) => {
847
- if (!isGetAccessorDeclaration(declaration) && !isSetAccessorDeclaration(declaration)) {
848
- return false;
849
- }
850
- const owner = ownerDeclaration(declaration);
851
- const name = declarationName(declaration);
852
- return Boolean(
853
- owner?.members?.some(
854
- (member) =>
855
- member !== declaration &&
856
- declarationName(member) === name &&
857
- (isGetAccessorDeclaration(member) || isSetAccessorDeclaration(member)),
858
- ),
859
- );
860
- };
861
-
862
847
  const sameNamedMemberCount = (declaration) => {
863
848
  const owner = ownerDeclaration(declaration);
864
849
  const name = declarationName(declaration);
@@ -2098,6 +2083,7 @@ export const analyzeSemanticQueries = (
2098
2083
  { createApi = (cwd) => createBackendApi({ cwd }) } = {},
2099
2084
  ) => {
2100
2085
  if (queries.length === 0) return emptyRequestedAnalysis();
2086
+ resetFileIdentityCache();
2101
2087
  const setupStartedAt = performance.now();
2102
2088
  const api = createApi(root);
2103
2089
  try {
@@ -2136,6 +2122,7 @@ export const createSemanticSession = (
2136
2122
  throw new Error("semantic session revision must increase");
2137
2123
  }
2138
2124
  state.fileChanges = fileChanges;
2125
+ resetFileIdentityCache();
2139
2126
  const setupStartedAt = performance.now();
2140
2127
  const result = executeSemanticAnalysis({
2141
2128
  root,