carrick 0.3.138 → 0.3.140

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 (33) hide show
  1. package/package.json +7 -6
  2. package/plugin/.claude-plugin/plugin.json +1 -1
  3. package/sidecar/dist/src/affected-set.d.ts +172 -0
  4. package/sidecar/dist/src/affected-set.js +936 -0
  5. package/sidecar/dist/src/capture/api.d.ts +34 -0
  6. package/sidecar/dist/src/capture/backfill.d.ts +37 -0
  7. package/sidecar/dist/src/capture/backfill.js +75 -0
  8. package/sidecar/dist/src/capture/index.js +419 -133
  9. package/sidecar/dist/src/capture/installed-package.js +36 -29
  10. package/sidecar/dist/src/capture/self-check.d.ts +14 -3
  11. package/sidecar/dist/src/capture/self-check.js +168 -30
  12. package/sidecar/dist/src/index.js +84 -14
  13. package/sidecar/dist/src/infer-cache.d.ts +0 -8
  14. package/sidecar/dist/src/infer-cache.js +0 -14
  15. package/sidecar/dist/src/library-claims.js +4 -3
  16. package/sidecar/dist/src/module-format.d.ts +13 -2
  17. package/sidecar/dist/src/module-format.js +31 -1
  18. package/sidecar/dist/src/origin.d.ts +10 -7
  19. package/sidecar/dist/src/origin.js +10 -7
  20. package/sidecar/dist/src/program-files.d.ts +34 -11
  21. package/sidecar/dist/src/program-files.js +52 -10
  22. package/sidecar/dist/src/project-loader.d.ts +7 -0
  23. package/sidecar/dist/src/project-loader.js +20 -0
  24. package/sidecar/dist/src/retype-deferred.d.ts +34 -0
  25. package/sidecar/dist/src/retype-deferred.js +326 -0
  26. package/sidecar/dist/src/retype-selector.d.ts +43 -0
  27. package/sidecar/dist/src/retype-selector.js +217 -0
  28. package/sidecar/dist/src/retype.d.ts +28 -3
  29. package/sidecar/dist/src/retype.js +106 -41
  30. package/sidecar/dist/src/type-inferrer.js +3 -2
  31. package/sidecar/dist/src/types.d.ts +113 -3
  32. package/sidecar/dist/src/validators.d.ts +413 -4
  33. package/sidecar/dist/src/validators.js +41 -1
@@ -28,7 +28,26 @@
28
28
  * reusing earlier answers) and their modes differ, the entry cannot be placed
29
29
  * and is left unresolved rather than given another import's mode.
30
30
  */
31
+ import tsMorphCommon from '@ts-morph/common';
31
32
  import { ts } from 'ts-morph';
33
+ // The `createHosts` ts-morph calls is a property of this same CommonJS
34
+ // exports object (ts-morph reads `common.createHosts` when a project builds
35
+ // its language service), so wrapping it here reaches every project built
36
+ // after this module loads. Both copies must be one instance, or the wrap
37
+ // would silently miss: fail at load instead.
38
+ if (tsMorphCommon.ts !== ts) {
39
+ throw new Error('@ts-morph/common is not the instance ts-morph loads; pin it to the version ts-morph requires');
40
+ }
41
+ const createHosts = tsMorphCommon.createHosts;
42
+ tsMorphCommon.createHosts = (options) => {
43
+ const hosts = createHosts(options);
44
+ const cache = options.resolutionHost.getModuleResolutionCache;
45
+ // The compiler host is the one ts-morph's own program is built with. The
46
+ // language service's programs keep their own host (a named follow-up).
47
+ if (cache)
48
+ hosts.compilerHost.getModuleResolutionCache = cache;
49
+ return hosts;
50
+ };
32
51
  /** Whether the compiler decides an import's mode from the importing file's format. */
33
52
  function formatDecidesMode(options) {
34
53
  const kind = options.module;
@@ -61,16 +80,27 @@ const UNPLACED = Symbol('unplaced');
61
80
  */
62
81
  export const moduleFormatResolutionHost = (redirect) => (host, getOptions) => {
63
82
  const resolutionHost = redirect ? redirect.resolutionHost(host) : host;
83
+ // One cache per project, handed to the compiler. The compiler resolves no
84
+ // module name itself while this host does, so only its package.json entries
85
+ // fill: each file's format scope, and the packages type references reach.
86
+ // It lives as long as the project, because no package.json changes while a
87
+ // scan runs; the edits a project sees are to its source files.
88
+ const programCache = ts.createModuleResolutionCache(process.cwd(), (fileName) => fileName, getOptions());
89
+ const packageJsonCache = programCache.getPackageJsonInfoCache();
90
+ // The resolver's own module answers stay per program (each rebuild passes
91
+ // a new options object), so an answer is never carried past a rebuild; only
92
+ // the package.json reads behind them are shared.
64
93
  let cache;
65
94
  let cacheOptions;
66
95
  const cacheFor = (options) => {
67
96
  if (!cache || cacheOptions !== options) {
68
- cache = ts.createModuleResolutionCache(process.cwd(), (fileName) => fileName, options);
97
+ cache = ts.createModuleResolutionCache(process.cwd(), (fileName) => fileName, options, packageJsonCache);
69
98
  cacheOptions = options;
70
99
  }
71
100
  return cache;
72
101
  };
73
102
  return {
103
+ getModuleResolutionCache: () => programCache,
74
104
  resolveModuleNames: (moduleNames, containingFile, _reusedNames, redirectedReference, options, containingSourceFile) => {
75
105
  const compilerOptions = options ?? getOptions();
76
106
  const resolutionCache = cacheFor(compilerOptions);
@@ -54,13 +54,16 @@ export declare function isExternalOrigin(program: ts.Program, sourceFile: ts.Sou
54
54
  * The compiler keeps that verdict on each program (rule 4 of
55
55
  * `isExternalOrigin`), but ts-morph rebuilds the program after any edit to the
56
56
  * project, such as a probe file created and removed or a file rewritten and
57
- * restored, and passes every file it has loaded as a ROOT file. The compiler
58
- * marks no root file as external, so after the first edit no dependency is.
59
- * Where a path test cannot answer (a Deno project serves npm types from its own
60
- * cache), every library type then read as the user's: framework machinery was
61
- * published as a contract and the printer inlined library types it keeps by
62
- * name. The resolver's answer for a file does not change with the project's
63
- * history, so it is kept here for as long as the project lives.
57
+ * restored, and by default passes every file it has loaded as a ROOT file.
58
+ * The compiler marks no root file as external, so after the first edit no
59
+ * dependency was. Where a path test cannot answer (a Deno project serves npm
60
+ * types from its own cache), every library type then read as the user's:
61
+ * framework machinery was published as a contract and the printer inlined
62
+ * library types it keeps by name. The resolver's answer for a file does not
63
+ * change with the project's history, so it is kept here for as long as the
64
+ * project lives. The loader's projects now pin their roots (carrick#3146), so
65
+ * a rebuild no longer names a dependency as a root; the record stays for the
66
+ * Deno resolver and for any project the loader does not build.
64
67
  *
65
68
  * The compiler's verdict also carries down: a file a library imports, even by
66
69
  * a relative path the resolver answers as not external, is reached while the
@@ -68,13 +68,16 @@ export function isExternalOrigin(program, sourceFile, repoRoot, imports) {
68
68
  * The compiler keeps that verdict on each program (rule 4 of
69
69
  * `isExternalOrigin`), but ts-morph rebuilds the program after any edit to the
70
70
  * project, such as a probe file created and removed or a file rewritten and
71
- * restored, and passes every file it has loaded as a ROOT file. The compiler
72
- * marks no root file as external, so after the first edit no dependency is.
73
- * Where a path test cannot answer (a Deno project serves npm types from its own
74
- * cache), every library type then read as the user's: framework machinery was
75
- * published as a contract and the printer inlined library types it keeps by
76
- * name. The resolver's answer for a file does not change with the project's
77
- * history, so it is kept here for as long as the project lives.
71
+ * restored, and by default passes every file it has loaded as a ROOT file.
72
+ * The compiler marks no root file as external, so after the first edit no
73
+ * dependency was. Where a path test cannot answer (a Deno project serves npm
74
+ * types from its own cache), every library type then read as the user's:
75
+ * framework machinery was published as a contract and the printer inlined
76
+ * library types it keeps by name. The resolver's answer for a file does not
77
+ * change with the project's history, so it is kept here for as long as the
78
+ * project lives. The loader's projects now pin their roots (carrick#3146), so
79
+ * a rebuild no longer names a dependency as a root; the record stays for the
80
+ * Deno resolver and for any project the loader does not build.
78
81
  *
79
82
  * The compiler's verdict also carries down: a file a library imports, even by
80
83
  * a relative path the resolver answers as not external, is reached while the
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * The files a program was built from, and files added to a program in an
3
- * order the caller gives (carrick#2027).
3
+ * order the caller gives (carrick#2027, carrick#3146).
4
4
  *
5
5
  * With `stableTypeOrdering` the compiler orders two types of the same name by
6
6
  * where their declarations sit in the program's file list. That list follows
@@ -8,24 +8,47 @@
8
8
  * imports. ts-morph hands the compiler every file it has loaded as a root, in
9
9
  * the order it loaded them, and a process asked about a file outside its
10
10
  * tsconfig loads it when it is asked. Two processes asked the same questions
11
- * in different orders therefore build from roots in different orders, and
12
- * print a union of two same-named types in different orders.
11
+ * in different orders would therefore build from roots in different orders,
12
+ * and print a union of two same-named types in different orders.
13
13
  *
14
14
  * The roots decide the whole program, not only its order. A dependency file
15
15
  * the compiler reached by resolving an import is not the same to it as the
16
16
  * same file named as a root: a second copy of a package reached by resolution
17
17
  * is folded into the first, and named as a root it is read as a file of its
18
- * own and its imports are followed. So the list here is the roots themselves.
19
- * A process that has built only what its tsconfig names, given another
20
- * process's roots in order, ends with the same roots and builds the same
21
- * program.
18
+ * own and its imports are followed. ts-morph names every file it has loaded
19
+ * as a root at each rebuild, the dependencies earlier builds reached among
20
+ * them, so a process's roots grew with its rebuilds (282 to 3,680 in one
21
+ * measured service) and a call's program depended on its process's history.
22
+ * `pinProgramRoots` ends that: a program's roots are the project's own files,
23
+ * never the compiler's library files or installed dependencies, in ts-morph's
24
+ * load order, and a rebuild does not change them.
22
25
  */
23
- import type { Project } from 'ts-morph';
26
+ import { type Project, type SourceFile } from 'ts-morph';
27
+ /**
28
+ * Pin the roots of every program `project` builds (carrick#3146): its own
29
+ * files (ts-morph's in-project files, in load order), less `resolvedByProject`,
30
+ * the files ts-morph's dependency resolution marked in-project when the
31
+ * project was built (a workspace or linked package outside `node_modules`).
32
+ * That is the first build's roots plus every file added through the project
33
+ * API since. The compiler's library files and the dependencies it reached are
34
+ * not roots, so a rebuild after an edit builds the same program and
35
+ * TypeScript can reuse the old one.
36
+ */
37
+ export declare function pinProgramRoots(project: Project, resolvedByProject: readonly SourceFile[]): void;
38
+ /**
39
+ * Set `sourceFile`'s text and keep the module format the project's resolver
40
+ * stamped on it (carrick#3146). Under `node16`..`nodenext` ts-morph's
41
+ * `replaceWithText` makes a fresh node without that format, and TypeScript
42
+ * then reuses only part of the old program. A text edit cannot change a file's
43
+ * format, which comes from its extension and its `package.json` scope, and the
44
+ * resolver stamps the same value in the rebuild, so the program is unchanged
45
+ * and the old one is reused whole.
46
+ */
47
+ export declare function replaceFileText(sourceFile: SourceFile, text: string): void;
24
48
  /**
25
49
  * The root files `project`'s program was built from, in order, as absolute
26
- * paths: every file the project had loaded when the program was last built,
27
- * the compiler's library files and the dependencies earlier builds reached
28
- * among them.
50
+ * paths: the project's own files, never the compiler's library files or
51
+ * installed dependencies (carrick#3146).
29
52
  */
30
53
  export declare function listProgramFiles(project: Project): string[];
31
54
  /**
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * The files a program was built from, and files added to a program in an
3
- * order the caller gives (carrick#2027).
3
+ * order the caller gives (carrick#2027, carrick#3146).
4
4
  *
5
5
  * With `stableTypeOrdering` the compiler orders two types of the same name by
6
6
  * where their declarations sit in the program's file list. That list follows
@@ -8,23 +8,65 @@
8
8
  * imports. ts-morph hands the compiler every file it has loaded as a root, in
9
9
  * the order it loaded them, and a process asked about a file outside its
10
10
  * tsconfig loads it when it is asked. Two processes asked the same questions
11
- * in different orders therefore build from roots in different orders, and
12
- * print a union of two same-named types in different orders.
11
+ * in different orders would therefore build from roots in different orders,
12
+ * and print a union of two same-named types in different orders.
13
13
  *
14
14
  * The roots decide the whole program, not only its order. A dependency file
15
15
  * the compiler reached by resolving an import is not the same to it as the
16
16
  * same file named as a root: a second copy of a package reached by resolution
17
17
  * is folded into the first, and named as a root it is read as a file of its
18
- * own and its imports are followed. So the list here is the roots themselves.
19
- * A process that has built only what its tsconfig names, given another
20
- * process's roots in order, ends with the same roots and builds the same
21
- * program.
18
+ * own and its imports are followed. ts-morph names every file it has loaded
19
+ * as a root at each rebuild, the dependencies earlier builds reached among
20
+ * them, so a process's roots grew with its rebuilds (282 to 3,680 in one
21
+ * measured service) and a call's program depended on its process's history.
22
+ * `pinProgramRoots` ends that: a program's roots are the project's own files,
23
+ * never the compiler's library files or installed dependencies, in ts-morph's
24
+ * load order, and a rebuild does not change them.
22
25
  */
26
+ import { Program } from 'ts-morph';
27
+ // `pinProgramRoots` wraps ts-morph's private `_reset`. If an upgrade changes
28
+ // its shape, fail at load rather than bring the growth back silently.
29
+ if (typeof Program.prototype._reset !== 'function' ||
30
+ Program.prototype._reset.length !== 2) {
31
+ throw new Error('ts-morph Program._reset changed: pinProgramRoots must be revisited (carrick#3146)');
32
+ }
33
+ /**
34
+ * Pin the roots of every program `project` builds (carrick#3146): its own
35
+ * files (ts-morph's in-project files, in load order), less `resolvedByProject`,
36
+ * the files ts-morph's dependency resolution marked in-project when the
37
+ * project was built (a workspace or linked package outside `node_modules`).
38
+ * That is the first build's roots plus every file added through the project
39
+ * API since. The compiler's library files and the dependencies it reached are
40
+ * not roots, so a rebuild after an edit builds the same program and
41
+ * TypeScript can reuse the old one.
42
+ */
43
+ export function pinProgramRoots(project, resolvedByProject) {
44
+ const resolved = new Set(resolvedByProject.map((file) => file.getFilePath()));
45
+ const program = project.getProgram();
46
+ const reset = program._reset.bind(program);
47
+ program._reset = (rootNames, host) => {
48
+ const own = new Set(project.getSourceFiles().map((file) => file.getFilePath()));
49
+ reset(rootNames.filter((name) => own.has(name) && !resolved.has(name)), host);
50
+ };
51
+ }
52
+ /**
53
+ * Set `sourceFile`'s text and keep the module format the project's resolver
54
+ * stamped on it (carrick#3146). Under `node16`..`nodenext` ts-morph's
55
+ * `replaceWithText` makes a fresh node without that format, and TypeScript
56
+ * then reuses only part of the old program. A text edit cannot change a file's
57
+ * format, which comes from its extension and its `package.json` scope, and the
58
+ * resolver stamps the same value in the rebuild, so the program is unchanged
59
+ * and the old one is reused whole.
60
+ */
61
+ export function replaceFileText(sourceFile, text) {
62
+ const format = sourceFile.compilerNode.impliedNodeFormat;
63
+ sourceFile.replaceWithText(text);
64
+ sourceFile.compilerNode.impliedNodeFormat = format;
65
+ }
23
66
  /**
24
67
  * The root files `project`'s program was built from, in order, as absolute
25
- * paths: every file the project had loaded when the program was last built,
26
- * the compiler's library files and the dependencies earlier builds reached
27
- * among them.
68
+ * paths: the project's own files, never the compiler's library files or
69
+ * installed dependencies (carrick#3146).
28
70
  */
29
71
  export function listProgramFiles(project) {
30
72
  return [...project.getProgram().compilerObject.getRootFileNames()];
@@ -187,6 +187,13 @@ export declare class ProjectLoader {
187
187
  * that, without building it (carrick#2027).
188
188
  */
189
189
  builtProject(): Project | undefined;
190
+ /**
191
+ * Every program a request has built: the default project and each owning
192
+ * project's (carrick#1604), without building any (carrick#3100).
193
+ */
194
+ builtProjects(): Project[];
195
+ /** The scanned repo's root when init named one, else the service root. */
196
+ getScanRoot(): string;
190
197
  /**
191
198
  * The registry package a resolved file belongs to, when the module graph
192
199
  * names one (carrick#1260). `undefined` for a project that did not resolve
@@ -16,6 +16,7 @@ import * as fs from 'node:fs';
16
16
  import { DenoProject, findDenoConfig, fileConfigPath, findServiceTsconfig, referenceRedirectOf, serviceConfigPath, sidecarCompilerOptions, untrustedReadBound, } from './capture/index.js';
17
17
  import { moduleFormatResolutionHost } from './module-format.js';
18
18
  import { ExternalImports, registerExternalImports } from './origin.js';
19
+ import { pinProgramRoots } from './program-files.js';
19
20
  /**
20
21
  * Source-file patterns used when the repo declares no tsconfig, relative to
21
22
  * the repo root. `node_modules` is excluded explicitly: a glob that matches
@@ -204,6 +205,7 @@ export class ProjectLoader {
204
205
  compilerOptions: sidecarCompilerOptions(optionsFor(kind, this.snapshotToCompilerOptions(snapshot))),
205
206
  skipAddingFilesFromTsConfig: true,
206
207
  });
208
+ pinProgramRoots(project, []);
207
209
  if (kind === 'listed')
208
210
  this.addDefaultSourceFiles(project);
209
211
  return project;
@@ -231,6 +233,7 @@ export class ProjectLoader {
231
233
  })),
232
234
  });
233
235
  registerExternalImports(project, imports);
236
+ pinProgramRoots(project, []);
234
237
  if (kind === 'listed') {
235
238
  for (const file of deno.parsed.fileNames)
236
239
  project.addSourceFileAtPath(file);
@@ -254,6 +257,7 @@ export class ProjectLoader {
254
257
  compilerOptions: sidecarCompilerOptions(optionsFor(kind, DEFAULT_COMPILER_OPTIONS)),
255
258
  skipAddingFilesFromTsConfig: true,
256
259
  });
260
+ pinProgramRoots(project, []);
257
261
  // Add source files from common locations
258
262
  if (kind === 'listed')
259
263
  this.addDefaultSourceFiles(project, (file) => this.projectKeyFor(file) === '');
@@ -377,13 +381,18 @@ export class ProjectLoader {
377
381
  // Over the tsconfig's own options: ts-morph lays these on top.
378
382
  compilerOptions: sidecarCompilerOptions(optionsFor(kind, options)),
379
383
  skipAddingFilesFromTsConfig: kind === 'scripts',
384
+ // The dependency pass is run below, so the files it marks in-project
385
+ // can be kept out of the program's roots (carrick#3146).
386
+ skipFileDependencyResolution: true,
380
387
  // Each import resolves in its file's own module format (carrick#1619),
381
388
  // a referenced project's unbuilt output reads as its source, under its
382
389
  // own options (carrick#2418), and every external-library answer is kept
383
390
  // past the program rebuilds that drop it (carrick#1731).
384
391
  resolutionHost: imports.recording(moduleFormatResolutionHost(referenceRedirectOf(configPath, this.repoRoot))),
385
392
  });
393
+ const resolved = kind === 'listed' ? project.resolveSourceFileDependencies() : [];
386
394
  registerExternalImports(project, imports);
395
+ pinProgramRoots(project, resolved);
387
396
  return project;
388
397
  }
389
398
  /**
@@ -509,6 +518,17 @@ export class ProjectLoader {
509
518
  builtProject() {
510
519
  return this.project ?? undefined;
511
520
  }
521
+ /**
522
+ * Every program a request has built: the default project and each owning
523
+ * project's (carrick#1604), without building any (carrick#3100).
524
+ */
525
+ builtProjects() {
526
+ return [...(this.project ? [this.project] : []), ...this.ownerProjects.values()];
527
+ }
528
+ /** The scanned repo's root when init named one, else the service root. */
529
+ getScanRoot() {
530
+ return this.scanRoot ?? this.repoRoot;
531
+ }
512
532
  /**
513
533
  * The registry package a resolved file belongs to, when the module graph
514
534
  * names one (carrick#1260). `undefined` for a project that did not resolve
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Whether the retype's "the call's type argument does not reach its result"
3
+ * can be answered before the rewrite for a call whose result stays deferred
4
+ * (carrick#3165, tier 2 of carrick#3158), for every stated type that has no
5
+ * top-level `undefined`.
6
+ *
7
+ * The shape is a generated client's method call: the first type argument is
8
+ * the response map, a later one is the enclosing function's own free type
9
+ * parameter, so the result is `Awaited` of a conditional that stays deferred.
10
+ * The reach test reads the result, its alias argument and the members of its
11
+ * apparent type. Each member is a union of the awaited arms' member types, and
12
+ * holds an `undefined` the stated type never put there, so it is identical to
13
+ * the stated type only when the stated type has a top-level `undefined`. The
14
+ * scanner applies the answer only when the producer's text has none
15
+ * (`states_no_top_level_undefined`).
16
+ *
17
+ * Reads only the consumer's code. Every rule fails closed: a shape this file
18
+ * does not understand answers `false` and the call goes to the full retype,
19
+ * which is unchanged.
20
+ *
21
+ * T0 the program is strict (`strictNullChecks`): without it `X | undefined`
22
+ * is `X` and the witness below is empty.
23
+ * T1 one signature (tier 1's C0) and a written type argument.
24
+ * T2 the first type parameter is `D` (the stated type). A later one is
25
+ * `free` (a bare type parameter written at the call) or `closed` (a
26
+ * written argument, or a default, naming no type parameter).
27
+ * T3 walk the return type. `D` never sits at a structural position, only
28
+ * inside the type of a member, which is recorded.
29
+ * T4 the result is the default library's `Awaited` of a conditional.
30
+ * T5 every property the test reads has an arm whose declaration names no
31
+ * `D` and whose type holds `undefined`.
32
+ */
33
+ import type { CallExpression } from 'ts-morph';
34
+ export declare function deferredResultNeedsUndefined(call: CallExpression): boolean;