carrick 0.3.140 → 0.3.141
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/package.json +6 -6
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/sidecar/dist/src/argument-modes.d.ts +56 -0
- package/sidecar/dist/src/argument-modes.js +448 -0
- package/sidecar/dist/src/loop-element.d.ts +58 -0
- package/sidecar/dist/src/loop-element.js +307 -0
- package/sidecar/dist/src/response-modes.d.ts +7 -2
- package/sidecar/dist/src/response-modes.js +1 -1
- package/sidecar/dist/src/type-inferrer.d.ts +43 -7
- package/sidecar/dist/src/type-inferrer.js +229 -9
- package/sidecar/dist/src/types.d.ts +8 -0
- package/sidecar/dist/src/validators.d.ts +13 -0
- package/sidecar/dist/src/validators.js +2 -0
- package/templates/skills/carrick-reuse.md +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "carrick",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.141",
|
|
4
4
|
"carrickCache": {
|
|
5
5
|
"cache_version": 22,
|
|
6
6
|
"intent_cache_version": 2
|
|
@@ -115,11 +115,11 @@
|
|
|
115
115
|
"zod": "^3.23.0"
|
|
116
116
|
},
|
|
117
117
|
"optionalDependencies": {
|
|
118
|
-
"@carrick-tools/cli-darwin-arm64": "0.3.
|
|
119
|
-
"@carrick-tools/cli-darwin-x64": "0.3.
|
|
120
|
-
"@carrick-tools/cli-linux-arm64": "0.3.
|
|
121
|
-
"@carrick-tools/cli-linux-x64": "0.3.
|
|
122
|
-
"@carrick-tools/cli-win32-x64": "0.3.
|
|
118
|
+
"@carrick-tools/cli-darwin-arm64": "0.3.141",
|
|
119
|
+
"@carrick-tools/cli-darwin-x64": "0.3.141",
|
|
120
|
+
"@carrick-tools/cli-linux-arm64": "0.3.141",
|
|
121
|
+
"@carrick-tools/cli-linux-x64": "0.3.141",
|
|
122
|
+
"@carrick-tools/cli-win32-x64": "0.3.141"
|
|
123
123
|
},
|
|
124
124
|
"devDependencies": {
|
|
125
125
|
"@types/node": "^24.13.3"
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "carrick",
|
|
3
3
|
"description": "Claude Code plugin for Carrick, which indexes TypeScript codebases across service and repository boundaries. After each edit it adds the file's routes, calls, and cross-service type mismatches to the session, and it registers Carrick's language server. It pairs with the Carrick MCP server, which lets agents search functions by intent rather than name.",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.141",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Carrick",
|
|
7
7
|
"email": "hello@carrick.tools"
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A response read from a function a route calls with its own arguments
|
|
3
|
+
* (carrick#2372).
|
|
4
|
+
*
|
|
5
|
+
* Two routes often share one executor and tell it apart by an argument:
|
|
6
|
+
* `execute(input, 'commit')` for `POST /x` and `execute(input, 'preview')`
|
|
7
|
+
* for `POST /x/preview`, or `read({ id })` for an item route and `read({})`
|
|
8
|
+
* for the list. The executor's sends, or its declared return, are the union
|
|
9
|
+
* of every caller's answers. The compiler gives the same type for every call
|
|
10
|
+
* of a signature that is neither generic nor overloaded, so the route's own
|
|
11
|
+
* answer has to be read from the executor's paths: the tests on a body's
|
|
12
|
+
* path that compare a parameter with a value the route's call states.
|
|
13
|
+
*
|
|
14
|
+
* - **What a call states.** A literal argument (a string, number, boolean,
|
|
15
|
+
* `null` or `undefined`) states its value; an argument left out states
|
|
16
|
+
* `undefined` (a parameter with a default states nothing); any other
|
|
17
|
+
* argument whose type holds neither `null` nor `undefined` states only that
|
|
18
|
+
* it is defined. A parameter the executor assigns to states nothing, nor
|
|
19
|
+
* does any parameter of a call with a spread argument. An object literal
|
|
20
|
+
* argument states each member the same way for a parameter read through a
|
|
21
|
+
* member access or a destructuring `const` (or a destructured parameter);
|
|
22
|
+
* a member it leaves out states `undefined` unless the literal spreads
|
|
23
|
+
* another object.
|
|
24
|
+
* - **What a test decides.** `===`, `==`, `!==` and `!=` between a stated
|
|
25
|
+
* parameter and a literal, `!`, `&&` and `||`, and the truthiness of a
|
|
26
|
+
* stated literal. Anything else decides nothing, and a body under a test
|
|
27
|
+
* that decides nothing is kept.
|
|
28
|
+
* - **Which bodies go.** A body one of whose path tests (an `if` around it,
|
|
29
|
+
* either arm of a conditional, or an earlier `if` that cannot complete)
|
|
30
|
+
* decides against the side the body is on. No other body is dropped.
|
|
31
|
+
*
|
|
32
|
+
* `tested` names every stated parameter some test in the executor reads as a
|
|
33
|
+
* mode (a stated literal, or a value stated as defined under a test it
|
|
34
|
+
* decides) and some other call of the executor states differently, whether
|
|
35
|
+
* or not the test decided anything: a union read from such an executor
|
|
36
|
+
* depends on which caller ran it, and where the bodies could not be told
|
|
37
|
+
* apart the caller says so rather than judging the route against every
|
|
38
|
+
* caller's answers. An executor only one call reaches, or whose callers all
|
|
39
|
+
* pass the same value, has no such mode.
|
|
40
|
+
*/
|
|
41
|
+
import { Node, type CallExpression } from 'ts-morph';
|
|
42
|
+
export interface ArgumentNarrowing {
|
|
43
|
+
/** The bodies no stated argument rules out, in the order given. */
|
|
44
|
+
kept: Node[];
|
|
45
|
+
/** Every stated parameter some test in the executor reads, sorted. */
|
|
46
|
+
tested: string[];
|
|
47
|
+
}
|
|
48
|
+
/** The function a call runs, when it has one body in the program. */
|
|
49
|
+
export declare function calledFunction(call: CallExpression): Node | undefined;
|
|
50
|
+
/** The one call in `caller` that runs `callee`, or `undefined` for none or several. */
|
|
51
|
+
export declare function callInto(caller: Node, callee: Node): CallExpression | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* The bodies of `callee` the arguments `call` passes leave reachable. See the
|
|
54
|
+
* module comment for what is read.
|
|
55
|
+
*/
|
|
56
|
+
export declare function narrowByArguments(callee: Node, call: CallExpression, bodies: Node[]): ArgumentNarrowing;
|
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A response read from a function a route calls with its own arguments
|
|
3
|
+
* (carrick#2372).
|
|
4
|
+
*
|
|
5
|
+
* Two routes often share one executor and tell it apart by an argument:
|
|
6
|
+
* `execute(input, 'commit')` for `POST /x` and `execute(input, 'preview')`
|
|
7
|
+
* for `POST /x/preview`, or `read({ id })` for an item route and `read({})`
|
|
8
|
+
* for the list. The executor's sends, or its declared return, are the union
|
|
9
|
+
* of every caller's answers. The compiler gives the same type for every call
|
|
10
|
+
* of a signature that is neither generic nor overloaded, so the route's own
|
|
11
|
+
* answer has to be read from the executor's paths: the tests on a body's
|
|
12
|
+
* path that compare a parameter with a value the route's call states.
|
|
13
|
+
*
|
|
14
|
+
* - **What a call states.** A literal argument (a string, number, boolean,
|
|
15
|
+
* `null` or `undefined`) states its value; an argument left out states
|
|
16
|
+
* `undefined` (a parameter with a default states nothing); any other
|
|
17
|
+
* argument whose type holds neither `null` nor `undefined` states only that
|
|
18
|
+
* it is defined. A parameter the executor assigns to states nothing, nor
|
|
19
|
+
* does any parameter of a call with a spread argument. An object literal
|
|
20
|
+
* argument states each member the same way for a parameter read through a
|
|
21
|
+
* member access or a destructuring `const` (or a destructured parameter);
|
|
22
|
+
* a member it leaves out states `undefined` unless the literal spreads
|
|
23
|
+
* another object.
|
|
24
|
+
* - **What a test decides.** `===`, `==`, `!==` and `!=` between a stated
|
|
25
|
+
* parameter and a literal, `!`, `&&` and `||`, and the truthiness of a
|
|
26
|
+
* stated literal. Anything else decides nothing, and a body under a test
|
|
27
|
+
* that decides nothing is kept.
|
|
28
|
+
* - **Which bodies go.** A body one of whose path tests (an `if` around it,
|
|
29
|
+
* either arm of a conditional, or an earlier `if` that cannot complete)
|
|
30
|
+
* decides against the side the body is on. No other body is dropped.
|
|
31
|
+
*
|
|
32
|
+
* `tested` names every stated parameter some test in the executor reads as a
|
|
33
|
+
* mode (a stated literal, or a value stated as defined under a test it
|
|
34
|
+
* decides) and some other call of the executor states differently, whether
|
|
35
|
+
* or not the test decided anything: a union read from such an executor
|
|
36
|
+
* depends on which caller ran it, and where the bodies could not be told
|
|
37
|
+
* apart the caller says so rather than judging the route against every
|
|
38
|
+
* caller's answers. An executor only one call reaches, or whose callers all
|
|
39
|
+
* pass the same value, has no such mode.
|
|
40
|
+
*/
|
|
41
|
+
import { Node, SyntaxKind } from 'ts-morph';
|
|
42
|
+
import { stepsOnPath } from './failure-path.js';
|
|
43
|
+
/** The function a call runs, when it has one body in the program. */
|
|
44
|
+
export function calledFunction(call) {
|
|
45
|
+
let symbol = call.getExpression().getSymbol();
|
|
46
|
+
if (symbol?.isAlias())
|
|
47
|
+
symbol = symbol.getAliasedSymbol();
|
|
48
|
+
const declarations = symbol?.getDeclarations() ?? [];
|
|
49
|
+
if (declarations.length !== 1)
|
|
50
|
+
return undefined;
|
|
51
|
+
const [declaration] = declarations;
|
|
52
|
+
if ((Node.isFunctionDeclaration(declaration) || Node.isMethodDeclaration(declaration)) &&
|
|
53
|
+
declaration.getBody()) {
|
|
54
|
+
return declaration;
|
|
55
|
+
}
|
|
56
|
+
if (Node.isVariableDeclaration(declaration) || Node.isPropertyAssignment(declaration)) {
|
|
57
|
+
let initializer = declaration.getInitializer();
|
|
58
|
+
while (initializer && (Node.isParenthesizedExpression(initializer) || Node.isAsExpression(initializer))) {
|
|
59
|
+
initializer = initializer.getExpression();
|
|
60
|
+
}
|
|
61
|
+
if (initializer && (Node.isArrowFunction(initializer) || Node.isFunctionExpression(initializer))) {
|
|
62
|
+
return initializer;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
/** The one call in `caller` that runs `callee`, or `undefined` for none or several. */
|
|
68
|
+
export function callInto(caller, callee) {
|
|
69
|
+
const calls = caller
|
|
70
|
+
.getDescendantsOfKind(SyntaxKind.CallExpression)
|
|
71
|
+
.filter((call) => calledFunction(call) === callee);
|
|
72
|
+
return calls.length === 1 ? calls[0] : undefined;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The bodies of `callee` the arguments `call` passes leave reachable. See the
|
|
76
|
+
* module comment for what is read.
|
|
77
|
+
*/
|
|
78
|
+
export function narrowByArguments(callee, call, bodies) {
|
|
79
|
+
const binding = bindArguments(callee, call);
|
|
80
|
+
if (binding.direct.size === 0 && binding.members.size === 0)
|
|
81
|
+
return { kept: bodies, tested: [] };
|
|
82
|
+
const kept = bodies.filter((body) => stepsOnPath(body, callee).every((step) => {
|
|
83
|
+
if (step.kind !== 'branch')
|
|
84
|
+
return true;
|
|
85
|
+
const decided = evaluate(step.condition, binding);
|
|
86
|
+
return decided === undefined || decided === step.whenTrue;
|
|
87
|
+
}));
|
|
88
|
+
const tested = testedParameters(callee, binding);
|
|
89
|
+
return { kept, tested: tested.length > 0 ? sharedModes(callee, call, binding, tested) : [] };
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The parameters of `tested` some other call of `callee` states differently:
|
|
93
|
+
* another value, or nothing this reading can name. A parameter every call
|
|
94
|
+
* states the same way picks nothing among the callers, so it is no mode.
|
|
95
|
+
*/
|
|
96
|
+
function sharedModes(callee, call, binding, tested) {
|
|
97
|
+
const name = calleeName(callee);
|
|
98
|
+
if (!name)
|
|
99
|
+
return [];
|
|
100
|
+
const own = statedByName(binding);
|
|
101
|
+
const differing = new Set();
|
|
102
|
+
for (const reference of name.findReferencesAsNodes()) {
|
|
103
|
+
const candidate = reference.getFirstAncestorByKind(SyntaxKind.CallExpression);
|
|
104
|
+
if (!candidate || candidate === call || calledFunction(candidate) !== callee)
|
|
105
|
+
continue;
|
|
106
|
+
const theirs = statedByName(bindArguments(callee, candidate));
|
|
107
|
+
for (const parameter of tested) {
|
|
108
|
+
if (theirs.get(parameter) === undefined || theirs.get(parameter) !== own.get(parameter))
|
|
109
|
+
differing.add(parameter);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return tested.filter((parameter) => differing.has(parameter));
|
|
113
|
+
}
|
|
114
|
+
/** The identifier that names `callee` where it is declared. */
|
|
115
|
+
function calleeName(callee) {
|
|
116
|
+
if (Node.isFunctionDeclaration(callee))
|
|
117
|
+
return callee.getNameNode();
|
|
118
|
+
if (Node.isMethodDeclaration(callee)) {
|
|
119
|
+
const name = callee.getNameNode();
|
|
120
|
+
return Node.isIdentifier(name) ? name : undefined;
|
|
121
|
+
}
|
|
122
|
+
const holder = callee.getParent();
|
|
123
|
+
if (holder && (Node.isVariableDeclaration(holder) || Node.isPropertyAssignment(holder))) {
|
|
124
|
+
const name = holder.getNameNode();
|
|
125
|
+
return Node.isIdentifier(name) ? name : undefined;
|
|
126
|
+
}
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
/** Each stated value of a binding, as text, by the name the executor reads it under. */
|
|
130
|
+
function statedByName(binding) {
|
|
131
|
+
const byName = new Map();
|
|
132
|
+
const entries = [...binding.direct.values(), ...[...binding.members.values()].flatMap((m) => [...m.values()])];
|
|
133
|
+
for (const entry of entries) {
|
|
134
|
+
byName.set(entry.name, entry.stated.kind === 'defined' ? 'defined' : `value:${String(entry.stated.value)}:${typeof entry.stated.value}`);
|
|
135
|
+
}
|
|
136
|
+
return byName;
|
|
137
|
+
}
|
|
138
|
+
function bindArguments(callee, call) {
|
|
139
|
+
const binding = { direct: new Map(), members: new Map() };
|
|
140
|
+
const stated = binding.direct;
|
|
141
|
+
if (!Node.isFunctionLikeDeclaration(callee) && !Node.isArrowFunction(callee) && !Node.isFunctionExpression(callee)) {
|
|
142
|
+
return binding;
|
|
143
|
+
}
|
|
144
|
+
const args = call.getArguments();
|
|
145
|
+
if (args.some((arg) => Node.isSpreadElement(arg)))
|
|
146
|
+
return binding;
|
|
147
|
+
const parameters = callee.getParameters();
|
|
148
|
+
parameters.forEach((parameter, index) => {
|
|
149
|
+
if (parameter.isRestParameter())
|
|
150
|
+
return;
|
|
151
|
+
const arg = args[index];
|
|
152
|
+
const name = parameter.getNameNode();
|
|
153
|
+
if (Node.isIdentifier(name)) {
|
|
154
|
+
const symbol = name.getSymbol();
|
|
155
|
+
if (!symbol || assignedIn(callee, symbol))
|
|
156
|
+
return;
|
|
157
|
+
const value = arg === undefined ? (parameter.hasInitializer() ? undefined : UNDEFINED) : statedBy(arg);
|
|
158
|
+
if (value)
|
|
159
|
+
stated.set(symbol, { name: name.getText(), stated: value });
|
|
160
|
+
if (arg === undefined)
|
|
161
|
+
return;
|
|
162
|
+
// Members read through `param.member` or `const { member } = param`.
|
|
163
|
+
const byName = new Map();
|
|
164
|
+
for (const member of memberAccessesOf(callee, symbol)) {
|
|
165
|
+
const memberValue = statedMember(arg, member);
|
|
166
|
+
if (memberValue)
|
|
167
|
+
byName.set(member, { name: member, stated: memberValue });
|
|
168
|
+
}
|
|
169
|
+
if (byName.size > 0)
|
|
170
|
+
binding.members.set(symbol, byName);
|
|
171
|
+
for (const [member, elementSymbol] of destructuredFrom(callee, symbol)) {
|
|
172
|
+
if (assignedIn(callee, elementSymbol))
|
|
173
|
+
continue;
|
|
174
|
+
const memberValue = statedMember(arg, member);
|
|
175
|
+
if (memberValue)
|
|
176
|
+
stated.set(elementSymbol, { name: member, stated: memberValue });
|
|
177
|
+
}
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (Node.isObjectBindingPattern(name) && arg !== undefined) {
|
|
181
|
+
for (const element of name.getElements()) {
|
|
182
|
+
if (element.getInitializer() || element.getDotDotDotToken())
|
|
183
|
+
continue;
|
|
184
|
+
const member = element.getPropertyNameNode()?.getText() ?? element.getName();
|
|
185
|
+
const symbol = element.getNameNode().getSymbol();
|
|
186
|
+
if (!symbol || assignedIn(callee, symbol))
|
|
187
|
+
continue;
|
|
188
|
+
const memberValue = statedMember(arg, member);
|
|
189
|
+
if (memberValue)
|
|
190
|
+
stated.set(symbol, { name: member, stated: memberValue });
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
return binding;
|
|
195
|
+
}
|
|
196
|
+
const UNDEFINED = { kind: 'value', value: undefined };
|
|
197
|
+
/** What one argument expression states. */
|
|
198
|
+
function statedBy(arg) {
|
|
199
|
+
const expression = peel(arg);
|
|
200
|
+
if (Node.isStringLiteral(expression) || Node.isNoSubstitutionTemplateLiteral(expression)) {
|
|
201
|
+
return { kind: 'value', value: expression.getLiteralValue() };
|
|
202
|
+
}
|
|
203
|
+
if (Node.isNumericLiteral(expression))
|
|
204
|
+
return { kind: 'value', value: expression.getLiteralValue() };
|
|
205
|
+
if (expression.getKind() === SyntaxKind.TrueKeyword)
|
|
206
|
+
return { kind: 'value', value: true };
|
|
207
|
+
if (expression.getKind() === SyntaxKind.FalseKeyword)
|
|
208
|
+
return { kind: 'value', value: false };
|
|
209
|
+
if (expression.getKind() === SyntaxKind.NullKeyword)
|
|
210
|
+
return { kind: 'value', value: null };
|
|
211
|
+
if (Node.isIdentifier(expression) && expression.getText() === 'undefined')
|
|
212
|
+
return UNDEFINED;
|
|
213
|
+
const type = expression.getType();
|
|
214
|
+
if (type.isAny() || type.isUnknown())
|
|
215
|
+
return undefined;
|
|
216
|
+
const members = type.isUnion() ? type.getUnionTypes() : [type];
|
|
217
|
+
if (members.some((member) => member.isNull() || member.isUndefined() || member.isVoid()))
|
|
218
|
+
return undefined;
|
|
219
|
+
return { kind: 'defined' };
|
|
220
|
+
}
|
|
221
|
+
/** What an argument states about one of its members, when it is an object literal. */
|
|
222
|
+
function statedMember(arg, member) {
|
|
223
|
+
const literal = peel(arg);
|
|
224
|
+
if (!Node.isObjectLiteralExpression(literal))
|
|
225
|
+
return undefined;
|
|
226
|
+
let found;
|
|
227
|
+
let spread = false;
|
|
228
|
+
for (const property of literal.getProperties()) {
|
|
229
|
+
if (Node.isSpreadAssignment(property)) {
|
|
230
|
+
spread = true;
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (Node.isPropertyAssignment(property) && property.getNameNode().getText() === member) {
|
|
234
|
+
if (Node.isComputedPropertyName(property.getNameNode()))
|
|
235
|
+
return undefined;
|
|
236
|
+
found = statedBy(property.getInitializerOrThrow());
|
|
237
|
+
}
|
|
238
|
+
else if (Node.isShorthandPropertyAssignment(property) && property.getName() === member) {
|
|
239
|
+
found = statedBy(property.getNameNode());
|
|
240
|
+
}
|
|
241
|
+
else if ((Node.isPropertyAssignment(property) && Node.isComputedPropertyName(property.getNameNode())) ||
|
|
242
|
+
Node.isGetAccessorDeclaration(property)) {
|
|
243
|
+
// A member this reading cannot name could be the one asked for.
|
|
244
|
+
if (found === undefined)
|
|
245
|
+
spread = true;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
// A later spread could overwrite an earlier member, and could hold one left out.
|
|
249
|
+
if (spread)
|
|
250
|
+
return undefined;
|
|
251
|
+
return found ?? UNDEFINED;
|
|
252
|
+
}
|
|
253
|
+
/** The bindings a destructuring `const` of the parameter `symbol` declares, by member name. */
|
|
254
|
+
function destructuredFrom(callee, symbol) {
|
|
255
|
+
const read = [];
|
|
256
|
+
for (const declaration of callee.getDescendantsOfKind(SyntaxKind.VariableDeclaration)) {
|
|
257
|
+
const initializer = declaration.getInitializer();
|
|
258
|
+
const pattern = declaration.getNameNode();
|
|
259
|
+
if (!initializer || !Node.isIdentifier(initializer) || initializer.getSymbol() !== symbol)
|
|
260
|
+
continue;
|
|
261
|
+
if (!Node.isObjectBindingPattern(pattern))
|
|
262
|
+
continue;
|
|
263
|
+
const list = declaration.getParent();
|
|
264
|
+
if (!list || !Node.isVariableDeclarationList(list) || list.getDeclarationKind() !== 'const')
|
|
265
|
+
continue;
|
|
266
|
+
for (const element of pattern.getElements()) {
|
|
267
|
+
if (element.getInitializer() || element.getDotDotDotToken())
|
|
268
|
+
continue;
|
|
269
|
+
const name = element.getNameNode();
|
|
270
|
+
if (!Node.isIdentifier(name))
|
|
271
|
+
continue;
|
|
272
|
+
const elementSymbol = name.getSymbol();
|
|
273
|
+
if (elementSymbol)
|
|
274
|
+
read.push([element.getPropertyNameNode()?.getText() ?? element.getName(), elementSymbol]);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return read;
|
|
278
|
+
}
|
|
279
|
+
/** The member names read as `param.member` on the parameter `symbol`; none when any is written. */
|
|
280
|
+
function memberAccessesOf(callee, symbol) {
|
|
281
|
+
const names = new Set();
|
|
282
|
+
for (const access of callee.getDescendantsOfKind(SyntaxKind.PropertyAccessExpression)) {
|
|
283
|
+
const object = access.getExpression();
|
|
284
|
+
if (!Node.isIdentifier(object) || object.getSymbol() !== symbol)
|
|
285
|
+
continue;
|
|
286
|
+
if (isWritten(access))
|
|
287
|
+
return [];
|
|
288
|
+
names.add(access.getName());
|
|
289
|
+
}
|
|
290
|
+
return [...names];
|
|
291
|
+
}
|
|
292
|
+
/** True when `symbol` is assigned to, or updated, anywhere in `scope`. */
|
|
293
|
+
function assignedIn(scope, symbol) {
|
|
294
|
+
return scope
|
|
295
|
+
.getDescendantsOfKind(SyntaxKind.Identifier)
|
|
296
|
+
.some((identifier) => identifier.getSymbol() === symbol && isWritten(identifier));
|
|
297
|
+
}
|
|
298
|
+
function isWritten(node) {
|
|
299
|
+
const parent = node.getParent();
|
|
300
|
+
if (!parent)
|
|
301
|
+
return false;
|
|
302
|
+
if (Node.isBinaryExpression(parent) && parent.getLeft() === node) {
|
|
303
|
+
const operator = parent.getOperatorToken().getKind();
|
|
304
|
+
return operator >= SyntaxKind.FirstAssignment && operator <= SyntaxKind.LastAssignment;
|
|
305
|
+
}
|
|
306
|
+
if (Node.isPrefixUnaryExpression(parent) || Node.isPostfixUnaryExpression(parent)) {
|
|
307
|
+
const operator = parent.getOperatorToken();
|
|
308
|
+
return operator === SyntaxKind.PlusPlusToken || operator === SyntaxKind.MinusMinusToken;
|
|
309
|
+
}
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
function peel(node) {
|
|
313
|
+
let current = node;
|
|
314
|
+
while (Node.isParenthesizedExpression(current) ||
|
|
315
|
+
Node.isAsExpression(current) ||
|
|
316
|
+
Node.isNonNullExpression(current) ||
|
|
317
|
+
Node.isSatisfiesExpression(current)) {
|
|
318
|
+
current = current.getExpression();
|
|
319
|
+
}
|
|
320
|
+
return current;
|
|
321
|
+
}
|
|
322
|
+
/** The entry a test operand reads, when it reads a stated value. */
|
|
323
|
+
function entryOf(node, binding) {
|
|
324
|
+
const operand = peel(node);
|
|
325
|
+
if (Node.isIdentifier(operand)) {
|
|
326
|
+
const symbol = operand.getSymbol();
|
|
327
|
+
return symbol ? binding.direct.get(symbol) : undefined;
|
|
328
|
+
}
|
|
329
|
+
if (Node.isPropertyAccessExpression(operand)) {
|
|
330
|
+
const object = peel(operand.getExpression());
|
|
331
|
+
const symbol = Node.isIdentifier(object) ? object.getSymbol() : undefined;
|
|
332
|
+
return symbol ? binding.members.get(symbol)?.get(operand.getName()) : undefined;
|
|
333
|
+
}
|
|
334
|
+
return undefined;
|
|
335
|
+
}
|
|
336
|
+
function statedOperand(node, binding) {
|
|
337
|
+
return entryOf(node, binding)?.stated;
|
|
338
|
+
}
|
|
339
|
+
/** The literal a test operand is, when it is one. */
|
|
340
|
+
function literalOperand(node) {
|
|
341
|
+
const operand = peel(node);
|
|
342
|
+
if (Node.isVoidExpression(operand))
|
|
343
|
+
return UNDEFINED;
|
|
344
|
+
if (Node.isStringLiteral(operand) ||
|
|
345
|
+
Node.isNoSubstitutionTemplateLiteral(operand) ||
|
|
346
|
+
Node.isNumericLiteral(operand) ||
|
|
347
|
+
operand.getKind() === SyntaxKind.TrueKeyword ||
|
|
348
|
+
operand.getKind() === SyntaxKind.FalseKeyword ||
|
|
349
|
+
operand.getKind() === SyntaxKind.NullKeyword ||
|
|
350
|
+
(Node.isIdentifier(operand) && operand.getText() === 'undefined')) {
|
|
351
|
+
return statedBy(operand);
|
|
352
|
+
}
|
|
353
|
+
return undefined;
|
|
354
|
+
}
|
|
355
|
+
/** What a stated value compared with a literal gives, or `undefined` when undecided. */
|
|
356
|
+
function compare(stated, literal, loose) {
|
|
357
|
+
if (literal.kind !== 'value')
|
|
358
|
+
return undefined;
|
|
359
|
+
if (stated.kind === 'defined') {
|
|
360
|
+
// Defined means neither null nor undefined; any other literal could match.
|
|
361
|
+
return literal.value === null || literal.value === undefined ? false : undefined;
|
|
362
|
+
}
|
|
363
|
+
if (loose && (stated.value === null || stated.value === undefined)) {
|
|
364
|
+
return literal.value === null || literal.value === undefined;
|
|
365
|
+
}
|
|
366
|
+
return stated.value === literal.value;
|
|
367
|
+
}
|
|
368
|
+
/** True, false, or `undefined` when the stated arguments do not decide `condition`. */
|
|
369
|
+
function evaluate(condition, binding) {
|
|
370
|
+
const test = peel(condition);
|
|
371
|
+
if (Node.isPrefixUnaryExpression(test) && test.getOperatorToken() === SyntaxKind.ExclamationToken) {
|
|
372
|
+
const inner = evaluate(test.getOperand(), binding);
|
|
373
|
+
return inner === undefined ? undefined : !inner;
|
|
374
|
+
}
|
|
375
|
+
if (Node.isBinaryExpression(test)) {
|
|
376
|
+
const operator = test.getOperatorToken().getKind();
|
|
377
|
+
if (operator === SyntaxKind.AmpersandAmpersandToken || operator === SyntaxKind.BarBarToken) {
|
|
378
|
+
const left = evaluate(test.getLeft(), binding);
|
|
379
|
+
const right = evaluate(test.getRight(), binding);
|
|
380
|
+
if (operator === SyntaxKind.AmpersandAmpersandToken) {
|
|
381
|
+
if (left === false || right === false)
|
|
382
|
+
return false;
|
|
383
|
+
return left === true && right === true ? true : undefined;
|
|
384
|
+
}
|
|
385
|
+
if (left === true || right === true)
|
|
386
|
+
return true;
|
|
387
|
+
return left === false && right === false ? false : undefined;
|
|
388
|
+
}
|
|
389
|
+
const equality = operator === SyntaxKind.EqualsEqualsEqualsToken || operator === SyntaxKind.EqualsEqualsToken;
|
|
390
|
+
const inequality = operator === SyntaxKind.ExclamationEqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken;
|
|
391
|
+
if (!equality && !inequality)
|
|
392
|
+
return undefined;
|
|
393
|
+
const loose = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken;
|
|
394
|
+
const pairs = [
|
|
395
|
+
[test.getLeft(), test.getRight()],
|
|
396
|
+
[test.getRight(), test.getLeft()],
|
|
397
|
+
];
|
|
398
|
+
for (const [maybeStated, maybeLiteral] of pairs) {
|
|
399
|
+
const stated = statedOperand(maybeStated, binding);
|
|
400
|
+
const literal = literalOperand(maybeLiteral);
|
|
401
|
+
if (!stated || !literal)
|
|
402
|
+
continue;
|
|
403
|
+
const equal = compare(stated, literal, loose);
|
|
404
|
+
if (equal === undefined)
|
|
405
|
+
return undefined;
|
|
406
|
+
return equality ? equal : !equal;
|
|
407
|
+
}
|
|
408
|
+
return undefined;
|
|
409
|
+
}
|
|
410
|
+
const stated = statedOperand(test, binding);
|
|
411
|
+
if (stated?.kind === 'value')
|
|
412
|
+
return Boolean(stated.value);
|
|
413
|
+
return undefined;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Every stated parameter a test anywhere in `callee` reads as a mode: a
|
|
417
|
+
* stated literal under any test, or a value stated as defined under a test
|
|
418
|
+
* that value decides.
|
|
419
|
+
*/
|
|
420
|
+
function testedParameters(callee, binding) {
|
|
421
|
+
const names = new Set();
|
|
422
|
+
const conditions = [];
|
|
423
|
+
callee.forEachDescendant((node) => {
|
|
424
|
+
if (Node.isIfStatement(node))
|
|
425
|
+
conditions.push(node.getExpression());
|
|
426
|
+
else if (Node.isConditionalExpression(node))
|
|
427
|
+
conditions.push(node.getCondition());
|
|
428
|
+
else if (Node.isSwitchStatement(node))
|
|
429
|
+
conditions.push(node.getExpression());
|
|
430
|
+
});
|
|
431
|
+
for (const condition of conditions) {
|
|
432
|
+
for (const node of [condition, ...condition.getDescendants()]) {
|
|
433
|
+
// A stated literal is a mode wherever a test reads it; a value stated
|
|
434
|
+
// only as defined is one where a test it decides reads it.
|
|
435
|
+
const entry = entryOf(node, binding);
|
|
436
|
+
if (entry?.stated.kind === 'value')
|
|
437
|
+
names.add(entry.name);
|
|
438
|
+
if (Node.isBinaryExpression(node) && evaluate(node, binding) !== undefined) {
|
|
439
|
+
for (const operand of [node.getLeft(), node.getRight()]) {
|
|
440
|
+
const compared = entryOf(operand, binding);
|
|
441
|
+
if (compared)
|
|
442
|
+
names.add(compared.name);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return [...names].sort();
|
|
448
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A route registered once per element of a constant list (carrick#1029).
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* for (const [path, key] of [['a', 'alpha'], ['b', 'beta']] as const) {
|
|
6
|
+
* app.get(`/items/${path}`, async () => ({ items: rows()[key] }));
|
|
7
|
+
* }
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* One handler serves two operations, `/items/a` and `/items/b`. Typed as
|
|
11
|
+
* written, `key` is `'alpha' | 'beta'`, so each operation's response is the
|
|
12
|
+
* union of both payloads, and a consumer of `/items/a` reads as mismatched.
|
|
13
|
+
*
|
|
14
|
+
* The scanner sends the row's own path (`route_path`) on a route row's
|
|
15
|
+
* request when its line serves more than one operation. This reads, from the
|
|
16
|
+
* syntax and the compiler's symbols only, which element of the list the path
|
|
17
|
+
* names, and returns the rewrite that makes the list that one element. The
|
|
18
|
+
* inferrer then types the same handler in the rewritten file: the compiler
|
|
19
|
+
* infers the response for that element, and no rule of ours decides it.
|
|
20
|
+
*
|
|
21
|
+
* Read only when every piece is stated:
|
|
22
|
+
*
|
|
23
|
+
* - the registration's path argument is a template whose every hole is a name
|
|
24
|
+
* one loop binds: a `for (const … of LIST)` or `LIST.forEach((…) => …)`
|
|
25
|
+
* around the registration, binding a name or an array pattern of names;
|
|
26
|
+
* - `LIST` is an array literal (through parentheses, `as const` and
|
|
27
|
+
* `satisfies`), or a `const` bound to one that nothing in its file mutates,
|
|
28
|
+
* or that is `as const`;
|
|
29
|
+
* - every element is a string, or an array of strings at the positions the
|
|
30
|
+
* holes read, with no spread;
|
|
31
|
+
* - exactly one element's path is the row's path, segment by segment from
|
|
32
|
+
* the end, a parameter matching a parameter of any name.
|
|
33
|
+
*
|
|
34
|
+
* Anything else returns `undefined`, and the request is typed as before.
|
|
35
|
+
*/
|
|
36
|
+
import { Node, type SourceFile } from 'ts-morph';
|
|
37
|
+
/** The rewrite that leaves only the picked element in the list. */
|
|
38
|
+
export interface LoopPick {
|
|
39
|
+
file: SourceFile;
|
|
40
|
+
/** The list expression's span in the file's original text. */
|
|
41
|
+
start: number;
|
|
42
|
+
end: number;
|
|
43
|
+
/** What replaces it. Holds no more newlines than the text it replaces. */
|
|
44
|
+
text: string;
|
|
45
|
+
/** Which element, of how many. */
|
|
46
|
+
index: number;
|
|
47
|
+
of: number;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The element of the list `registration` is made once for that `routePath`
|
|
51
|
+
* names, or `undefined` when the code does not state it.
|
|
52
|
+
*/
|
|
53
|
+
export declare function loopElementPick(registration: Node, routePath: string): LoopPick | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Whether `full` ends with `own`, segment by segment: the row's path holds
|
|
56
|
+
* the route's own path after whatever prefix its mounts add.
|
|
57
|
+
*/
|
|
58
|
+
export declare function endsWithPath(full: string, own: string): boolean;
|