carrick 0.3.147 → 0.3.149

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.
@@ -20,11 +20,12 @@
20
20
  import * as path from 'node:path';
21
21
  import { Node, SyntaxKind, VariableDeclarationKind, ts, } from 'ts-morph';
22
22
  import { typeToStringAsDeclared } from './print-type.js';
23
+ import { functionBodyOf, peelValueWrappers } from './function-body.js';
23
24
  import { validateInferRequestItem } from './validators.js';
24
25
  import { declarationSite } from './bundler.js';
25
26
  import { printedTypeHoldsTopType, relateSymbolToRead } from './symbol-relation.js';
26
27
  import { notePrintedType, PrintedTypes } from './printed-names.js';
27
- import { externalImportsOf, isExternalOrigin } from './origin.js';
28
+ import { expandOriginOf, externalImportsOf, isExternalOrigin } from './origin.js';
28
29
  import { BINARY_BODY_READS, isPlatformBodyRead, JSON_READ, RAW_TEXT_FORMAT, WHOLE_BODY_READS, } from './platform-body-read.js';
29
30
  import { reachedOnlyOnFailure } from './failure-path.js';
30
31
  import { readResponseModes } from './response-modes.js';
@@ -35,11 +36,12 @@ import { endLineOf, startLineOf } from './line-index.js';
35
36
  import { callsOf, descendantsOf, descendantsOnLine, descendantsOnLines, wrap, } from './node-index.js';
36
37
  import { elapsedMs, inferTiming, phaseClock, timedPhase } from './infer-timing.js';
37
38
  import { importedNameOf, packageNameOf } from './library-claims.js';
39
+ import { constituentsOf } from './capture/index.js';
38
40
  import { StatedParams } from './stated-params.js';
39
41
  import { bindingToleratesAbsence, handlerToleratesAbsentBody } from './absent-body.js';
40
42
  import { replaceFileText } from './program-files.js';
41
43
  import { loopElementPick } from './loop-element.js';
42
- import { classifyStatusCodes, constituents, httpStatus, statesNoBody, statesNoShape, statusKeyCode, statusTableOf, successEntries, } from './capture/index.js';
44
+ import { classifyStatusCodes, httpStatus, statesNoBody, statesNoShape, statusKeyCode, statusTableOf, successEntries, } from './capture/index.js';
43
45
  import { addedDiagnostics, applyInsertions, fileDiagnostics, literalInsertions, mapBack, mapForward, normalise, pathOf, } from './unwidened.js';
44
46
  import { expandTypeStructural, jsonWireType, } from './type-structural-expander.js';
45
47
  /**
@@ -458,11 +460,7 @@ export class TypeInferrer {
458
460
  * classify one declaration two ways.
459
461
  */
460
462
  expandOrigin() {
461
- return {
462
- program: this.project.getProgram().compilerObject,
463
- repoRoot: this.repoRoot,
464
- imports: externalImportsOf(this.project),
465
- };
463
+ return expandOriginOf(this.project, this.repoRoot);
466
464
  }
467
465
  /**
468
466
  * Infer types for the given requests
@@ -4131,7 +4129,7 @@ export class TypeInferrer {
4131
4129
  for (const arg of args) {
4132
4130
  const entries = statusTableOf(checker, arg.compilerType);
4133
4131
  if (entries) {
4134
- tableValues.set(arg, new Set(entries.flatMap((entry) => constituents(checker.getTypeOfSymbolAtLocation(entry.symbol, at.compilerNode)))));
4132
+ tableValues.set(arg, new Set(entries.flatMap((entry) => constituentsOf(checker.getTypeOfSymbolAtLocation(entry.symbol, at.compilerNode)))));
4135
4133
  }
4136
4134
  }
4137
4135
  const bodies = new Map();
@@ -4145,7 +4143,7 @@ export class TypeInferrer {
4145
4143
  if (held.getText() === arg.getText())
4146
4144
  return true;
4147
4145
  const values = tableValues.get(arg);
4148
- const heldTypes = constituents(held.compilerType);
4146
+ const heldTypes = constituentsOf(held.compilerType);
4149
4147
  if (values &&
4150
4148
  heldTypes.length === values.size &&
4151
4149
  heldTypes.every((member) => values.has(member))) {
@@ -6367,19 +6365,7 @@ export class TypeInferrer {
6367
6365
  * expression.
6368
6366
  */
6369
6367
  builderFunctionOf(symbol) {
6370
- const bodies = [];
6371
- for (const declaration of symbol.getDeclarations()) {
6372
- if (Node.isFunctionDeclaration(declaration) && declaration.getBody()) {
6373
- bodies.push(declaration);
6374
- }
6375
- else if (Node.isVariableDeclaration(declaration)) {
6376
- const initializer = declaration.getInitializer();
6377
- const fn = initializer ? this.peelTransparentExpression(initializer) : undefined;
6378
- if (fn && (Node.isArrowFunction(fn) || Node.isFunctionExpression(fn)))
6379
- bodies.push(fn);
6380
- }
6381
- }
6382
- return bodies.length === 1 ? bodies[0] : undefined;
6368
+ return functionBodyOf(symbol.getDeclarations(), { methods: false });
6383
6369
  }
6384
6370
  /**
6385
6371
  * The payload a builder call serialises. The source states it, and the
@@ -6562,6 +6548,8 @@ export class TypeInferrer {
6562
6548
  const status = this.responseSiteStatus(call);
6563
6549
  if (status === 'error' || status === 'redirect')
6564
6550
  return undefined;
6551
+ if (Node.isCallExpression(call) && this.callsFailureHelper(call, depth))
6552
+ return undefined;
6565
6553
  // carrick#3053: a send the service's labels name carries its body in the
6566
6554
  // slot the label states, read as a labelled call send reads it.
6567
6555
  const labelled = this.statedParams.sendAt(call);
@@ -6590,6 +6578,37 @@ export class TypeInferrer {
6590
6578
  }
6591
6579
  return undefined;
6592
6580
  }
6581
+ /**
6582
+ * True when a call's callee is a function this repository declares whose
6583
+ * every returned branch is a send stating an error or redirect status, or a
6584
+ * call to another such function (carrick#3523). `return failure(result)` is
6585
+ * then an error branch, as `json({ error }, { status: 400 })` written inline
6586
+ * is, and the value handed to it is never the success body. A callee that
6587
+ * returns anything else (a success send, a status the source does not fix,
6588
+ * a non-send) keeps the ordinary argument reading.
6589
+ */
6590
+ callsFailureHelper(call, depth) {
6591
+ if (depth > RESPONSE_HELPER_MAX_DEPTH)
6592
+ return false;
6593
+ let symbol = call.getExpression().getSymbol();
6594
+ if (symbol?.isAlias())
6595
+ symbol = symbol.getAliasedSymbol() ?? symbol;
6596
+ if (!symbol || this.symbolIsLibOrExternalOrigin(symbol))
6597
+ return false;
6598
+ const helper = this.builderFunctionOf(symbol);
6599
+ if (!helper)
6600
+ return false;
6601
+ const branches = this.returnedExpressions(helper).flatMap((expression) => this.expandResponseBranches(expression, 0));
6602
+ if (branches.length === 0)
6603
+ return false;
6604
+ return branches.every((branch) => {
6605
+ const status = this.responseSiteStatus(branch);
6606
+ if (status === 'error' || status === 'redirect')
6607
+ return true;
6608
+ const inner = this.peelTransparentExpression(branch);
6609
+ return Node.isCallExpression(inner) && this.callsFailureHelper(inner, depth + 1);
6610
+ });
6611
+ }
6593
6612
  /**
6594
6613
  * True when a node's type reads as a response payload.
6595
6614
  *
@@ -7425,9 +7444,9 @@ export class TypeInferrer {
7425
7444
  */
7426
7445
  asHandlerFunction(node, followWrapperCalls = true) {
7427
7446
  const expr = this.unwrapExpressionNode(node);
7428
- if (Node.isArrowFunction(expr) || Node.isFunctionExpression(expr)) {
7429
- return expr;
7430
- }
7447
+ const inline = functionBodyOf([peelValueWrappers(expr)], { methods: false });
7448
+ if (inline)
7449
+ return inline;
7431
7450
  if (Node.isIdentifier(expr)) {
7432
7451
  for (const def of expr.getDefinitionNodes()) {
7433
7452
  const func = this.functionFromDeclaration(def, followWrapperCalls);
@@ -7450,7 +7469,9 @@ export class TypeInferrer {
7450
7469
  */
7451
7470
  functionFromDeclaration(decl, followWrapperCalls = true) {
7452
7471
  if (Node.isFunctionDeclaration(decl)) {
7453
- return decl;
7472
+ // A declaration with no body anywhere (`declare function`) is still the
7473
+ // handler's function; its body is out of reach, which callers read.
7474
+ return functionBodyOf(decl.getSymbol()?.getDeclarations() ?? [decl], { methods: false }) ?? decl;
7454
7475
  }
7455
7476
  if (Node.isArrowFunction(decl) || Node.isFunctionExpression(decl)) {
7456
7477
  return decl;
@@ -7469,10 +7490,10 @@ export class TypeInferrer {
7469
7490
  if (varDecl) {
7470
7491
  const initializer = varDecl.getInitializer();
7471
7492
  if (initializer) {
7493
+ const direct = functionBodyOf([varDecl], { methods: false });
7494
+ if (direct)
7495
+ return direct;
7472
7496
  const inner = this.unwrapExpressionNode(initializer);
7473
- if (Node.isArrowFunction(inner) || Node.isFunctionExpression(inner)) {
7474
- return inner;
7475
- }
7476
7497
  if (followWrapperCalls && Node.isCallExpression(inner)) {
7477
7498
  return this.functionWrappedBy(inner, 0);
7478
7499
  }
@@ -9245,47 +9266,24 @@ export class TypeInferrer {
9245
9266
  let symbol = call.getExpression().getSymbol();
9246
9267
  if (symbol?.isAlias())
9247
9268
  symbol = symbol.getAliasedSymbol() ?? symbol;
9248
- for (const declaration of symbol?.getDeclarations() ?? []) {
9249
- const fn = Node.isFunctionDeclaration(declaration) || Node.isMethodDeclaration(declaration)
9250
- ? declaration
9251
- : Node.isVariableDeclaration(declaration)
9252
- ? this.unwrapExpressionNode(declaration.getInitializer())
9253
- : undefined;
9254
- if (!fn ||
9255
- !(Node.isFunctionDeclaration(fn) ||
9256
- Node.isMethodDeclaration(fn) ||
9257
- Node.isArrowFunction(fn) ||
9258
- Node.isFunctionExpression(fn)) ||
9259
- !fn.getBody() ||
9260
- fn.getSourceFile().isDeclarationFile() ||
9261
- fn.getSourceFile().isInNodeModules()) {
9262
- continue;
9263
- }
9264
- const parameter = fn.getParameters()[index];
9265
- const parameterSymbol = parameter?.getNameNode().getSymbol()?.compilerSymbol;
9266
- if (!parameter || parameter.isRestParameter() || !parameterSymbol)
9267
- continue;
9268
- const returned = this.returnedExpressions(fn);
9269
- let returnsArgument = false;
9270
- const everyReturnPasses = returned.every((expression) => {
9271
- let node = expression;
9272
- while (Node.isParenthesizedExpression(node) ||
9273
- Node.isAsExpression(node) ||
9274
- Node.isTypeAssertion(node) ||
9275
- Node.isSatisfiesExpression(node) ||
9276
- Node.isNonNullExpression(node)) {
9277
- node = node.getExpression();
9278
- }
9279
- if (Node.isIdentifier(node) && node.getSymbol()?.compilerSymbol === parameterSymbol) {
9280
- returnsArgument = true;
9281
- return true;
9282
- }
9283
- return this.isEmptyDefault(node);
9284
- });
9285
- if (everyReturnPasses && returnsArgument)
9286
- return fn;
9269
+ const fn = functionBodyOf(symbol?.getDeclarations() ?? []);
9270
+ if (!fn || fn.getSourceFile().isDeclarationFile() || fn.getSourceFile().isInNodeModules()) {
9271
+ return undefined;
9287
9272
  }
9288
- return undefined;
9273
+ const parameter = fn.getParameters()[index];
9274
+ const parameterSymbol = parameter?.getNameNode().getSymbol()?.compilerSymbol;
9275
+ if (!parameter || parameter.isRestParameter() || !parameterSymbol)
9276
+ return undefined;
9277
+ let returnsArgument = false;
9278
+ const everyReturnPasses = this.returnedExpressions(fn).every((expression) => {
9279
+ const node = peelValueWrappers(expression);
9280
+ if (Node.isIdentifier(node) && node.getSymbol()?.compilerSymbol === parameterSymbol) {
9281
+ returnsArgument = true;
9282
+ return true;
9283
+ }
9284
+ return this.isEmptyDefault(node);
9285
+ });
9286
+ return everyReturnPasses && returnsArgument ? fn : undefined;
9289
9287
  }
9290
9288
  /** `{}`, `[]`, `null` or `undefined`: a default that states no member. */
9291
9289
  isEmptyDefault(node) {