carrick 0.3.154 → 0.3.155

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 (150) hide show
  1. package/bin/carrick.mjs +26 -4
  2. package/dist/cli.d.ts +6 -0
  3. package/dist/cli.js +1 -0
  4. package/dist/cli.js.map +1 -1
  5. package/dist/contract.d.ts +10 -0
  6. package/dist/contract.js.map +1 -1
  7. package/dist/facts/diff.d.ts +27 -0
  8. package/dist/facts/diff.js +134 -0
  9. package/dist/facts/diff.js.map +1 -0
  10. package/dist/facts/enabled.d.ts +2 -0
  11. package/dist/facts/enabled.js +9 -0
  12. package/dist/facts/enabled.js.map +1 -0
  13. package/dist/facts/fetch.d.ts +48 -0
  14. package/dist/facts/fetch.js +235 -0
  15. package/dist/facts/fetch.js.map +1 -0
  16. package/dist/facts/files.d.ts +21 -0
  17. package/dist/facts/files.js +58 -0
  18. package/dist/facts/files.js.map +1 -0
  19. package/dist/facts/lines.d.ts +70 -0
  20. package/dist/facts/lines.js +335 -0
  21. package/dist/facts/lines.js.map +1 -0
  22. package/dist/facts/match.d.ts +91 -0
  23. package/dist/facts/match.js +1122 -0
  24. package/dist/facts/match.js.map +1 -0
  25. package/dist/facts/peers.d.ts +48 -0
  26. package/dist/facts/peers.js +143 -0
  27. package/dist/facts/peers.js.map +1 -0
  28. package/dist/facts/refresh-run.d.ts +2 -0
  29. package/dist/facts/refresh-run.js +18 -0
  30. package/dist/facts/refresh-run.js.map +1 -0
  31. package/dist/facts/rules/packages.d.ts +16 -0
  32. package/dist/facts/rules/packages.js +157 -0
  33. package/dist/facts/rules/packages.js.map +1 -0
  34. package/dist/facts/rules/same-name.d.ts +28 -0
  35. package/dist/facts/rules/same-name.js +131 -0
  36. package/dist/facts/rules/same-name.js.map +1 -0
  37. package/dist/facts/store.d.ts +119 -0
  38. package/dist/facts/store.js +341 -0
  39. package/dist/facts/store.js.map +1 -0
  40. package/dist/git-remote.d.ts +6 -0
  41. package/dist/git-remote.js +37 -0
  42. package/dist/git-remote.js.map +1 -0
  43. package/dist/hook/apply-patch.d.ts +23 -0
  44. package/dist/hook/apply-patch.js +92 -0
  45. package/dist/hook/apply-patch.js.map +1 -1
  46. package/dist/hook/bash-targets.d.ts +8 -0
  47. package/dist/hook/bash-targets.js +23 -11
  48. package/dist/hook/bash-targets.js.map +1 -1
  49. package/dist/hook/edit-flow.d.ts +29 -2
  50. package/dist/hook/edit-flow.js +80 -29
  51. package/dist/hook/edit-flow.js.map +1 -1
  52. package/dist/hook/late.d.ts +29 -0
  53. package/dist/hook/late.js +121 -26
  54. package/dist/hook/late.js.map +1 -1
  55. package/dist/hook/post-bash.js +45 -22
  56. package/dist/hook/post-bash.js.map +1 -1
  57. package/dist/hook/post-edit.d.ts +16 -1
  58. package/dist/hook/post-edit.js +177 -13
  59. package/dist/hook/post-edit.js.map +1 -1
  60. package/dist/hook/post-read.js +8 -12
  61. package/dist/hook/post-read.js.map +1 -1
  62. package/dist/hook/post-search.d.ts +2 -0
  63. package/dist/hook/post-search.js +25 -0
  64. package/dist/hook/post-search.js.map +1 -0
  65. package/dist/hook/probe.d.ts +18 -3
  66. package/dist/hook/probe.js +30 -6
  67. package/dist/hook/probe.js.map +1 -1
  68. package/dist/hook/rechecks.d.ts +66 -11
  69. package/dist/hook/rechecks.js +115 -29
  70. package/dist/hook/rechecks.js.map +1 -1
  71. package/dist/hook/refresh.d.ts +9 -0
  72. package/dist/hook/refresh.js +10 -0
  73. package/dist/hook/refresh.js.map +1 -1
  74. package/dist/hook/reuse.d.ts +16 -2
  75. package/dist/hook/reuse.js +49 -6
  76. package/dist/hook/reuse.js.map +1 -1
  77. package/dist/hook/session-key.d.ts +12 -0
  78. package/dist/hook/session-key.js +30 -0
  79. package/dist/hook/session-key.js.map +1 -0
  80. package/dist/hook/session-start.js +14 -7
  81. package/dist/hook/session-start.js.map +1 -1
  82. package/dist/hook/stop-wait.d.ts +2 -9
  83. package/dist/hook/stop-wait.js +96 -113
  84. package/dist/hook/stop-wait.js.map +1 -1
  85. package/dist/hook/stop.js +13 -8
  86. package/dist/hook/stop.js.map +1 -1
  87. package/dist/hook/subagent-stop.d.ts +2 -0
  88. package/dist/hook/subagent-stop.js +45 -0
  89. package/dist/hook/subagent-stop.js.map +1 -0
  90. package/dist/hook/timings.d.ts +72 -0
  91. package/dist/hook/timings.js +175 -0
  92. package/dist/hook/timings.js.map +1 -0
  93. package/dist/hook/user-prompt.js +17 -13
  94. package/dist/hook/user-prompt.js.map +1 -1
  95. package/dist/hook/worktree.d.ts +11 -0
  96. package/dist/hook/worktree.js +27 -1
  97. package/dist/hook/worktree.js.map +1 -1
  98. package/dist/init/repo-copies.d.ts +6 -0
  99. package/dist/init/repo-copies.js +7 -3
  100. package/dist/init/repo-copies.js.map +1 -1
  101. package/dist/init/repos.d.ts +2 -2
  102. package/dist/init/repos.js +1 -34
  103. package/dist/init/repos.js.map +1 -1
  104. package/dist/init/settings.js +25 -13
  105. package/dist/init/settings.js.map +1 -1
  106. package/dist/render.d.ts +8 -33
  107. package/dist/render.js +31 -122
  108. package/dist/render.js.map +1 -1
  109. package/package.json +6 -6
  110. package/plugin/.claude-plugin/plugin.json +1 -1
  111. package/plugin/hooks/hooks.json +44 -3
  112. package/sidecar/dist/src/capture/anchors.d.ts +12 -1
  113. package/sidecar/dist/src/capture/anchors.js +28 -47
  114. package/sidecar/dist/src/capture/api.d.ts +41 -1
  115. package/sidecar/dist/src/capture/augmentations.d.ts +2 -0
  116. package/sidecar/dist/src/capture/augmentations.js +1 -1
  117. package/sidecar/dist/src/capture/check-classify.d.ts +12 -1
  118. package/sidecar/dist/src/capture/check-classify.js +43 -0
  119. package/sidecar/dist/src/capture/check-deep.d.ts +44 -1
  120. package/sidecar/dist/src/capture/check-deep.js +161 -2
  121. package/sidecar/dist/src/capture/check.js +23 -2
  122. package/sidecar/dist/src/capture/checkout.d.ts +9 -3
  123. package/sidecar/dist/src/capture/checkout.js +12 -5
  124. package/sidecar/dist/src/capture/deep-walk.d.ts +25 -0
  125. package/sidecar/dist/src/capture/deep-walk.js +41 -0
  126. package/sidecar/dist/src/capture/deno-project.d.ts +22 -0
  127. package/sidecar/dist/src/capture/deno-project.js +48 -1
  128. package/sidecar/dist/src/capture/index.d.ts +2 -1
  129. package/sidecar/dist/src/capture/index.js +14 -4
  130. package/sidecar/dist/src/capture/machinery.d.ts +8 -53
  131. package/sidecar/dist/src/capture/machinery.js +10 -157
  132. package/sidecar/dist/src/capture/self-check.d.ts +7 -0
  133. package/sidecar/dist/src/capture/self-check.js +44 -1
  134. package/sidecar/dist/src/capture/site-node.d.ts +166 -0
  135. package/sidecar/dist/src/capture/site-node.js +570 -0
  136. package/sidecar/dist/src/capture/tool-env.d.ts +12 -0
  137. package/sidecar/dist/src/capture/tool-env.js +26 -0
  138. package/sidecar/dist/src/library-claims.js +211 -20
  139. package/sidecar/dist/src/node-index.d.ts +2 -0
  140. package/sidecar/dist/src/node-index.js +5 -1
  141. package/sidecar/dist/src/position-rows.d.ts +39 -4
  142. package/sidecar/dist/src/position-rows.js +233 -16
  143. package/sidecar/dist/src/project-loader.js +13 -5
  144. package/sidecar/dist/src/retype.d.ts +13 -0
  145. package/sidecar/dist/src/retype.js +126 -0
  146. package/sidecar/dist/src/type-inferrer.d.ts +33 -145
  147. package/sidecar/dist/src/type-inferrer.js +68 -524
  148. package/sidecar/dist/src/types.d.ts +55 -2
  149. package/sidecar/dist/src/validators.d.ts +800 -142
  150. package/sidecar/dist/src/validators.js +11 -1
@@ -57,11 +57,11 @@ import * as fs from 'node:fs';
57
57
  import * as path from 'node:path';
58
58
  import * as zlib from 'node:zlib';
59
59
  import { ts } from 'ts-morph';
60
- import { WriteGuard, installedPackageDirectory, isDeclarationFileName, moduleExport, toPosix } from './capture/index.js';
60
+ import { WriteGuard, installedPackageDirectory, isDeclarationFileName, isRelative, moduleExport, toPosix } from './capture/index.js';
61
61
  import { attributedDeclaration, installedPackageOf, typeDeclaration } from './declaration-attribution.js';
62
62
  import { typeToStringAsDeclared } from './print-type.js';
63
63
  /** The format's version, written in the header. */
64
- export const POSITION_ROWS_VERSION = 6;
64
+ export const POSITION_ROWS_VERSION = 7;
65
65
  /** The type is `any`, declared or inferred. Never set with the error type. */
66
66
  export const FLAG_ANY = 1;
67
67
  /** The type is the compiler's error type: something did not resolve. */
@@ -87,11 +87,21 @@ export const FLAG_MODULE_DEFAULT = 64;
87
87
  * function returns, or an awaitable of one), so the library runs it and
88
88
  * hands its result back (carrick#2634). Set when the call passes at least
89
89
  * one argument at a function-typed parameter and every such parameter
90
- * meets that return. The last free bit of the `u8`: the next flag widens
91
- * the field and bumps the format. Pinned to `FLAG_RUNS_HANDED` in
92
- * `src/position_rows.rs` by `position-rows-runs-handed.test.ts`.
90
+ * meets that return. The last bit of the first byte: the flags widened to
91
+ * sixteen bits with {@link FLAG_DECLARED_TYPE} (format 7). Pinned to
92
+ * `FLAG_RUNS_HANDED` in `src/position_rows.rs` by
93
+ * `position-rows-runs-handed.test.ts`.
93
94
  */
94
95
  export const FLAG_RUNS_HANDED = 128;
96
+ /**
97
+ * A `receiver_type` row only: the row names the type alias a package
98
+ * declares that the receiver was *declared* as, not a type the value holds
99
+ * ("declared, not held"). The compiler's printed type of a proxy is what the
100
+ * alias expands to (an alias of an alias, an intersection), so a reader cannot
101
+ * get the alias from the value. Pinned to `FLAG_DECLARED_TYPE` in
102
+ * `src/position_rows.rs` by `position-rows-declared-type.test.ts`.
103
+ */
104
+ export const FLAG_DECLARED_TYPE = 256;
95
105
  /** The most `decl` rows one position lists. */
96
106
  export const DECL_CAP = 16;
97
107
  const COMPARE_OPERATORS = new Set([
@@ -175,6 +185,7 @@ export function writePositionRows(options) {
175
185
  }
176
186
  read.set(index, sourceFile?.getFilePath());
177
187
  }
188
+ loadRequiredPackages(project, [...read.values()].filter((f) => f !== undefined));
178
189
  // Built once, after every file the group adds. The language service's
179
190
  // program is the one walked, as it was when go-to-implementation answered
180
191
  // over it (carrick#2430, dropped in #2561): its rows are pinned.
@@ -433,6 +444,9 @@ export class PositionWalker {
433
444
  }
434
445
  if (kind === 'member' && !(row[8] & FLAG_CHECKER_THREW)) {
435
446
  rows.push(...this.receiverTypeRows(node, row));
447
+ const declared = this.declaredTypeRow(node);
448
+ if (declared)
449
+ rows.push(declared);
436
450
  }
437
451
  if (kind === 'call' && !(row[8] & FLAG_CHECKER_THREW)) {
438
452
  const typed = this.typedDeclRow(node, row[1], row[2]);
@@ -823,6 +837,115 @@ export class PositionWalker {
823
837
  return [];
824
838
  }
825
839
  }
840
+ /**
841
+ * The {@link FLAG_DECLARED_TYPE} row of a member read's receiver: the type
842
+ * alias a package declares that the receiver was declared as. Read from
843
+ * the declaration, never the value: the receiver symbol's annotation, else
844
+ * the declared return of the make its initializer calls (or the call the
845
+ * receiver is), walked through the checkout's own aliases to the first
846
+ * package alias (the first constituent of a union or intersection that has
847
+ * one). Only aliases: an interface or class is what the class rows are for.
848
+ * Only where the member read sits on a call's callee chain
849
+ * (`a.b.c()`); a plain property read gives none. At most one row per
850
+ * receiver span, outside {@link DECL_CAP}, and not subject to the
851
+ * same-package skip of the class rows. A throw writes none.
852
+ */
853
+ declaredTypeRow(node) {
854
+ if (!ts.isPropertyAccessExpression(node) && !ts.isElementAccessExpression(node))
855
+ return undefined;
856
+ if (!isOnCalleeChain(node))
857
+ return undefined;
858
+ try {
859
+ const receiver = node.expression;
860
+ const typeNode = this.declaredTypeNode(receiver);
861
+ if (!typeNode)
862
+ return undefined;
863
+ const found = this.packageAlias(typeNode, new Set(), 0);
864
+ if (!found)
865
+ return undefined;
866
+ const { symbol, declaration, declFile, declLine, declPackage } = found;
867
+ const start = receiver.getStart(node.getSourceFile());
868
+ const flags = this.publication(symbol, declaration) | FLAG_DECLARED_TYPE;
869
+ return ['receiver_type', start, receiver.getEnd(), ts.symbolName(symbol), declFile, declLine, declPackage, null, flags];
870
+ }
871
+ catch {
872
+ return undefined;
873
+ }
874
+ }
875
+ /** The type node a receiver is declared by: its symbol's annotation, or the declared return of the call it is made by. */
876
+ declaredTypeNode(receiver) {
877
+ const expression = skipWrappers(receiver);
878
+ if (ts.isCallExpression(expression))
879
+ return this.returnTypeNode(expression);
880
+ const name = ts.isPropertyAccessExpression(expression)
881
+ ? expression.name
882
+ : ts.isIdentifier(expression)
883
+ ? expression
884
+ : undefined;
885
+ let symbol = name ? this.checker.getSymbolAtLocation(name) : undefined;
886
+ if (symbol && symbol.flags & ts.SymbolFlags.Alias)
887
+ symbol = this.checker.getAliasedSymbol(symbol);
888
+ const declaration = symbol?.valueDeclaration;
889
+ if (!declaration ||
890
+ !(ts.isVariableDeclaration(declaration) ||
891
+ ts.isPropertyDeclaration(declaration) ||
892
+ ts.isPropertySignature(declaration) ||
893
+ ts.isParameter(declaration))) {
894
+ return undefined;
895
+ }
896
+ if (declaration.type)
897
+ return declaration.type;
898
+ const initializer = 'initializer' in declaration ? declaration.initializer : undefined;
899
+ if (!initializer || ts.isParameter(declaration))
900
+ return undefined;
901
+ const made = skipWrappers(initializer);
902
+ return ts.isCallExpression(made) ? this.returnTypeNode(made) : undefined;
903
+ }
904
+ /** The declared return type node of the signature a call resolves to. */
905
+ returnTypeNode(call) {
906
+ const type = this.checker.getResolvedSignature(call)?.declaration?.type;
907
+ return type && ts.isTypeNode(type) ? type : undefined;
908
+ }
909
+ /**
910
+ * The first type alias a package declares that `typeNode` names, through
911
+ * the checkout's own aliases (`type Runner = Inner`), a union or an
912
+ * intersection, in order. An alias, interface or class of the checkout
913
+ * that is not an alias is a stop; so is the default library.
914
+ */
915
+ packageAlias(typeNode, seen, depth) {
916
+ if (depth > DECLARED_WALK_DEPTH)
917
+ return undefined;
918
+ if (ts.isParenthesizedTypeNode(typeNode))
919
+ return this.packageAlias(typeNode.type, seen, depth + 1);
920
+ if (ts.isUnionTypeNode(typeNode) || ts.isIntersectionTypeNode(typeNode)) {
921
+ for (const member of typeNode.types) {
922
+ const found = this.packageAlias(member, seen, depth + 1);
923
+ if (found)
924
+ return found;
925
+ }
926
+ return undefined;
927
+ }
928
+ if (!ts.isTypeReferenceNode(typeNode))
929
+ return undefined;
930
+ const name = ts.isQualifiedName(typeNode.typeName) ? typeNode.typeName.right : typeNode.typeName;
931
+ let symbol = this.checker.getSymbolAtLocation(name);
932
+ if (symbol && symbol.flags & ts.SymbolFlags.Alias)
933
+ symbol = this.checker.getAliasedSymbol(symbol);
934
+ if (!symbol || seen.has(symbol) || !(symbol.flags & ts.SymbolFlags.TypeAlias))
935
+ return undefined;
936
+ seen.add(symbol);
937
+ const declaration = symbol.declarations?.find(ts.isTypeAliasDeclaration);
938
+ if (!declaration)
939
+ return undefined;
940
+ const file = declaration.getSourceFile();
941
+ const { declFile, declLine, declPackage } = this.located(file, declaration.getStart(file));
942
+ if (declFile === null || declFile.startsWith('lib:'))
943
+ return undefined;
944
+ if (declFile.startsWith('pkg:') || declFile.split('/').includes('node_modules')) {
945
+ return declPackage === null ? undefined : { symbol, declaration, declFile, declLine, declPackage };
946
+ }
947
+ return this.packageAlias(declaration.type, seen, depth + 1);
948
+ }
826
949
  /**
827
950
  * The name a `receiver_type` row gives a class. A class written
828
951
  * `export default class Cursor` has the symbol name `default`, which is
@@ -1009,21 +1132,19 @@ export class PositionWalker {
1009
1132
  }
1010
1133
  /** `"m"` of a call `require("m")` to the global `require`, else `undefined`. */
1011
1134
  requireSpecifier(expression) {
1012
- if (!ts.isCallExpression(expression) || expression.arguments.length !== 1)
1013
- return undefined;
1014
- const callee = expression.expression;
1015
- const argument = expression.arguments[0];
1016
- if (!ts.isIdentifier(callee) || callee.text !== 'require' || !ts.isStringLiteral(argument))
1135
+ const specifier = requireCallSpecifier(expression);
1136
+ if (specifier === undefined || !ts.isCallExpression(expression))
1017
1137
  return undefined;
1018
- // The global `require` is declared in a declaration file (or nowhere); a
1019
- // `require` the service writes is a declaration in its own source.
1020
- const own = (this.checker.getSymbolAtLocation(callee)?.declarations ?? []).some((d) => !d.getSourceFile().isDeclarationFile);
1021
- return own ? undefined : argument.text;
1138
+ // The global `require` is declared in a declaration file, by a `declare`
1139
+ // in the source (carrick#3971), or nowhere; a `require` the service
1140
+ // writes is a declaration in its own source with a body.
1141
+ const own = (this.checker.getSymbolAtLocation(expression.expression)?.declarations ?? []).some((d) => !d.getSourceFile().isDeclarationFile && !isAmbient(d));
1142
+ return own ? undefined : specifier;
1022
1143
  }
1023
1144
  exportOfRequired(binding) {
1024
1145
  const sourceOf = (specifier) => {
1025
- const resolved = ts.resolveModuleName(specifier, binding.from.fileName, this.program.getCompilerOptions(), this.resolutionHost(), undefined, undefined, ts.ModuleKind.CommonJS).resolvedModule;
1026
- return resolved ? this.program.getSourceFile(resolved.resolvedFileName) : undefined;
1146
+ const resolved = resolveRequired(specifier, binding.from.fileName, this.program.getCompilerOptions(), this.resolutionHost());
1147
+ return resolved ? this.program.getSourceFile(resolved) : undefined;
1027
1148
  };
1028
1149
  // The require that runs wins; the cast is read only when it names no file.
1029
1150
  const target = sourceOf(binding.specifier) ?? (binding.castSpecifier !== undefined ? sourceOf(binding.castSpecifier) : undefined);
@@ -1039,10 +1160,106 @@ export class PositionWalker {
1039
1160
  return this.host;
1040
1161
  }
1041
1162
  }
1163
+ /** Whether a node is under a `declare`: it declares a value and gives it no body. */
1164
+ function isAmbient(node) {
1165
+ for (let current = node; current; current = current.parent) {
1166
+ if (ts.canHaveModifiers(current) && ts.getModifiers(current)?.some((m) => m.kind === ts.SyntaxKind.DeclareKeyword))
1167
+ return true;
1168
+ }
1169
+ return false;
1170
+ }
1171
+ /** `"m"` of a call `require("m")` with one string literal, read from the syntax alone. */
1172
+ function requireCallSpecifier(expression) {
1173
+ if (!ts.isCallExpression(expression) || expression.arguments.length !== 1)
1174
+ return undefined;
1175
+ const callee = expression.expression;
1176
+ const argument = expression.arguments[0];
1177
+ if (!ts.isIdentifier(callee) || callee.text !== 'require' || !ts.isStringLiteral(argument))
1178
+ return undefined;
1179
+ return argument.text;
1180
+ }
1181
+ /** The file `require(specifier)` in the file `from` resolves to, as CommonJS. */
1182
+ function resolveRequired(specifier, from, options, host) {
1183
+ return ts.resolveModuleName(specifier, from, options, host, undefined, undefined, ts.ModuleKind.CommonJS).resolvedModule
1184
+ ?.resolvedFileName;
1185
+ }
1186
+ /**
1187
+ * Add to `project` the module of each package that a TypeScript file of
1188
+ * `fileNames` names in a `require("m")` call, before its program is built.
1189
+ * TypeScript follows such a call only in a JavaScript file, so a package that
1190
+ * is required and never imported is not in the program, and the carrick#2454
1191
+ * rule finds no module to name (carrick#3971). An `import` of the package
1192
+ * would load the same file. Relative specifiers are left as they are: a
1193
+ * required repo file the program does not list stays out of it. So is a
1194
+ * package's JavaScript: an import of a package with no types loads none.
1195
+ */
1196
+ function loadRequiredPackages(project, fileNames) {
1197
+ const options = project.getCompilerOptions();
1198
+ let host;
1199
+ for (const fileName of fileNames) {
1200
+ if (!/\.[cm]?tsx?$/.test(fileName) || isDeclarationFileName(fileName))
1201
+ continue;
1202
+ const sourceFile = project.getSourceFile(fileName)?.compilerNode;
1203
+ if (!sourceFile || !sourceFile.text.includes('require'))
1204
+ continue;
1205
+ const specifiers = new Set();
1206
+ const visit = (node) => {
1207
+ if (ts.isCallExpression(node)) {
1208
+ const specifier = requireCallSpecifier(node);
1209
+ if (specifier !== undefined && !isRelative(specifier))
1210
+ specifiers.add(specifier);
1211
+ }
1212
+ ts.forEachChild(node, visit);
1213
+ };
1214
+ visit(sourceFile);
1215
+ for (const specifier of specifiers) {
1216
+ host ??= ts.createCompilerHost(options);
1217
+ const resolved = resolveRequired(specifier, sourceFile.fileName, options, host);
1218
+ if (!resolved || /\.[cm]?jsx?$/.test(resolved) || project.getSourceFile(resolved))
1219
+ continue;
1220
+ try {
1221
+ project.addSourceFileAtPath(resolved);
1222
+ }
1223
+ catch {
1224
+ // A module that cannot be read is left out, as before.
1225
+ }
1226
+ }
1227
+ }
1228
+ }
1042
1229
  /** A row's fields after its kind and span. */
1043
1230
  function tail(row) {
1044
1231
  return [row[3], row[4], row[5], row[6], row[7], row[8]];
1045
1232
  }
1233
+ /** How far a declared type is walked through the checkout's own aliases. */
1234
+ const DECLARED_WALK_DEPTH = 8;
1235
+ /** An expression without the wrappers that do not change what it is: `(x)`, `x!`. */
1236
+ function skipWrappers(expression) {
1237
+ let current = expression;
1238
+ while (ts.isParenthesizedExpression(current) || ts.isNonNullExpression(current))
1239
+ current = current.expression;
1240
+ return current;
1241
+ }
1242
+ /** Whether a member read is the callee, or an object on the callee's chain, of a call (`a.b.c()`). */
1243
+ function isOnCalleeChain(node) {
1244
+ let current = node;
1245
+ for (;;) {
1246
+ const parent = current.parent;
1247
+ if (!parent)
1248
+ return false;
1249
+ if (ts.isCallExpression(parent))
1250
+ return parent.expression === current;
1251
+ if (ts.isParenthesizedExpression(parent) || ts.isNonNullExpression(parent)) {
1252
+ current = parent;
1253
+ continue;
1254
+ }
1255
+ if ((ts.isPropertyAccessExpression(parent) || ts.isElementAccessExpression(parent)) &&
1256
+ parent.expression === current) {
1257
+ current = parent;
1258
+ continue;
1259
+ }
1260
+ return false;
1261
+ }
1262
+ }
1046
1263
  /** Order two `decl_file` names, `null` last. */
1047
1264
  export function compareNames(a, b) {
1048
1265
  if (a === b)
@@ -226,11 +226,16 @@ export class ProjectLoader {
226
226
  this.buildProject = (kind) => {
227
227
  const deno = new DenoProject(denoConfig, this.repoRoot);
228
228
  this.denoProject = deno;
229
+ // Under CARRICK_UNTRUSTED_TREE the bound also admits the npm
230
+ // packages this graph resolved, and the resolver and the root
231
+ // list below hand the compiler nothing it refuses (carrick#4076).
232
+ const bound = untrustedReadBound(this.repoRoot, deno.readRoots);
233
+ deno.bindReadBound(bound);
229
234
  // The graph's external-library verdicts, kept past the program
230
235
  // rebuilds that drop them (carrick#1731).
231
236
  const imports = new ExternalImports();
232
237
  const project = new Project({
233
- ...this.fileSystemOption(),
238
+ ...this.fileSystemOption(bound),
234
239
  compilerOptions: sidecarCompilerOptions(optionsFor(kind, deno.parsed.options)),
235
240
  skipAddingFilesFromTsConfig: true,
236
241
  resolutionHost: imports.recording((host, getOptions) => ({
@@ -241,8 +246,12 @@ export class ProjectLoader {
241
246
  registerExternalImports(project, imports);
242
247
  pinProgramRoots(project, []);
243
248
  if (kind === 'listed') {
244
- for (const file of deno.parsed.fileNames)
245
- project.addSourceFileAtPath(file);
249
+ // A listed file or a `types` root the bound refuses (a tracked
250
+ // link out, an outside path) is skipped, read as missing.
251
+ for (const file of deno.parsed.fileNames) {
252
+ if (!bound || bound.allows(file))
253
+ project.addSourceFileAtPath(file);
254
+ }
246
255
  for (const diagnostic of deno.diagnostics)
247
256
  this.logError(diagnostic);
248
257
  }
@@ -365,8 +374,7 @@ export class ProjectLoader {
365
374
  * the compiler's own files (carrick#3509) and, under CARRICK_UNTRUSTED_TREE,
366
375
  * nothing whose real path is outside the checkout (carrick#2230).
367
376
  */
368
- fileSystemOption() {
369
- const bound = untrustedReadBound(this.repoRoot);
377
+ fileSystemOption(bound = untrustedReadBound(this.repoRoot)) {
370
378
  return defaultLibraryOptions(bound ? boundedFileSystem(bound) : undefined);
371
379
  }
372
380
  /**
@@ -74,6 +74,19 @@ export declare class Retyper {
74
74
  * type is stated by the returned plan.
75
75
  */
76
76
  private beforeRewrite;
77
+ /**
78
+ * Why the call does not state the response when the type argument the
79
+ * retype would state there (its first, written or inferred) holds a type
80
+ * parameter of a declaration the call sits in (carrick#3897), or
81
+ * `undefined`. `return send<T>(url)` inside `read<T>(): Promise<T>` is a
82
+ * pass-through: the call site cannot bind `T`, and the response is read
83
+ * where `read` is called. Stating the producer's type there only makes the
84
+ * compiler compare it with the open `T` ("'T' could be instantiated with an
85
+ * arbitrary type"), which no reader of the response wrote. An enclosing
86
+ * parameter in any other type argument (a generated client's mode flag)
87
+ * leaves the response stated, and the call is judged.
88
+ */
89
+ private unboundTypeParameterReason;
77
90
  /**
78
91
  * The rewrite that states the producer's UNWIDENED return (carrick#1516),
79
92
  * when the item carries one that can be stated at this call.
@@ -201,11 +201,34 @@ export class Retyper {
201
201
  // value escapes to are invisible and an "agrees" would claim them.
202
202
  return escape;
203
203
  }
204
+ const unbound = this.unboundTypeParameterReason(call);
205
+ if (unbound)
206
+ return unbound;
204
207
  const plan = this.plan(call);
205
208
  if (typeof plan === 'string')
206
209
  return plan;
207
210
  return { sourceFile, call, plan };
208
211
  }
212
+ /**
213
+ * Why the call does not state the response when the type argument the
214
+ * retype would state there (its first, written or inferred) holds a type
215
+ * parameter of a declaration the call sits in (carrick#3897), or
216
+ * `undefined`. `return send<T>(url)` inside `read<T>(): Promise<T>` is a
217
+ * pass-through: the call site cannot bind `T`, and the response is read
218
+ * where `read` is called. Stating the producer's type there only makes the
219
+ * compiler compare it with the open `T` ("'T' could be instantiated with an
220
+ * arbitrary type"), which no reader of the response wrote. An enclosing
221
+ * parameter in any other type argument (a generated client's mode flag)
222
+ * leaves the response stated, and the call is judged.
223
+ */
224
+ unboundTypeParameterReason(call) {
225
+ const checker = this.project.getTypeChecker().compilerObject;
226
+ const signature = checker.getResolvedSignature(call.compilerNode);
227
+ const stated = signature && checker.getTypeArgumentsForResolvedSignature(signature)?.[0];
228
+ return stated
229
+ ? unboundReason(checker, "the call's type argument", stated, call.compilerNode)
230
+ : undefined;
231
+ }
209
232
  /**
210
233
  * The rewrite that states the producer's UNWIDENED return (carrick#1516),
211
234
  * when the item carries one that can be stated at this call.
@@ -282,6 +305,11 @@ export class Retyper {
282
305
  const reads = bodyReadsOf(call, (read) => this.inferrer.isPlatformBodyRead(read, JSON_READ, 'undeclared_reads'));
283
306
  if (typeof reads === 'string')
284
307
  return reads;
308
+ for (const read of reads) {
309
+ const unbound = bodyReadUnboundReason(read);
310
+ if (unbound)
311
+ return unbound;
312
+ }
285
313
  if (reads.length > 0) {
286
314
  const edits = reads.map(bodyReadEdit);
287
315
  return (producer, wire) => (useWire) => {
@@ -493,6 +521,104 @@ function abstain(item, reason) {
493
521
  function runFailed(outcome) {
494
522
  return { ...outcome, run_failed: true };
495
523
  }
524
+ /**
525
+ * The abstention for a response whose type holds a type parameter of a
526
+ * declaration it sits in (carrick#3897), or `undefined`. The one place the
527
+ * sentence is built, for a call's result and for a body read alike.
528
+ */
529
+ function unboundReason(checker, what, type, at) {
530
+ const name = enclosingTypeParameterIn(checker, type, at);
531
+ if (name === undefined)
532
+ return undefined;
533
+ return (`${what} ('${checker.typeToString(type)}') uses the enclosing declaration's type ` +
534
+ `parameter '${name}', so the call does not state the response and its readers ` +
535
+ `are that declaration's callers`);
536
+ }
537
+ /**
538
+ * {@link unboundReason} for a body read (carrick#3897): the type a cast on
539
+ * it states, or else the type the place it flows into expects
540
+ * (`return res.json()` inside `read<T>(): Promise<T>`). The retype states
541
+ * the producer's type at either, so a type parameter there is the same
542
+ * pass-through as a call's result.
543
+ */
544
+ function bodyReadUnboundReason(read) {
545
+ const checker = read.getProject().getTypeChecker().compilerObject;
546
+ let top = read;
547
+ for (let parent = top.getParent(); parent; parent = top.getParent()) {
548
+ if (Node.isAsExpression(parent)) {
549
+ const cast = parent.getTypeNodeOrThrow();
550
+ return unboundReason(checker, 'the type the body read is cast to', cast.getType().compilerType, read.compilerNode);
551
+ }
552
+ if (!Node.isAwaitExpression(parent) && !Node.isParenthesizedExpression(parent))
553
+ break;
554
+ top = parent;
555
+ }
556
+ const expected = checker.getContextualType(top.compilerNode);
557
+ return expected
558
+ ? unboundReason(checker, 'the type the body read flows into', expected, read.compilerNode)
559
+ : undefined;
560
+ }
561
+ /** How deep {@link enclosingTypeParameterIn} reads into a type. */
562
+ const TYPE_PARAMETER_DEPTH = 4;
563
+ /**
564
+ * The name of a type parameter in `type` that a declaration enclosing `at`
565
+ * declares (a function, a method, a class), or `undefined`. Reads union and
566
+ * intersection members, type arguments, indexed accesses, conditional
567
+ * branches and the properties of an anonymous object type, to a fixed depth.
568
+ * A polymorphic `this` is not one: a call can hold it without passing a
569
+ * response through.
570
+ */
571
+ function enclosingTypeParameterIn(checker, type, at) {
572
+ const seen = new Set();
573
+ const encloses = (declaring) => {
574
+ for (let node = at.parent; node; node = node.parent) {
575
+ if (node === declaring)
576
+ return true;
577
+ }
578
+ return false;
579
+ };
580
+ const visit = (current, depth) => {
581
+ if (depth > TYPE_PARAMETER_DEPTH || seen.has(current))
582
+ return undefined;
583
+ seen.add(current);
584
+ if (current.flags & ts.TypeFlags.TypeParameter) {
585
+ if (current.isThisType)
586
+ return undefined;
587
+ const declared = current.symbol?.declarations?.some((decl) => ts.isTypeParameterDeclaration(decl) && encloses(decl.parent));
588
+ if (declared)
589
+ return current.symbol.getName();
590
+ }
591
+ const parts = [...(current.aliasTypeArguments ?? [])];
592
+ if (current.isUnionOrIntersection())
593
+ parts.push(...current.types);
594
+ if (current.flags & ts.TypeFlags.IndexedAccess) {
595
+ const access = current;
596
+ parts.push(access.objectType, access.indexType);
597
+ }
598
+ if (current.flags & ts.TypeFlags.Conditional) {
599
+ const conditional = current;
600
+ parts.push(conditional.checkType, conditional.extendsType);
601
+ }
602
+ if (current.flags & ts.TypeFlags.Object) {
603
+ const flags = current.objectFlags;
604
+ if (flags & ts.ObjectFlags.Reference) {
605
+ parts.push(...checker.getTypeArguments(current));
606
+ }
607
+ if (flags & ts.ObjectFlags.Anonymous) {
608
+ for (const property of current.getProperties()) {
609
+ parts.push(checker.getTypeOfSymbolAtLocation(property, at));
610
+ }
611
+ }
612
+ }
613
+ for (const part of parts) {
614
+ const found = visit(part, depth + 1);
615
+ if (found !== undefined)
616
+ return found;
617
+ }
618
+ return undefined;
619
+ };
620
+ return visit(type, 0);
621
+ }
496
622
  function asLocator(item) {
497
623
  return {
498
624
  file_path: item.file_path,