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.
- package/package.json +7 -6
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/sidecar/dist/src/affected-set.d.ts +172 -0
- package/sidecar/dist/src/affected-set.js +936 -0
- package/sidecar/dist/src/capture/api.d.ts +34 -0
- package/sidecar/dist/src/capture/backfill.d.ts +37 -0
- package/sidecar/dist/src/capture/backfill.js +75 -0
- package/sidecar/dist/src/capture/index.js +419 -133
- package/sidecar/dist/src/capture/installed-package.js +36 -29
- package/sidecar/dist/src/capture/self-check.d.ts +14 -3
- package/sidecar/dist/src/capture/self-check.js +168 -30
- package/sidecar/dist/src/index.js +84 -14
- 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/module-format.d.ts +13 -2
- package/sidecar/dist/src/module-format.js +31 -1
- 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.d.ts +7 -0
- package/sidecar/dist/src/project-loader.js +20 -0
- 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 +28 -3
- package/sidecar/dist/src/retype.js +106 -41
- package/sidecar/dist/src/type-inferrer.js +3 -2
- package/sidecar/dist/src/types.d.ts +113 -3
- package/sidecar/dist/src/validators.d.ts +413 -4
- package/sidecar/dist/src/validators.js +41 -1
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether the type argument a retype fills can only SELECT (carrick#3158): in
|
|
3
|
+
* the call's result it appears as a conditional's check or extends type, or
|
|
4
|
+
* not at all. Such a call's result does not depend on the type stated, as
|
|
5
|
+
* long as the stated type is not a union, so the retype's "the call's type
|
|
6
|
+
* argument does not reach its result" can be answered before the rewrite for
|
|
7
|
+
* any stated type that is a new object type.
|
|
8
|
+
*
|
|
9
|
+
* Reads only the consumer's code. Every rule fails closed: a shape this
|
|
10
|
+
* file does not understand answers `false` and the call goes to the full
|
|
11
|
+
* retype, which is unchanged.
|
|
12
|
+
*
|
|
13
|
+
* C0 one signature, with type parameters and a written return type.
|
|
14
|
+
* C1 every type parameter after the first is closed: a written argument
|
|
15
|
+
* (explicit branch) or a default naming no type parameter. When the
|
|
16
|
+
* stated type breaks the first parameter's constraint, TypeScript fills
|
|
17
|
+
* unwritten arguments with their raw defaults, so a default that names
|
|
18
|
+
* a type parameter would change the result.
|
|
19
|
+
* C3 the call sits in no generic declaration: a producer type naming a
|
|
20
|
+
* type parameter in scope would defer the conditional.
|
|
21
|
+
* C2 the first parameter appears in the return type only in the check or
|
|
22
|
+
* extends type of a conditional that resolves (extends type without
|
|
23
|
+
* `infer`, only tracked type parameters named in either type).
|
|
24
|
+
*/
|
|
25
|
+
import { ts } from 'ts-morph';
|
|
26
|
+
import type { CallExpression } from 'ts-morph';
|
|
27
|
+
export declare function typeArgumentOnlySelects(call: CallExpression): boolean;
|
|
28
|
+
export declare function resolvedDeclaration(call: CallExpression): ts.SignatureDeclaration | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* C0: the declaration the call resolves to, when the callee has one call
|
|
31
|
+
* signature (not a union), that signature has type parameters, and its
|
|
32
|
+
* return type is written.
|
|
33
|
+
*/
|
|
34
|
+
export declare function singleSignatureDeclaration(call: CallExpression): (ts.SignatureDeclaration & {
|
|
35
|
+
type: ts.TypeNode;
|
|
36
|
+
}) | undefined;
|
|
37
|
+
/** Whether `test` holds for any direct child (`forEachChild` stops only on a truthy return). */
|
|
38
|
+
export declare function anyChild(node: ts.Node, test: (child: ts.Node) => boolean): boolean;
|
|
39
|
+
/** Names no type parameter, no `this` and no `typeof`. */
|
|
40
|
+
export declare function isClosedType(node: ts.Node, checker: ts.TypeChecker): boolean;
|
|
41
|
+
export declare function referencedSymbol(node: ts.TypeReferenceNode, checker: ts.TypeChecker): ts.Symbol | undefined;
|
|
42
|
+
export declare function isTypeParameter(node: ts.TypeReferenceNode, checker: ts.TypeChecker): boolean;
|
|
43
|
+
export declare function holdsInfer(node: ts.Node): boolean;
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether the type argument a retype fills can only SELECT (carrick#3158): in
|
|
3
|
+
* the call's result it appears as a conditional's check or extends type, or
|
|
4
|
+
* not at all. Such a call's result does not depend on the type stated, as
|
|
5
|
+
* long as the stated type is not a union, so the retype's "the call's type
|
|
6
|
+
* argument does not reach its result" can be answered before the rewrite for
|
|
7
|
+
* any stated type that is a new object type.
|
|
8
|
+
*
|
|
9
|
+
* Reads only the consumer's code. Every rule fails closed: a shape this
|
|
10
|
+
* file does not understand answers `false` and the call goes to the full
|
|
11
|
+
* retype, which is unchanged.
|
|
12
|
+
*
|
|
13
|
+
* C0 one signature, with type parameters and a written return type.
|
|
14
|
+
* C1 every type parameter after the first is closed: a written argument
|
|
15
|
+
* (explicit branch) or a default naming no type parameter. When the
|
|
16
|
+
* stated type breaks the first parameter's constraint, TypeScript fills
|
|
17
|
+
* unwritten arguments with their raw defaults, so a default that names
|
|
18
|
+
* a type parameter would change the result.
|
|
19
|
+
* C3 the call sits in no generic declaration: a producer type naming a
|
|
20
|
+
* type parameter in scope would defer the conditional.
|
|
21
|
+
* C2 the first parameter appears in the return type only in the check or
|
|
22
|
+
* extends type of a conditional that resolves (extends type without
|
|
23
|
+
* `infer`, only tracked type parameters named in either type).
|
|
24
|
+
*/
|
|
25
|
+
import { Node, ts } from 'ts-morph';
|
|
26
|
+
const MAX_DEPTH = 64;
|
|
27
|
+
export function typeArgumentOnlySelects(call) {
|
|
28
|
+
// A producer type may name a type parameter in scope at the call. The
|
|
29
|
+
// conditional then defers and the stated type reaches the result, so the
|
|
30
|
+
// full retype must judge it.
|
|
31
|
+
if (call.getAncestors().some((a) => Node.isTypeParametered(a) && a.getTypeParameters().length > 0)) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
const typeArgs = call.getTypeArguments();
|
|
35
|
+
const checker = call.getProject().getTypeChecker().compilerObject;
|
|
36
|
+
const declaration = singleSignatureDeclaration(call);
|
|
37
|
+
const parameters = declaration?.typeParameters;
|
|
38
|
+
if (!declaration || !parameters)
|
|
39
|
+
return false;
|
|
40
|
+
const explicit = typeArgs.length > 0;
|
|
41
|
+
if (typeArgs.length > parameters.length)
|
|
42
|
+
return false;
|
|
43
|
+
// C1.
|
|
44
|
+
const roles = new Map();
|
|
45
|
+
for (let i = 0; i < parameters.length; i++) {
|
|
46
|
+
const symbol = checker.getSymbolAtLocation(parameters[i].name);
|
|
47
|
+
if (!symbol)
|
|
48
|
+
return false;
|
|
49
|
+
if (i === 0) {
|
|
50
|
+
roles.set(symbol, 'D');
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const written = explicit && i < typeArgs.length ? typeArgs[i].compilerNode : undefined;
|
|
54
|
+
const source = written ?? parameters[i].default;
|
|
55
|
+
if (!source || !isClosedType(source, checker))
|
|
56
|
+
return false;
|
|
57
|
+
roles.set(symbol, 'closed');
|
|
58
|
+
}
|
|
59
|
+
// C2.
|
|
60
|
+
return selectsOnly(declaration.type, roles, checker, new Set(), 0);
|
|
61
|
+
}
|
|
62
|
+
export function resolvedDeclaration(call) {
|
|
63
|
+
const signature = call
|
|
64
|
+
.getProject()
|
|
65
|
+
.getTypeChecker()
|
|
66
|
+
.compilerObject.getResolvedSignature(call.compilerNode);
|
|
67
|
+
return signature?.getDeclaration();
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* C0: the declaration the call resolves to, when the callee has one call
|
|
71
|
+
* signature (not a union), that signature has type parameters, and its
|
|
72
|
+
* return type is written.
|
|
73
|
+
*/
|
|
74
|
+
export function singleSignatureDeclaration(call) {
|
|
75
|
+
const declaration = resolvedDeclaration(call);
|
|
76
|
+
const parameters = declaration?.typeParameters;
|
|
77
|
+
if (!declaration || !parameters || parameters.length === 0 || !declaration.type)
|
|
78
|
+
return undefined;
|
|
79
|
+
const callee = call.getExpression().getType();
|
|
80
|
+
if (callee.isUnion() || callee.getCallSignatures().length !== 1)
|
|
81
|
+
return undefined;
|
|
82
|
+
return declaration;
|
|
83
|
+
}
|
|
84
|
+
/** Whether `test` holds for any direct child (`forEachChild` stops only on a truthy return). */
|
|
85
|
+
export function anyChild(node, test) {
|
|
86
|
+
return ts.forEachChild(node, (child) => (test(child) ? true : undefined)) ?? false;
|
|
87
|
+
}
|
|
88
|
+
/** Names no type parameter, no `this` and no `typeof`. */
|
|
89
|
+
export function isClosedType(node, checker) {
|
|
90
|
+
if (ts.isTypeQueryNode(node) || ts.isThisTypeNode(node))
|
|
91
|
+
return false;
|
|
92
|
+
if (ts.isTypeReferenceNode(node) && isTypeParameter(node, checker))
|
|
93
|
+
return false;
|
|
94
|
+
return !anyChild(node, (child) => !isClosedType(child, checker));
|
|
95
|
+
}
|
|
96
|
+
export function referencedSymbol(node, checker) {
|
|
97
|
+
const name = ts.isQualifiedName(node.typeName) ? node.typeName.right : node.typeName;
|
|
98
|
+
const symbol = checker.getSymbolAtLocation(name);
|
|
99
|
+
if (symbol && symbol.flags & ts.SymbolFlags.Alias) {
|
|
100
|
+
try {
|
|
101
|
+
return checker.getAliasedSymbol(symbol);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return symbol;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return symbol;
|
|
108
|
+
}
|
|
109
|
+
export function isTypeParameter(node, checker) {
|
|
110
|
+
const symbol = referencedSymbol(node, checker);
|
|
111
|
+
return !!symbol && (symbol.flags & ts.SymbolFlags.TypeParameter) !== 0;
|
|
112
|
+
}
|
|
113
|
+
/** Whether `node` names a type parameter whose role is `D`. */
|
|
114
|
+
function namesD(node, roles, checker) {
|
|
115
|
+
if (ts.isTypeReferenceNode(node)) {
|
|
116
|
+
const symbol = referencedSymbol(node, checker);
|
|
117
|
+
if (symbol && roles.get(symbol) === 'D')
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
return anyChild(node, (child) => namesD(child, roles, checker));
|
|
121
|
+
}
|
|
122
|
+
/** A conditional's check or extends type: tracked parameters only. */
|
|
123
|
+
function namesOnlyTracked(node, roles, checker) {
|
|
124
|
+
if (ts.isThisTypeNode(node) || ts.isTypeQueryNode(node))
|
|
125
|
+
return false;
|
|
126
|
+
if (ts.isTypeReferenceNode(node)) {
|
|
127
|
+
const symbol = referencedSymbol(node, checker);
|
|
128
|
+
if (symbol && symbol.flags & ts.SymbolFlags.TypeParameter && !roles.has(symbol))
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
return !anyChild(node, (child) => !namesOnlyTracked(child, roles, checker));
|
|
132
|
+
}
|
|
133
|
+
export function holdsInfer(node) {
|
|
134
|
+
if (ts.isInferTypeNode(node))
|
|
135
|
+
return true;
|
|
136
|
+
return anyChild(node, holdsInfer);
|
|
137
|
+
}
|
|
138
|
+
/** C2: `D` reaches nothing in `node` except as a resolving conditional's selector. */
|
|
139
|
+
function selectsOnly(node, roles, checker, visiting, depth) {
|
|
140
|
+
if (depth > MAX_DEPTH)
|
|
141
|
+
return false;
|
|
142
|
+
if (ts.isTypeQueryNode(node))
|
|
143
|
+
return false;
|
|
144
|
+
if (ts.isConditionalTypeNode(node) && namesD(node, roles, checker)) {
|
|
145
|
+
if (holdsInfer(node.extendsType))
|
|
146
|
+
return false;
|
|
147
|
+
if (!namesOnlyTracked(node.checkType, roles, checker) ||
|
|
148
|
+
!namesOnlyTracked(node.extendsType, roles, checker)) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
return (selectsOnly(node.trueType, roles, checker, visiting, depth + 1) &&
|
|
152
|
+
selectsOnly(node.falseType, roles, checker, visiting, depth + 1));
|
|
153
|
+
}
|
|
154
|
+
if (ts.isTypeReferenceNode(node)) {
|
|
155
|
+
const symbol = referencedSymbol(node, checker);
|
|
156
|
+
if (!symbol)
|
|
157
|
+
return !namesD(node, roles, checker);
|
|
158
|
+
if (symbol.flags & ts.SymbolFlags.TypeParameter)
|
|
159
|
+
return roles.get(symbol) !== 'D';
|
|
160
|
+
if (symbol.flags & ts.SymbolFlags.TypeAlias) {
|
|
161
|
+
if (!namesD(node, roles, checker))
|
|
162
|
+
return true;
|
|
163
|
+
return aliasSelectsOnly(node, symbol, roles, checker, visiting, depth);
|
|
164
|
+
}
|
|
165
|
+
// An interface, a class or an enum: its arguments are read like any node.
|
|
166
|
+
}
|
|
167
|
+
return !anyChild(node, (child) => !selectsOnly(child, roles, checker, visiting, depth + 1));
|
|
168
|
+
}
|
|
169
|
+
/** A reference to a type alias whose arguments name `D`: walk the alias. */
|
|
170
|
+
function aliasSelectsOnly(node, symbol, roles, checker, visiting, depth) {
|
|
171
|
+
// A JSDoc typedef, or an alias this cannot read, has `D` among its
|
|
172
|
+
// arguments: not decided.
|
|
173
|
+
const declaration = symbol.declarations?.find(ts.isTypeAliasDeclaration);
|
|
174
|
+
if (!declaration || symbol.declarations?.length !== 1)
|
|
175
|
+
return false;
|
|
176
|
+
if (visiting.has(symbol))
|
|
177
|
+
return false;
|
|
178
|
+
const parameters = declaration.typeParameters ?? [];
|
|
179
|
+
const args = node.typeArguments ?? [];
|
|
180
|
+
if (args.length > parameters.length)
|
|
181
|
+
return false;
|
|
182
|
+
const inner = new Map();
|
|
183
|
+
for (let i = 0; i < parameters.length; i++) {
|
|
184
|
+
const parameter = checker.getSymbolAtLocation(parameters[i].name);
|
|
185
|
+
if (!parameter)
|
|
186
|
+
return false;
|
|
187
|
+
const written = i < args.length ? args[i] : undefined;
|
|
188
|
+
const source = written ?? parameters[i].default;
|
|
189
|
+
if (!source)
|
|
190
|
+
return false;
|
|
191
|
+
// The scope the argument is read in: the caller's, or the alias's own for
|
|
192
|
+
// a default.
|
|
193
|
+
const scope = written ? roles : inner;
|
|
194
|
+
if (!namesD(source, scope, checker)) {
|
|
195
|
+
inner.set(parameter, 'closed');
|
|
196
|
+
}
|
|
197
|
+
else if (bareD(source, scope, checker)) {
|
|
198
|
+
inner.set(parameter, 'D');
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
visiting.add(symbol);
|
|
205
|
+
const ok = selectsOnly(declaration.type, inner, checker, visiting, depth + 1);
|
|
206
|
+
visiting.delete(symbol);
|
|
207
|
+
return ok;
|
|
208
|
+
}
|
|
209
|
+
/** `D` itself, not a type built from it. */
|
|
210
|
+
function bareD(node, roles, checker) {
|
|
211
|
+
if (ts.isParenthesizedTypeNode(node))
|
|
212
|
+
return bareD(node.type, roles, checker);
|
|
213
|
+
if (!ts.isTypeReferenceNode(node) || node.typeArguments?.length)
|
|
214
|
+
return false;
|
|
215
|
+
const symbol = referencedSymbol(node, checker);
|
|
216
|
+
return !!symbol && roles.get(symbol) === 'D';
|
|
217
|
+
}
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
import { ts } from 'ts-morph';
|
|
28
28
|
import type { Project } from 'ts-morph';
|
|
29
29
|
import type { TypeInferrer } from './type-inferrer.js';
|
|
30
|
-
import type { RetypeItem, RetypeOutcome } from './types.js';
|
|
30
|
+
import type { RetypeItem, RetypeLocatorItem, RetypeOutcome, RetypePrescreen } from './types.js';
|
|
31
31
|
/**
|
|
32
32
|
* The check phase's structural any/unknown walk (`findDisqualifyingTopTypes`
|
|
33
33
|
* in the capture bundle): member paths below `root` that hold a top type.
|
|
@@ -59,14 +59,30 @@ export declare class Retyper {
|
|
|
59
59
|
*/
|
|
60
60
|
run(items: RetypeItem[], budgetMs: number, onJudged?: () => void): RetypeOutcome[];
|
|
61
61
|
private runOne;
|
|
62
|
+
/**
|
|
63
|
+
* Per item, the reason `run` abstains for before any rewrite, which no
|
|
64
|
+
* producer type changes, or `undefined` when only the rewrite can decide
|
|
65
|
+
* the call (carrick#3132). The scan asks this in the consumer's own
|
|
66
|
+
* analysis, so a consumer whose calls all abstain here builds no retype
|
|
67
|
+
* program. A call that throws here is left to the retype, which reports
|
|
68
|
+
* the failure itself.
|
|
69
|
+
*/
|
|
70
|
+
prescreen(items: RetypeLocatorItem[]): Array<Omit<RetypePrescreen, 'item_id'>>;
|
|
71
|
+
/**
|
|
72
|
+
* The call and how to state a type at it, or why the retype abstains
|
|
73
|
+
* before any rewrite. Reads only the consumer's own code: the producer's
|
|
74
|
+
* type is stated by the returned plan.
|
|
75
|
+
*/
|
|
76
|
+
private beforeRewrite;
|
|
62
77
|
/**
|
|
63
78
|
* The rewrite that states the producer's UNWIDENED return (carrick#1516),
|
|
64
79
|
* when the item carries one that can be stated at this call.
|
|
65
80
|
*/
|
|
66
81
|
private unwidenedPlan;
|
|
67
82
|
/**
|
|
68
|
-
* How to state
|
|
69
|
-
*
|
|
83
|
+
* How to state a producer's type at this call, or why it cannot be done
|
|
84
|
+
* whatever that type is. The builder it returns takes the type, and then
|
|
85
|
+
* builds the wire and the declared form from one decision.
|
|
70
86
|
*/
|
|
71
87
|
private plan;
|
|
72
88
|
/**
|
|
@@ -108,4 +124,13 @@ export declare class Retyper {
|
|
|
108
124
|
* consumer against the wrong thing, so the item abstains.
|
|
109
125
|
*/
|
|
110
126
|
private typeArgumentReachesResult;
|
|
127
|
+
/** What the call yields once awaited: the type the reach test compares. */
|
|
128
|
+
private resultOf;
|
|
129
|
+
/**
|
|
130
|
+
* The abstention for a call whose type argument is not in its result. The
|
|
131
|
+
* one place the sentence is built: the reach test gives it after the
|
|
132
|
+
* rewrite, and the prescreen gives it before one (carrick#3158), so the two
|
|
133
|
+
* can never word it differently.
|
|
134
|
+
*/
|
|
135
|
+
private notReachedReason;
|
|
111
136
|
}
|