carrick 0.3.161 → 0.3.163
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/dist/contract.d.ts +8 -0
- package/dist/contract.js.map +1 -1
- package/dist/facts/store.d.ts +12 -0
- package/dist/facts/store.js +15 -0
- package/dist/facts/store.js.map +1 -1
- package/dist/held/daemon.d.ts +8 -0
- package/dist/held/daemon.js.map +1 -1
- package/dist/held/early.d.ts +1 -1
- package/dist/held/early.js +89 -11
- package/dist/held/early.js.map +1 -1
- package/dist/held/message-rows.d.ts +3 -0
- package/dist/held/message-rows.js +41 -0
- package/dist/held/message-rows.js.map +1 -0
- package/dist/held/place.d.ts +11 -2
- package/dist/held/place.js +4 -1
- package/dist/held/place.js.map +1 -1
- package/dist/held/runtime.d.ts +1 -1
- package/dist/held/runtime.js +75 -17
- package/dist/held/runtime.js.map +1 -1
- package/dist/held/shadow.d.ts +2 -0
- package/dist/held/shadow.js.map +1 -1
- package/dist/hook/confirm.js +40 -21
- package/dist/hook/confirm.js.map +1 -1
- package/dist/init/codex.d.ts +1 -1
- package/dist/init/codex.js +8 -4
- package/dist/init/codex.js.map +1 -1
- package/dist/init/doctor.d.ts +10 -13
- package/dist/init/doctor.js +26 -92
- package/dist/init/doctor.js.map +1 -1
- package/dist/init/launcher.d.ts +35 -0
- package/dist/init/launcher.js +85 -0
- package/dist/init/launcher.js.map +1 -0
- package/dist/init/outdated.d.ts +3 -4
- package/dist/init/outdated.js +19 -8
- package/dist/init/outdated.js.map +1 -1
- package/dist/init/remove.js +4 -0
- package/dist/init/remove.js.map +1 -1
- package/dist/init/repo-copies.d.ts +1 -1
- package/dist/init/repo-copies.js +13 -4
- package/dist/init/repo-copies.js.map +1 -1
- package/dist/init/run.js +17 -26
- package/dist/init/run.js.map +1 -1
- package/dist/init/self-update.js +7 -4
- package/dist/init/self-update.js.map +1 -1
- package/dist/init/settings.d.ts +0 -34
- package/dist/init/settings.js +11 -45
- package/dist/init/settings.js.map +1 -1
- package/dist/init/task-skills.d.ts +1 -1
- package/dist/init/task-skills.js +1 -0
- package/dist/init/task-skills.js.map +1 -1
- package/dist/render.d.ts +6 -0
- package/dist/render.js +27 -7
- package/dist/render.js.map +1 -1
- package/package.json +6 -6
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/hooks/carrick-hook.sh +122 -0
- package/plugin/hooks/hooks.json +8 -8
- package/sidecar/dist/src/capture/anchors.d.ts +2 -2
- package/sidecar/dist/src/capture/anchors.js +25 -10
- package/sidecar/dist/src/capture/api.d.ts +10 -0
- package/sidecar/dist/src/capture/check-classify.d.ts +11 -4
- package/sidecar/dist/src/capture/check-classify.js +65 -25
- package/sidecar/dist/src/capture/check-fields.d.ts +2 -1
- package/sidecar/dist/src/capture/check-fields.js +10 -2
- package/sidecar/dist/src/capture/check-probe.d.ts +82 -25
- package/sidecar/dist/src/capture/check-probe.js +106 -37
- package/sidecar/dist/src/capture/index.d.ts +3 -2
- package/sidecar/dist/src/capture/index.js +14 -2
- package/sidecar/dist/src/capture/reply-status.d.ts +86 -0
- package/sidecar/dist/src/capture/reply-status.js +255 -0
- package/sidecar/dist/src/capture/site-node.d.ts +34 -0
- package/sidecar/dist/src/capture/site-node.js +258 -0
- package/sidecar/dist/src/engine-stamp.json +1 -1
- package/sidecar/dist/src/held-shadow.d.ts +23 -2
- package/sidecar/dist/src/held-shadow.js +42 -8
- package/sidecar/dist/src/stated-params.d.ts +3 -2
- package/sidecar/dist/src/type-inferrer.d.ts +91 -55
- package/sidecar/dist/src/type-inferrer.js +350 -218
- package/sidecar/dist/src/types.d.ts +10 -0
- package/sidecar/dist/src/validators.d.ts +150 -28
- package/sidecar/dist/src/validators.js +15 -1
- package/templates/carrick-hook.sh +122 -0
- package/templates/skills/carrick-audit.md +97 -0
- package/templates/skills/carrick-census.md +73 -87
|
@@ -37,13 +37,13 @@ import { declaredTypeName, homeOfSchemaValue, homeOfTypeDeclaration, isTypeSpace
|
|
|
37
37
|
import { callsOf, descendantsOf, descendantsOnLine, descendantsOnLines, wrap, wrapCompilerNode, } from './node-index.js';
|
|
38
38
|
import { elapsedMs, inferTiming, phaseClock, timedPhase } from './infer-timing.js';
|
|
39
39
|
import { importedNameOf, packageNameOf } from './library-claims.js';
|
|
40
|
-
import { callNamedBy, callResultNamesNoCall, constituentsOf, isFrameworkMachinery, nodeByLocatorText, serialisedConditionalType, normalizeLocatorText, symbolDeclaredOutside, typeIsOrContainsMachinery, wrapsValueTransparently, } from './capture/index.js';
|
|
40
|
+
import { anchorInHandler, callNamedBy, callResultNamesNoCall, constituentsOf, isFrameworkMachinery, nodeByLocatorText, serialisedConditionalType, normalizeLocatorText, symbolDeclaredOutside, typeIsOrContainsMachinery, wrapsValueTransparently, } from './capture/index.js';
|
|
41
41
|
import { StatedParams } from './stated-params.js';
|
|
42
42
|
import { bindingToleratesAbsence, handlerToleratesAbsentBody } from './absent-body.js';
|
|
43
43
|
import { replaceFileText } from './program-files.js';
|
|
44
44
|
import { enclosingTypeParameterIn, instantiatedAtCall } from './type-parameters.js';
|
|
45
45
|
import { endsWithPath, loopElementPick } from './loop-element.js';
|
|
46
|
-
import { classifyStatusCodes,
|
|
46
|
+
import { classifyStatusCodes, STATUS_MEMBER_NAMES, chainStatus, isDroppedReplyStatus, replyStatus, statedStatusCodes, statusCodesOfType, valueStatus, statesNoBody, statesNoShape, statusKeyCode, statusTableOf, successEntries, TYPE_REASONS, } from './capture/index.js';
|
|
47
47
|
import { addedDiagnostics, applyInsertions, fileDiagnostics, literalInsertions, mapBack, mapForward, normalise, pathOf, } from './unwidened.js';
|
|
48
48
|
import { expandTypeStructural, jsonWireType, } from './type-structural-expander.js';
|
|
49
49
|
/**
|
|
@@ -152,13 +152,6 @@ const TEXT_READ = new Set([RAW_TEXT_FORMAT]);
|
|
|
152
152
|
* backstop against a pathologically nested initializer.
|
|
153
153
|
*/
|
|
154
154
|
const WRAPPER_CALL_MAX_DEPTH = 3;
|
|
155
|
-
/**
|
|
156
|
-
* Members an options object uses to state an HTTP status. A >= 400 status marks
|
|
157
|
-
* the branch an error path, whose shape is not the endpoint's contract.
|
|
158
|
-
*/
|
|
159
|
-
const STATUS_MEMBER_NAMES = ['status', 'statusCode'];
|
|
160
|
-
/** An error or redirect send carries no body the route's contract describes. */
|
|
161
|
-
const isDroppedResponseStatus = (status) => status === 'error' || status === 'redirect';
|
|
162
155
|
/**
|
|
163
156
|
* The reasons an `unknown` answer is a decision, not a failure to read, on the
|
|
164
157
|
* response path that reads this set: the words `capture/type-reasons.json`
|
|
@@ -203,24 +196,6 @@ function distinctByText(items, text) {
|
|
|
203
196
|
return true;
|
|
204
197
|
});
|
|
205
198
|
}
|
|
206
|
-
/**
|
|
207
|
-
* The status codes a TYPE fixes: a numeric literal, or a union made only of
|
|
208
|
-
* them, every one in the HTTP range. `undefined` for anything else, including
|
|
209
|
-
* a plain `number`.
|
|
210
|
-
*/
|
|
211
|
-
function statusCodesOfType(type) {
|
|
212
|
-
const members = type.isUnion() ? type.getUnionTypes() : [type];
|
|
213
|
-
const codes = [];
|
|
214
|
-
for (const member of members) {
|
|
215
|
-
if (!member.isNumberLiteral())
|
|
216
|
-
return undefined;
|
|
217
|
-
const code = httpStatus(member.getLiteralValue());
|
|
218
|
-
if (code === undefined)
|
|
219
|
-
return undefined;
|
|
220
|
-
codes.push(code);
|
|
221
|
-
}
|
|
222
|
-
return codes.length > 0 ? codes : undefined;
|
|
223
|
-
}
|
|
224
199
|
/**
|
|
225
200
|
* The parts of a request that ARE the body, as validator middleware names them
|
|
226
201
|
* (`validate('json', Schema)`). HTTP vocabulary for how a body is carried — the
|
|
@@ -327,6 +302,12 @@ export class TypeInferrer {
|
|
|
327
302
|
* named one. The open-parameter check (carrick#4201) reads it.
|
|
328
303
|
*/
|
|
329
304
|
printedTypes = new WeakMap();
|
|
305
|
+
/**
|
|
306
|
+
* The type the source states for a call result whose text is that
|
|
307
|
+
* statement (a cast, an annotation, a narrowing). The consumer floor
|
|
308
|
+
* (carrick#4513 P1) reads it beside the printed type.
|
|
309
|
+
*/
|
|
310
|
+
statedTypes = new WeakMap();
|
|
330
311
|
unwidenedLimits;
|
|
331
312
|
now;
|
|
332
313
|
/**
|
|
@@ -382,7 +363,8 @@ export class TypeInferrer {
|
|
|
382
363
|
* order.
|
|
383
364
|
*/
|
|
384
365
|
const loopRows = [];
|
|
385
|
-
for (const
|
|
366
|
+
for (const asked of requests) {
|
|
367
|
+
let request = asked;
|
|
386
368
|
const slotStarted = performance.now();
|
|
387
369
|
const phasesStarted = phaseClock();
|
|
388
370
|
const firstInFile = !this.filesAsked.has(request.file_path);
|
|
@@ -410,6 +392,11 @@ export class TypeInferrer {
|
|
|
410
392
|
// carrick#1029: a route registered once per loop element is read
|
|
411
393
|
// last. Its reading rewrites its file, and a rewrite forgets every
|
|
412
394
|
// node of the file that the readings below still hold.
|
|
395
|
+
// Stale-position T2: every later reader of this request (the
|
|
396
|
+
// unwidened re-read included) reads the node the handler bound picked.
|
|
397
|
+
const anchored = this.anchoredRequest(request);
|
|
398
|
+
if (typeof anchored !== 'string')
|
|
399
|
+
request = anchored;
|
|
413
400
|
if (this.loopPickFor(request)) {
|
|
414
401
|
loopRows.push({ request, at: inferredTypes.length });
|
|
415
402
|
continue;
|
|
@@ -973,6 +960,45 @@ export class TypeInferrer {
|
|
|
973
960
|
}
|
|
974
961
|
return request;
|
|
975
962
|
}
|
|
963
|
+
/**
|
|
964
|
+
* Stale-position T2: the request this route row is read with, or why it
|
|
965
|
+
* has no reading. Only a producer route request that carries its handler's
|
|
966
|
+
* lines (`handler_lines`) and is located by text is checked, by the
|
|
967
|
+
* capture's rule (`anchorInHandler`). A node found outside the handler and
|
|
968
|
+
* what its return reaches, whose text names one answer inside them, is
|
|
969
|
+
* read there: the request comes back with that node's line, and is used
|
|
970
|
+
* only if the locator, asked again at that line, finds exactly that node.
|
|
971
|
+
*/
|
|
972
|
+
anchoredRequest(request) {
|
|
973
|
+
const lines = request.handler_lines;
|
|
974
|
+
if (!lines || !request.expression_text || request.span_start !== undefined)
|
|
975
|
+
return request;
|
|
976
|
+
if (request.infer_kind !== 'request_body' &&
|
|
977
|
+
request.infer_kind !== 'response_body' &&
|
|
978
|
+
request.infer_kind !== 'function_return') {
|
|
979
|
+
return request;
|
|
980
|
+
}
|
|
981
|
+
const sourceFile = this.getSourceFile(request.file_path);
|
|
982
|
+
if (!sourceFile)
|
|
983
|
+
return request;
|
|
984
|
+
const node = this.findNodeByText(sourceFile, request.expression_text, request.expression_line);
|
|
985
|
+
if (!node)
|
|
986
|
+
return request;
|
|
987
|
+
const own = anchorInHandler(this.project.getTypeChecker().compilerObject, sourceFile.compilerNode, node.compilerNode, lines, request.infer_kind, request.expression_text);
|
|
988
|
+
if ('reason' in own)
|
|
989
|
+
return own.reason;
|
|
990
|
+
if (own.node === node.compilerNode)
|
|
991
|
+
return request;
|
|
992
|
+
const line = sourceFile.compilerNode.getLineAndCharacterOfPosition(own.node.getStart(sourceFile.compilerNode))
|
|
993
|
+
.line + 1;
|
|
994
|
+
const moved = { ...request, expression_line: line };
|
|
995
|
+
const again = this.findNodeByText(sourceFile, request.expression_text, line);
|
|
996
|
+
if (again?.compilerNode !== own.node) {
|
|
997
|
+
return (`anchor_outside_handler: the text re-found at line ${line} inside the route's handler ` +
|
|
998
|
+
'is not the node its locator reads there');
|
|
999
|
+
}
|
|
1000
|
+
return moved;
|
|
1001
|
+
}
|
|
976
1002
|
/**
|
|
977
1003
|
* Infer a single type from a request
|
|
978
1004
|
*/
|
|
@@ -982,6 +1008,16 @@ export class TypeInferrer {
|
|
|
982
1008
|
this.logError(`Source file not found: ${request.file_path}`);
|
|
983
1009
|
return null;
|
|
984
1010
|
}
|
|
1011
|
+
// Stale-position T2: a node found by text outside the row's own handler
|
|
1012
|
+
// and what its return reaches is another node's type. The answer is the
|
|
1013
|
+
// one node its text names inside them, or none, final: it does not fall
|
|
1014
|
+
// to the registration, the line or the containing function below.
|
|
1015
|
+
const anchored = this.anchoredRequest(request);
|
|
1016
|
+
if (typeof anchored === 'string') {
|
|
1017
|
+
this.log(`${anchored} (${request.file_path}:${request.expression_line ?? request.line_number})`);
|
|
1018
|
+
return null;
|
|
1019
|
+
}
|
|
1020
|
+
request = anchored;
|
|
985
1021
|
switch (request.infer_kind) {
|
|
986
1022
|
case 'function_return':
|
|
987
1023
|
return this.withoutOpenTypeParameter(request, this.inferFunctionReturn(sourceFile, request, extractionConfig));
|
|
@@ -1028,11 +1064,82 @@ export class TypeInferrer {
|
|
|
1028
1064
|
return inferred;
|
|
1029
1065
|
const printed = this.printedTypes.get(inferred);
|
|
1030
1066
|
const node = this.readNodes.get(inferred.source_location);
|
|
1067
|
+
const stated = this.statedTypes.get(inferred);
|
|
1068
|
+
if (request.infer_kind === 'call_result') {
|
|
1069
|
+
// carrick#4513 P1: a consumer's call result, printed or stated by the
|
|
1070
|
+
// source, states no type when the type is a bare type parameter
|
|
1071
|
+
// (whoever declares it) or a generic class an installed package
|
|
1072
|
+
// declares. A parameter nested inside a body shape is not floored
|
|
1073
|
+
// here: the shape is the caller's, and the capture prints that one
|
|
1074
|
+
// position as unknown.
|
|
1075
|
+
for (const type of [printed, stated]) {
|
|
1076
|
+
const floor = type ? this.consumerFloorOf(type) : undefined;
|
|
1077
|
+
if (floor?.kind === 'parameter')
|
|
1078
|
+
return this.boundByCallers(request, inferred.source_location, floor.name);
|
|
1079
|
+
if (floor?.kind === 'library_class')
|
|
1080
|
+
return this.libraryClassAbstain(request, inferred.source_location, floor.name);
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1031
1083
|
if (!printed || !node)
|
|
1032
1084
|
return inferred;
|
|
1033
1085
|
const open = this.openTypeParameterAt(printed, node);
|
|
1034
1086
|
return open === undefined ? inferred : this.boundByCallers(request, inferred.source_location, open);
|
|
1035
1087
|
}
|
|
1088
|
+
/**
|
|
1089
|
+
* What a consumer's call result is when it is no body (carrick#4513 P1),
|
|
1090
|
+
* read after `await` (`Promise` and other thenables peeled):
|
|
1091
|
+
* - a bare type parameter, other than a polymorphic `this`: only the
|
|
1092
|
+
* callers of whatever declares it set it, a library class's own `T`
|
|
1093
|
+
* included;
|
|
1094
|
+
* - a union member, nullish ones aside, that is an instance of a generic
|
|
1095
|
+
* class an installed package declares (a result, future or page class
|
|
1096
|
+
* around the body). JSON never builds a class instance, so the class is
|
|
1097
|
+
* the library's carrier of the body, and its declaration prints its own
|
|
1098
|
+
* parameters open. A class without type parameters (an SDK's data
|
|
1099
|
+
* class) and a TypeScript lib type (`ArrayBuffer`, `Date`) are kept.
|
|
1100
|
+
*/
|
|
1101
|
+
consumerFloorOf(type) {
|
|
1102
|
+
const awaited = this.awaitedBeyondPromise(this.unwrapPromiseType(type)) ?? this.unwrapPromiseType(type);
|
|
1103
|
+
const members = awaited.isUnion() ? awaited.getUnionTypes() : [awaited];
|
|
1104
|
+
for (const member of members) {
|
|
1105
|
+
if (isNullishType(member))
|
|
1106
|
+
continue;
|
|
1107
|
+
if (member.isTypeParameter()) {
|
|
1108
|
+
if (member.compilerType.isThisType)
|
|
1109
|
+
continue;
|
|
1110
|
+
return { kind: 'parameter', name: member.getText() };
|
|
1111
|
+
}
|
|
1112
|
+
const library = this.genericLibraryClassOf(member);
|
|
1113
|
+
if (library !== undefined)
|
|
1114
|
+
return { kind: 'library_class', name: library };
|
|
1115
|
+
}
|
|
1116
|
+
return undefined;
|
|
1117
|
+
}
|
|
1118
|
+
/** The name of the generic class an installed package declares that `type` is an instance of. */
|
|
1119
|
+
genericLibraryClassOf(type) {
|
|
1120
|
+
const symbol = type.getSymbol();
|
|
1121
|
+
if (!symbol || !(symbol.getFlags() & ts.SymbolFlags.Class))
|
|
1122
|
+
return undefined;
|
|
1123
|
+
const compiler = type.compilerType;
|
|
1124
|
+
const declared = (compiler.target ?? compiler);
|
|
1125
|
+
if (!declared.typeParameters?.length)
|
|
1126
|
+
return undefined;
|
|
1127
|
+
// TypeScript's lib files declare no classes (`ArrayBuffer`, `Date` are
|
|
1128
|
+
// an interface beside a value), so the class flag already leaves them.
|
|
1129
|
+
return this.declaringPackageOf(type) === undefined ? undefined : symbol.getName();
|
|
1130
|
+
}
|
|
1131
|
+
/** The decided answer for a call result that is a generic library class instance (carrick#4513 P1). */
|
|
1132
|
+
libraryClassAbstain(request, location, name) {
|
|
1133
|
+
this.log(`Read at ${request.file_path}:${request.line_number} is an instance of the library class '${name}', ` +
|
|
1134
|
+
'which carries the body rather than being it; leaving it unknown');
|
|
1135
|
+
return this.decidedAbstain(request, location, {
|
|
1136
|
+
path: '',
|
|
1137
|
+
kind: 'unknown',
|
|
1138
|
+
reason: 'machinery_envelope',
|
|
1139
|
+
detail: `the type read here is an instance of the generic class '${name}' an installed package ` +
|
|
1140
|
+
'declares, which carries the body rather than being it, so this site states no type',
|
|
1141
|
+
});
|
|
1142
|
+
}
|
|
1036
1143
|
/**
|
|
1037
1144
|
* The name of a type parameter `type` holds that a declaration around
|
|
1038
1145
|
* `node`, or `node` itself, declares. A function or class read at itself is
|
|
@@ -1199,7 +1306,9 @@ export class TypeInferrer {
|
|
|
1199
1306
|
// resolves to `AsyncGenerator<Order>` (or `Promise<AsyncGenerator<Order>>`),
|
|
1200
1307
|
// whose contract lives in the yield position. Unwrap it to the yield type so
|
|
1201
1308
|
// both forms reduce to `Order` before structural expansion / rule matching.
|
|
1202
|
-
|
|
1309
|
+
// A returned library thenable (a query builder's un-awaited result) is
|
|
1310
|
+
// what `await` yields for it, the value the handler sends.
|
|
1311
|
+
const awaitedType = this.unwrapAsyncIterableType(this.awaitedBeyondPromise(returnType) ?? this.unwrapPromiseType(returnType));
|
|
1203
1312
|
// carrick#2200: a `{ statusCode, body: string }` return is transport the
|
|
1204
1313
|
// handler built itself; its contract is the value it serialised into
|
|
1205
1314
|
// `body`. Decided before the extraction config, whose rule for the
|
|
@@ -1812,10 +1921,11 @@ export class TypeInferrer {
|
|
|
1812
1921
|
if (walked === null) {
|
|
1813
1922
|
// The walk read everything the handler returns and published nothing.
|
|
1814
1923
|
// A located expression inside one of those returns was part of that.
|
|
1815
|
-
|
|
1924
|
+
const holding = this.returnedSendHolding(returned, located);
|
|
1925
|
+
if (holding) {
|
|
1816
1926
|
this.log(`Response locator at ${request.file_path}:${request.line_number} names a send ` +
|
|
1817
1927
|
'its handler returns, which carries no body a contract can describe; abstaining');
|
|
1818
|
-
return null;
|
|
1928
|
+
return this.noSuccessPayloadOf(request, handler, returned, holding) ?? null;
|
|
1819
1929
|
}
|
|
1820
1930
|
// Outside the return, it is either a send itself or what one was handed.
|
|
1821
1931
|
const sends = [
|
|
@@ -1823,7 +1933,7 @@ export class TypeInferrer {
|
|
|
1823
1933
|
this.receivingCallOf(located, () => true),
|
|
1824
1934
|
];
|
|
1825
1935
|
const statesNoBody = sends.some((send) => {
|
|
1826
|
-
const status = send ? this.
|
|
1936
|
+
const status = send ? replyStatus(this.replyChecker(), send.compilerNode, { readChain: false }) : 'undecided';
|
|
1827
1937
|
return status === 'error' || status === 'redirect';
|
|
1828
1938
|
});
|
|
1829
1939
|
if (statesNoBody) {
|
|
@@ -1844,9 +1954,14 @@ export class TypeInferrer {
|
|
|
1844
1954
|
// Blueprint row N34: the callee resolves to nothing, so only the model's
|
|
1845
1955
|
// locator says the returned call is a response sender. That is a lead,
|
|
1846
1956
|
// never a type the check enforces: the row keeps its honest `any`, and the
|
|
1847
|
-
// located argument's reading rides in the provenance for a reader.
|
|
1957
|
+
// located argument's reading rides in the provenance for a reader. Where
|
|
1958
|
+
// every reply the handler returns states an error or redirect status or
|
|
1959
|
+
// carries no body, the lead says there is no success body: decided.
|
|
1848
1960
|
const sender = this.returnedSendHolding(returned, located);
|
|
1849
1961
|
if (sender) {
|
|
1962
|
+
const noSuccess = this.noSuccessPayloadOf(request, handler, returned, sender);
|
|
1963
|
+
if (noSuccess)
|
|
1964
|
+
return noSuccess;
|
|
1850
1965
|
// The printed type names a module the use site does not import by its
|
|
1851
1966
|
// declaring file, `import("/abs/path").Name`, and this text reaches a
|
|
1852
1967
|
// reader: keep the bare name, before truncation can split the path.
|
|
@@ -1862,6 +1977,86 @@ export class TypeInferrer {
|
|
|
1862
1977
|
}
|
|
1863
1978
|
return walked;
|
|
1864
1979
|
}
|
|
1980
|
+
/**
|
|
1981
|
+
* The decided answer of a declared handler none of whose replies carries a
|
|
1982
|
+
* success body, or `undefined` when one might (reply-status T2a).
|
|
1983
|
+
*
|
|
1984
|
+
* Every branch of what the handler returns (`responseReturnedExpressions`,
|
|
1985
|
+
* conditionals split; a returned call whose callbacks' returns are already
|
|
1986
|
+
* listed, such as `.match(ok, err)`, is a container and not judged itself)
|
|
1987
|
+
* must be a reply that states an error or redirect status
|
|
1988
|
+
* (`replyStatus`, chain read), or a reply stating a success status that
|
|
1989
|
+
* carries no body: no first argument, `null`, `undefined`, or an init
|
|
1990
|
+
* object (`statesResponseInit`). A branch counts as a reply only when it
|
|
1991
|
+
* is the located send, makes the same call as the located send (callee
|
|
1992
|
+
* root and member, so `reply.status(400).send(x)` matches
|
|
1993
|
+
* `reply.status(410).send(y)`), or types as framework machinery: a status
|
|
1994
|
+
* read off an argument of any other call (`clip(rows, 404)`) is no reply's
|
|
1995
|
+
* status. A status no source states (`new Response(null)`, `next()`) or a
|
|
1996
|
+
* computed one keeps the row undecided.
|
|
1997
|
+
*/
|
|
1998
|
+
noSuccessPayloadOf(request, handler, returned, locatedSend) {
|
|
1999
|
+
const branches = returned.flatMap((expression) => this.expandResponseBranches(expression, 0));
|
|
2000
|
+
const judged = branches.filter((branch) => !branches.some((inner) => inner !== branch &&
|
|
2001
|
+
branch.getStart() <= inner.getStart() &&
|
|
2002
|
+
inner.getEnd() <= branch.getEnd()));
|
|
2003
|
+
const sentWith = locatedSend ? this.sendCalleeKey(locatedSend) : undefined;
|
|
2004
|
+
const isReply = (branch) => branch === locatedSend ||
|
|
2005
|
+
(sentWith !== undefined && this.sendCalleeKey(branch) === sentWith) ||
|
|
2006
|
+
this.typeIsOrContainsResponseMachinery(this.unwrapPromiseType(branch.getType()));
|
|
2007
|
+
const carriesNoSuccessBody = (branch) => {
|
|
2008
|
+
if (!isReply(branch))
|
|
2009
|
+
return false;
|
|
2010
|
+
const status = replyStatus(this.replyChecker(), branch.compilerNode, { readChain: true });
|
|
2011
|
+
if (isDroppedReplyStatus(status))
|
|
2012
|
+
return true;
|
|
2013
|
+
if (status !== 'success')
|
|
2014
|
+
return false;
|
|
2015
|
+
if (!Node.isCallExpression(branch) && !Node.isNewExpression(branch))
|
|
2016
|
+
return false;
|
|
2017
|
+
const body = branch.getArguments()[0];
|
|
2018
|
+
if (body === undefined)
|
|
2019
|
+
return true;
|
|
2020
|
+
const peeled = this.peelTransparentExpression(body);
|
|
2021
|
+
return (Node.isNullLiteral(peeled) ||
|
|
2022
|
+
(Node.isIdentifier(peeled) && peeled.getText() === 'undefined') ||
|
|
2023
|
+
this.statesResponseInit(peeled));
|
|
2024
|
+
};
|
|
2025
|
+
if (judged.length === 0 || !judged.every(carriesNoSuccessBody)) {
|
|
2026
|
+
return undefined;
|
|
2027
|
+
}
|
|
2028
|
+
this.log(`Response locator at ${request.file_path}:${request.line_number} names a send of a ` +
|
|
2029
|
+
'handler whose every reply states an error or redirect status or carries no body; ' +
|
|
2030
|
+
'no success payload');
|
|
2031
|
+
return this.decidedAbstain(request, this.getNodeLocation(handler), {
|
|
2032
|
+
path: '',
|
|
2033
|
+
kind: 'unknown',
|
|
2034
|
+
reason: 'no_success_payload',
|
|
2035
|
+
detail: "every response this route's handler sends states an error or redirect status, " +
|
|
2036
|
+
'or carries no body a JSON contract can describe, so it publishes no success body',
|
|
2037
|
+
});
|
|
2038
|
+
}
|
|
2039
|
+
/**
|
|
2040
|
+
* What a send calls, as two sends of one reply API share it: `new` and the
|
|
2041
|
+
* constructor's text, or the root of the callee chain and the member
|
|
2042
|
+
* called (`reply.status(400).send` -> `reply…send`), or the callee's text.
|
|
2043
|
+
*/
|
|
2044
|
+
sendCalleeKey(node) {
|
|
2045
|
+
const send = this.peelTransparentExpression(node);
|
|
2046
|
+
if (Node.isNewExpression(send)) {
|
|
2047
|
+
return `new ${send.getExpression().getText()}`;
|
|
2048
|
+
}
|
|
2049
|
+
if (!Node.isCallExpression(send))
|
|
2050
|
+
return undefined;
|
|
2051
|
+
const callee = send.getExpression();
|
|
2052
|
+
if (!Node.isPropertyAccessExpression(callee))
|
|
2053
|
+
return callee.getText();
|
|
2054
|
+
let root = callee.getExpression();
|
|
2055
|
+
while (Node.isPropertyAccessExpression(root) || Node.isCallExpression(root)) {
|
|
2056
|
+
root = root.getExpression();
|
|
2057
|
+
}
|
|
2058
|
+
return `${root.getText()}…${callee.getName()}`;
|
|
2059
|
+
}
|
|
1865
2060
|
/**
|
|
1866
2061
|
* The call inside one of `returned` that the located expression is, or is
|
|
1867
2062
|
* an argument of: the send a located payload was handed to. `undefined`
|
|
@@ -1905,7 +2100,7 @@ export class TypeInferrer {
|
|
|
1905
2100
|
* True when a located call hands back the reply of the registration handler
|
|
1906
2101
|
* it sits in (`handsBackHandlerParameter`, `isRegisteredHandler`), is
|
|
1907
2102
|
* handed a status-shaped value, read as a chain's status call is read
|
|
1908
|
-
* (`
|
|
2103
|
+
* (`valueStatus`, as `chainStatus` reads it), and does not take any
|
|
1909
2104
|
* value in that slot (`takesAnyValue`): `reply.code(204)`,
|
|
1910
2105
|
* `res.status(204)`. Such a call states a status and names no body
|
|
1911
2106
|
* (carrick#4300); a send of a number (`reply.send(count)`) keeps its
|
|
@@ -1919,13 +2114,13 @@ export class TypeInferrer {
|
|
|
1919
2114
|
}
|
|
1920
2115
|
/**
|
|
1921
2116
|
* The one status-only shape: a call that hands one of the handler's
|
|
1922
|
-
* parameters back, is handed a status-shaped value (`
|
|
2117
|
+
* parameters back, is handed a status-shaped value (`valueStatus`), and
|
|
1923
2118
|
* does not take any value in that slot (`takesAnyValue`). Read by the
|
|
1924
2119
|
* located-call check and by `returnsHandlerParameterType` (carrick#4341).
|
|
1925
2120
|
*/
|
|
1926
2121
|
statesOnlyStatus(call, handler) {
|
|
1927
2122
|
return (Node.isCallExpression(call) &&
|
|
1928
|
-
this.
|
|
2123
|
+
valueStatus(this.replyChecker(), call.getArguments()[0]?.compilerNode) !== 'undecided' &&
|
|
1929
2124
|
!this.takesAnyValue(call) &&
|
|
1930
2125
|
this.handsBackHandlerParameter(call, handler));
|
|
1931
2126
|
}
|
|
@@ -2055,7 +2250,7 @@ export class TypeInferrer {
|
|
|
2055
2250
|
* expression selects the HANDLER: the function that contains it, on a row
|
|
2056
2251
|
* whose line is a route registration. Its returned sends are enumerated
|
|
2057
2252
|
* (conditional branches split), each is classified by the status it states
|
|
2058
|
-
* (`
|
|
2253
|
+
* (`replyStatus`), the error and redirect branches are dropped, and
|
|
2059
2254
|
* the success bodies are joined exactly as the payload walk joins return
|
|
2060
2255
|
* statements.
|
|
2061
2256
|
*
|
|
@@ -2121,8 +2316,8 @@ export class TypeInferrer {
|
|
|
2121
2316
|
const locatedBranch = locatingBranches.find((branch) => contains(branch, site));
|
|
2122
2317
|
if (!locatedBranch)
|
|
2123
2318
|
return undefined;
|
|
2124
|
-
const dropped =
|
|
2125
|
-
const siteStatus = this.
|
|
2319
|
+
const dropped = isDroppedReplyStatus;
|
|
2320
|
+
const siteStatus = replyStatus(this.replyChecker(), site.compilerNode, { readChain: true });
|
|
2126
2321
|
if (!tied && !dropped(siteStatus))
|
|
2127
2322
|
return undefined;
|
|
2128
2323
|
let branches;
|
|
@@ -2146,7 +2341,7 @@ export class TypeInferrer {
|
|
|
2146
2341
|
}
|
|
2147
2342
|
let survivors = [];
|
|
2148
2343
|
for (const branch of branches) {
|
|
2149
|
-
const status = branch === locatedBranch ? siteStatus : this.
|
|
2344
|
+
const status = branch === locatedBranch ? siteStatus : replyStatus(this.replyChecker(), branch.compilerNode, { readChain: true });
|
|
2150
2345
|
if (status === 'variable') {
|
|
2151
2346
|
this.log(`Response status at ${request.file_path}:${this.getNodeLocation(branch).start_line} ` +
|
|
2152
2347
|
'is not a literal; keeping that send in the route contract');
|
|
@@ -2305,7 +2500,7 @@ export class TypeInferrer {
|
|
|
2305
2500
|
return null;
|
|
2306
2501
|
const survivors = this.responseSendStatements(handler, (candidate) => this.isLabelledSend(candidate))
|
|
2307
2502
|
.map((s) => s.send)
|
|
2308
|
-
.filter((send) => !
|
|
2503
|
+
.filter((send) => !isDroppedReplyStatus(replyStatus(this.replyChecker(), send.compilerNode, { readChain: true })));
|
|
2309
2504
|
if (survivors.length === 0)
|
|
2310
2505
|
return null;
|
|
2311
2506
|
const recovered = this.recoverPayloadFromResponseExpressions(survivors, false, this.wireFormatFor(request));
|
|
@@ -2668,7 +2863,90 @@ export class TypeInferrer {
|
|
|
2668
2863
|
this.readNodes.delete(this.getNodeLocation(node));
|
|
2669
2864
|
return { type: atCaller.type };
|
|
2670
2865
|
}
|
|
2866
|
+
/**
|
|
2867
|
+
* The type of `call` as the call `through_call` names instantiates the
|
|
2868
|
+
* wrapper it is made in (carrick#4375): `typeAtThroughCall`, with no
|
|
2869
|
+
* answer recorded. A request that names no call, or a call that binds
|
|
2870
|
+
* nothing the type holds, reads the call's own type.
|
|
2871
|
+
*/
|
|
2872
|
+
callTypeAsCallerBinds(call, request) {
|
|
2873
|
+
if (!request.through_call)
|
|
2874
|
+
return call.getType();
|
|
2875
|
+
const atCaller = this.typeAtThroughCall(call, request);
|
|
2876
|
+
return atCaller.kind === 'instantiated' ? atCaller.type : call.getType();
|
|
2877
|
+
}
|
|
2878
|
+
/**
|
|
2879
|
+
* A call's result as the caller reads it. A request made below a wrapper
|
|
2880
|
+
* (`through_call` names the call that reaches the wrapper) is read at the
|
|
2881
|
+
* call it names, where the caller states what it reads, in two cases:
|
|
2882
|
+
*
|
|
2883
|
+
* - the read below finds only transport (a decided `machinery_envelope`,
|
|
2884
|
+
* or the response object itself, stated by nothing): the wrapper hands
|
|
2885
|
+
* the transport on, and the body is read off it by its caller;
|
|
2886
|
+
* - the caller chooses a raw text body: a text read of the value, or a
|
|
2887
|
+
* text format passed at the call (carrick#4513). A raw text body has no
|
|
2888
|
+
* JSON shape, so the mark is read at the deciding call whichever call
|
|
2889
|
+
* the type came from.
|
|
2890
|
+
*
|
|
2891
|
+
* A read below the wrapper that finds a body keeps it.
|
|
2892
|
+
*/
|
|
2671
2893
|
inferCallResult(sourceFile, request, extractionConfig) {
|
|
2894
|
+
const read = this.inferCallResultBelowWrapper(sourceFile, request, extractionConfig);
|
|
2895
|
+
const through = request.through_call;
|
|
2896
|
+
if (!through)
|
|
2897
|
+
return read;
|
|
2898
|
+
const callerFile = this.getSourceFile(through.file_path);
|
|
2899
|
+
const deciding = callerFile
|
|
2900
|
+
? this.findCallExpressionAtSpan(callerFile, through.span_start, through.span_end)
|
|
2901
|
+
: undefined;
|
|
2902
|
+
if (!callerFile || !deciding || !callNamedBy(deciding.compilerNode))
|
|
2903
|
+
return read;
|
|
2904
|
+
if (read && !this.readIsOnlyTransport(sourceFile, request, read)) {
|
|
2905
|
+
return this.markedAtDecidingCall(read, deciding);
|
|
2906
|
+
}
|
|
2907
|
+
const decidingRequest = {
|
|
2908
|
+
...request,
|
|
2909
|
+
file_path: through.file_path,
|
|
2910
|
+
line_number: deciding.getStartLineNumber(),
|
|
2911
|
+
span_start: through.span_start,
|
|
2912
|
+
span_end: through.span_end,
|
|
2913
|
+
expression_text: undefined,
|
|
2914
|
+
expression_line: undefined,
|
|
2915
|
+
through_call: undefined,
|
|
2916
|
+
result: undefined,
|
|
2917
|
+
};
|
|
2918
|
+
const atDeciding = this.inferCallResultBelowWrapper(callerFile, decidingRequest, extractionConfig);
|
|
2919
|
+
if (!atDeciding || this.readIsOnlyTransport(callerFile, decidingRequest, atDeciding)) {
|
|
2920
|
+
return read ?? atDeciding;
|
|
2921
|
+
}
|
|
2922
|
+
this.log(`Call result at ${request.file_path}:${request.line_number} is only transport; ` +
|
|
2923
|
+
`reading it at the call that reaches the wrapper, ${through.file_path}:${decidingRequest.line_number}`);
|
|
2924
|
+
return atDeciding;
|
|
2925
|
+
}
|
|
2926
|
+
/** The read states nothing but transport: a decided envelope, or the response object unstated. */
|
|
2927
|
+
readIsOnlyTransport(sourceFile, request, read) {
|
|
2928
|
+
if ((read.any_provenance ?? []).some((p) => p.path === '' && p.reason === 'machinery_envelope')) {
|
|
2929
|
+
return true;
|
|
2930
|
+
}
|
|
2931
|
+
if (read.is_explicit || (read.any_provenance ?? []).length > 0 || request.result)
|
|
2932
|
+
return false;
|
|
2933
|
+
const call = this.resolveTargetCallExpression(sourceFile, request);
|
|
2934
|
+
if (!call)
|
|
2935
|
+
return false;
|
|
2936
|
+
const value = this.unwrapPromiseType(call.getType());
|
|
2937
|
+
return (this.typeIsOrContainsResponseMachinery(value) &&
|
|
2938
|
+
read.type_string.replace(/\s+/g, ' ').trim() === typeText(value, call).replace(/\s+/g, ' ').trim());
|
|
2939
|
+
}
|
|
2940
|
+
/** The raw-text mark the deciding call states, added to a read that did not carry it. */
|
|
2941
|
+
markedAtDecidingCall(read, deciding) {
|
|
2942
|
+
if (read.raw_text_read || !isBareStringText(read.type_string))
|
|
2943
|
+
return read;
|
|
2944
|
+
const func = this.findContainingFunctionForNode(deciding);
|
|
2945
|
+
const terminal = this.resolveCallResultTerminalNode(deciding, func).terminal;
|
|
2946
|
+
markRawTextRead(read, this.textReadAtTerminal(deciding, terminal) || this.callChoosesTextBody(deciding));
|
|
2947
|
+
return read;
|
|
2948
|
+
}
|
|
2949
|
+
inferCallResultBelowWrapper(sourceFile, request, extractionConfig) {
|
|
2672
2950
|
const callExpr = this.resolveTargetCallExpression(sourceFile, request);
|
|
2673
2951
|
// A call's result is read off the call. A locator that finds no call names
|
|
2674
2952
|
// nothing this can read, and whatever other node its text overlaps is not
|
|
@@ -3007,7 +3285,12 @@ export class TypeInferrer {
|
|
|
3007
3285
|
// type. Where no rule matched that type as the names peel it, the same
|
|
3008
3286
|
// protocol reading the text takes is read here, so a subclass of
|
|
3009
3287
|
// `Promise` anchors the row on what it resolves to and never on itself.
|
|
3010
|
-
|
|
3288
|
+
//
|
|
3289
|
+
// carrick#4513: a call read below a wrapper is anchored on its payload as
|
|
3290
|
+
// the call `through_call` names instantiates it, as its text is
|
|
3291
|
+
// (`callResultAtThroughCall`): read inside a generic wrapper, the call's
|
|
3292
|
+
// own type holds the wrapper's type parameter, which is never an anchor.
|
|
3293
|
+
let callPayloadType = this.unwrapPromiseType(this.callTypeAsCallerBinds(callExpr, request));
|
|
3011
3294
|
let callUnwrap = this.unwrapTypeWithConfig(callPayloadType, callExpr, extractionConfig);
|
|
3012
3295
|
let readByProtocol = false;
|
|
3013
3296
|
if (!callUnwrap.wasUnwrapped) {
|
|
@@ -3068,7 +3351,11 @@ export class TypeInferrer {
|
|
|
3068
3351
|
// call is what judges such a call today.
|
|
3069
3352
|
if (!anchor || !this.primaryTypeSymbol(anchor.element)) {
|
|
3070
3353
|
const typeArgs = callExpr.getTypeArguments();
|
|
3071
|
-
|
|
3354
|
+
// carrick#4513: a type argument that is the enclosing function's own
|
|
3355
|
+
// type parameter (`http.get<T>(…)` inside `callApi<T>`) names no type
|
|
3356
|
+
// of this site's: only that function's callers set it (the
|
|
3357
|
+
// carrick#4201 rule).
|
|
3358
|
+
if (typeArgs.length === 1 && this.openTypeParameterAt(typeArgs[0].getType(), callExpr) === undefined) {
|
|
3072
3359
|
const argType = this.unwrapPromiseType(typeArgs[0].getType());
|
|
3073
3360
|
const argUnwrap = this.unwrapTypeWithConfig(argType, typeArgs[0], extractionConfig);
|
|
3074
3361
|
const argSource = argUnwrap.wasUnwrapped
|
|
@@ -3086,6 +3373,13 @@ export class TypeInferrer {
|
|
|
3086
3373
|
// carrick#2148: a carrier the call's reading left (carrick#1376) is the
|
|
3087
3374
|
// transport's bookkeeping, not the body, so it has no home either.
|
|
3088
3375
|
leftCarrier && !anchor ? null : undefined);
|
|
3376
|
+
const statedNode = narrowed
|
|
3377
|
+
? narrowed.typeNode
|
|
3378
|
+
: explicitType
|
|
3379
|
+
? this.explicitTypeNodeFromAncestor(terminalNode)
|
|
3380
|
+
: undefined;
|
|
3381
|
+
if (statedNode)
|
|
3382
|
+
this.statedTypes.set(inferred, statedNode.getType());
|
|
3089
3383
|
// carrick#1749: say when the text is the source's own statement of the
|
|
3090
3384
|
// body, and what that statement is rooted at, so the scanner can tell a
|
|
3091
3385
|
// model symbol that names the body from one that names a part of it.
|
|
@@ -6269,7 +6563,7 @@ export class TypeInferrer {
|
|
|
6269
6563
|
return false;
|
|
6270
6564
|
return STATUS_MEMBER_NAMES.some((name) => {
|
|
6271
6565
|
const status = this.envelopeMemberType(member, name, at);
|
|
6272
|
-
return status !== undefined && (status.isNumber() || statusCodesOfType(status) !== undefined);
|
|
6566
|
+
return status !== undefined && (status.isNumber() || statusCodesOfType(status.compilerType) !== undefined);
|
|
6273
6567
|
});
|
|
6274
6568
|
});
|
|
6275
6569
|
}
|
|
@@ -6482,7 +6776,7 @@ export class TypeInferrer {
|
|
|
6482
6776
|
everyReturnStatesDroppedStatus(func) {
|
|
6483
6777
|
const branches = this.returnedExpressions(func).flatMap((expression) => this.expandResponseBranches(expression, 0));
|
|
6484
6778
|
return (branches.length > 0 &&
|
|
6485
|
-
branches.every((branch) =>
|
|
6779
|
+
branches.every((branch) => isDroppedReplyStatus(replyStatus(this.replyChecker(), branch.compilerNode, { readChain: true }))));
|
|
6486
6780
|
}
|
|
6487
6781
|
/** An `unknown` answer the inferrer decided, carrying its reason at the root. */
|
|
6488
6782
|
decidedAbstain(request, location, reason) {
|
|
@@ -6537,7 +6831,7 @@ export class TypeInferrer {
|
|
|
6537
6831
|
const inner = envelopes[0];
|
|
6538
6832
|
const innerStatus = inner ? this.envelopeOwnStatus(inner) : 'undecided';
|
|
6539
6833
|
if (innerStatus === 'undecided' || innerStatus === 'variable') {
|
|
6540
|
-
const outer = this.
|
|
6834
|
+
const outer = replyStatus(this.replyChecker(), node.compilerNode, { readChain: false });
|
|
6541
6835
|
if (outer === 'error' || outer === 'redirect')
|
|
6542
6836
|
return 'dropped';
|
|
6543
6837
|
}
|
|
@@ -6548,7 +6842,7 @@ export class TypeInferrer {
|
|
|
6548
6842
|
/** The status a builder call sends: its status argument, or the builder's literal. */
|
|
6549
6843
|
builderCallStatus(call, builder) {
|
|
6550
6844
|
if (builder.statusParameter !== undefined) {
|
|
6551
|
-
return this.
|
|
6845
|
+
return valueStatus(this.replyChecker(), call.getArguments()[builder.statusParameter]?.compilerNode);
|
|
6552
6846
|
}
|
|
6553
6847
|
return builder.fixedStatus ?? 'undecided';
|
|
6554
6848
|
}
|
|
@@ -6573,22 +6867,10 @@ export class TypeInferrer {
|
|
|
6573
6867
|
for (const name of STATUS_MEMBER_NAMES) {
|
|
6574
6868
|
const value = this.envelopeMemberValue(literal, name);
|
|
6575
6869
|
if (value)
|
|
6576
|
-
return this.
|
|
6870
|
+
return valueStatus(this.replyChecker(), value.compilerNode);
|
|
6577
6871
|
}
|
|
6578
6872
|
return 'undecided';
|
|
6579
6873
|
}
|
|
6580
|
-
/** The status a value states, classified as a send's status argument is. */
|
|
6581
|
-
statusOfValue(value) {
|
|
6582
|
-
if (!value)
|
|
6583
|
-
return 'undecided';
|
|
6584
|
-
const read = this.statedStatusCodes(this.peelTransparentExpression(value));
|
|
6585
|
-
if (read === 'variable')
|
|
6586
|
-
return 'variable';
|
|
6587
|
-
if (!read)
|
|
6588
|
-
return 'undecided';
|
|
6589
|
-
const kind = classifyStatusCodes(read);
|
|
6590
|
-
return kind === 'mixed' ? 'variable' : kind;
|
|
6591
|
-
}
|
|
6592
6874
|
/**
|
|
6593
6875
|
* The value node an object literal gives a member: a property assignment's
|
|
6594
6876
|
* initializer, or a shorthand's own name.
|
|
@@ -6695,7 +6977,7 @@ export class TypeInferrer {
|
|
|
6695
6977
|
const statusParameter = this.parameterIndexOf(statusValue, params);
|
|
6696
6978
|
let fixedStatus;
|
|
6697
6979
|
if (statusParameter === undefined) {
|
|
6698
|
-
const codes = this.
|
|
6980
|
+
const codes = statedStatusCodes(this.replyChecker(), this.peelTransparentExpression(statusValue).compilerNode);
|
|
6699
6981
|
if (!Array.isArray(codes))
|
|
6700
6982
|
return undefined;
|
|
6701
6983
|
const kind = classifyStatusCodes(codes);
|
|
@@ -6910,7 +7192,7 @@ export class TypeInferrer {
|
|
|
6910
7192
|
if (args.length === 0)
|
|
6911
7193
|
return undefined;
|
|
6912
7194
|
// An error or redirect branch sends no success body (carrick#1161).
|
|
6913
|
-
const status = this.
|
|
7195
|
+
const status = replyStatus(this.replyChecker(), call.compilerNode, { readChain: false });
|
|
6914
7196
|
if (status === 'error' || status === 'redirect')
|
|
6915
7197
|
return undefined;
|
|
6916
7198
|
if (Node.isCallExpression(call) && this.callsFailureHelper(call, depth))
|
|
@@ -6967,7 +7249,7 @@ export class TypeInferrer {
|
|
|
6967
7249
|
if (branches.length === 0)
|
|
6968
7250
|
return false;
|
|
6969
7251
|
return branches.every((branch) => {
|
|
6970
|
-
const status = this.
|
|
7252
|
+
const status = replyStatus(this.replyChecker(), branch.compilerNode, { readChain: false });
|
|
6971
7253
|
if (status === 'error' || status === 'redirect')
|
|
6972
7254
|
return true;
|
|
6973
7255
|
const inner = this.peelTransparentExpression(branch);
|
|
@@ -7097,165 +7379,15 @@ export class TypeInferrer {
|
|
|
7097
7379
|
statesInit = true;
|
|
7098
7380
|
continue;
|
|
7099
7381
|
}
|
|
7100
|
-
if (name === 'status' && this.
|
|
7382
|
+
if (name === 'status' && statedStatusCodes(this.replyChecker(), value.compilerNode) !== undefined) {
|
|
7101
7383
|
statesInit = true;
|
|
7102
7384
|
}
|
|
7103
7385
|
}
|
|
7104
7386
|
return statesInit;
|
|
7105
7387
|
}
|
|
7106
|
-
/**
|
|
7107
|
-
|
|
7108
|
-
|
|
7109
|
-
* Read in order, and the first that states anything decides:
|
|
7110
|
-
*
|
|
7111
|
-
* 1. an argument past the body that carries a status: a numeric literal
|
|
7112
|
-
* (`send(body, 404)`), an options object (`{ status: 401 }`), or any
|
|
7113
|
-
* expression whose TYPE is a status literal or a union of them
|
|
7114
|
-
* (`let code: 400 | 500`). A numeric argument whose type is a plain
|
|
7115
|
-
* `number` states a status the source does not fix, which is
|
|
7116
|
-
* `variable`: the branch fails open into the union and is logged.
|
|
7117
|
-
* 2. the send call's own RESULT type, when a member of it is typed as a
|
|
7118
|
-
* status literal. A framework that types its sends records the status
|
|
7119
|
-
* there, and a redirect helper's default `302` is only visible there.
|
|
7120
|
-
* A result whose status member spans success and error codes (the
|
|
7121
|
-
* default of a typed `json(body, status?)`) says nothing either way.
|
|
7122
|
-
*
|
|
7123
|
-
* A status in the FIRST argument is a field of the body and is never read.
|
|
7124
|
-
* No method or framework name is consulted anywhere.
|
|
7125
|
-
*/
|
|
7126
|
-
responseSiteStatus(expression, readChain = false) {
|
|
7127
|
-
const call = this.peelTransparentExpression(expression);
|
|
7128
|
-
if (!Node.isCallExpression(call) && !Node.isNewExpression(call)) {
|
|
7129
|
-
return 'undecided';
|
|
7130
|
-
}
|
|
7131
|
-
const args = call.getArguments().map((a) => this.peelTransparentExpression(a));
|
|
7132
|
-
const stated = [];
|
|
7133
|
-
let variable = false;
|
|
7134
|
-
for (const arg of args.slice(1)) {
|
|
7135
|
-
const read = this.statedStatusCodes(arg);
|
|
7136
|
-
if (read === 'variable') {
|
|
7137
|
-
variable = true;
|
|
7138
|
-
}
|
|
7139
|
-
else if (read) {
|
|
7140
|
-
stated.push(...read);
|
|
7141
|
-
}
|
|
7142
|
-
}
|
|
7143
|
-
if (stated.length > 0) {
|
|
7144
|
-
const kind = classifyStatusCodes(stated);
|
|
7145
|
-
return kind === 'mixed' ? 'variable' : kind;
|
|
7146
|
-
}
|
|
7147
|
-
if (variable)
|
|
7148
|
-
return 'variable';
|
|
7149
|
-
// carrick#2005: a status an earlier call of the same chain states
|
|
7150
|
-
// (`reply.status(401).json(body)`). Read only where the caller asks for
|
|
7151
|
-
// it; the return walk does not read it yet (carrick#1962).
|
|
7152
|
-
if (readChain) {
|
|
7153
|
-
const chained = this.chainStatus(call);
|
|
7154
|
-
if (chained)
|
|
7155
|
-
return chained;
|
|
7156
|
-
}
|
|
7157
|
-
const typed = this.sendResultStatusCodes(call);
|
|
7158
|
-
if (typed) {
|
|
7159
|
-
const kind = classifyStatusCodes(typed);
|
|
7160
|
-
if (kind !== 'mixed')
|
|
7161
|
-
return kind;
|
|
7162
|
-
}
|
|
7163
|
-
return 'undecided';
|
|
7164
|
-
}
|
|
7165
|
-
/**
|
|
7166
|
-
* The status an earlier call of a send's own chain states: the receiver
|
|
7167
|
-
* calls `call` is invoked on (`a.status(401).json(body)` has `a.status(401)`),
|
|
7168
|
-
* innermost last. Each receiver call's first argument is read by the rule
|
|
7169
|
-
* an argument past a body is, so a numeric literal or a literal-typed value
|
|
7170
|
-
* counts and a plain `number` is `variable`. `undefined` when no receiver
|
|
7171
|
-
* call states a status. No method name is consulted: a receiver call that
|
|
7172
|
-
* is handed no status-shaped value says nothing.
|
|
7173
|
-
*/
|
|
7174
|
-
chainStatus(call) {
|
|
7175
|
-
if (!Node.isCallExpression(call))
|
|
7176
|
-
return undefined;
|
|
7177
|
-
let callee = call.getExpression();
|
|
7178
|
-
while (Node.isPropertyAccessExpression(callee)) {
|
|
7179
|
-
const receiver = this.peelTransparentExpression(callee.getExpression());
|
|
7180
|
-
if (!Node.isCallExpression(receiver))
|
|
7181
|
-
return undefined;
|
|
7182
|
-
const status = this.statusOfValue(receiver.getArguments()[0]);
|
|
7183
|
-
if (status !== 'undecided')
|
|
7184
|
-
return status;
|
|
7185
|
-
callee = receiver.getExpression();
|
|
7186
|
-
}
|
|
7187
|
-
return undefined;
|
|
7188
|
-
}
|
|
7189
|
-
/**
|
|
7190
|
-
* The HTTP status codes an argument states, `'variable'` when it is a
|
|
7191
|
-
* status-shaped value the source does not fix, or `undefined` when it says
|
|
7192
|
-
* nothing about a status.
|
|
7193
|
-
*
|
|
7194
|
-
* Read from the AST first: `{ status: 400 }` in an argument position widens
|
|
7195
|
-
* to `{ status: number }`, so the literal only survives syntactically. The
|
|
7196
|
-
* type read behind it catches `as const`, hoisted option objects and
|
|
7197
|
-
* literal-typed variables. Only values in the HTTP range count: an
|
|
7198
|
-
* arbitrary number named `status` on a domain object (`{ status: 2 }`)
|
|
7199
|
-
* states nothing about transport.
|
|
7200
|
-
*/
|
|
7201
|
-
statedStatusCodes(node) {
|
|
7202
|
-
if (Node.isNumericLiteral(node)) {
|
|
7203
|
-
const code = httpStatus(node.getLiteralValue());
|
|
7204
|
-
return code === undefined ? undefined : [code];
|
|
7205
|
-
}
|
|
7206
|
-
if (Node.isObjectLiteralExpression(node)) {
|
|
7207
|
-
for (const name of STATUS_MEMBER_NAMES) {
|
|
7208
|
-
const property = node.getProperty(name);
|
|
7209
|
-
if (!property || !Node.isPropertyAssignment(property))
|
|
7210
|
-
continue;
|
|
7211
|
-
const initializer = property.getInitializer();
|
|
7212
|
-
if (!initializer)
|
|
7213
|
-
continue;
|
|
7214
|
-
const read = this.statedStatusCodes(this.peelTransparentExpression(initializer));
|
|
7215
|
-
if (read)
|
|
7216
|
-
return read;
|
|
7217
|
-
}
|
|
7218
|
-
return undefined;
|
|
7219
|
-
}
|
|
7220
|
-
const type = node.getType();
|
|
7221
|
-
const fromType = statusCodesOfType(type);
|
|
7222
|
-
if (fromType)
|
|
7223
|
-
return fromType;
|
|
7224
|
-
if (type.isNumber())
|
|
7225
|
-
return 'variable';
|
|
7226
|
-
for (const name of STATUS_MEMBER_NAMES) {
|
|
7227
|
-
const property = type.getProperty(name);
|
|
7228
|
-
const declaration = property?.getDeclarations()[0];
|
|
7229
|
-
if (!property || !declaration)
|
|
7230
|
-
continue;
|
|
7231
|
-
const propertyType = property.getTypeAtLocation(declaration).getNonNullableType();
|
|
7232
|
-
const codes = statusCodesOfType(propertyType);
|
|
7233
|
-
if (codes)
|
|
7234
|
-
return codes;
|
|
7235
|
-
if (propertyType.isNumber())
|
|
7236
|
-
return 'variable';
|
|
7237
|
-
}
|
|
7238
|
-
return undefined;
|
|
7239
|
-
}
|
|
7240
|
-
/**
|
|
7241
|
-
* Status codes a send call's result type records: the members (of the
|
|
7242
|
-
* result, or of each part of an intersection result) typed as a status
|
|
7243
|
-
* literal or a union of them. `undefined` when none is.
|
|
7244
|
-
*/
|
|
7245
|
-
sendResultStatusCodes(call) {
|
|
7246
|
-
const result = this.unwrapPromiseType(call.getType());
|
|
7247
|
-
const parts = result.isIntersection() ? result.getIntersectionTypes() : [result];
|
|
7248
|
-
const codes = [];
|
|
7249
|
-
for (const part of parts) {
|
|
7250
|
-
if (part.isAny() || part.isUnknown() || !part.isObject())
|
|
7251
|
-
continue;
|
|
7252
|
-
for (const property of part.getProperties()) {
|
|
7253
|
-
const codesHere = statusCodesOfType(property.getTypeAtLocation(call));
|
|
7254
|
-
if (codesHere)
|
|
7255
|
-
codes.push(...codesHere);
|
|
7256
|
-
}
|
|
7257
|
-
}
|
|
7258
|
-
return codes.length > 0 ? codes : undefined;
|
|
7388
|
+
/** The raw checker the shared reply-status reader (`capture/reply-status.ts`) reads with. */
|
|
7389
|
+
replyChecker() {
|
|
7390
|
+
return this.project.getTypeChecker().compilerObject;
|
|
7259
7391
|
}
|
|
7260
7392
|
/**
|
|
7261
7393
|
* Peel the wrappers that do not change an expression's payload: parentheses
|
|
@@ -9360,7 +9492,7 @@ export class TypeInferrer {
|
|
|
9360
9492
|
if (sourceFile.isDeclarationFile())
|
|
9361
9493
|
return false;
|
|
9362
9494
|
return !isExternalOrigin(this.project.getProgram().compilerObject, sourceFile.compilerNode, this.repoRoot, externalImportsOf(this.project));
|
|
9363
|
-
}, (call) => this.
|
|
9495
|
+
}, (call) => chainStatus(this.replyChecker(), call.compilerNode), (value) => valueStatus(this.replyChecker(), value.compilerNode), (value) => this.unwrapJsonStringifyArg(this.unwrapExpressionNode(value)));
|
|
9364
9496
|
}
|
|
9365
9497
|
/**
|
|
9366
9498
|
* carrick#2674: where today's reading of a route's response is empty
|