carrick 0.3.161 → 0.3.162

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/dist/contract.d.ts +8 -0
  2. package/dist/contract.js.map +1 -1
  3. package/dist/facts/store.d.ts +12 -0
  4. package/dist/facts/store.js +15 -0
  5. package/dist/facts/store.js.map +1 -1
  6. package/dist/held/daemon.d.ts +8 -0
  7. package/dist/held/daemon.js.map +1 -1
  8. package/dist/held/early.d.ts +1 -1
  9. package/dist/held/early.js +29 -10
  10. package/dist/held/early.js.map +1 -1
  11. package/dist/held/message-rows.d.ts +3 -0
  12. package/dist/held/message-rows.js +41 -0
  13. package/dist/held/message-rows.js.map +1 -0
  14. package/dist/held/place.d.ts +11 -2
  15. package/dist/held/place.js +4 -1
  16. package/dist/held/place.js.map +1 -1
  17. package/dist/held/runtime.d.ts +1 -1
  18. package/dist/held/runtime.js +47 -17
  19. package/dist/held/runtime.js.map +1 -1
  20. package/dist/held/shadow.d.ts +2 -0
  21. package/dist/held/shadow.js.map +1 -1
  22. package/dist/hook/confirm.js +40 -21
  23. package/dist/hook/confirm.js.map +1 -1
  24. package/dist/init/codex.d.ts +1 -1
  25. package/dist/init/codex.js +8 -4
  26. package/dist/init/codex.js.map +1 -1
  27. package/dist/init/doctor.d.ts +10 -13
  28. package/dist/init/doctor.js +26 -92
  29. package/dist/init/doctor.js.map +1 -1
  30. package/dist/init/launcher.d.ts +35 -0
  31. package/dist/init/launcher.js +85 -0
  32. package/dist/init/launcher.js.map +1 -0
  33. package/dist/init/outdated.d.ts +3 -4
  34. package/dist/init/outdated.js +19 -8
  35. package/dist/init/outdated.js.map +1 -1
  36. package/dist/init/remove.js +4 -0
  37. package/dist/init/remove.js.map +1 -1
  38. package/dist/init/repo-copies.d.ts +1 -1
  39. package/dist/init/repo-copies.js +13 -4
  40. package/dist/init/repo-copies.js.map +1 -1
  41. package/dist/init/run.js +17 -26
  42. package/dist/init/run.js.map +1 -1
  43. package/dist/init/self-update.js +7 -4
  44. package/dist/init/self-update.js.map +1 -1
  45. package/dist/init/settings.d.ts +0 -34
  46. package/dist/init/settings.js +11 -45
  47. package/dist/init/settings.js.map +1 -1
  48. package/dist/render.d.ts +6 -0
  49. package/dist/render.js +27 -7
  50. package/dist/render.js.map +1 -1
  51. package/package.json +6 -6
  52. package/plugin/.claude-plugin/plugin.json +1 -1
  53. package/plugin/hooks/carrick-hook.sh +122 -0
  54. package/plugin/hooks/hooks.json +8 -8
  55. package/sidecar/dist/src/capture/anchors.d.ts +2 -2
  56. package/sidecar/dist/src/capture/anchors.js +25 -10
  57. package/sidecar/dist/src/capture/api.d.ts +10 -0
  58. package/sidecar/dist/src/capture/check-classify.d.ts +11 -4
  59. package/sidecar/dist/src/capture/check-classify.js +65 -25
  60. package/sidecar/dist/src/capture/check-fields.d.ts +2 -1
  61. package/sidecar/dist/src/capture/check-fields.js +10 -2
  62. package/sidecar/dist/src/capture/check-probe.d.ts +77 -25
  63. package/sidecar/dist/src/capture/check-probe.js +100 -37
  64. package/sidecar/dist/src/capture/index.d.ts +3 -2
  65. package/sidecar/dist/src/capture/index.js +14 -2
  66. package/sidecar/dist/src/capture/reply-status.d.ts +86 -0
  67. package/sidecar/dist/src/capture/reply-status.js +255 -0
  68. package/sidecar/dist/src/capture/site-node.d.ts +34 -0
  69. package/sidecar/dist/src/capture/site-node.js +258 -0
  70. package/sidecar/dist/src/engine-stamp.json +1 -1
  71. package/sidecar/dist/src/held-shadow.d.ts +23 -2
  72. package/sidecar/dist/src/held-shadow.js +42 -8
  73. package/sidecar/dist/src/stated-params.d.ts +3 -2
  74. package/sidecar/dist/src/type-inferrer.d.ts +15 -55
  75. package/sidecar/dist/src/type-inferrer.js +83 -213
  76. package/sidecar/dist/src/types.d.ts +10 -0
  77. package/sidecar/dist/src/validators.d.ts +150 -28
  78. package/sidecar/dist/src/validators.js +15 -1
  79. package/templates/carrick-hook.sh +122 -0
@@ -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, httpStatus, statesNoBody, statesNoShape, statusKeyCode, statusTableOf, successEntries, TYPE_REASONS, } from './capture/index.js';
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
@@ -382,7 +357,8 @@ export class TypeInferrer {
382
357
  * order.
383
358
  */
384
359
  const loopRows = [];
385
- for (const request of requests) {
360
+ for (const asked of requests) {
361
+ let request = asked;
386
362
  const slotStarted = performance.now();
387
363
  const phasesStarted = phaseClock();
388
364
  const firstInFile = !this.filesAsked.has(request.file_path);
@@ -410,6 +386,11 @@ export class TypeInferrer {
410
386
  // carrick#1029: a route registered once per loop element is read
411
387
  // last. Its reading rewrites its file, and a rewrite forgets every
412
388
  // node of the file that the readings below still hold.
389
+ // Stale-position T2: every later reader of this request (the
390
+ // unwidened re-read included) reads the node the handler bound picked.
391
+ const anchored = this.anchoredRequest(request);
392
+ if (typeof anchored !== 'string')
393
+ request = anchored;
413
394
  if (this.loopPickFor(request)) {
414
395
  loopRows.push({ request, at: inferredTypes.length });
415
396
  continue;
@@ -973,6 +954,45 @@ export class TypeInferrer {
973
954
  }
974
955
  return request;
975
956
  }
957
+ /**
958
+ * Stale-position T2: the request this route row is read with, or why it
959
+ * has no reading. Only a producer route request that carries its handler's
960
+ * lines (`handler_lines`) and is located by text is checked, by the
961
+ * capture's rule (`anchorInHandler`). A node found outside the handler and
962
+ * what its return reaches, whose text names one answer inside them, is
963
+ * read there: the request comes back with that node's line, and is used
964
+ * only if the locator, asked again at that line, finds exactly that node.
965
+ */
966
+ anchoredRequest(request) {
967
+ const lines = request.handler_lines;
968
+ if (!lines || !request.expression_text || request.span_start !== undefined)
969
+ return request;
970
+ if (request.infer_kind !== 'request_body' &&
971
+ request.infer_kind !== 'response_body' &&
972
+ request.infer_kind !== 'function_return') {
973
+ return request;
974
+ }
975
+ const sourceFile = this.getSourceFile(request.file_path);
976
+ if (!sourceFile)
977
+ return request;
978
+ const node = this.findNodeByText(sourceFile, request.expression_text, request.expression_line);
979
+ if (!node)
980
+ return request;
981
+ const own = anchorInHandler(this.project.getTypeChecker().compilerObject, sourceFile.compilerNode, node.compilerNode, lines, request.infer_kind, request.expression_text);
982
+ if ('reason' in own)
983
+ return own.reason;
984
+ if (own.node === node.compilerNode)
985
+ return request;
986
+ const line = sourceFile.compilerNode.getLineAndCharacterOfPosition(own.node.getStart(sourceFile.compilerNode))
987
+ .line + 1;
988
+ const moved = { ...request, expression_line: line };
989
+ const again = this.findNodeByText(sourceFile, request.expression_text, line);
990
+ if (again?.compilerNode !== own.node) {
991
+ return (`anchor_outside_handler: the text re-found at line ${line} inside the route's handler ` +
992
+ 'is not the node its locator reads there');
993
+ }
994
+ return moved;
995
+ }
976
996
  /**
977
997
  * Infer a single type from a request
978
998
  */
@@ -982,6 +1002,16 @@ export class TypeInferrer {
982
1002
  this.logError(`Source file not found: ${request.file_path}`);
983
1003
  return null;
984
1004
  }
1005
+ // Stale-position T2: a node found by text outside the row's own handler
1006
+ // and what its return reaches is another node's type. The answer is the
1007
+ // one node its text names inside them, or none, final: it does not fall
1008
+ // to the registration, the line or the containing function below.
1009
+ const anchored = this.anchoredRequest(request);
1010
+ if (typeof anchored === 'string') {
1011
+ this.log(`${anchored} (${request.file_path}:${request.expression_line ?? request.line_number})`);
1012
+ return null;
1013
+ }
1014
+ request = anchored;
985
1015
  switch (request.infer_kind) {
986
1016
  case 'function_return':
987
1017
  return this.withoutOpenTypeParameter(request, this.inferFunctionReturn(sourceFile, request, extractionConfig));
@@ -1199,7 +1229,9 @@ export class TypeInferrer {
1199
1229
  // resolves to `AsyncGenerator<Order>` (or `Promise<AsyncGenerator<Order>>`),
1200
1230
  // whose contract lives in the yield position. Unwrap it to the yield type so
1201
1231
  // both forms reduce to `Order` before structural expansion / rule matching.
1202
- const awaitedType = this.unwrapAsyncIterableType(this.unwrapPromiseType(returnType));
1232
+ // A returned library thenable (a query builder's un-awaited result) is
1233
+ // what `await` yields for it, the value the handler sends.
1234
+ const awaitedType = this.unwrapAsyncIterableType(this.awaitedBeyondPromise(returnType) ?? this.unwrapPromiseType(returnType));
1203
1235
  // carrick#2200: a `{ statusCode, body: string }` return is transport the
1204
1236
  // handler built itself; its contract is the value it serialised into
1205
1237
  // `body`. Decided before the extraction config, whose rule for the
@@ -1823,7 +1855,7 @@ export class TypeInferrer {
1823
1855
  this.receivingCallOf(located, () => true),
1824
1856
  ];
1825
1857
  const statesNoBody = sends.some((send) => {
1826
- const status = send ? this.responseSiteStatus(send) : 'undecided';
1858
+ const status = send ? replyStatus(this.replyChecker(), send.compilerNode, { readChain: false }) : 'undecided';
1827
1859
  return status === 'error' || status === 'redirect';
1828
1860
  });
1829
1861
  if (statesNoBody) {
@@ -1905,7 +1937,7 @@ export class TypeInferrer {
1905
1937
  * True when a located call hands back the reply of the registration handler
1906
1938
  * it sits in (`handsBackHandlerParameter`, `isRegisteredHandler`), is
1907
1939
  * handed a status-shaped value, read as a chain's status call is read
1908
- * (`statusOfValue`, as `chainStatus` reads it), and does not take any
1940
+ * (`valueStatus`, as `chainStatus` reads it), and does not take any
1909
1941
  * value in that slot (`takesAnyValue`): `reply.code(204)`,
1910
1942
  * `res.status(204)`. Such a call states a status and names no body
1911
1943
  * (carrick#4300); a send of a number (`reply.send(count)`) keeps its
@@ -1919,13 +1951,13 @@ export class TypeInferrer {
1919
1951
  }
1920
1952
  /**
1921
1953
  * The one status-only shape: a call that hands one of the handler's
1922
- * parameters back, is handed a status-shaped value (`statusOfValue`), and
1954
+ * parameters back, is handed a status-shaped value (`valueStatus`), and
1923
1955
  * does not take any value in that slot (`takesAnyValue`). Read by the
1924
1956
  * located-call check and by `returnsHandlerParameterType` (carrick#4341).
1925
1957
  */
1926
1958
  statesOnlyStatus(call, handler) {
1927
1959
  return (Node.isCallExpression(call) &&
1928
- this.statusOfValue(call.getArguments()[0]) !== 'undecided' &&
1960
+ valueStatus(this.replyChecker(), call.getArguments()[0]?.compilerNode) !== 'undecided' &&
1929
1961
  !this.takesAnyValue(call) &&
1930
1962
  this.handsBackHandlerParameter(call, handler));
1931
1963
  }
@@ -2055,7 +2087,7 @@ export class TypeInferrer {
2055
2087
  * expression selects the HANDLER: the function that contains it, on a row
2056
2088
  * whose line is a route registration. Its returned sends are enumerated
2057
2089
  * (conditional branches split), each is classified by the status it states
2058
- * (`responseSiteStatus`), the error and redirect branches are dropped, and
2090
+ * (`replyStatus`), the error and redirect branches are dropped, and
2059
2091
  * the success bodies are joined exactly as the payload walk joins return
2060
2092
  * statements.
2061
2093
  *
@@ -2121,8 +2153,8 @@ export class TypeInferrer {
2121
2153
  const locatedBranch = locatingBranches.find((branch) => contains(branch, site));
2122
2154
  if (!locatedBranch)
2123
2155
  return undefined;
2124
- const dropped = isDroppedResponseStatus;
2125
- const siteStatus = this.responseSiteStatus(site, true);
2156
+ const dropped = isDroppedReplyStatus;
2157
+ const siteStatus = replyStatus(this.replyChecker(), site.compilerNode, { readChain: true });
2126
2158
  if (!tied && !dropped(siteStatus))
2127
2159
  return undefined;
2128
2160
  let branches;
@@ -2146,7 +2178,7 @@ export class TypeInferrer {
2146
2178
  }
2147
2179
  let survivors = [];
2148
2180
  for (const branch of branches) {
2149
- const status = branch === locatedBranch ? siteStatus : this.responseSiteStatus(branch, true);
2181
+ const status = branch === locatedBranch ? siteStatus : replyStatus(this.replyChecker(), branch.compilerNode, { readChain: true });
2150
2182
  if (status === 'variable') {
2151
2183
  this.log(`Response status at ${request.file_path}:${this.getNodeLocation(branch).start_line} ` +
2152
2184
  'is not a literal; keeping that send in the route contract');
@@ -2305,7 +2337,7 @@ export class TypeInferrer {
2305
2337
  return null;
2306
2338
  const survivors = this.responseSendStatements(handler, (candidate) => this.isLabelledSend(candidate))
2307
2339
  .map((s) => s.send)
2308
- .filter((send) => !isDroppedResponseStatus(this.responseSiteStatus(send, true)));
2340
+ .filter((send) => !isDroppedReplyStatus(replyStatus(this.replyChecker(), send.compilerNode, { readChain: true })));
2309
2341
  if (survivors.length === 0)
2310
2342
  return null;
2311
2343
  const recovered = this.recoverPayloadFromResponseExpressions(survivors, false, this.wireFormatFor(request));
@@ -6269,7 +6301,7 @@ export class TypeInferrer {
6269
6301
  return false;
6270
6302
  return STATUS_MEMBER_NAMES.some((name) => {
6271
6303
  const status = this.envelopeMemberType(member, name, at);
6272
- return status !== undefined && (status.isNumber() || statusCodesOfType(status) !== undefined);
6304
+ return status !== undefined && (status.isNumber() || statusCodesOfType(status.compilerType) !== undefined);
6273
6305
  });
6274
6306
  });
6275
6307
  }
@@ -6482,7 +6514,7 @@ export class TypeInferrer {
6482
6514
  everyReturnStatesDroppedStatus(func) {
6483
6515
  const branches = this.returnedExpressions(func).flatMap((expression) => this.expandResponseBranches(expression, 0));
6484
6516
  return (branches.length > 0 &&
6485
- branches.every((branch) => isDroppedResponseStatus(this.responseSiteStatus(branch, true))));
6517
+ branches.every((branch) => isDroppedReplyStatus(replyStatus(this.replyChecker(), branch.compilerNode, { readChain: true }))));
6486
6518
  }
6487
6519
  /** An `unknown` answer the inferrer decided, carrying its reason at the root. */
6488
6520
  decidedAbstain(request, location, reason) {
@@ -6537,7 +6569,7 @@ export class TypeInferrer {
6537
6569
  const inner = envelopes[0];
6538
6570
  const innerStatus = inner ? this.envelopeOwnStatus(inner) : 'undecided';
6539
6571
  if (innerStatus === 'undecided' || innerStatus === 'variable') {
6540
- const outer = this.responseSiteStatus(node);
6572
+ const outer = replyStatus(this.replyChecker(), node.compilerNode, { readChain: false });
6541
6573
  if (outer === 'error' || outer === 'redirect')
6542
6574
  return 'dropped';
6543
6575
  }
@@ -6548,7 +6580,7 @@ export class TypeInferrer {
6548
6580
  /** The status a builder call sends: its status argument, or the builder's literal. */
6549
6581
  builderCallStatus(call, builder) {
6550
6582
  if (builder.statusParameter !== undefined) {
6551
- return this.statusOfValue(call.getArguments()[builder.statusParameter]);
6583
+ return valueStatus(this.replyChecker(), call.getArguments()[builder.statusParameter]?.compilerNode);
6552
6584
  }
6553
6585
  return builder.fixedStatus ?? 'undecided';
6554
6586
  }
@@ -6573,22 +6605,10 @@ export class TypeInferrer {
6573
6605
  for (const name of STATUS_MEMBER_NAMES) {
6574
6606
  const value = this.envelopeMemberValue(literal, name);
6575
6607
  if (value)
6576
- return this.statusOfValue(value);
6608
+ return valueStatus(this.replyChecker(), value.compilerNode);
6577
6609
  }
6578
6610
  return 'undecided';
6579
6611
  }
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
6612
  /**
6593
6613
  * The value node an object literal gives a member: a property assignment's
6594
6614
  * initializer, or a shorthand's own name.
@@ -6695,7 +6715,7 @@ export class TypeInferrer {
6695
6715
  const statusParameter = this.parameterIndexOf(statusValue, params);
6696
6716
  let fixedStatus;
6697
6717
  if (statusParameter === undefined) {
6698
- const codes = this.statedStatusCodes(this.peelTransparentExpression(statusValue));
6718
+ const codes = statedStatusCodes(this.replyChecker(), this.peelTransparentExpression(statusValue).compilerNode);
6699
6719
  if (!Array.isArray(codes))
6700
6720
  return undefined;
6701
6721
  const kind = classifyStatusCodes(codes);
@@ -6910,7 +6930,7 @@ export class TypeInferrer {
6910
6930
  if (args.length === 0)
6911
6931
  return undefined;
6912
6932
  // An error or redirect branch sends no success body (carrick#1161).
6913
- const status = this.responseSiteStatus(call);
6933
+ const status = replyStatus(this.replyChecker(), call.compilerNode, { readChain: false });
6914
6934
  if (status === 'error' || status === 'redirect')
6915
6935
  return undefined;
6916
6936
  if (Node.isCallExpression(call) && this.callsFailureHelper(call, depth))
@@ -6967,7 +6987,7 @@ export class TypeInferrer {
6967
6987
  if (branches.length === 0)
6968
6988
  return false;
6969
6989
  return branches.every((branch) => {
6970
- const status = this.responseSiteStatus(branch);
6990
+ const status = replyStatus(this.replyChecker(), branch.compilerNode, { readChain: false });
6971
6991
  if (status === 'error' || status === 'redirect')
6972
6992
  return true;
6973
6993
  const inner = this.peelTransparentExpression(branch);
@@ -7097,165 +7117,15 @@ export class TypeInferrer {
7097
7117
  statesInit = true;
7098
7118
  continue;
7099
7119
  }
7100
- if (name === 'status' && this.statedStatusCodes(value) !== undefined) {
7120
+ if (name === 'status' && statedStatusCodes(this.replyChecker(), value.compilerNode) !== undefined) {
7101
7121
  statesInit = true;
7102
7122
  }
7103
7123
  }
7104
7124
  return statesInit;
7105
7125
  }
7106
- /**
7107
- * What a response send states about its HTTP status (carrick#1161).
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;
7126
+ /** The raw checker the shared reply-status reader (`capture/reply-status.ts`) reads with. */
7127
+ replyChecker() {
7128
+ return this.project.getTypeChecker().compilerObject;
7259
7129
  }
7260
7130
  /**
7261
7131
  * Peel the wrappers that do not change an expression's payload: parentheses
@@ -9360,7 +9230,7 @@ export class TypeInferrer {
9360
9230
  if (sourceFile.isDeclarationFile())
9361
9231
  return false;
9362
9232
  return !isExternalOrigin(this.project.getProgram().compilerObject, sourceFile.compilerNode, this.repoRoot, externalImportsOf(this.project));
9363
- }, (call) => this.chainStatus(call), (value) => this.statusOfValue(value), (value) => this.unwrapJsonStringifyArg(this.unwrapExpressionNode(value)));
9233
+ }, (call) => chainStatus(this.replyChecker(), call.compilerNode), (value) => valueStatus(this.replyChecker(), value.compilerNode), (value) => this.unwrapJsonStringifyArg(this.unwrapExpressionNode(value)));
9364
9234
  }
9365
9235
  /**
9366
9236
  * carrick#2674: where today's reading of a route's response is empty
@@ -1072,6 +1072,16 @@ export interface InferRequestItem {
1072
1072
  * this call instantiates it; one it leaves open is never published.
1073
1073
  */
1074
1074
  through_call?: ThroughCall;
1075
+ /**
1076
+ * Stale-position T2: the lines of the handler this route request is for, set
1077
+ * where the registration hands in a function literal. A node found by
1078
+ * `expression_text` or by line is used only inside them; otherwise the
1079
+ * request is answered `unknown`.
1080
+ */
1081
+ handler_lines?: {
1082
+ start_line: number;
1083
+ end_line: number;
1084
+ };
1075
1085
  }
1076
1086
  /**
1077
1087
  * Response status