carrick 0.3.153 → 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.
- package/bin/carrick.mjs +26 -4
- package/dist/cli.d.ts +6 -0
- package/dist/cli.js +1 -0
- package/dist/cli.js.map +1 -1
- package/dist/contract.d.ts +37 -3
- package/dist/contract.js +8 -0
- package/dist/contract.js.map +1 -1
- package/dist/facts/diff.d.ts +27 -0
- package/dist/facts/diff.js +134 -0
- package/dist/facts/diff.js.map +1 -0
- package/dist/facts/enabled.d.ts +2 -0
- package/dist/facts/enabled.js +9 -0
- package/dist/facts/enabled.js.map +1 -0
- package/dist/facts/fetch.d.ts +48 -0
- package/dist/facts/fetch.js +235 -0
- package/dist/facts/fetch.js.map +1 -0
- package/dist/facts/files.d.ts +21 -0
- package/dist/facts/files.js +58 -0
- package/dist/facts/files.js.map +1 -0
- package/dist/facts/lines.d.ts +70 -0
- package/dist/facts/lines.js +335 -0
- package/dist/facts/lines.js.map +1 -0
- package/dist/facts/match.d.ts +91 -0
- package/dist/facts/match.js +1122 -0
- package/dist/facts/match.js.map +1 -0
- package/dist/facts/peers.d.ts +48 -0
- package/dist/facts/peers.js +143 -0
- package/dist/facts/peers.js.map +1 -0
- package/dist/facts/refresh-run.d.ts +2 -0
- package/dist/facts/refresh-run.js +18 -0
- package/dist/facts/refresh-run.js.map +1 -0
- package/dist/facts/rules/packages.d.ts +16 -0
- package/dist/facts/rules/packages.js +157 -0
- package/dist/facts/rules/packages.js.map +1 -0
- package/dist/facts/rules/same-name.d.ts +28 -0
- package/dist/facts/rules/same-name.js +131 -0
- package/dist/facts/rules/same-name.js.map +1 -0
- package/dist/facts/store.d.ts +119 -0
- package/dist/facts/store.js +341 -0
- package/dist/facts/store.js.map +1 -0
- package/dist/git-remote.d.ts +6 -0
- package/dist/git-remote.js +37 -0
- package/dist/git-remote.js.map +1 -0
- package/dist/hook/apply-patch.d.ts +23 -0
- package/dist/hook/apply-patch.js +92 -0
- package/dist/hook/apply-patch.js.map +1 -1
- package/dist/hook/bash-targets.d.ts +8 -0
- package/dist/hook/bash-targets.js +23 -11
- package/dist/hook/bash-targets.js.map +1 -1
- package/dist/hook/edit-flow.d.ts +29 -2
- package/dist/hook/edit-flow.js +80 -29
- package/dist/hook/edit-flow.js.map +1 -1
- package/dist/hook/late.d.ts +29 -0
- package/dist/hook/late.js +121 -26
- package/dist/hook/late.js.map +1 -1
- package/dist/hook/post-bash.js +45 -22
- package/dist/hook/post-bash.js.map +1 -1
- package/dist/hook/post-edit.d.ts +16 -1
- package/dist/hook/post-edit.js +177 -13
- package/dist/hook/post-edit.js.map +1 -1
- package/dist/hook/post-read.js +8 -12
- package/dist/hook/post-read.js.map +1 -1
- package/dist/hook/post-search.d.ts +2 -0
- package/dist/hook/post-search.js +25 -0
- package/dist/hook/post-search.js.map +1 -0
- package/dist/hook/probe.d.ts +18 -3
- package/dist/hook/probe.js +30 -6
- package/dist/hook/probe.js.map +1 -1
- package/dist/hook/rechecks.d.ts +66 -11
- package/dist/hook/rechecks.js +115 -29
- package/dist/hook/rechecks.js.map +1 -1
- package/dist/hook/refresh.d.ts +9 -0
- package/dist/hook/refresh.js +10 -0
- package/dist/hook/refresh.js.map +1 -1
- package/dist/hook/reuse.d.ts +16 -2
- package/dist/hook/reuse.js +49 -6
- package/dist/hook/reuse.js.map +1 -1
- package/dist/hook/session-key.d.ts +12 -0
- package/dist/hook/session-key.js +30 -0
- package/dist/hook/session-key.js.map +1 -0
- package/dist/hook/session-start.js +14 -7
- package/dist/hook/session-start.js.map +1 -1
- package/dist/hook/stop-wait.d.ts +2 -9
- package/dist/hook/stop-wait.js +96 -113
- package/dist/hook/stop-wait.js.map +1 -1
- package/dist/hook/stop.js +13 -8
- package/dist/hook/stop.js.map +1 -1
- package/dist/hook/subagent-stop.d.ts +2 -0
- package/dist/hook/subagent-stop.js +45 -0
- package/dist/hook/subagent-stop.js.map +1 -0
- package/dist/hook/timings.d.ts +72 -0
- package/dist/hook/timings.js +175 -0
- package/dist/hook/timings.js.map +1 -0
- package/dist/hook/user-prompt.js +17 -13
- package/dist/hook/user-prompt.js.map +1 -1
- package/dist/hook/worktree.d.ts +11 -0
- package/dist/hook/worktree.js +27 -1
- package/dist/hook/worktree.js.map +1 -1
- package/dist/init/repo-copies.d.ts +6 -0
- package/dist/init/repo-copies.js +7 -3
- package/dist/init/repo-copies.js.map +1 -1
- package/dist/init/repos.d.ts +2 -2
- package/dist/init/repos.js +1 -34
- package/dist/init/repos.js.map +1 -1
- package/dist/init/settings.js +25 -13
- package/dist/init/settings.js.map +1 -1
- package/dist/render.d.ts +8 -33
- package/dist/render.js +42 -120
- package/dist/render.js.map +1 -1
- package/package.json +6 -6
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/hooks/hooks.json +44 -3
- package/scripts/install-scanned-deps.sh +25 -2
- package/sidecar/dist/src/capture/anchors.d.ts +12 -1
- package/sidecar/dist/src/capture/anchors.js +28 -47
- package/sidecar/dist/src/capture/api.d.ts +41 -1
- package/sidecar/dist/src/capture/augmentations.d.ts +2 -0
- package/sidecar/dist/src/capture/augmentations.js +1 -1
- package/sidecar/dist/src/capture/check-classify.d.ts +12 -1
- package/sidecar/dist/src/capture/check-classify.js +43 -0
- package/sidecar/dist/src/capture/check-deep.d.ts +44 -1
- package/sidecar/dist/src/capture/check-deep.js +161 -2
- package/sidecar/dist/src/capture/check.js +23 -2
- package/sidecar/dist/src/capture/checkout.d.ts +9 -3
- package/sidecar/dist/src/capture/checkout.js +12 -5
- package/sidecar/dist/src/capture/deep-walk.d.ts +25 -0
- package/sidecar/dist/src/capture/deep-walk.js +41 -0
- package/sidecar/dist/src/capture/deno-project.d.ts +22 -0
- package/sidecar/dist/src/capture/deno-project.js +48 -1
- package/sidecar/dist/src/capture/index.d.ts +3 -1
- package/sidecar/dist/src/capture/index.js +14 -4
- package/sidecar/dist/src/capture/machinery.d.ts +8 -53
- package/sidecar/dist/src/capture/machinery.js +10 -157
- package/sidecar/dist/src/capture/node-builder.d.ts +6 -0
- package/sidecar/dist/src/capture/self-check.d.ts +7 -0
- package/sidecar/dist/src/capture/self-check.js +44 -1
- package/sidecar/dist/src/capture/site-node.d.ts +166 -0
- package/sidecar/dist/src/capture/site-node.js +570 -0
- package/sidecar/dist/src/capture/tool-env.d.ts +12 -0
- package/sidecar/dist/src/capture/tool-env.js +26 -0
- package/sidecar/dist/src/default-library.d.ts +39 -0
- package/sidecar/dist/src/default-library.js +132 -0
- package/sidecar/dist/src/definition-resolver.js +2 -0
- package/sidecar/dist/src/index.js +1 -1
- package/sidecar/dist/src/library-claims.d.ts +2 -0
- package/sidecar/dist/src/library-claims.js +387 -37
- package/sidecar/dist/src/node-index.d.ts +2 -0
- package/sidecar/dist/src/node-index.js +5 -1
- package/sidecar/dist/src/position-rows.d.ts +78 -7
- package/sidecar/dist/src/position-rows.js +343 -18
- package/sidecar/dist/src/project-loader.d.ts +3 -3
- package/sidecar/dist/src/project-loader.js +18 -9
- package/sidecar/dist/src/retype.d.ts +13 -0
- package/sidecar/dist/src/retype.js +126 -0
- package/sidecar/dist/src/type-inferrer.d.ts +47 -145
- package/sidecar/dist/src/type-inferrer.js +102 -529
- package/sidecar/dist/src/types.d.ts +64 -2
- package/sidecar/dist/src/validators.d.ts +806 -142
- package/sidecar/dist/src/validators.js +12 -1
|
@@ -33,10 +33,10 @@ import { callInto, calledFunction, narrowByArguments } from './argument-modes.js
|
|
|
33
33
|
import { functionAtLine } from './function-line-index.js';
|
|
34
34
|
import { attributedDeclaration, installedPackageOf } from './declaration-attribution.js';
|
|
35
35
|
import { endLineOf, startLineOf } from './line-index.js';
|
|
36
|
-
import { callsOf, descendantsOf, descendantsOnLine, descendantsOnLines, wrap, } from './node-index.js';
|
|
36
|
+
import { callsOf, descendantsOf, descendantsOnLine, descendantsOnLines, wrap, wrapCompilerNode, } from './node-index.js';
|
|
37
37
|
import { elapsedMs, inferTiming, phaseClock, timedPhase } from './infer-timing.js';
|
|
38
38
|
import { importedNameOf, packageNameOf } from './library-claims.js';
|
|
39
|
-
import { constituentsOf } from './capture/index.js';
|
|
39
|
+
import { constituentsOf, isFrameworkMachinery, nodeByLocatorText, normalizeLocatorText, symbolDeclaredOutside, typeIsOrContainsMachinery, } from './capture/index.js';
|
|
40
40
|
import { StatedParams } from './stated-params.js';
|
|
41
41
|
import { bindingToleratesAbsence, handlerToleratesAbsentBody } from './absent-body.js';
|
|
42
42
|
import { replaceFileText } from './program-files.js';
|
|
@@ -96,52 +96,6 @@ const BUILTIN_ANCHOR_SYMBOLS = new Set([
|
|
|
96
96
|
'RegExp',
|
|
97
97
|
'Error',
|
|
98
98
|
]);
|
|
99
|
-
/**
|
|
100
|
-
* Strongly-discriminating member names of HTTP transport machinery — the
|
|
101
|
-
* fetch/DOM `Response` & `Request`, a Node `http.ServerResponse`, a framework
|
|
102
|
-
* reply object. A type declared in a lib or installed-dependency origin that carries
|
|
103
|
-
* a subset of these is framework machinery, never a user contract: the
|
|
104
|
-
* PRODUCER-side structural mirror of the consumer `machineryIndicators`
|
|
105
|
-
* (ExtractionConfig), used to reject a wrapper envelope whose response field is
|
|
106
|
-
* a raw `Response` (carrick#371) instead of emitting it as a comparable type.
|
|
107
|
-
*
|
|
108
|
-
* Framework-agnostic by construction: no framework NAME appears here, only the
|
|
109
|
-
* shared HTTP-message surface. The names are deliberately the ones no
|
|
110
|
-
* JSON payload ever carries (`ok`, `redirected`, `bodyUsed`, `arrayBuffer`,
|
|
111
|
-
* `writeHead`, ...), so the origin gate + `MACHINERY_INDICATOR_THRESHOLD` never
|
|
112
|
-
* fire on real data. Kept in lockstep with the capture-seam mirror
|
|
113
|
-
* `capture/machinery.ts` (the seam forbids sharing a module across it, same as
|
|
114
|
-
* `BUILTIN_ANCHOR_SYMBOLS` mirrors `socket_io.rs`). Exported so a drift-guard
|
|
115
|
-
* test (`machinery-indicator-mirror.test.ts`) asserts the two sets stay equal.
|
|
116
|
-
*/
|
|
117
|
-
export const MACHINERY_MEMBER_INDICATORS = new Set([
|
|
118
|
-
// fetch / DOM Response & Request body-consumer surface
|
|
119
|
-
'ok',
|
|
120
|
-
'redirected',
|
|
121
|
-
'bodyUsed',
|
|
122
|
-
'arrayBuffer',
|
|
123
|
-
'blob',
|
|
124
|
-
'formData',
|
|
125
|
-
'clone',
|
|
126
|
-
'json',
|
|
127
|
-
'statusText',
|
|
128
|
-
// FROZEN (old path, 2026-10-21): the reply-object half below only (the
|
|
129
|
-
// fetch half above and the threshold stay). Deletions and safety fixes
|
|
130
|
-
// only. Fixes for wrong or missing types go in labelled reply objects
|
|
131
|
-
// (carrick#2472 C4) and the language library rows (carrick#2445). Delete
|
|
132
|
-
// when a delete-and-replay on the four repos moves no row wrongly, after
|
|
133
|
-
// carrick-cloud#2379 and carrick#2445 (carrick#2472, C6).
|
|
134
|
-
// Node http ServerResponse / reply-object surface
|
|
135
|
-
'statusCode',
|
|
136
|
-
'statusMessage',
|
|
137
|
-
'setHeader',
|
|
138
|
-
'getHeader',
|
|
139
|
-
'removeHeader',
|
|
140
|
-
'writeHead',
|
|
141
|
-
'flushHeaders',
|
|
142
|
-
]);
|
|
143
|
-
/** Machinery needs at least this many indicator members to be recognized. */
|
|
144
|
-
const MACHINERY_INDICATOR_THRESHOLD = 3;
|
|
145
99
|
/**
|
|
146
100
|
* True when a binary expression COMPARES rather than assigns or computes: its
|
|
147
101
|
* value is a `boolean` the operator manufactured, never a payload
|
|
@@ -201,6 +155,8 @@ const WRAPPER_CALL_MAX_DEPTH = 3;
|
|
|
201
155
|
* the branch an error path, whose shape is not the endpoint's contract.
|
|
202
156
|
*/
|
|
203
157
|
const STATUS_MEMBER_NAMES = ['status', 'statusCode'];
|
|
158
|
+
/** An error or redirect send carries no body the route's contract describes. */
|
|
159
|
+
const isDroppedResponseStatus = (status) => status === 'error' || status === 'redirect';
|
|
204
160
|
/**
|
|
205
161
|
* The reasons an `unknown` answer is a decision, not a failure to read: the
|
|
206
162
|
* list `DECIDED_ABSTAIN_REASONS` in `src/engine/type_compat_v2.rs` holds
|
|
@@ -312,83 +268,6 @@ function typeText(type, enclosingNode) {
|
|
|
312
268
|
* `text` is `string`, alone or beside `null` and `undefined` in either order:
|
|
313
269
|
* the only payload a raw-text read publishes (carrick#1842).
|
|
314
270
|
*/
|
|
315
|
-
/**
|
|
316
|
-
* Source text with every plain string literal written in double quotes:
|
|
317
|
-
* `'/a'`, `"/a"` and an interpolation-free `` `/a` `` are one value, so text
|
|
318
|
-
* that differs only in its quotes compares equal (carrick#2056).
|
|
319
|
-
*
|
|
320
|
-
* The text is read left to right, pairing quotes the way the scanner does,
|
|
321
|
-
* so a template's closing backtick never pairs with the next template's
|
|
322
|
-
* opening one. A literal holding a quote, a backslash or an interpolation is
|
|
323
|
-
* copied as it is, and so is an unterminated one with everything after it.
|
|
324
|
-
*/
|
|
325
|
-
export function canonicalStringQuotes(text) {
|
|
326
|
-
const isQuote = (ch) => ch === "'" || ch === '"' || ch === '`';
|
|
327
|
-
// The index just past the literal opening at `start`, or -1.
|
|
328
|
-
const literalEnd = (start) => {
|
|
329
|
-
const quote = text[start];
|
|
330
|
-
let j = start + 1;
|
|
331
|
-
while (j < text.length) {
|
|
332
|
-
const ch = text[j];
|
|
333
|
-
if (ch === '\\') {
|
|
334
|
-
j += 2;
|
|
335
|
-
}
|
|
336
|
-
else if (ch === quote) {
|
|
337
|
-
return j + 1;
|
|
338
|
-
}
|
|
339
|
-
else if (quote === '`' && ch === '$' && text[j + 1] === '{') {
|
|
340
|
-
const close = codeEnd(j + 2);
|
|
341
|
-
if (close < 0)
|
|
342
|
-
return -1;
|
|
343
|
-
j = close + 1;
|
|
344
|
-
}
|
|
345
|
-
else {
|
|
346
|
-
j++;
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
return -1;
|
|
350
|
-
};
|
|
351
|
-
// The index of the `}` that closes an interpolation whose code starts at `start`, or -1.
|
|
352
|
-
const codeEnd = (start) => {
|
|
353
|
-
let depth = 0;
|
|
354
|
-
let j = start;
|
|
355
|
-
while (j < text.length) {
|
|
356
|
-
const ch = text[j];
|
|
357
|
-
if (isQuote(ch)) {
|
|
358
|
-
const end = literalEnd(j);
|
|
359
|
-
if (end < 0)
|
|
360
|
-
return -1;
|
|
361
|
-
j = end;
|
|
362
|
-
continue;
|
|
363
|
-
}
|
|
364
|
-
if (ch === '{')
|
|
365
|
-
depth++;
|
|
366
|
-
if (ch === '}') {
|
|
367
|
-
if (depth === 0)
|
|
368
|
-
return j;
|
|
369
|
-
depth--;
|
|
370
|
-
}
|
|
371
|
-
j++;
|
|
372
|
-
}
|
|
373
|
-
return -1;
|
|
374
|
-
};
|
|
375
|
-
let out = '';
|
|
376
|
-
let i = 0;
|
|
377
|
-
while (i < text.length) {
|
|
378
|
-
if (!isQuote(text[i])) {
|
|
379
|
-
out += text[i];
|
|
380
|
-
i++;
|
|
381
|
-
continue;
|
|
382
|
-
}
|
|
383
|
-
const end = literalEnd(i);
|
|
384
|
-
if (end < 0)
|
|
385
|
-
return out + text.slice(i);
|
|
386
|
-
const body = text.slice(i + 1, end - 1);
|
|
387
|
-
out += /['"`\\]|\$\{/.test(body) ? text.slice(i, end) : `"${body}"`;
|
|
388
|
-
i = end;
|
|
389
|
-
}
|
|
390
|
-
return out;
|
|
391
|
-
}
|
|
392
271
|
function isBareStringText(text) {
|
|
393
272
|
const members = text.split('|').map((member) => member.trim());
|
|
394
273
|
return (members.includes('string') &&
|
|
@@ -1501,11 +1380,11 @@ export class TypeInferrer {
|
|
|
1501
1380
|
return { param: byName, node: byName };
|
|
1502
1381
|
}
|
|
1503
1382
|
// 2. Whole binding pattern, whitespace-normalized.
|
|
1504
|
-
const normalizedTarget =
|
|
1383
|
+
const normalizedTarget = normalizeLocatorText(paramName);
|
|
1505
1384
|
for (const param of params) {
|
|
1506
1385
|
const nameNode = param.getNameNode();
|
|
1507
1386
|
if ((Node.isObjectBindingPattern(nameNode) || Node.isArrayBindingPattern(nameNode)) &&
|
|
1508
|
-
|
|
1387
|
+
normalizeLocatorText(nameNode.getText()) === normalizedTarget) {
|
|
1509
1388
|
return { param, node: param };
|
|
1510
1389
|
}
|
|
1511
1390
|
}
|
|
@@ -1538,7 +1417,7 @@ export class TypeInferrer {
|
|
|
1538
1417
|
return declared;
|
|
1539
1418
|
}
|
|
1540
1419
|
this.log(`Line ${request.line_number} is a route registration; following handler return`);
|
|
1541
|
-
return this.buildFunctionReturnInferredType(request, atLine.handler, extractionConfig, true);
|
|
1420
|
+
return (this.buildFunctionReturnInferredType(request, atLine.handler, extractionConfig, true) ?? this.labelledSendBodies(request, atLine.handler));
|
|
1542
1421
|
}
|
|
1543
1422
|
// carrick#807: the request named an expression and the file holds none
|
|
1544
1423
|
// that matches. Where its line declares the handler, that handler is
|
|
@@ -2068,13 +1947,8 @@ export class TypeInferrer {
|
|
|
2068
1947
|
// carrick#2472 C2: the service's labels answer first. A labelled send is
|
|
2069
1948
|
// a send, an unlabelled member of a labelled reply object is a setter;
|
|
2070
1949
|
// where no label speaks, the structural reading decides.
|
|
2071
|
-
const labelledSend = (candidate) => this.statedParams.sendAt(this.peelTransparentExpression(candidate));
|
|
2072
|
-
const isLabelledSend = (candidate) => {
|
|
2073
|
-
const labelled = labelledSend(candidate);
|
|
2074
|
-
return labelled !== undefined && labelled !== 'setter';
|
|
2075
|
-
};
|
|
2076
1950
|
const isSend = (candidate) => {
|
|
2077
|
-
const labelled =
|
|
1951
|
+
const labelled = this.statedParams.sendAt(this.peelTransparentExpression(candidate));
|
|
2078
1952
|
if (labelled !== undefined)
|
|
2079
1953
|
return labelled !== 'setter';
|
|
2080
1954
|
return this.isResponseSend(candidate, extractionConfig);
|
|
@@ -2099,7 +1973,7 @@ export class TypeInferrer {
|
|
|
2099
1973
|
const locatedBranch = locatingBranches.find((branch) => contains(branch, site));
|
|
2100
1974
|
if (!locatedBranch)
|
|
2101
1975
|
return undefined;
|
|
2102
|
-
const dropped =
|
|
1976
|
+
const dropped = isDroppedResponseStatus;
|
|
2103
1977
|
const siteStatus = this.responseSiteStatus(site, true);
|
|
2104
1978
|
if (!tied && !dropped(siteStatus))
|
|
2105
1979
|
return undefined;
|
|
@@ -2112,7 +1986,7 @@ export class TypeInferrer {
|
|
|
2112
1986
|
// FROZEN (old path, 2026-10-21): the `statementEndsFunctionPath` test
|
|
2113
1987
|
// here; see that method.
|
|
2114
1988
|
.filter((s) => s.send === locatedBranch ||
|
|
2115
|
-
isLabelledSend(s.send) ||
|
|
1989
|
+
this.isLabelledSend(s.send) ||
|
|
2116
1990
|
this.statementEndsFunctionPath(s.statement, func))
|
|
2117
1991
|
.map((s) => s.send),
|
|
2118
1992
|
];
|
|
@@ -2264,6 +2138,35 @@ export class TypeInferrer {
|
|
|
2264
2138
|
inferred.response_modes = { reads: sorted };
|
|
2265
2139
|
return inferred;
|
|
2266
2140
|
}
|
|
2141
|
+
/** A call the service's labels name a body send; a labelled setter is not one (carrick#2472 C2). */
|
|
2142
|
+
isLabelledSend(candidate) {
|
|
2143
|
+
const labelled = this.statedParams.sendAt(this.peelTransparentExpression(candidate));
|
|
2144
|
+
return labelled !== undefined && labelled !== 'setter';
|
|
2145
|
+
}
|
|
2146
|
+
/**
|
|
2147
|
+
* carrick#4081: a line-only row at a registration whose handler returns
|
|
2148
|
+
* nothing. Its body is what the handler sends: every labelled send it makes
|
|
2149
|
+
* as a statement, error and redirect sends dropped, the success bodies
|
|
2150
|
+
* joined, as `inferFromResponseSites` reads them from a located send. Only
|
|
2151
|
+
* labelled sends count; an unlabelled one leaves the row unread. A reply
|
|
2152
|
+
* parameter that states its payload answers first (`withStatedReply`), so
|
|
2153
|
+
* this yields to it.
|
|
2154
|
+
*/
|
|
2155
|
+
labelledSendBodies(request, handler) {
|
|
2156
|
+
if (!this.statedParams.readsReplies || this.statedParams.reply(handler))
|
|
2157
|
+
return null;
|
|
2158
|
+
const survivors = this.responseSendStatements(handler, (candidate) => this.isLabelledSend(candidate))
|
|
2159
|
+
.map((s) => s.send)
|
|
2160
|
+
.filter((send) => !isDroppedResponseStatus(this.responseSiteStatus(send, true)));
|
|
2161
|
+
if (survivors.length === 0)
|
|
2162
|
+
return null;
|
|
2163
|
+
const recovered = this.recoverPayloadFromResponseExpressions(survivors, false, this.wireFormatFor(request));
|
|
2164
|
+
if (!recovered)
|
|
2165
|
+
return null;
|
|
2166
|
+
this.log(`Route at ${request.file_path}:${request.line_number} returns nothing; ` +
|
|
2167
|
+
'publishing the success bodies its handler sends through labelled sends');
|
|
2168
|
+
return this.inferredFromRecoveredPayload(request, recovered);
|
|
2169
|
+
}
|
|
2267
2170
|
/**
|
|
2268
2171
|
* The response sends `func` makes as statements (`res.status(409).json(e);`),
|
|
2269
2172
|
* for a handler that sends through a parameter and returns nothing. Only the
|
|
@@ -2664,6 +2567,10 @@ export class TypeInferrer {
|
|
|
2664
2567
|
// machinery. A wrapper rule that DID unwrap the envelope, or a type the
|
|
2665
2568
|
// source itself states, is a contract and keeps its answer.
|
|
2666
2569
|
//
|
|
2570
|
+
// carrick#3741: a one-argument result envelope read only for its own
|
|
2571
|
+
// members (`res.success`, `res.error`) is decided the same way. The site
|
|
2572
|
+
// never reads the payload, and the envelope is not the wire body.
|
|
2573
|
+
//
|
|
2667
2574
|
// A single explicit call generic (`client.get<Order[]>(url)`) is the
|
|
2668
2575
|
// caller's own payload claim and states the contract as plainly as an
|
|
2669
2576
|
// `as` does — it is what the anchor below falls back to on a checkout
|
|
@@ -2675,7 +2582,8 @@ export class TypeInferrer {
|
|
|
2675
2582
|
// engine/type_compat_v2.rs). No anchor and no depth go with it, so the
|
|
2676
2583
|
// row also reads as blind rather than as a sighted answer.
|
|
2677
2584
|
if (use.projectionOnly &&
|
|
2678
|
-
this.projectionReadsGenericPayload(callExpr, use.projections)
|
|
2585
|
+
(this.projectionReadsGenericPayload(callExpr, use.projections) ||
|
|
2586
|
+
this.isOneArgumentEnvelope(callExpr)) &&
|
|
2679
2587
|
!unwrapResult.wasUnwrapped &&
|
|
2680
2588
|
!explicitType &&
|
|
2681
2589
|
!carrierPayload &&
|
|
@@ -3845,6 +3753,26 @@ export class TypeInferrer {
|
|
|
3845
3753
|
return parts.some((part) => argTexts.has(part.getText()));
|
|
3846
3754
|
});
|
|
3847
3755
|
}
|
|
3756
|
+
/**
|
|
3757
|
+
* True when the call answers a ONE-ARGUMENT result envelope: a union whose
|
|
3758
|
+
* branches say whether the call worked and one of which holds the single
|
|
3759
|
+
* type argument as a member (`{ success: true; data: T } | { success: false;
|
|
3760
|
+
* error: string }`), carrick#3741. carrick#1376 needs two arguments to tell
|
|
3761
|
+
* the success side from the failure side; with one there is nothing to tell
|
|
3762
|
+
* apart, and the envelope is the client's bookkeeping around `T`.
|
|
3763
|
+
*
|
|
3764
|
+
* A site that reads only such an envelope's own members (its status, its
|
|
3765
|
+
* error) reads no payload, so the envelope is not a response contract. A
|
|
3766
|
+
* union with no type argument (`{ ok: true } | { ok: false; reason: string }`)
|
|
3767
|
+
* is not an envelope here and keeps its declared answer (carrick#1375).
|
|
3768
|
+
*/
|
|
3769
|
+
isOneArgumentEnvelope(callExpr) {
|
|
3770
|
+
const shape = this.resultCarrierArguments(callExpr.getType(), callExpr, [], 1);
|
|
3771
|
+
if (!shape || shape.readOffCall)
|
|
3772
|
+
return false;
|
|
3773
|
+
const own = [...shape.carrier.getAliasTypeArguments(), ...shape.carrier.getTypeArguments()];
|
|
3774
|
+
return own.length === 1 && shape.carried.length === 1;
|
|
3775
|
+
}
|
|
3848
3776
|
/**
|
|
3849
3777
|
* The payload a RESULT CARRIER carries, or `undefined` when `type` is not
|
|
3850
3778
|
* one or its success side cannot be told from its failure side
|
|
@@ -4089,13 +4017,13 @@ export class TypeInferrer {
|
|
|
4089
4017
|
* promise-like around it is peeled, and the type arguments a branch of it
|
|
4090
4018
|
* holds as a member. `undefined` when `type` is not a carrier.
|
|
4091
4019
|
*/
|
|
4092
|
-
resultCarrierArguments(type, at, callArgs = []) {
|
|
4020
|
+
resultCarrierArguments(type, at, callArgs = [], minArguments = 2) {
|
|
4093
4021
|
// carrick#1841: a conditional deferred on a type parameter is a carrier
|
|
4094
4022
|
// where one of its branches is.
|
|
4095
4023
|
const deferredBranches = this.deferredConditionalBranches(type);
|
|
4096
4024
|
if (deferredBranches) {
|
|
4097
4025
|
for (const branch of deferredBranches) {
|
|
4098
|
-
const shape = this.resultCarrierArguments(branch, at, callArgs);
|
|
4026
|
+
const shape = this.resultCarrierArguments(branch, at, callArgs, minArguments);
|
|
4099
4027
|
if (shape)
|
|
4100
4028
|
return shape;
|
|
4101
4029
|
}
|
|
@@ -4114,7 +4042,7 @@ export class TypeInferrer {
|
|
|
4114
4042
|
// arguments its call was built from (`callResultArguments`).
|
|
4115
4043
|
const readOffCall = own.length === 0;
|
|
4116
4044
|
const args = readOffCall ? callArgs : own;
|
|
4117
|
-
if (args.length <
|
|
4045
|
+
if (args.length < minArguments) {
|
|
4118
4046
|
return undefined;
|
|
4119
4047
|
}
|
|
4120
4048
|
// carrick#1841: on a carrier read off the call, a member every branch
|
|
@@ -5330,84 +5258,21 @@ export class TypeInferrer {
|
|
|
5330
5258
|
* envelope `{ response: Response; error?: undefined } | { ...; error: Error }`
|
|
5331
5259
|
* was captured as the response contract. Machinery is never a comparable
|
|
5332
5260
|
* contract, so a response path that resolves here abstains (honest `unknown`)
|
|
5333
|
-
* rather than emit a concrete-but-false type.
|
|
5334
|
-
*
|
|
5335
|
-
*
|
|
5336
|
-
* 1. the type itself is machinery (`typeIsFrameworkMachinery`);
|
|
5337
|
-
* 2. a union/intersection member is machinery (the envelope union);
|
|
5338
|
-
* 3. a DIRECT property's type is machinery, ONE level of descent only
|
|
5339
|
-
* (`{ response: Response; error }`).
|
|
5340
|
-
*
|
|
5341
|
-
* DELIBERATELY NOT DETECTED — listed so this comment never overstates the
|
|
5342
|
-
* guarantee (an overstated safety comment is what bit sibling PR #442). Each is
|
|
5343
|
-
* a non-regression: it leaves the pre-existing verdict unchanged and never
|
|
5344
|
-
* manufactures a new wrong one, tracked as a follow-up:
|
|
5345
|
-
* - machinery nested deeper than one property level (a property whose type
|
|
5346
|
-
* is itself a nested object wrapping the machinery);
|
|
5347
|
-
* - a property typed `Promise<Response>` (the property type is NOT awaited /
|
|
5348
|
-
* Promise-unwrapped before the machinery check);
|
|
5349
|
-
* - an array element type: `Response[]` is not descended to its element;
|
|
5350
|
-
* - `interface X extends Response` declared in USER source — the origin gate
|
|
5351
|
-
* is lib/installed-dependency only, so a user-declared subtype reads as a real
|
|
5352
|
-
* contract, not machinery;
|
|
5353
|
-
* - a function / call-signature return type: the response paths that call
|
|
5354
|
-
* this resolve a handler's RETURN (an envelope/object), never a function
|
|
5355
|
-
* value, so no call-signature descent happens here. The capture-seam
|
|
5356
|
-
* mirror `capture/machinery.ts` DOES descend call signatures — that is
|
|
5357
|
-
* where the wrapper-FUNCTION type the Infer fallback resolves is caught.
|
|
5358
|
-
*
|
|
5359
|
-
* The origin gate in `typeIsFrameworkMachinery` keeps a user object whose
|
|
5360
|
-
* fields merely share a member name from tripping.
|
|
5261
|
+
* rather than emit a concrete-but-false type. The capture asks the same
|
|
5262
|
+
* question of the same function (`capture/site-node.ts`, carrick#3500), which
|
|
5263
|
+
* states what it detects and what it does not.
|
|
5361
5264
|
*/
|
|
5362
5265
|
typeIsOrContainsResponseMachinery(type) {
|
|
5363
|
-
return this.
|
|
5364
|
-
}
|
|
5365
|
-
typeIsOrContainsMachinery(type, depth) {
|
|
5366
|
-
if (this.typeIsFrameworkMachinery(type)) {
|
|
5367
|
-
return true;
|
|
5368
|
-
}
|
|
5369
|
-
// Union/intersection: any envelope member wrapping machinery taints it.
|
|
5370
|
-
if (type.isUnion()) {
|
|
5371
|
-
return type
|
|
5372
|
-
.getUnionTypes()
|
|
5373
|
-
.some((part) => this.typeIsOrContainsMachinery(part, depth));
|
|
5374
|
-
}
|
|
5375
|
-
if (type.isIntersection()) {
|
|
5376
|
-
return type
|
|
5377
|
-
.getIntersectionTypes()
|
|
5378
|
-
.some((part) => this.typeIsOrContainsMachinery(part, depth));
|
|
5379
|
-
}
|
|
5380
|
-
// One level of property descent: `{ response: Response; error?: undefined }`.
|
|
5381
|
-
if (depth < 1) {
|
|
5382
|
-
for (const prop of type.getProperties()) {
|
|
5383
|
-
const decl = prop.getDeclarations()[0];
|
|
5384
|
-
if (!decl)
|
|
5385
|
-
continue;
|
|
5386
|
-
const propType = prop.getTypeAtLocation(decl);
|
|
5387
|
-
if (this.typeIsOrContainsMachinery(propType, depth + 1)) {
|
|
5388
|
-
return true;
|
|
5389
|
-
}
|
|
5390
|
-
}
|
|
5391
|
-
}
|
|
5392
|
-
return false;
|
|
5266
|
+
return typeIsOrContainsMachinery(this.checker(), type.compilerType, this.declaredOutside);
|
|
5393
5267
|
}
|
|
5394
|
-
/**
|
|
5395
|
-
* True when `type` itself is an HTTP-machinery type: it structurally carries
|
|
5396
|
-
* at least `MACHINERY_INDICATOR_THRESHOLD` of the strongly-discriminating
|
|
5397
|
-
* `MACHINERY_MEMBER_INDICATORS`, AND its symbol is declared in a lib
|
|
5398
|
-
* (`lib.dom.d.ts`, ...) or installed-dependency origin. Both gates are required —
|
|
5399
|
-
* the indicator subset alone essentially never matches a JSON payload, and
|
|
5400
|
-
* the origin gate makes certain a user's own local type sharing those member
|
|
5401
|
-
* names is never mistaken for framework machinery (the advisor's guard).
|
|
5402
|
-
*/
|
|
5403
5268
|
/**
|
|
5404
5269
|
* Blueprint row N8: true when `type` is framework machinery by where it is
|
|
5405
5270
|
* declared, never by its name. Either it passes the structural test
|
|
5406
|
-
* (`
|
|
5407
|
-
* library or an installed package), or an installed
|
|
5408
|
-
* labels a server framework declares it
|
|
5409
|
-
* covers app and router instances that
|
|
5410
|
-
* service's own `Context` or `Router` is neither.
|
|
5271
|
+
* (`isFrameworkMachinery` in `capture/site-node.ts`: the transport's
|
|
5272
|
+
* members, declared by a library or an installed package), or an installed
|
|
5273
|
+
* package the store labels a server framework declares it
|
|
5274
|
+
* (`frameworkPackages`), which also covers app and router instances that
|
|
5275
|
+
* carry no reply members. A service's own `Context` or `Router` is neither.
|
|
5411
5276
|
*/
|
|
5412
5277
|
isFrameworkMachinery(type) {
|
|
5413
5278
|
if (this.typeIsFrameworkMachinery(type)) {
|
|
@@ -5420,65 +5285,28 @@ export class TypeInferrer {
|
|
|
5420
5285
|
return declaring !== undefined && this.frameworkPackages.has(importedNameOf(declaring));
|
|
5421
5286
|
}
|
|
5422
5287
|
typeIsFrameworkMachinery(type) {
|
|
5423
|
-
|
|
5424
|
-
return false;
|
|
5425
|
-
}
|
|
5426
|
-
const symbol = type.getSymbol() ?? type.getAliasSymbol();
|
|
5427
|
-
return this.symbolIsLibOrExternalOrigin(symbol);
|
|
5288
|
+
return isFrameworkMachinery(this.checker(), type.compilerType, this.declaredOutside);
|
|
5428
5289
|
}
|
|
5429
|
-
|
|
5430
|
-
|
|
5431
|
-
* `MACHINERY_MEMBER_INDICATORS` (own + apparent). Deduplicates by name (own and
|
|
5432
|
-
* apparent property lists overlap) and early-returns the moment the threshold
|
|
5433
|
-
* is reached — the callers only need the boolean, never the full count.
|
|
5434
|
-
*/
|
|
5435
|
-
hasMachineryIndicatorThreshold(type) {
|
|
5436
|
-
const matched = new Set();
|
|
5437
|
-
const consider = (name) => {
|
|
5438
|
-
if (MACHINERY_MEMBER_INDICATORS.has(name)) {
|
|
5439
|
-
matched.add(name);
|
|
5440
|
-
}
|
|
5441
|
-
return matched.size >= MACHINERY_INDICATOR_THRESHOLD;
|
|
5442
|
-
};
|
|
5443
|
-
for (const prop of type.getProperties()) {
|
|
5444
|
-
if (consider(prop.getName()))
|
|
5445
|
-
return true;
|
|
5446
|
-
}
|
|
5447
|
-
for (const prop of type.getApparentProperties()) {
|
|
5448
|
-
if (consider(prop.getName()))
|
|
5449
|
-
return true;
|
|
5450
|
-
}
|
|
5451
|
-
return false;
|
|
5290
|
+
checker() {
|
|
5291
|
+
return this.project.getTypeChecker().compilerObject;
|
|
5452
5292
|
}
|
|
5453
5293
|
/**
|
|
5454
|
-
*
|
|
5294
|
+
* Whether a declaration's file is a runtime/library origin
|
|
5455
5295
|
* (`isExternalOrigin`): a TypeScript lib, an installed dependency however
|
|
5456
5296
|
* the program resolved it, or the runtime declarations Carrick materialises
|
|
5457
5297
|
* under `.carrick/deno/` — framework/runtime machinery, not user source.
|
|
5298
|
+
* The program is read per call: the project adds source files lazily and
|
|
5299
|
+
* rebuilds it.
|
|
5300
|
+
*/
|
|
5301
|
+
declaredOutside = (sourceFile) => isExternalOrigin(this.project.getProgram().compilerObject, sourceFile, this.repoRoot, externalImportsOf(this.project));
|
|
5302
|
+
/**
|
|
5303
|
+
* True when the symbol is declared in a runtime/library origin
|
|
5304
|
+
* (`declaredOutside`), itself or through the import alias it stands for.
|
|
5458
5305
|
* Works on a bare checkout: the DOM `Response`/`Request` resolve from the
|
|
5459
|
-
* bundled `lib.dom.d.ts` even with no installed dependencies.
|
|
5460
|
-
* read per call: the project adds source files lazily and rebuilds it.
|
|
5306
|
+
* bundled `lib.dom.d.ts` even with no installed dependencies.
|
|
5461
5307
|
*/
|
|
5462
5308
|
symbolIsLibOrExternalOrigin(symbol) {
|
|
5463
|
-
|
|
5464
|
-
return false;
|
|
5465
|
-
}
|
|
5466
|
-
const program = this.project.getProgram().compilerObject;
|
|
5467
|
-
for (const decl of symbol.getDeclarations()) {
|
|
5468
|
-
if (isExternalOrigin(program, decl.getSourceFile().compilerNode, this.repoRoot, externalImportsOf(this.project))) {
|
|
5469
|
-
return true;
|
|
5470
|
-
}
|
|
5471
|
-
}
|
|
5472
|
-
try {
|
|
5473
|
-
const aliased = symbol.getAliasedSymbol?.();
|
|
5474
|
-
if (aliased && aliased !== symbol) {
|
|
5475
|
-
return this.symbolIsLibOrExternalOrigin(aliased);
|
|
5476
|
-
}
|
|
5477
|
-
}
|
|
5478
|
-
catch {
|
|
5479
|
-
// Ignore errors when resolving the aliased symbol.
|
|
5480
|
-
}
|
|
5481
|
-
return false;
|
|
5309
|
+
return symbolDeclaredOutside(this.checker(), symbol?.compilerSymbol, this.declaredOutside);
|
|
5482
5310
|
}
|
|
5483
5311
|
/**
|
|
5484
5312
|
* Check if a type has machinery indicator properties/methods.
|
|
@@ -5656,8 +5484,12 @@ export class TypeInferrer {
|
|
|
5656
5484
|
}
|
|
5657
5485
|
/** The annotation `extractExplicitTypeFromAncestor` prints, as a node. */
|
|
5658
5486
|
explicitTypeNodeFromAncestor(node) {
|
|
5487
|
+
// The walk stops at the first function boundary: past it, the annotation
|
|
5488
|
+
// describes the object or value the function sits in, not this read.
|
|
5659
5489
|
const varDecl = node.getFirstAncestorByKind(SyntaxKind.VariableDeclaration);
|
|
5660
|
-
|
|
5490
|
+
const boundary = this.findContainingFunctionForNode(node);
|
|
5491
|
+
const crossesBoundary = !!varDecl && !!boundary && boundary.getStart() > varDecl.getStart();
|
|
5492
|
+
if (varDecl && !crossesBoundary) {
|
|
5661
5493
|
const typeNode = varDecl.getTypeNode();
|
|
5662
5494
|
if (typeNode) {
|
|
5663
5495
|
return typeNode;
|
|
@@ -9871,24 +9703,16 @@ export class TypeInferrer {
|
|
|
9871
9703
|
}
|
|
9872
9704
|
}
|
|
9873
9705
|
/**
|
|
9874
|
-
* Find a node by matching expression text near a target line
|
|
9875
|
-
*
|
|
9876
|
-
*
|
|
9877
|
-
* 1. Get all descendant nodes within [lineNumber - searchRadius, lineNumber + searchRadius]
|
|
9878
|
-
* 2. Normalize whitespace for comparison
|
|
9879
|
-
* 3. Try exact match first (after normalization), prefer closest to target line
|
|
9880
|
-
* 4. Fall back to substring match (LLM text in node text, or vice versa)
|
|
9881
|
-
* 5. Return smallest matching node closest to target line
|
|
9706
|
+
* Find a node by matching expression text near a target line: the shared
|
|
9707
|
+
* locator (`nodeByLocatorText`, carrick#3500) over every node that starts
|
|
9708
|
+
* within `searchRadius` lines of it, the one the capture runs too.
|
|
9882
9709
|
*/
|
|
9883
9710
|
findNodeByText(sourceFile, expressionText, lineNumber, searchRadius = 5) {
|
|
9884
|
-
// Only the window's nodes are wrapped: `matchByText` keeps no other.
|
|
9885
9711
|
const inWindow = lineNumber
|
|
9886
9712
|
? descendantsOnLines(sourceFile, lineNumber - searchRadius, lineNumber + searchRadius)
|
|
9887
9713
|
: descendantsOf(sourceFile);
|
|
9888
|
-
const
|
|
9889
|
-
|
|
9890
|
-
.map((entry) => wrap(sourceFile, entry));
|
|
9891
|
-
return this.matchByText(nodes, expressionText, lineNumber, searchRadius);
|
|
9714
|
+
const found = nodeByLocatorText(sourceFile.compilerNode, inWindow.filter((entry) => entry.kind !== SyntaxKind.SyntaxList).map((entry) => entry.node), expressionText, lineNumber, searchRadius);
|
|
9715
|
+
return found === undefined ? undefined : wrapCompilerNode(sourceFile, found);
|
|
9892
9716
|
}
|
|
9893
9717
|
/**
|
|
9894
9718
|
* Find a CallExpression by matching expression text near a target line.
|
|
@@ -9897,259 +9721,8 @@ export class TypeInferrer {
|
|
|
9897
9721
|
const inWindow = lineNumber
|
|
9898
9722
|
? callsOf(sourceFile).filter((entry) => entry.line >= lineNumber - searchRadius && entry.line <= lineNumber + searchRadius)
|
|
9899
9723
|
: callsOf(sourceFile);
|
|
9900
|
-
const
|
|
9901
|
-
return
|
|
9902
|
-
}
|
|
9903
|
-
/**
|
|
9904
|
-
* Shared text-matching logic for node resolution.
|
|
9905
|
-
* Normalizes whitespace once per candidate, then tries exact match,
|
|
9906
|
-
* then substring match (preferring containing matches).
|
|
9907
|
-
*/
|
|
9908
|
-
matchByText(nodes, expressionText, lineNumber, searchRadius = 5) {
|
|
9909
|
-
const normalizedTarget = this.normalizeWhitespace(expressionText);
|
|
9910
|
-
if (!normalizedTarget)
|
|
9911
|
-
return undefined;
|
|
9912
|
-
// Filter to nodes within the search window and pre-compute normalized text
|
|
9913
|
-
const candidates = (lineNumber
|
|
9914
|
-
? nodes.filter((node) => {
|
|
9915
|
-
const nodeLine = startLineOf(node);
|
|
9916
|
-
return nodeLine >= lineNumber - searchRadius && nodeLine <= lineNumber + searchRadius;
|
|
9917
|
-
})
|
|
9918
|
-
: nodes).map((node) => ({ node, text: this.normalizeWhitespace(node.getText()) }));
|
|
9919
|
-
if (candidates.length === 0)
|
|
9920
|
-
return undefined;
|
|
9921
|
-
// Try exact match (normalized whitespace)
|
|
9922
|
-
const exactMatches = candidates.filter((c) => c.text === normalizedTarget);
|
|
9923
|
-
if (exactMatches.length > 0) {
|
|
9924
|
-
return this.pickBestMatch(exactMatches.map((c) => c.node), lineNumber);
|
|
9925
|
-
}
|
|
9926
|
-
// A bare identifier target must match a node exactly: substring matching
|
|
9927
|
-
// would bind `users` to `usersCsv` (or to any enclosing node that merely
|
|
9928
|
-
// contains the identifier somewhere) and report a confidently wrong type.
|
|
9929
|
-
// Failing here is correct — the caller records an error and the alias
|
|
9930
|
-
// pads to `unknown` downstream.
|
|
9931
|
-
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(normalizedTarget)) {
|
|
9932
|
-
return undefined;
|
|
9933
|
-
}
|
|
9934
|
-
// A locator that elides members (`body: { a: x, ... }`) stands for the
|
|
9935
|
-
// whole node it opens: the text before the elision is where the source
|
|
9936
|
-
// opens. Never a node the text merely contains, which is one member of the
|
|
9937
|
-
// payload it names.
|
|
9938
|
-
// An opening shorter than this (`{`) names no node in particular.
|
|
9939
|
-
const MIN_ELIDED_OPENING_LEN = 8;
|
|
9940
|
-
const elidedOpening = this.elidedLocatorOpening(normalizedTarget);
|
|
9941
|
-
if (elidedOpening !== undefined) {
|
|
9942
|
-
if (elidedOpening.length < MIN_ELIDED_OPENING_LEN)
|
|
9943
|
-
return undefined;
|
|
9944
|
-
const opened = candidates.filter((c) => c.text.startsWith(elidedOpening));
|
|
9945
|
-
if (opened.length === 0)
|
|
9946
|
-
return undefined;
|
|
9947
|
-
if (lineNumber !== undefined) {
|
|
9948
|
-
const distance = (c) => Math.abs(startLineOf(c.node) - lineNumber);
|
|
9949
|
-
const nearest = Math.min(...opened.map(distance));
|
|
9950
|
-
return this.pickBestMatch(opened.filter((c) => distance(c) === nearest).map((c) => c.node), lineNumber);
|
|
9951
|
-
}
|
|
9952
|
-
return this.pickBestMatch(opened.map((c) => c.node), lineNumber);
|
|
9953
|
-
}
|
|
9954
|
-
// Fall back to substring match
|
|
9955
|
-
// For the reverse direction (target contains node text), require a minimum node text
|
|
9956
|
-
// length to avoid matching tiny identifiers like "res" or "body" too broadly.
|
|
9957
|
-
// Also require the node to cover at least half of the target: the reverse
|
|
9958
|
-
// branch exists for a locator with minor syntactic drift around the true
|
|
9959
|
-
// node, not for binding to a fragment. Without the floor, a 100+ char
|
|
9960
|
-
// locator whose exact match failed would bind to the smallest embedded
|
|
9961
|
-
// sub-expression (pickBestMatch prefers small nodes), e.g. the 8-char
|
|
9962
|
-
// literal "active" inside a res.json object payload (#335).
|
|
9963
|
-
//
|
|
9964
|
-
// A node the text itself places strictly inside what it names is a
|
|
9965
|
-
// fragment, however much of the text it covers (carrick#2056): the
|
|
9966
|
-
// `new URLSearchParams(…)` inside a call's URL argument, or the object a
|
|
9967
|
-
// callback nested in the named object returns. Only the node the text
|
|
9968
|
-
// names, or a wrapper the text adds around it, may be bound this way. A
|
|
9969
|
-
// fragment still points at what the text names where the source agrees
|
|
9970
|
-
// with the text up to it: the enclosing node of the named kind whose text
|
|
9971
|
-
// opens with the text's own opening up to the fragment (`new Response(`
|
|
9972
|
-
// before a payload, when the text dropped the call's second argument).
|
|
9973
|
-
const MIN_REVERSE_MATCH_LEN = 8;
|
|
9974
|
-
const MIN_REVERSE_MATCH_COVERAGE = 0.5;
|
|
9975
|
-
const containingMatches = candidates.filter((c) => c.text.includes(normalizedTarget));
|
|
9976
|
-
if (containingMatches.length > 0) {
|
|
9977
|
-
// A text that is only a wrapped call's first line (`load({`) is held by
|
|
9978
|
-
// every sibling that opens alike, and the smallest of them is not the
|
|
9979
|
-
// one the locator names (carrick#2114). The nodes whose own text OPENS
|
|
9980
|
-
// with the text are the ones a first line names: among those the stated
|
|
9981
|
-
// line decides, then size. A fragment from the middle of a node opens
|
|
9982
|
-
// nothing, and stays on the smallest node holding it.
|
|
9983
|
-
const opening = lineNumber === undefined
|
|
9984
|
-
? []
|
|
9985
|
-
: containingMatches.filter((c) => c.text.startsWith(normalizedTarget));
|
|
9986
|
-
if (lineNumber !== undefined && opening.length > 0) {
|
|
9987
|
-
const distance = (c) => Math.abs(startLineOf(c.node) - lineNumber);
|
|
9988
|
-
const nearest = Math.min(...opening.map(distance));
|
|
9989
|
-
return this.pickBestMatch(opening.filter((c) => distance(c) === nearest).map((c) => c.node), lineNumber);
|
|
9990
|
-
}
|
|
9991
|
-
return this.pickBestMatch(containingMatches.map((c) => c.node), lineNumber);
|
|
9992
|
-
}
|
|
9993
|
-
const reverseMatches = candidates.filter((c) => c.text.length >= MIN_REVERSE_MATCH_LEN &&
|
|
9994
|
-
c.text.length >= normalizedTarget.length * MIN_REVERSE_MATCH_COVERAGE &&
|
|
9995
|
-
normalizedTarget.includes(c.text));
|
|
9996
|
-
if (reverseMatches.length === 0)
|
|
9997
|
-
return undefined;
|
|
9998
|
-
const named = this.locatorNames(normalizedTarget);
|
|
9999
|
-
if (!named) {
|
|
10000
|
-
return this.pickBestMatch(reverseMatches.map((c) => c.node), lineNumber);
|
|
10001
|
-
}
|
|
10002
|
-
const textOf = new Map(candidates.map((c) => [c.node, c.text]));
|
|
10003
|
-
const bound = new Set();
|
|
10004
|
-
for (const c of reverseMatches) {
|
|
10005
|
-
if (named.texts.has(c.text)) {
|
|
10006
|
-
bound.add(c.node);
|
|
10007
|
-
continue;
|
|
10008
|
-
}
|
|
10009
|
-
const at = normalizedTarget.indexOf(c.text, named.rootStart + 1);
|
|
10010
|
-
if (at < 0)
|
|
10011
|
-
continue;
|
|
10012
|
-
const opening = normalizedTarget.slice(named.rootStart, at);
|
|
10013
|
-
const enclosing = c.node.getFirstAncestor((ancestor) => ancestor.getKind() === named.rootKind &&
|
|
10014
|
-
textOf.get(ancestor)?.startsWith(opening) === true);
|
|
10015
|
-
if (enclosing)
|
|
10016
|
-
bound.add(enclosing);
|
|
10017
|
-
}
|
|
10018
|
-
return bound.size > 0 ? this.pickBestMatch([...bound], lineNumber) : undefined;
|
|
10019
|
-
}
|
|
10020
|
-
/**
|
|
10021
|
-
* The text a locator opens with before an elision, or `undefined` when the
|
|
10022
|
-
* locator has none. An elision is `...` followed only by closing brackets
|
|
10023
|
-
* (`{ a: x, ... }`); `...rest` is a spread and source text. An opening too
|
|
10024
|
-
* short to tell nodes apart is judged by the caller.
|
|
10025
|
-
*/
|
|
10026
|
-
elidedLocatorOpening(normalizedTarget) {
|
|
10027
|
-
const match = /^(.*?)[\s,]*\.\.\.[\s,)}\]]*$/s.exec(normalizedTarget);
|
|
10028
|
-
if (!match)
|
|
10029
|
-
return undefined;
|
|
10030
|
-
return match[1];
|
|
10031
|
-
}
|
|
10032
|
-
/**
|
|
10033
|
-
* What a locator's text names, read off its own parse (carrick#2056):
|
|
10034
|
-
*
|
|
10035
|
-
* - `texts`: the normalized text of the expression it names and of each
|
|
10036
|
-
* wrapper it carries around it (`return`, a `const x =` binding, a label,
|
|
10037
|
-
* `await`, parentheses, a cast, `satisfies`, `!`). A node strictly inside
|
|
10038
|
-
* that expression is not among them;
|
|
10039
|
-
* - `rootKind` and `rootStart`: the innermost of those, its kind, and where
|
|
10040
|
-
* its text starts in the locator.
|
|
10041
|
-
*
|
|
10042
|
-
* `null` when the text does not parse as one expression or one statement:
|
|
10043
|
-
* there is no structure to read, and the caller keeps its text rule.
|
|
10044
|
-
*/
|
|
10045
|
-
locatorNames(normalizedTarget) {
|
|
10046
|
-
const parsed = (source) => {
|
|
10047
|
-
const file = ts.createSourceFile('locator.ts', source, ts.ScriptTarget.Latest, true);
|
|
10048
|
-
const diagnostics = file
|
|
10049
|
-
.parseDiagnostics;
|
|
10050
|
-
return diagnostics && diagnostics.length === 0 && file.statements.length === 1 ? file : null;
|
|
10051
|
-
};
|
|
10052
|
-
// An object literal is a block as a statement, so try the text as an
|
|
10053
|
-
// expression first.
|
|
10054
|
-
const EXPRESSION_OPENING = '(\n';
|
|
10055
|
-
const asExpression = parsed(`${EXPRESSION_OPENING}${normalizedTarget}\n);`);
|
|
10056
|
-
let node;
|
|
10057
|
-
let file = asExpression;
|
|
10058
|
-
let offset = 0;
|
|
10059
|
-
if (asExpression) {
|
|
10060
|
-
const statement = asExpression.statements[0];
|
|
10061
|
-
if (ts.isExpressionStatement(statement) && ts.isParenthesizedExpression(statement.expression)) {
|
|
10062
|
-
node = statement.expression.expression;
|
|
10063
|
-
offset = EXPRESSION_OPENING.length;
|
|
10064
|
-
}
|
|
10065
|
-
}
|
|
10066
|
-
else {
|
|
10067
|
-
file = parsed(normalizedTarget);
|
|
10068
|
-
node = file?.statements[0];
|
|
10069
|
-
}
|
|
10070
|
-
if (!file || !node)
|
|
10071
|
-
return null;
|
|
10072
|
-
const texts = new Set();
|
|
10073
|
-
let root;
|
|
10074
|
-
while (node) {
|
|
10075
|
-
if (ts.isLabeledStatement(node)) {
|
|
10076
|
-
node = node.statement;
|
|
10077
|
-
}
|
|
10078
|
-
else if (ts.isExpressionStatement(node) || ts.isReturnStatement(node)) {
|
|
10079
|
-
node = node.expression;
|
|
10080
|
-
}
|
|
10081
|
-
else if (ts.isVariableStatement(node)) {
|
|
10082
|
-
const declarations = node.declarationList.declarations;
|
|
10083
|
-
node = declarations.length === 1 ? declarations[0].initializer : undefined;
|
|
10084
|
-
}
|
|
10085
|
-
else {
|
|
10086
|
-
texts.add(this.normalizeWhitespace(node.getText(file)));
|
|
10087
|
-
root = node;
|
|
10088
|
-
node =
|
|
10089
|
-
ts.isParenthesizedExpression(node) ||
|
|
10090
|
-
ts.isAwaitExpression(node) ||
|
|
10091
|
-
ts.isAsExpression(node) ||
|
|
10092
|
-
ts.isSatisfiesExpression(node) ||
|
|
10093
|
-
ts.isNonNullExpression(node) ||
|
|
10094
|
-
ts.isTypeAssertionExpression(node)
|
|
10095
|
-
? node.expression
|
|
10096
|
-
: undefined;
|
|
10097
|
-
}
|
|
10098
|
-
}
|
|
10099
|
-
if (!root)
|
|
10100
|
-
return null;
|
|
10101
|
-
return { texts, rootKind: root.kind, rootStart: root.getStart(file) - offset };
|
|
10102
|
-
}
|
|
10103
|
-
/**
|
|
10104
|
-
* Pick the best match from a set of candidate nodes:
|
|
10105
|
-
* smallest range, then closest to target line.
|
|
10106
|
-
*/
|
|
10107
|
-
pickBestMatch(nodes, targetLine) {
|
|
10108
|
-
return nodes.reduce((best, current) => {
|
|
10109
|
-
const bestRange = best.getEnd() - best.getStart();
|
|
10110
|
-
const currentRange = current.getEnd() - current.getStart();
|
|
10111
|
-
// Prefer smaller nodes
|
|
10112
|
-
if (currentRange !== bestRange) {
|
|
10113
|
-
return currentRange < bestRange ? current : best;
|
|
10114
|
-
}
|
|
10115
|
-
// Tie-break by proximity to target line
|
|
10116
|
-
if (targetLine !== undefined) {
|
|
10117
|
-
const bestDist = Math.abs(startLineOf(best) - targetLine);
|
|
10118
|
-
const currentDist = Math.abs(startLineOf(current) - targetLine);
|
|
10119
|
-
return currentDist < bestDist ? current : best;
|
|
10120
|
-
}
|
|
10121
|
-
return best;
|
|
10122
|
-
});
|
|
10123
|
-
}
|
|
10124
|
-
/**
|
|
10125
|
-
* Normalize whitespace for text comparison:
|
|
10126
|
-
* collapse runs of whitespace into single spaces, trim.
|
|
10127
|
-
*
|
|
10128
|
-
* Also strips trailing commas before `}` `)` `]`: multi-line source
|
|
10129
|
-
* literals carry them but the LLM's single-line locator print does not,
|
|
10130
|
-
* and that one comma used to defeat exact AND containment matching for
|
|
10131
|
-
* the payload and every enclosing node (#335). Applied symmetrically to
|
|
10132
|
-
* node text and target, so both sides compare equal.
|
|
10133
|
-
*
|
|
10134
|
-
* Also strips the space left AFTER `(` `[` `{` by the collapse: a
|
|
10135
|
-
* multi-line call whose arguments start on the next line normalizes to
|
|
10136
|
-
* `f( x)` while the LLM's compact print is `f(x)`, and that one space
|
|
10137
|
-
* defeated exact matching for the call and every enclosing node — the
|
|
10138
|
-
* opening-delimiter mirror of the #335 trailing comma (#336). Symmetric
|
|
10139
|
-
* for the same reason.
|
|
10140
|
-
*/
|
|
10141
|
-
normalizeWhitespace(text) {
|
|
10142
|
-
const collapsed = text
|
|
10143
|
-
.replace(/\s+/g, ' ')
|
|
10144
|
-
// A member chain broken before its dot (`client\n .list(…)`) reads the
|
|
10145
|
-
// same as `client.list(…)`; a space around `.` / `?.` means nothing
|
|
10146
|
-
// (carrick#1162).
|
|
10147
|
-
.replace(/\s*(\?\.|\.)\s*/g, '$1')
|
|
10148
|
-
.replace(/\s*,?\s*([}\)\]])/g, '$1')
|
|
10149
|
-
.replace(/([\(\[{])\s+/g, '$1')
|
|
10150
|
-
.trim();
|
|
10151
|
-
// A string literal reads the same in any quotes (carrick#2056).
|
|
10152
|
-
return canonicalStringQuotes(collapsed);
|
|
9724
|
+
const found = nodeByLocatorText(sourceFile.compilerNode, inWindow.map((entry) => entry.node), expressionText, lineNumber, searchRadius);
|
|
9725
|
+
return found === undefined ? undefined : wrapCompilerNode(sourceFile, found);
|
|
10153
9726
|
}
|
|
10154
9727
|
/**
|
|
10155
9728
|
* Format a human-readable location string for error messages.
|