carrick 0.3.139 → 0.3.141
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/package.json +6 -6
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/sidecar/dist/src/argument-modes.d.ts +56 -0
- package/sidecar/dist/src/argument-modes.js +448 -0
- package/sidecar/dist/src/capture/api.d.ts +11 -0
- package/sidecar/dist/src/capture/self-check.js +131 -1
- package/sidecar/dist/src/index.js +13 -20
- package/sidecar/dist/src/infer-cache.d.ts +0 -8
- package/sidecar/dist/src/infer-cache.js +0 -14
- package/sidecar/dist/src/library-claims.js +4 -3
- package/sidecar/dist/src/loop-element.d.ts +58 -0
- package/sidecar/dist/src/loop-element.js +307 -0
- package/sidecar/dist/src/origin.d.ts +10 -7
- package/sidecar/dist/src/origin.js +10 -7
- package/sidecar/dist/src/program-files.d.ts +34 -11
- package/sidecar/dist/src/program-files.js +52 -10
- package/sidecar/dist/src/project-loader.js +9 -0
- package/sidecar/dist/src/response-modes.d.ts +7 -2
- package/sidecar/dist/src/response-modes.js +1 -1
- package/sidecar/dist/src/retype-deferred.d.ts +34 -0
- package/sidecar/dist/src/retype-deferred.js +326 -0
- package/sidecar/dist/src/retype-selector.d.ts +43 -0
- package/sidecar/dist/src/retype-selector.js +217 -0
- package/sidecar/dist/src/retype.d.ts +11 -2
- package/sidecar/dist/src/retype.js +39 -14
- package/sidecar/dist/src/type-inferrer.d.ts +43 -7
- package/sidecar/dist/src/type-inferrer.js +232 -11
- package/sidecar/dist/src/types.d.ts +21 -1
- package/sidecar/dist/src/validators.d.ts +25 -12
- package/sidecar/dist/src/validators.js +2 -0
- package/templates/skills/carrick-reuse.md +1 -1
|
@@ -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.
|
|
58
|
-
* marks no root file as external, so after the first edit no
|
|
59
|
-
* Where a path test cannot answer (a Deno project serves npm
|
|
60
|
-
* cache), every library type then read as the user's:
|
|
61
|
-
* published as a contract and the printer inlined
|
|
62
|
-
* name. The resolver's answer for a file does not
|
|
63
|
-
* history, so it is kept here for as long as the
|
|
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.
|
|
72
|
-
* marks no root file as external, so after the first edit no
|
|
73
|
-
* Where a path test cannot answer (a Deno project serves npm
|
|
74
|
-
* cache), every library type then read as the user's:
|
|
75
|
-
* published as a contract and the printer inlined
|
|
76
|
-
* name. The resolver's answer for a file does not
|
|
77
|
-
* history, so it is kept here for as long as the
|
|
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,
|
|
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.
|
|
19
|
-
*
|
|
20
|
-
* process's roots
|
|
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
|
|
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:
|
|
27
|
-
*
|
|
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,
|
|
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.
|
|
19
|
-
*
|
|
20
|
-
* process's roots
|
|
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:
|
|
26
|
-
*
|
|
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()];
|
|
@@ -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
|
/**
|
|
@@ -35,7 +35,7 @@ import { Node, type Symbol as TsSymbol } from 'ts-morph';
|
|
|
35
35
|
* are read so far; another protocol (a socket message, a pub/sub payload)
|
|
36
36
|
* adds its own source here.
|
|
37
37
|
*/
|
|
38
|
-
export type MessageSource = 'query' | 'body' | 'unplaced';
|
|
38
|
+
export type MessageSource = 'query' | 'body' | 'argument' | 'unplaced';
|
|
39
39
|
/** One incoming-message field a response union depends on. */
|
|
40
40
|
export interface ResponseModeRead {
|
|
41
41
|
/**
|
|
@@ -43,7 +43,10 @@ export interface ResponseModeRead {
|
|
|
43
43
|
* `body`: a top-level property of the awaited result of a zero-argument
|
|
44
44
|
* `json()` the default lib or an installed library declares. `unplaced`:
|
|
45
45
|
* anything else (a header, a path parameter, the method, a framework's own
|
|
46
|
-
* accessor, a `json()` the repo declares).
|
|
46
|
+
* accessor, a `json()` the repo declares). `argument`: not the message,
|
|
47
|
+
* what the route passes (an argument, or a member of one) to a function
|
|
48
|
+
* other callers also call, which picks that function's body (carrick#2372,
|
|
49
|
+
* `argument-modes.ts`).
|
|
47
50
|
*/
|
|
48
51
|
location: MessageSource;
|
|
49
52
|
field: string;
|
|
@@ -75,3 +78,5 @@ export interface JoinedBody {
|
|
|
75
78
|
* symbol is declared by a TypeScript lib or an installed library.
|
|
76
79
|
*/
|
|
77
80
|
export declare function readResponseModes(bodies: JoinedBody[], join: (texts: string[]) => string, isLibOrExternal: (symbol: TsSymbol | undefined) => boolean): ResponseModes | undefined;
|
|
81
|
+
/** The text of a string literal, `undefined` for anything else. */
|
|
82
|
+
export declare function stringLiteral(node: Node | undefined): string | undefined;
|
|
@@ -261,7 +261,7 @@ function readCondition(condition, isLibOrExternal) {
|
|
|
261
261
|
return inside.length > 0 ? { kind: 'unread', reads: inside } : undefined;
|
|
262
262
|
}
|
|
263
263
|
/** The text of a string literal, `undefined` for anything else. */
|
|
264
|
-
function stringLiteral(node) {
|
|
264
|
+
export function stringLiteral(node) {
|
|
265
265
|
if (!node)
|
|
266
266
|
return undefined;
|
|
267
267
|
let inner = node;
|
|
@@ -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;
|
|
@@ -0,0 +1,326 @@
|
|
|
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 { ts } from 'ts-morph';
|
|
34
|
+
import { anyChild, holdsInfer, isClosedType, isTypeParameter, referencedSymbol, singleSignatureDeclaration, } from './retype-selector.js';
|
|
35
|
+
/** A shape the rule does not read: the call is left to the full retype. */
|
|
36
|
+
class Refuse extends Error {
|
|
37
|
+
}
|
|
38
|
+
const MAX_DEPTH = 64;
|
|
39
|
+
export function deferredResultNeedsUndefined(call) {
|
|
40
|
+
try {
|
|
41
|
+
return decide(call);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function decide(call) {
|
|
48
|
+
const project = call.getProject();
|
|
49
|
+
const program = project.getProgram().compilerObject;
|
|
50
|
+
const checker = project.getTypeChecker().compilerObject;
|
|
51
|
+
// T0.
|
|
52
|
+
const options = program.getCompilerOptions();
|
|
53
|
+
if ((options.strictNullChecks ?? options.strict ?? false) !== true)
|
|
54
|
+
return false;
|
|
55
|
+
// T1.
|
|
56
|
+
const typeArgs = call.getTypeArguments();
|
|
57
|
+
if (typeArgs.length === 0)
|
|
58
|
+
return false;
|
|
59
|
+
const declaration = singleSignatureDeclaration(call);
|
|
60
|
+
const parameters = declaration?.typeParameters;
|
|
61
|
+
if (!declaration || !parameters || typeArgs.length > parameters.length)
|
|
62
|
+
return false;
|
|
63
|
+
// T2.
|
|
64
|
+
const env = { roles: new Map(), values: new Map() };
|
|
65
|
+
for (let i = 0; i < parameters.length; i++) {
|
|
66
|
+
const symbol = checker.getSymbolAtLocation(parameters[i].name);
|
|
67
|
+
if (!symbol)
|
|
68
|
+
return false;
|
|
69
|
+
if (i === 0) {
|
|
70
|
+
env.roles.set(symbol, 'D');
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const written = i < typeArgs.length ? typeArgs[i].compilerNode : undefined;
|
|
74
|
+
const source = written ?? parameters[i].default;
|
|
75
|
+
if (!source)
|
|
76
|
+
return false;
|
|
77
|
+
if (written && isBareTypeParameter(written, checker)) {
|
|
78
|
+
env.roles.set(symbol, 'free');
|
|
79
|
+
}
|
|
80
|
+
else if (isClosedType(source, checker)) {
|
|
81
|
+
env.roles.set(symbol, 'closed');
|
|
82
|
+
env.values.set(symbol, source);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// T3.
|
|
89
|
+
const recorded = new Map();
|
|
90
|
+
new Walk(checker, program, recorded).walk(declaration.type, env, 0);
|
|
91
|
+
// T4.
|
|
92
|
+
const callType = call.getType().compilerType;
|
|
93
|
+
const result = checker.getAwaitedType(callType) ?? callType;
|
|
94
|
+
if (!(result.flags & ts.TypeFlags.Conditional))
|
|
95
|
+
return false;
|
|
96
|
+
const alias = result.aliasSymbol;
|
|
97
|
+
if (alias?.escapedName !== 'Awaited' ||
|
|
98
|
+
!alias.declarations?.length ||
|
|
99
|
+
!alias.declarations.every((d) => program.isSourceFileDefaultLibrary(d.getSourceFile()))) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
if (result.aliasTypeArguments?.length !== 1 ||
|
|
103
|
+
!(result.aliasTypeArguments[0].flags & ts.TypeFlags.Conditional)) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
// T5.
|
|
107
|
+
const apparent = checker.getApparentType(result);
|
|
108
|
+
const arms = apparent.isUnion() ? apparent.types : [apparent];
|
|
109
|
+
for (const property of checker.getPropertiesOfType(result)) {
|
|
110
|
+
const witnessed = arms.some((arm) => {
|
|
111
|
+
const member = checker.getPropertyOfType(arm, property.getName());
|
|
112
|
+
if (!member || member.declarations?.length !== 1)
|
|
113
|
+
return false;
|
|
114
|
+
if (recorded.get(member.declarations[0]) !== false)
|
|
115
|
+
return false;
|
|
116
|
+
const type = checker.getTypeOfSymbol(member);
|
|
117
|
+
return (type.isUnion() ? type.types : [type]).some((t) => (t.flags & ts.TypeFlags.Undefined) !== 0);
|
|
118
|
+
});
|
|
119
|
+
if (!witnessed)
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
/** A bare reference (no type arguments) to a type parameter. */
|
|
125
|
+
function isBareTypeParameter(node, checker) {
|
|
126
|
+
return ts.isTypeReferenceNode(node) && !node.typeArguments?.length && isTypeParameter(node, checker);
|
|
127
|
+
}
|
|
128
|
+
class Walk {
|
|
129
|
+
checker;
|
|
130
|
+
program;
|
|
131
|
+
recorded;
|
|
132
|
+
constructor(checker, program, recorded) {
|
|
133
|
+
this.checker = checker;
|
|
134
|
+
this.program = program;
|
|
135
|
+
this.recorded = recorded;
|
|
136
|
+
}
|
|
137
|
+
/** Whether any type reference in `node` is a parameter whose role is `D`. */
|
|
138
|
+
namesD(node, env) {
|
|
139
|
+
if (ts.isTypeReferenceNode(node)) {
|
|
140
|
+
const symbol = referencedSymbol(node, this.checker);
|
|
141
|
+
if (symbol && env.roles.get(symbol) === 'D')
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
return anyChild(node, (child) => this.namesD(child, env));
|
|
145
|
+
}
|
|
146
|
+
/** The symbol of a bare reference to a parameter `env` tracks. */
|
|
147
|
+
bareTracked(node, env) {
|
|
148
|
+
if (ts.isParenthesizedTypeNode(node))
|
|
149
|
+
return this.bareTracked(node.type, env);
|
|
150
|
+
if (!ts.isTypeReferenceNode(node) || node.typeArguments?.length)
|
|
151
|
+
return undefined;
|
|
152
|
+
const symbol = referencedSymbol(node, this.checker);
|
|
153
|
+
return symbol && env.roles.has(symbol) ? symbol : undefined;
|
|
154
|
+
}
|
|
155
|
+
walk(node, env, depth) {
|
|
156
|
+
if (depth > MAX_DEPTH)
|
|
157
|
+
throw new Refuse('too deep');
|
|
158
|
+
if (ts.isParenthesizedTypeNode(node))
|
|
159
|
+
return this.walk(node.type, env, depth + 1);
|
|
160
|
+
if (ts.isUnionTypeNode(node)) {
|
|
161
|
+
for (const member of node.types)
|
|
162
|
+
this.walk(member, env, depth + 1);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (ts.isIntersectionTypeNode(node))
|
|
166
|
+
return this.intersection(node, env, depth);
|
|
167
|
+
if (ts.isTypeLiteralNode(node))
|
|
168
|
+
return this.literal(node, env);
|
|
169
|
+
if (ts.isConditionalTypeNode(node))
|
|
170
|
+
return this.conditional(node, env, depth);
|
|
171
|
+
if (ts.isTypeReferenceNode(node))
|
|
172
|
+
return this.reference(node, env, depth);
|
|
173
|
+
if (node.kind === ts.SyntaxKind.UndefinedKeyword)
|
|
174
|
+
return;
|
|
175
|
+
if (ts.isLiteralTypeNode(node) && node.literal.kind === ts.SyntaxKind.NullKeyword)
|
|
176
|
+
return;
|
|
177
|
+
throw new Refuse(`node kind ${ts.SyntaxKind[node.kind]} at a structural position`);
|
|
178
|
+
}
|
|
179
|
+
/** Every member is a property signature; each is recorded, not walked. */
|
|
180
|
+
literal(node, env) {
|
|
181
|
+
for (const member of node.members) {
|
|
182
|
+
if (!ts.isPropertySignature(member))
|
|
183
|
+
throw new Refuse('a member that is not a property');
|
|
184
|
+
const name = propertyName(member);
|
|
185
|
+
if (name === undefined || name === 'then')
|
|
186
|
+
throw new Refuse('a member the walk cannot read');
|
|
187
|
+
this.recorded.set(member, (this.recorded.get(member) ?? false) || (!!member.type && this.namesD(member.type, env)));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
intersection(node, env, depth) {
|
|
191
|
+
const seen = new Set();
|
|
192
|
+
for (const operand of node.types) {
|
|
193
|
+
for (const name of this.operandNames(operand, 0)) {
|
|
194
|
+
if (seen.has(name))
|
|
195
|
+
throw new Refuse(`'${name}' is declared by two members`);
|
|
196
|
+
seen.add(name);
|
|
197
|
+
}
|
|
198
|
+
this.walk(operand, env, depth + 1);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* The property names an intersection member declares, reached through
|
|
203
|
+
* parentheses, alias references and unions to type literals only.
|
|
204
|
+
*/
|
|
205
|
+
operandNames(node, depth) {
|
|
206
|
+
if (depth > MAX_DEPTH)
|
|
207
|
+
throw new Refuse('too deep');
|
|
208
|
+
if (ts.isParenthesizedTypeNode(node))
|
|
209
|
+
return this.operandNames(node.type, depth + 1);
|
|
210
|
+
if (ts.isUnionTypeNode(node)) {
|
|
211
|
+
const names = new Set();
|
|
212
|
+
for (const member of node.types) {
|
|
213
|
+
for (const name of this.operandNames(member, depth + 1))
|
|
214
|
+
names.add(name);
|
|
215
|
+
}
|
|
216
|
+
return names;
|
|
217
|
+
}
|
|
218
|
+
if (ts.isTypeLiteralNode(node)) {
|
|
219
|
+
const names = new Set();
|
|
220
|
+
for (const member of node.members) {
|
|
221
|
+
const name = ts.isPropertySignature(member) ? propertyName(member) : undefined;
|
|
222
|
+
if (name === undefined)
|
|
223
|
+
throw new Refuse('a member the walk cannot read');
|
|
224
|
+
names.add(name);
|
|
225
|
+
}
|
|
226
|
+
return names;
|
|
227
|
+
}
|
|
228
|
+
if (ts.isTypeReferenceNode(node)) {
|
|
229
|
+
const declaration = this.aliasDeclaration(referencedSymbol(node, this.checker));
|
|
230
|
+
if (declaration)
|
|
231
|
+
return this.operandNames(declaration.type, depth + 1);
|
|
232
|
+
}
|
|
233
|
+
throw new Refuse('an intersection member that is not a type literal');
|
|
234
|
+
}
|
|
235
|
+
conditional(node, env, depth) {
|
|
236
|
+
if (this.namesD(node.checkType, env) || this.namesD(node.extendsType, env)) {
|
|
237
|
+
throw new Refuse('a selector names the stated type');
|
|
238
|
+
}
|
|
239
|
+
if (holdsInfer(node.extendsType))
|
|
240
|
+
throw new Refuse('infer');
|
|
241
|
+
// A closed selector on a parameter whose value is a known concrete type
|
|
242
|
+
// takes one branch, the same for any stated type.
|
|
243
|
+
const selector = this.bareTracked(node.checkType, env);
|
|
244
|
+
const value = selector ? env.values.get(selector) : undefined;
|
|
245
|
+
if (selector && value && env.roles.get(selector) === 'closed' && isClosedType(node.extendsType, this.checker)) {
|
|
246
|
+
const checked = this.checker.getTypeFromTypeNode(value);
|
|
247
|
+
const against = this.checker.getTypeFromTypeNode(node.extendsType);
|
|
248
|
+
if (!checked.isUnion() &&
|
|
249
|
+
!(checked.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.Never))) {
|
|
250
|
+
const taken = this.checker.isTypeAssignableTo(checked, against) ? node.trueType : node.falseType;
|
|
251
|
+
return this.walk(taken, env, depth + 1);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
this.walk(node.trueType, env, depth + 1);
|
|
255
|
+
this.walk(node.falseType, env, depth + 1);
|
|
256
|
+
}
|
|
257
|
+
reference(node, env, depth) {
|
|
258
|
+
const symbol = referencedSymbol(node, this.checker);
|
|
259
|
+
if (!symbol)
|
|
260
|
+
throw new Refuse('an unresolved reference');
|
|
261
|
+
if (symbol.flags & ts.SymbolFlags.TypeParameter)
|
|
262
|
+
throw new Refuse('a type parameter at a structural position');
|
|
263
|
+
const declaration = this.aliasDeclaration(symbol);
|
|
264
|
+
if (declaration)
|
|
265
|
+
return this.alias(node, declaration, env, depth);
|
|
266
|
+
const isLibraryPromise = symbol.escapedName === 'Promise' &&
|
|
267
|
+
!!symbol.declarations?.length &&
|
|
268
|
+
symbol.declarations.every((d) => this.program.isSourceFileDefaultLibrary(d.getSourceFile()));
|
|
269
|
+
if (isLibraryPromise && node.typeArguments?.length === 1) {
|
|
270
|
+
return this.walk(node.typeArguments[0], env, depth + 1);
|
|
271
|
+
}
|
|
272
|
+
throw new Refuse(`a reference to '${symbol.escapedName}' at a structural position`);
|
|
273
|
+
}
|
|
274
|
+
/** The one declaration of a type alias this walk can read. */
|
|
275
|
+
aliasDeclaration(symbol) {
|
|
276
|
+
if (!symbol || !(symbol.flags & ts.SymbolFlags.TypeAlias))
|
|
277
|
+
return undefined;
|
|
278
|
+
const declaration = symbol.declarations?.[0];
|
|
279
|
+
if (symbol.declarations?.length !== 1 || !declaration || !ts.isTypeAliasDeclaration(declaration)) {
|
|
280
|
+
return undefined;
|
|
281
|
+
}
|
|
282
|
+
return declaration;
|
|
283
|
+
}
|
|
284
|
+
/** Map the alias's parameters from this reference's arguments, then walk its body. */
|
|
285
|
+
alias(node, declaration, env, depth) {
|
|
286
|
+
const parameters = declaration.typeParameters ?? [];
|
|
287
|
+
const args = node.typeArguments ?? [];
|
|
288
|
+
if (args.length > parameters.length)
|
|
289
|
+
throw new Refuse('too many alias arguments');
|
|
290
|
+
const inner = { roles: new Map(), values: new Map() };
|
|
291
|
+
for (let i = 0; i < parameters.length; i++) {
|
|
292
|
+
const parameter = this.checker.getSymbolAtLocation(parameters[i].name);
|
|
293
|
+
if (!parameter)
|
|
294
|
+
throw new Refuse('an unreadable alias parameter');
|
|
295
|
+
const written = i < args.length ? args[i] : undefined;
|
|
296
|
+
const source = written ?? parameters[i].default;
|
|
297
|
+
if (!source)
|
|
298
|
+
throw new Refuse('a missing alias argument');
|
|
299
|
+
// A written argument is read where it was written; a default, against
|
|
300
|
+
// the alias's own earlier parameters.
|
|
301
|
+
const scope = written ? env : inner;
|
|
302
|
+
const tracked = this.bareTracked(source, scope);
|
|
303
|
+
if (tracked) {
|
|
304
|
+
inner.roles.set(parameter, scope.roles.get(tracked));
|
|
305
|
+
const value = scope.values.get(tracked);
|
|
306
|
+
if (value)
|
|
307
|
+
inner.values.set(parameter, value);
|
|
308
|
+
}
|
|
309
|
+
else if (this.namesD(source, scope)) {
|
|
310
|
+
throw new Refuse('an alias argument built from the stated type');
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
inner.roles.set(parameter, 'closed');
|
|
314
|
+
if (isClosedType(source, this.checker))
|
|
315
|
+
inner.values.set(parameter, source);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
this.walk(declaration.type, inner, depth + 1);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
function propertyName(member) {
|
|
322
|
+
const name = member.name;
|
|
323
|
+
if (ts.isIdentifier(name) || ts.isStringLiteral(name))
|
|
324
|
+
return name.text;
|
|
325
|
+
return undefined;
|
|
326
|
+
}
|