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.
- 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 +10 -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 +31 -122
- 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/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 +2 -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/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/library-claims.js +211 -20
- 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 +39 -4
- package/sidecar/dist/src/position-rows.js +233 -16
- package/sidecar/dist/src/project-loader.js +13 -5
- 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 +33 -145
- package/sidecar/dist/src/type-inferrer.js +68 -524
- package/sidecar/dist/src/types.d.ts +55 -2
- package/sidecar/dist/src/validators.d.ts +800 -142
- package/sidecar/dist/src/validators.js +11 -1
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a site is, and whether its type is framework machinery: one answer for
|
|
3
|
+
* the inferrer and the capture (carrick#3500).
|
|
4
|
+
*
|
|
5
|
+
* Both find the node a locator's text names near its line, then refuse to
|
|
6
|
+
* publish a type that is HTTP transport machinery. Each used to keep its own
|
|
7
|
+
* copy, and the copies disagreed (string quotes in the locator, which shapes
|
|
8
|
+
* of machinery were refused), so a wrong node or a transport type could be
|
|
9
|
+
* published by one path and refused by the other. They share the text rule
|
|
10
|
+
* here: the inferrer runs `nodeByLocatorText`, the capture an exact match
|
|
11
|
+
* alone (`firstExactLocatorMatch`). The module lives inside `capture/`
|
|
12
|
+
* because that bundle may import only itself and `typescript`; the inferrer
|
|
13
|
+
* reaches it through `capture/index.js` and hands it compiler nodes.
|
|
14
|
+
*/
|
|
15
|
+
import ts from 'typescript';
|
|
16
|
+
// ===========================================================================
|
|
17
|
+
// Framework machinery
|
|
18
|
+
// ===========================================================================
|
|
19
|
+
/**
|
|
20
|
+
* Strongly-discriminating member names of HTTP transport machinery: a
|
|
21
|
+
* fetch/DOM `Response` & `Request`, a Node `http.ServerResponse`, a framework
|
|
22
|
+
* reply object. A type declared in a lib or installed-dependency origin that
|
|
23
|
+
* carries a subset of these is framework machinery, never a user contract: the
|
|
24
|
+
* PRODUCER-side structural mirror of the consumer `machineryIndicators`
|
|
25
|
+
* (ExtractionConfig), used to reject a wrapper envelope whose response field is
|
|
26
|
+
* a raw `Response` (carrick#371) instead of emitting it as a comparable type.
|
|
27
|
+
*
|
|
28
|
+
* Framework-agnostic by construction: no framework NAME appears here, only the
|
|
29
|
+
* shared HTTP-message surface. The names are deliberately the ones no
|
|
30
|
+
* JSON payload ever carries (`ok`, `redirected`, `bodyUsed`, `arrayBuffer`,
|
|
31
|
+
* `writeHead`, ...), so the origin gate + `MACHINERY_INDICATOR_THRESHOLD` never
|
|
32
|
+
* fire on real data.
|
|
33
|
+
*/
|
|
34
|
+
export const MACHINERY_MEMBER_INDICATORS = new Set([
|
|
35
|
+
// fetch / DOM Response & Request body-consumer surface
|
|
36
|
+
'ok',
|
|
37
|
+
'redirected',
|
|
38
|
+
'bodyUsed',
|
|
39
|
+
'arrayBuffer',
|
|
40
|
+
'blob',
|
|
41
|
+
'formData',
|
|
42
|
+
'clone',
|
|
43
|
+
'json',
|
|
44
|
+
'statusText',
|
|
45
|
+
// FROZEN (old path, 2026-10-21): the reply-object half below only (the
|
|
46
|
+
// fetch half above and the threshold stay). Deletions and safety fixes
|
|
47
|
+
// only. Fixes for wrong or missing types go in labelled reply objects
|
|
48
|
+
// (carrick#2472 C4) and the language library rows (carrick#2445). Delete
|
|
49
|
+
// when a delete-and-replay on the four repos moves no row wrongly, after
|
|
50
|
+
// carrick-cloud#2379 and carrick#2445 (carrick#2472, C6).
|
|
51
|
+
// Node http ServerResponse / reply-object surface
|
|
52
|
+
'statusCode',
|
|
53
|
+
'statusMessage',
|
|
54
|
+
'setHeader',
|
|
55
|
+
'getHeader',
|
|
56
|
+
'removeHeader',
|
|
57
|
+
'writeHead',
|
|
58
|
+
'flushHeaders',
|
|
59
|
+
]);
|
|
60
|
+
/** Machinery needs at least this many indicator members to be recognized. */
|
|
61
|
+
const MACHINERY_INDICATOR_THRESHOLD = 3;
|
|
62
|
+
/**
|
|
63
|
+
* True when `type` itself is an HTTP-machinery type: it structurally carries
|
|
64
|
+
* at least `MACHINERY_INDICATOR_THRESHOLD` DISTINCT indicator members (own and
|
|
65
|
+
* apparent), AND its symbol is declared in a lib or installed-dependency
|
|
66
|
+
* origin. Both gates are required: the indicator subset alone essentially
|
|
67
|
+
* never matches a JSON payload, and the origin gate keeps a user's own type
|
|
68
|
+
* that shares those member names from reading as machinery.
|
|
69
|
+
*/
|
|
70
|
+
export function isFrameworkMachinery(checker, type, declaredOutside) {
|
|
71
|
+
if (!hasMachineryIndicatorThreshold(type)) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
return symbolDeclaredOutside(checker, type.getSymbol() ?? type.aliasSymbol, declaredOutside);
|
|
75
|
+
}
|
|
76
|
+
function hasMachineryIndicatorThreshold(type) {
|
|
77
|
+
const matched = new Set();
|
|
78
|
+
const consider = (name) => {
|
|
79
|
+
if (MACHINERY_MEMBER_INDICATORS.has(name)) {
|
|
80
|
+
matched.add(name);
|
|
81
|
+
}
|
|
82
|
+
return matched.size >= MACHINERY_INDICATOR_THRESHOLD;
|
|
83
|
+
};
|
|
84
|
+
for (const prop of type.getProperties()) {
|
|
85
|
+
if (consider(prop.getName()))
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
for (const prop of type.getApparentProperties()) {
|
|
89
|
+
if (consider(prop.getName()))
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* True when one of the symbol's declarations is outside the user's source, or
|
|
96
|
+
* the declaration an import alias stands for is. Works on a bare checkout:
|
|
97
|
+
* the DOM `Response`/`Request` resolve from the bundled `lib.dom.d.ts` even
|
|
98
|
+
* with no installed dependencies.
|
|
99
|
+
*/
|
|
100
|
+
export function symbolDeclaredOutside(checker, symbol, declaredOutside) {
|
|
101
|
+
if (!symbol) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
for (const decl of symbol.getDeclarations() ?? []) {
|
|
105
|
+
if (declaredOutside(decl.getSourceFile())) {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (symbol.flags & ts.SymbolFlags.Alias) {
|
|
110
|
+
try {
|
|
111
|
+
const aliased = checker.getAliasedSymbol(symbol);
|
|
112
|
+
if (aliased && aliased !== symbol) {
|
|
113
|
+
return symbolDeclaredOutside(checker, aliased, declaredOutside);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
// Ignore errors when resolving the aliased symbol.
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* True when `type` IS or CONTAINS framework machinery: a type that resolves
|
|
124
|
+
* here is never a comparable contract, so a path that reads it abstains
|
|
125
|
+
* (honest `unknown`) rather than publish a concrete-but-false type.
|
|
126
|
+
*
|
|
127
|
+
* DETECTS, exactly:
|
|
128
|
+
* 1. the type itself is machinery (`isFrameworkMachinery`);
|
|
129
|
+
* 2. a union/intersection member is machinery (the envelope union
|
|
130
|
+
* `{ response: Response; error?: undefined } | { ...; error: Error }`);
|
|
131
|
+
* 3. a DIRECT property whose type is machinery (`{ response: Response }`),
|
|
132
|
+
* ONE level of descent only, including a property no declaration states
|
|
133
|
+
* (a mapped `Record<'response', Response>`);
|
|
134
|
+
* 4. a CALL SIGNATURE whose AWAITED return type is machinery: the wrapper
|
|
135
|
+
* function `(req) => Promise<Response>`.
|
|
136
|
+
*
|
|
137
|
+
* DELIBERATELY NOT DETECTED (carrick#447), stated so this comment never
|
|
138
|
+
* overstates the guarantee. Each leaves the pre-existing verdict in place:
|
|
139
|
+
* - machinery nested deeper than one level;
|
|
140
|
+
* - a PROPERTY typed `Promise<Response>` (only call-signature returns are
|
|
141
|
+
* awaited);
|
|
142
|
+
* - an array element type: `Response[]` is not descended to its element;
|
|
143
|
+
* - `interface X extends Response` declared in USER source: the origin gate
|
|
144
|
+
* is lib/installed-dependency only.
|
|
145
|
+
*
|
|
146
|
+
* The depth cap + origin gate keep a legitimate payload that merely references
|
|
147
|
+
* a machinery type far inside from over-abstaining.
|
|
148
|
+
*/
|
|
149
|
+
export function typeIsOrContainsMachinery(checker, type, declaredOutside) {
|
|
150
|
+
return isOrContains(checker, type, declaredOutside, 0);
|
|
151
|
+
}
|
|
152
|
+
function isOrContains(checker, type, declaredOutside, depth) {
|
|
153
|
+
if (isFrameworkMachinery(checker, type, declaredOutside)) {
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
if (type.isUnion() || type.isIntersection()) {
|
|
157
|
+
return type.types.some((part) => isOrContains(checker, part, declaredOutside, depth));
|
|
158
|
+
}
|
|
159
|
+
if (depth >= 1) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
for (const sig of checker.getSignaturesOfType(type, ts.SignatureKind.Call)) {
|
|
163
|
+
const returnType = sig.getReturnType();
|
|
164
|
+
const awaited = checker.getAwaitedType(returnType) ?? returnType;
|
|
165
|
+
if (isOrContains(checker, awaited, declaredOutside, depth + 1)) {
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
for (const prop of checker.getPropertiesOfType(type)) {
|
|
170
|
+
if (isOrContains(checker, checker.getTypeOfSymbol(prop), declaredOutside, depth + 1)) {
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
// ===========================================================================
|
|
177
|
+
// Node locator
|
|
178
|
+
// ===========================================================================
|
|
179
|
+
/**
|
|
180
|
+
* Source text with every plain string literal written in double quotes:
|
|
181
|
+
* `'/a'`, `"/a"` and an interpolation-free `` `/a` `` are one value, so text
|
|
182
|
+
* that differs only in its quotes compares equal (carrick#2056).
|
|
183
|
+
*
|
|
184
|
+
* The text is read left to right, pairing quotes the way the scanner does,
|
|
185
|
+
* so a template's closing backtick never pairs with the next template's
|
|
186
|
+
* opening one. A literal holding a quote, a backslash or an interpolation is
|
|
187
|
+
* copied as it is, and so is an unterminated one with everything after it.
|
|
188
|
+
*/
|
|
189
|
+
export function canonicalStringQuotes(text) {
|
|
190
|
+
const isQuote = (ch) => ch === "'" || ch === '"' || ch === '`';
|
|
191
|
+
// The index just past the literal opening at `start`, or -1.
|
|
192
|
+
const literalEnd = (start) => {
|
|
193
|
+
const quote = text[start];
|
|
194
|
+
let j = start + 1;
|
|
195
|
+
while (j < text.length) {
|
|
196
|
+
const ch = text[j];
|
|
197
|
+
if (ch === '\\') {
|
|
198
|
+
j += 2;
|
|
199
|
+
}
|
|
200
|
+
else if (ch === quote) {
|
|
201
|
+
return j + 1;
|
|
202
|
+
}
|
|
203
|
+
else if (quote === '`' && ch === '$' && text[j + 1] === '{') {
|
|
204
|
+
const close = codeEnd(j + 2);
|
|
205
|
+
if (close < 0)
|
|
206
|
+
return -1;
|
|
207
|
+
j = close + 1;
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
j++;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return -1;
|
|
214
|
+
};
|
|
215
|
+
// The index of the `}` that closes an interpolation whose code starts at `start`, or -1.
|
|
216
|
+
const codeEnd = (start) => {
|
|
217
|
+
let depth = 0;
|
|
218
|
+
let j = start;
|
|
219
|
+
while (j < text.length) {
|
|
220
|
+
const ch = text[j];
|
|
221
|
+
if (isQuote(ch)) {
|
|
222
|
+
const end = literalEnd(j);
|
|
223
|
+
if (end < 0)
|
|
224
|
+
return -1;
|
|
225
|
+
j = end;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (ch === '{')
|
|
229
|
+
depth++;
|
|
230
|
+
if (ch === '}') {
|
|
231
|
+
if (depth === 0)
|
|
232
|
+
return j;
|
|
233
|
+
depth--;
|
|
234
|
+
}
|
|
235
|
+
j++;
|
|
236
|
+
}
|
|
237
|
+
return -1;
|
|
238
|
+
};
|
|
239
|
+
let out = '';
|
|
240
|
+
let i = 0;
|
|
241
|
+
while (i < text.length) {
|
|
242
|
+
if (!isQuote(text[i])) {
|
|
243
|
+
out += text[i];
|
|
244
|
+
i++;
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
const end = literalEnd(i);
|
|
248
|
+
if (end < 0)
|
|
249
|
+
return out + text.slice(i);
|
|
250
|
+
const body = text.slice(i + 1, end - 1);
|
|
251
|
+
out += /['"`\\]|\$\{/.test(body) ? text.slice(i, end) : `"${body}"`;
|
|
252
|
+
i = end;
|
|
253
|
+
}
|
|
254
|
+
return out;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Runs of whitespace collapse to one space, and a member chain broken before
|
|
258
|
+
* its dot (`client\n .list(…)`) reads the same as `client.list(…)`: a space
|
|
259
|
+
* around `.` / `?.` means nothing (carrick#1162). Not trimmed.
|
|
260
|
+
*/
|
|
261
|
+
function collapseLocatorWhitespace(text) {
|
|
262
|
+
return text.replace(/\s+/g, ' ').replace(/\s*(\?\.|\.)\s*/g, '$1');
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* The text the capture's exact match compares: whitespace collapsed
|
|
266
|
+
* (`collapseLocatorWhitespace`), trimmed, and every plain string literal in
|
|
267
|
+
* double quotes (carrick#2056). Unlike `normalizeLocatorText` it keeps
|
|
268
|
+
* trailing commas and the space after an opening bracket, so a payload written
|
|
269
|
+
* over several lines is not matched by a compact print of it (see
|
|
270
|
+
* `firstExactLocatorMatch`).
|
|
271
|
+
*/
|
|
272
|
+
export function exactLocatorText(text) {
|
|
273
|
+
return canonicalStringQuotes(collapseLocatorWhitespace(text).trim());
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Normalize whitespace for text comparison, the inferrer's rule:
|
|
277
|
+
* `collapseLocatorWhitespace`, then trim.
|
|
278
|
+
*
|
|
279
|
+
* Also strips trailing commas before `}` `)` `]`: multi-line source
|
|
280
|
+
* literals carry them but the LLM's single-line locator print does not,
|
|
281
|
+
* and that one comma used to defeat exact AND containment matching for
|
|
282
|
+
* the payload and every enclosing node (#335). Applied symmetrically to
|
|
283
|
+
* node text and target, so both sides compare equal.
|
|
284
|
+
*
|
|
285
|
+
* Also strips the space left AFTER `(` `[` `{` by the collapse: a
|
|
286
|
+
* multi-line call whose arguments start on the next line normalizes to
|
|
287
|
+
* `f( x)` while the LLM's compact print is `f(x)`, and that one space
|
|
288
|
+
* defeated exact matching for the call and every enclosing node — the
|
|
289
|
+
* opening-delimiter mirror of the #335 trailing comma (#336). Symmetric
|
|
290
|
+
* for the same reason.
|
|
291
|
+
*
|
|
292
|
+
* A string literal reads the same in any quotes (carrick#2056).
|
|
293
|
+
*/
|
|
294
|
+
export function normalizeLocatorText(text) {
|
|
295
|
+
const collapsed = collapseLocatorWhitespace(text)
|
|
296
|
+
.replace(/\s*,?\s*([}\)\]])/g, '$1')
|
|
297
|
+
.replace(/([\(\[{])\s+/g, '$1')
|
|
298
|
+
.trim();
|
|
299
|
+
return canonicalStringQuotes(collapsed);
|
|
300
|
+
}
|
|
301
|
+
/** The default number of lines either side of a locator's line it may match. */
|
|
302
|
+
const LOCATOR_SEARCH_RADIUS = 5;
|
|
303
|
+
function startLine(sourceFile, node) {
|
|
304
|
+
return sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* The capture's locator: the first node in pre-order that `accept`s, whose
|
|
308
|
+
* text equals the locator's (`exactLocatorText`), and that ends on or after
|
|
309
|
+
* `fromLine` (a chain broken before its dot starts a line above the line the
|
|
310
|
+
* analyzer reports for the call), anywhere in the file.
|
|
311
|
+
*
|
|
312
|
+
* Exact only, with no line window: the capture publishes the type of the node
|
|
313
|
+
* it binds, and each of `nodeByLocatorText`'s further tiers moved published
|
|
314
|
+
* answers when the capture ran them (carrick#3500): a body read stated at a
|
|
315
|
+
* handler's opening line bound the whole handler, and a compact print of a
|
|
316
|
+
* multi-line payload bound the payload where the call on its line was asked.
|
|
317
|
+
* A text with no exact match leaves the caller its line fallback.
|
|
318
|
+
*/
|
|
319
|
+
export function firstExactLocatorMatch(sourceFile, expressionText, fromLine, accept) {
|
|
320
|
+
const wanted = exactLocatorText(expressionText);
|
|
321
|
+
if (!wanted)
|
|
322
|
+
return undefined;
|
|
323
|
+
let best;
|
|
324
|
+
const visit = (node) => {
|
|
325
|
+
if (best)
|
|
326
|
+
return;
|
|
327
|
+
if (accept(node) && exactLocatorText(node.getText(sourceFile)) === wanted) {
|
|
328
|
+
const endLine = sourceFile.getLineAndCharacterOfPosition(node.getEnd()).line + 1;
|
|
329
|
+
if (fromLine === undefined || endLine >= fromLine) {
|
|
330
|
+
best = node;
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
node.forEachChild(visit);
|
|
335
|
+
};
|
|
336
|
+
visit(sourceFile);
|
|
337
|
+
return best;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* The node a locator's text names, among `nodes`, near `lineNumber`.
|
|
341
|
+
*
|
|
342
|
+
* Normalizes whitespace once per candidate, then tries an exact match, then a
|
|
343
|
+
* locator that elides members, then a node containing the text (preferring
|
|
344
|
+
* the nodes the text opens), then a node the text contains. Among several,
|
|
345
|
+
* the smallest node wins, then the one nearest the line. The caller chooses
|
|
346
|
+
* the candidates (every node, or only calls). The inferrer's locator; the
|
|
347
|
+
* capture runs `firstExactLocatorMatch`.
|
|
348
|
+
*/
|
|
349
|
+
export function nodeByLocatorText(sourceFile, nodes, expressionText, lineNumber, searchRadius = LOCATOR_SEARCH_RADIUS) {
|
|
350
|
+
const normalizedTarget = normalizeLocatorText(expressionText);
|
|
351
|
+
if (!normalizedTarget)
|
|
352
|
+
return undefined;
|
|
353
|
+
const lineOf = (node) => startLine(sourceFile, node);
|
|
354
|
+
// Filter to nodes within the search window and pre-compute normalized text
|
|
355
|
+
const candidates = (lineNumber
|
|
356
|
+
? nodes.filter((node) => {
|
|
357
|
+
const nodeLine = lineOf(node);
|
|
358
|
+
return nodeLine >= lineNumber - searchRadius && nodeLine <= lineNumber + searchRadius;
|
|
359
|
+
})
|
|
360
|
+
: nodes).map((node) => ({ node, text: normalizeLocatorText(node.getText(sourceFile)) }));
|
|
361
|
+
if (candidates.length === 0)
|
|
362
|
+
return undefined;
|
|
363
|
+
const best = (found) => pickBestMatch(sourceFile, found, lineNumber);
|
|
364
|
+
// Try exact match (normalized whitespace)
|
|
365
|
+
const exactMatches = candidates.filter((c) => c.text === normalizedTarget);
|
|
366
|
+
if (exactMatches.length > 0) {
|
|
367
|
+
return best(exactMatches.map((c) => c.node));
|
|
368
|
+
}
|
|
369
|
+
// A bare identifier target must match a node exactly: substring matching
|
|
370
|
+
// would bind `users` to `usersCsv` (or to any enclosing node that merely
|
|
371
|
+
// contains the identifier somewhere) and report a confidently wrong type.
|
|
372
|
+
// Failing here is correct — the caller records an error and the alias
|
|
373
|
+
// pads to `unknown` downstream.
|
|
374
|
+
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(normalizedTarget)) {
|
|
375
|
+
return undefined;
|
|
376
|
+
}
|
|
377
|
+
// A locator that elides members (`body: { a: x, ... }`) stands for the
|
|
378
|
+
// whole node it opens: the text before the elision is where the source
|
|
379
|
+
// opens. Never a node the text merely contains, which is one member of the
|
|
380
|
+
// payload it names.
|
|
381
|
+
// An opening shorter than this (`{`) names no node in particular.
|
|
382
|
+
const MIN_ELIDED_OPENING_LEN = 8;
|
|
383
|
+
const elidedOpening = elidedLocatorOpening(normalizedTarget);
|
|
384
|
+
if (elidedOpening !== undefined) {
|
|
385
|
+
if (elidedOpening.length < MIN_ELIDED_OPENING_LEN)
|
|
386
|
+
return undefined;
|
|
387
|
+
const opened = candidates.filter((c) => c.text.startsWith(elidedOpening));
|
|
388
|
+
if (opened.length === 0)
|
|
389
|
+
return undefined;
|
|
390
|
+
if (lineNumber !== undefined) {
|
|
391
|
+
const distance = (c) => Math.abs(lineOf(c.node) - lineNumber);
|
|
392
|
+
const nearest = Math.min(...opened.map(distance));
|
|
393
|
+
return best(opened.filter((c) => distance(c) === nearest).map((c) => c.node));
|
|
394
|
+
}
|
|
395
|
+
return best(opened.map((c) => c.node));
|
|
396
|
+
}
|
|
397
|
+
// Fall back to substring match
|
|
398
|
+
// For the reverse direction (target contains node text), require a minimum node text
|
|
399
|
+
// length to avoid matching tiny identifiers like "res" or "body" too broadly.
|
|
400
|
+
// Also require the node to cover at least half of the target: the reverse
|
|
401
|
+
// branch exists for a locator with minor syntactic drift around the true
|
|
402
|
+
// node, not for binding to a fragment. Without the floor, a 100+ char
|
|
403
|
+
// locator whose exact match failed would bind to the smallest embedded
|
|
404
|
+
// sub-expression (pickBestMatch prefers small nodes), e.g. the 8-char
|
|
405
|
+
// literal "active" inside a res.json object payload (#335).
|
|
406
|
+
//
|
|
407
|
+
// A node the text itself places strictly inside what it names is a
|
|
408
|
+
// fragment, however much of the text it covers (carrick#2056): the
|
|
409
|
+
// `new URLSearchParams(…)` inside a call's URL argument, or the object a
|
|
410
|
+
// callback nested in the named object returns. Only the node the text
|
|
411
|
+
// names, or a wrapper the text adds around it, may be bound this way. A
|
|
412
|
+
// fragment still points at what the text names where the source agrees
|
|
413
|
+
// with the text up to it: the enclosing node of the named kind whose text
|
|
414
|
+
// opens with the text's own opening up to the fragment (`new Response(`
|
|
415
|
+
// before a payload, when the text dropped the call's second argument).
|
|
416
|
+
const MIN_REVERSE_MATCH_LEN = 8;
|
|
417
|
+
const MIN_REVERSE_MATCH_COVERAGE = 0.5;
|
|
418
|
+
const containingMatches = candidates.filter((c) => c.text.includes(normalizedTarget));
|
|
419
|
+
if (containingMatches.length > 0) {
|
|
420
|
+
// A text that is only a wrapped call's first line (`load({`) is held by
|
|
421
|
+
// every sibling that opens alike, and the smallest of them is not the
|
|
422
|
+
// one the locator names (carrick#2114). The nodes whose own text OPENS
|
|
423
|
+
// with the text are the ones a first line names: among those the stated
|
|
424
|
+
// line decides, then size. A fragment from the middle of a node opens
|
|
425
|
+
// nothing, and stays on the smallest node holding it.
|
|
426
|
+
const opening = lineNumber === undefined
|
|
427
|
+
? []
|
|
428
|
+
: containingMatches.filter((c) => c.text.startsWith(normalizedTarget));
|
|
429
|
+
if (lineNumber !== undefined && opening.length > 0) {
|
|
430
|
+
const distance = (c) => Math.abs(lineOf(c.node) - lineNumber);
|
|
431
|
+
const nearest = Math.min(...opening.map(distance));
|
|
432
|
+
return best(opening.filter((c) => distance(c) === nearest).map((c) => c.node));
|
|
433
|
+
}
|
|
434
|
+
return best(containingMatches.map((c) => c.node));
|
|
435
|
+
}
|
|
436
|
+
const reverseMatches = candidates.filter((c) => c.text.length >= MIN_REVERSE_MATCH_LEN &&
|
|
437
|
+
c.text.length >= normalizedTarget.length * MIN_REVERSE_MATCH_COVERAGE &&
|
|
438
|
+
normalizedTarget.includes(c.text));
|
|
439
|
+
if (reverseMatches.length === 0)
|
|
440
|
+
return undefined;
|
|
441
|
+
const named = locatorNames(normalizedTarget);
|
|
442
|
+
if (!named) {
|
|
443
|
+
return best(reverseMatches.map((c) => c.node));
|
|
444
|
+
}
|
|
445
|
+
const textOf = new Map(candidates.map((c) => [c.node, c.text]));
|
|
446
|
+
const bound = new Set();
|
|
447
|
+
for (const c of reverseMatches) {
|
|
448
|
+
if (named.texts.has(c.text)) {
|
|
449
|
+
bound.add(c.node);
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
const at = normalizedTarget.indexOf(c.text, named.rootStart + 1);
|
|
453
|
+
if (at < 0)
|
|
454
|
+
continue;
|
|
455
|
+
const opening = normalizedTarget.slice(named.rootStart, at);
|
|
456
|
+
let enclosing = c.node.parent;
|
|
457
|
+
while (enclosing &&
|
|
458
|
+
!(enclosing.kind === named.rootKind && textOf.get(enclosing)?.startsWith(opening) === true)) {
|
|
459
|
+
enclosing = enclosing.parent;
|
|
460
|
+
}
|
|
461
|
+
if (enclosing)
|
|
462
|
+
bound.add(enclosing);
|
|
463
|
+
}
|
|
464
|
+
return bound.size > 0 ? best([...bound]) : undefined;
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* The text a locator opens with before an elision, or `undefined` when the
|
|
468
|
+
* locator has none. An elision is `...` followed only by closing brackets
|
|
469
|
+
* (`{ a: x, ... }`); `...rest` is a spread and source text. An opening too
|
|
470
|
+
* short to tell nodes apart is judged by the caller.
|
|
471
|
+
*/
|
|
472
|
+
function elidedLocatorOpening(normalizedTarget) {
|
|
473
|
+
const match = /^(.*?)[\s,]*\.\.\.[\s,)}\]]*$/s.exec(normalizedTarget);
|
|
474
|
+
if (!match)
|
|
475
|
+
return undefined;
|
|
476
|
+
return match[1];
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* What a locator's text names, read off its own parse (carrick#2056):
|
|
480
|
+
*
|
|
481
|
+
* - `texts`: the normalized text of the expression it names and of each
|
|
482
|
+
* wrapper it carries around it (`return`, a `const x =` binding, a label,
|
|
483
|
+
* `await`, parentheses, a cast, `satisfies`, `!`). A node strictly inside
|
|
484
|
+
* that expression is not among them;
|
|
485
|
+
* - `rootKind` and `rootStart`: the innermost of those, its kind, and where
|
|
486
|
+
* its text starts in the locator.
|
|
487
|
+
*
|
|
488
|
+
* `null` when the text does not parse as one expression or one statement:
|
|
489
|
+
* there is no structure to read, and the caller keeps its text rule.
|
|
490
|
+
* Exported for the reference walk in `infer-line-index.test.ts`.
|
|
491
|
+
*/
|
|
492
|
+
export function locatorNames(normalizedTarget) {
|
|
493
|
+
const parsed = (source) => {
|
|
494
|
+
const file = ts.createSourceFile('locator.ts', source, ts.ScriptTarget.Latest, true);
|
|
495
|
+
const diagnostics = file
|
|
496
|
+
.parseDiagnostics;
|
|
497
|
+
return diagnostics && diagnostics.length === 0 && file.statements.length === 1 ? file : null;
|
|
498
|
+
};
|
|
499
|
+
// An object literal is a block as a statement, so try the text as an
|
|
500
|
+
// expression first.
|
|
501
|
+
const EXPRESSION_OPENING = '(\n';
|
|
502
|
+
const asExpression = parsed(`${EXPRESSION_OPENING}${normalizedTarget}\n);`);
|
|
503
|
+
let node;
|
|
504
|
+
let file = asExpression;
|
|
505
|
+
let offset = 0;
|
|
506
|
+
if (asExpression) {
|
|
507
|
+
const statement = asExpression.statements[0];
|
|
508
|
+
if (ts.isExpressionStatement(statement) && ts.isParenthesizedExpression(statement.expression)) {
|
|
509
|
+
node = statement.expression.expression;
|
|
510
|
+
offset = EXPRESSION_OPENING.length;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
else {
|
|
514
|
+
file = parsed(normalizedTarget);
|
|
515
|
+
node = file?.statements[0];
|
|
516
|
+
}
|
|
517
|
+
if (!file || !node)
|
|
518
|
+
return null;
|
|
519
|
+
const texts = new Set();
|
|
520
|
+
let root;
|
|
521
|
+
while (node) {
|
|
522
|
+
if (ts.isLabeledStatement(node)) {
|
|
523
|
+
node = node.statement;
|
|
524
|
+
}
|
|
525
|
+
else if (ts.isExpressionStatement(node) || ts.isReturnStatement(node)) {
|
|
526
|
+
node = node.expression;
|
|
527
|
+
}
|
|
528
|
+
else if (ts.isVariableStatement(node)) {
|
|
529
|
+
const declarations = node.declarationList.declarations;
|
|
530
|
+
node = declarations.length === 1 ? declarations[0].initializer : undefined;
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
texts.add(normalizeLocatorText(node.getText(file)));
|
|
534
|
+
root = node;
|
|
535
|
+
node =
|
|
536
|
+
ts.isParenthesizedExpression(node) ||
|
|
537
|
+
ts.isAwaitExpression(node) ||
|
|
538
|
+
ts.isAsExpression(node) ||
|
|
539
|
+
ts.isSatisfiesExpression(node) ||
|
|
540
|
+
ts.isNonNullExpression(node) ||
|
|
541
|
+
ts.isTypeAssertionExpression(node)
|
|
542
|
+
? node.expression
|
|
543
|
+
: undefined;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
if (!root)
|
|
547
|
+
return null;
|
|
548
|
+
return { texts, rootKind: root.kind, rootStart: root.getStart(file) - offset };
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Pick the best match from a set of candidate nodes:
|
|
552
|
+
* smallest range, then closest to target line.
|
|
553
|
+
*/
|
|
554
|
+
function pickBestMatch(sourceFile, nodes, targetLine) {
|
|
555
|
+
return nodes.reduce((best, current) => {
|
|
556
|
+
const bestRange = best.getEnd() - best.getStart(sourceFile);
|
|
557
|
+
const currentRange = current.getEnd() - current.getStart(sourceFile);
|
|
558
|
+
// Prefer smaller nodes
|
|
559
|
+
if (currentRange !== bestRange) {
|
|
560
|
+
return currentRange < bestRange ? current : best;
|
|
561
|
+
}
|
|
562
|
+
// Tie-break by proximity to target line
|
|
563
|
+
if (targetLine !== undefined) {
|
|
564
|
+
const bestDist = Math.abs(startLine(sourceFile, best) - targetLine);
|
|
565
|
+
const currentDist = Math.abs(startLine(sourceFile, current) - targetLine);
|
|
566
|
+
return currentDist < bestDist ? current : best;
|
|
567
|
+
}
|
|
568
|
+
return best;
|
|
569
|
+
});
|
|
570
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The environment for a package tool the scan spawns (Deno, the check's
|
|
3
|
+
* package manager).
|
|
4
|
+
*
|
|
5
|
+
* Such a tool needs PATH, HOME, its cache and proxy settings, and whatever
|
|
6
|
+
* registry auth the user configured for it (for example NPM_TOKEN). It never
|
|
7
|
+
* needs Carrick's own credentials or the CI runner's tokens, and it can read
|
|
8
|
+
* config the scanned tree supplies, so those keys are left out. Every other
|
|
9
|
+
* key passes through unchanged.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isDroppedToolEnvKey(key: string): boolean;
|
|
12
|
+
export declare function toolEnv(extra?: NodeJS.ProcessEnv, source?: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The environment for a package tool the scan spawns (Deno, the check's
|
|
3
|
+
* package manager).
|
|
4
|
+
*
|
|
5
|
+
* Such a tool needs PATH, HOME, its cache and proxy settings, and whatever
|
|
6
|
+
* registry auth the user configured for it (for example NPM_TOKEN). It never
|
|
7
|
+
* needs Carrick's own credentials or the CI runner's tokens, and it can read
|
|
8
|
+
* config the scanned tree supplies, so those keys are left out. Every other
|
|
9
|
+
* key passes through unchanged.
|
|
10
|
+
*/
|
|
11
|
+
/** Exact keys a package tool never receives. */
|
|
12
|
+
const DROPPED_KEYS = new Set(['GITHUB_TOKEN', 'GH_TOKEN']);
|
|
13
|
+
/** Key prefixes a package tool never receives. */
|
|
14
|
+
const DROPPED_PREFIXES = ['CARRICK_', 'ACTIONS_', 'AWS_'];
|
|
15
|
+
export function isDroppedToolEnvKey(key) {
|
|
16
|
+
const upper = key.toUpperCase();
|
|
17
|
+
return DROPPED_KEYS.has(upper) || DROPPED_PREFIXES.some(prefix => upper.startsWith(prefix));
|
|
18
|
+
}
|
|
19
|
+
export function toolEnv(extra = {}, source = process.env) {
|
|
20
|
+
const env = {};
|
|
21
|
+
for (const [key, value] of Object.entries(source)) {
|
|
22
|
+
if (value !== undefined && !isDroppedToolEnvKey(key))
|
|
23
|
+
env[key] = value;
|
|
24
|
+
}
|
|
25
|
+
return { ...env, ...extra };
|
|
26
|
+
}
|