carrick 0.3.161 → 0.3.163
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/dist/contract.d.ts +8 -0
- package/dist/contract.js.map +1 -1
- package/dist/facts/store.d.ts +12 -0
- package/dist/facts/store.js +15 -0
- package/dist/facts/store.js.map +1 -1
- package/dist/held/daemon.d.ts +8 -0
- package/dist/held/daemon.js.map +1 -1
- package/dist/held/early.d.ts +1 -1
- package/dist/held/early.js +89 -11
- package/dist/held/early.js.map +1 -1
- package/dist/held/message-rows.d.ts +3 -0
- package/dist/held/message-rows.js +41 -0
- package/dist/held/message-rows.js.map +1 -0
- package/dist/held/place.d.ts +11 -2
- package/dist/held/place.js +4 -1
- package/dist/held/place.js.map +1 -1
- package/dist/held/runtime.d.ts +1 -1
- package/dist/held/runtime.js +75 -17
- package/dist/held/runtime.js.map +1 -1
- package/dist/held/shadow.d.ts +2 -0
- package/dist/held/shadow.js.map +1 -1
- package/dist/hook/confirm.js +40 -21
- package/dist/hook/confirm.js.map +1 -1
- package/dist/init/codex.d.ts +1 -1
- package/dist/init/codex.js +8 -4
- package/dist/init/codex.js.map +1 -1
- package/dist/init/doctor.d.ts +10 -13
- package/dist/init/doctor.js +26 -92
- package/dist/init/doctor.js.map +1 -1
- package/dist/init/launcher.d.ts +35 -0
- package/dist/init/launcher.js +85 -0
- package/dist/init/launcher.js.map +1 -0
- package/dist/init/outdated.d.ts +3 -4
- package/dist/init/outdated.js +19 -8
- package/dist/init/outdated.js.map +1 -1
- package/dist/init/remove.js +4 -0
- package/dist/init/remove.js.map +1 -1
- package/dist/init/repo-copies.d.ts +1 -1
- package/dist/init/repo-copies.js +13 -4
- package/dist/init/repo-copies.js.map +1 -1
- package/dist/init/run.js +17 -26
- package/dist/init/run.js.map +1 -1
- package/dist/init/self-update.js +7 -4
- package/dist/init/self-update.js.map +1 -1
- package/dist/init/settings.d.ts +0 -34
- package/dist/init/settings.js +11 -45
- package/dist/init/settings.js.map +1 -1
- package/dist/init/task-skills.d.ts +1 -1
- package/dist/init/task-skills.js +1 -0
- package/dist/init/task-skills.js.map +1 -1
- package/dist/render.d.ts +6 -0
- package/dist/render.js +27 -7
- package/dist/render.js.map +1 -1
- package/package.json +6 -6
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/hooks/carrick-hook.sh +122 -0
- package/plugin/hooks/hooks.json +8 -8
- package/sidecar/dist/src/capture/anchors.d.ts +2 -2
- package/sidecar/dist/src/capture/anchors.js +25 -10
- package/sidecar/dist/src/capture/api.d.ts +10 -0
- package/sidecar/dist/src/capture/check-classify.d.ts +11 -4
- package/sidecar/dist/src/capture/check-classify.js +65 -25
- package/sidecar/dist/src/capture/check-fields.d.ts +2 -1
- package/sidecar/dist/src/capture/check-fields.js +10 -2
- package/sidecar/dist/src/capture/check-probe.d.ts +82 -25
- package/sidecar/dist/src/capture/check-probe.js +106 -37
- package/sidecar/dist/src/capture/index.d.ts +3 -2
- package/sidecar/dist/src/capture/index.js +14 -2
- package/sidecar/dist/src/capture/reply-status.d.ts +86 -0
- package/sidecar/dist/src/capture/reply-status.js +255 -0
- package/sidecar/dist/src/capture/site-node.d.ts +34 -0
- package/sidecar/dist/src/capture/site-node.js +258 -0
- package/sidecar/dist/src/engine-stamp.json +1 -1
- package/sidecar/dist/src/held-shadow.d.ts +23 -2
- package/sidecar/dist/src/held-shadow.js +42 -8
- package/sidecar/dist/src/stated-params.d.ts +3 -2
- package/sidecar/dist/src/type-inferrer.d.ts +91 -55
- package/sidecar/dist/src/type-inferrer.js +350 -218
- package/sidecar/dist/src/types.d.ts +10 -0
- package/sidecar/dist/src/validators.d.ts +150 -28
- package/sidecar/dist/src/validators.js +15 -1
- package/templates/carrick-hook.sh +122 -0
- package/templates/skills/carrick-audit.md +97 -0
- package/templates/skills/carrick-census.md +73 -87
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a reply's HTTP status says about its body (carrick#1161), read off the
|
|
3
|
+
* compiler's own API so every path that holds a located reply decides it the
|
|
4
|
+
* same way.
|
|
5
|
+
*
|
|
6
|
+
* `success` (below 300) and `undecided` (no status stated anywhere) send the
|
|
7
|
+
* route's contract; `error` (400 and up) and `redirect` (3xx) do not; and
|
|
8
|
+
* `variable` is a status the source computes, which fails open into the union
|
|
9
|
+
* so no success body is ever guessed away.
|
|
10
|
+
*
|
|
11
|
+
* Inputs are a checker and a node. Nothing here consults a method, a
|
|
12
|
+
* framework or a library name: the status is read from an argument past the
|
|
13
|
+
* body, from an earlier call of the same chain, or from the call's result
|
|
14
|
+
* type. Whether a call IS a reply stays with each caller.
|
|
15
|
+
*/
|
|
16
|
+
import ts from 'typescript';
|
|
17
|
+
export type ReplyStatus = 'success' | 'error' | 'redirect' | 'variable' | 'undecided';
|
|
18
|
+
/**
|
|
19
|
+
* Members an options object uses to state an HTTP status. A >= 400 status marks
|
|
20
|
+
* the branch an error path, whose shape is not the endpoint's contract.
|
|
21
|
+
*/
|
|
22
|
+
export declare const STATUS_MEMBER_NAMES: readonly ["status", "statusCode"];
|
|
23
|
+
/** An error or redirect reply carries no body the route's contract describes. */
|
|
24
|
+
export declare function isDroppedReplyStatus(status: ReplyStatus): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* The status codes a TYPE fixes: a numeric literal, or a union made only of
|
|
27
|
+
* them, every one in the HTTP range. `undefined` for anything else, including
|
|
28
|
+
* a plain `number`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function statusCodesOfType(type: ts.Type): number[] | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* The HTTP status codes an argument states, `'variable'` when it is a
|
|
33
|
+
* status-shaped value the source does not fix, or `undefined` when it says
|
|
34
|
+
* nothing about a status.
|
|
35
|
+
*
|
|
36
|
+
* Read from the AST first: `{ status: 400 }` in an argument position widens
|
|
37
|
+
* to `{ status: number }`, so the literal only survives syntactically. The
|
|
38
|
+
* type read behind it catches `as const`, hoisted option objects and
|
|
39
|
+
* literal-typed variables. Only values in the HTTP range count: an
|
|
40
|
+
* arbitrary number named `status` on a domain object (`{ status: 2 }`)
|
|
41
|
+
* states nothing about transport.
|
|
42
|
+
*/
|
|
43
|
+
export declare function statedStatusCodes(checker: ts.TypeChecker, node: ts.Node): number[] | 'variable' | undefined;
|
|
44
|
+
/** The status a value states, classified as a send's status argument is. */
|
|
45
|
+
export declare function valueStatus(checker: ts.TypeChecker, value: ts.Node | undefined): ReplyStatus;
|
|
46
|
+
/**
|
|
47
|
+
* The status an earlier call of a send's own chain states: the receiver
|
|
48
|
+
* calls `call` is invoked on (`a.status(401).json(body)` has `a.status(401)`),
|
|
49
|
+
* innermost last. Each receiver call's first argument is read by the rule
|
|
50
|
+
* an argument past a body is, so a numeric literal or a literal-typed value
|
|
51
|
+
* counts and a plain `number` is `variable`. `undefined` when no receiver
|
|
52
|
+
* call states a status. No method name is consulted: a receiver call that
|
|
53
|
+
* is handed no status-shaped value says nothing.
|
|
54
|
+
*/
|
|
55
|
+
export declare function chainStatus(checker: ts.TypeChecker, call: ts.Node): ReplyStatus | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Status codes a send call's result type records: the members (of the result,
|
|
58
|
+
* or of each part of an intersection result) typed as a status literal or a
|
|
59
|
+
* union of them. `undefined` when none is.
|
|
60
|
+
*/
|
|
61
|
+
export declare function resultTypeStatusCodes(checker: ts.TypeChecker, call: ts.Node): number[] | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* What a reply states about its HTTP status (carrick#1161).
|
|
64
|
+
*
|
|
65
|
+
* Read in order, and the first that states anything decides:
|
|
66
|
+
*
|
|
67
|
+
* 1. an argument past the body that carries a status: a numeric literal
|
|
68
|
+
* (`send(body, 404)`), an options object (`{ status: 401 }`), or any
|
|
69
|
+
* expression whose TYPE is a status literal or a union of them
|
|
70
|
+
* (`let code: 400 | 500`). A numeric argument whose type is a plain
|
|
71
|
+
* `number` states a status the source does not fix, which is
|
|
72
|
+
* `variable`: the branch fails open into the union and is logged.
|
|
73
|
+
* 2. the call's own chain, when `readChain` is set (carrick#2005):
|
|
74
|
+
* `reply.status(401).json(body)`.
|
|
75
|
+
* 3. the call's own RESULT type, when a member of it is typed as a status
|
|
76
|
+
* literal. A framework that types its sends records the status there, and
|
|
77
|
+
* a redirect helper's default `302` is only visible there. A result whose
|
|
78
|
+
* status member spans success and error codes (the default of a typed
|
|
79
|
+
* `json(body, status?)`) says nothing either way.
|
|
80
|
+
*
|
|
81
|
+
* A status in the FIRST argument is a field of the body and is never read.
|
|
82
|
+
* No method or framework name is consulted anywhere.
|
|
83
|
+
*/
|
|
84
|
+
export declare function replyStatus(checker: ts.TypeChecker, expression: ts.Node, opts: {
|
|
85
|
+
readChain: boolean;
|
|
86
|
+
}): ReplyStatus;
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a reply's HTTP status says about its body (carrick#1161), read off the
|
|
3
|
+
* compiler's own API so every path that holds a located reply decides it the
|
|
4
|
+
* same way.
|
|
5
|
+
*
|
|
6
|
+
* `success` (below 300) and `undecided` (no status stated anywhere) send the
|
|
7
|
+
* route's contract; `error` (400 and up) and `redirect` (3xx) do not; and
|
|
8
|
+
* `variable` is a status the source computes, which fails open into the union
|
|
9
|
+
* so no success body is ever guessed away.
|
|
10
|
+
*
|
|
11
|
+
* Inputs are a checker and a node. Nothing here consults a method, a
|
|
12
|
+
* framework or a library name: the status is read from an argument past the
|
|
13
|
+
* body, from an earlier call of the same chain, or from the call's result
|
|
14
|
+
* type. Whether a call IS a reply stays with each caller.
|
|
15
|
+
*/
|
|
16
|
+
import ts from 'typescript';
|
|
17
|
+
import { classifyStatusCodes, httpStatus } from './status-table.js';
|
|
18
|
+
/**
|
|
19
|
+
* Members an options object uses to state an HTTP status. A >= 400 status marks
|
|
20
|
+
* the branch an error path, whose shape is not the endpoint's contract.
|
|
21
|
+
*/
|
|
22
|
+
export const STATUS_MEMBER_NAMES = ['status', 'statusCode'];
|
|
23
|
+
/** An error or redirect reply carries no body the route's contract describes. */
|
|
24
|
+
export function isDroppedReplyStatus(status) {
|
|
25
|
+
return status === 'error' || status === 'redirect';
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The status codes a TYPE fixes: a numeric literal, or a union made only of
|
|
29
|
+
* them, every one in the HTTP range. `undefined` for anything else, including
|
|
30
|
+
* a plain `number`.
|
|
31
|
+
*/
|
|
32
|
+
export function statusCodesOfType(type) {
|
|
33
|
+
const members = type.flags & ts.TypeFlags.Union ? type.types : [type];
|
|
34
|
+
const codes = [];
|
|
35
|
+
for (const member of members) {
|
|
36
|
+
if (!(member.flags & ts.TypeFlags.NumberLiteral))
|
|
37
|
+
return undefined;
|
|
38
|
+
const code = httpStatus(member.value);
|
|
39
|
+
if (code === undefined)
|
|
40
|
+
return undefined;
|
|
41
|
+
codes.push(code);
|
|
42
|
+
}
|
|
43
|
+
return codes.length > 0 ? codes : undefined;
|
|
44
|
+
}
|
|
45
|
+
/** Peel parentheses and `await`; `as` and `satisfies` are kept. */
|
|
46
|
+
function peel(node) {
|
|
47
|
+
let current = node;
|
|
48
|
+
while (ts.isParenthesizedExpression(current) || ts.isAwaitExpression(current)) {
|
|
49
|
+
current = current.expression;
|
|
50
|
+
}
|
|
51
|
+
return current;
|
|
52
|
+
}
|
|
53
|
+
/** The value of the first object-literal member `name` written as a plain identifier. */
|
|
54
|
+
function propertyNamed(literal, name) {
|
|
55
|
+
return literal.properties.find((property) => property.name !== undefined && ts.isIdentifier(property.name) && property.name.text === name);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The HTTP status codes an argument states, `'variable'` when it is a
|
|
59
|
+
* status-shaped value the source does not fix, or `undefined` when it says
|
|
60
|
+
* nothing about a status.
|
|
61
|
+
*
|
|
62
|
+
* Read from the AST first: `{ status: 400 }` in an argument position widens
|
|
63
|
+
* to `{ status: number }`, so the literal only survives syntactically. The
|
|
64
|
+
* type read behind it catches `as const`, hoisted option objects and
|
|
65
|
+
* literal-typed variables. Only values in the HTTP range count: an
|
|
66
|
+
* arbitrary number named `status` on a domain object (`{ status: 2 }`)
|
|
67
|
+
* states nothing about transport.
|
|
68
|
+
*/
|
|
69
|
+
export function statedStatusCodes(checker, node) {
|
|
70
|
+
if (ts.isNumericLiteral(node)) {
|
|
71
|
+
const code = httpStatus(parseFloat(node.text));
|
|
72
|
+
return code === undefined ? undefined : [code];
|
|
73
|
+
}
|
|
74
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
75
|
+
for (const name of STATUS_MEMBER_NAMES) {
|
|
76
|
+
const property = propertyNamed(node, name);
|
|
77
|
+
if (!property || !ts.isPropertyAssignment(property))
|
|
78
|
+
continue;
|
|
79
|
+
const read = statedStatusCodes(checker, peel(property.initializer));
|
|
80
|
+
if (read)
|
|
81
|
+
return read;
|
|
82
|
+
}
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
const type = checker.getTypeAtLocation(node);
|
|
86
|
+
const fromType = statusCodesOfType(type);
|
|
87
|
+
if (fromType)
|
|
88
|
+
return fromType;
|
|
89
|
+
if (type.flags & ts.TypeFlags.Number)
|
|
90
|
+
return 'variable';
|
|
91
|
+
for (const name of STATUS_MEMBER_NAMES) {
|
|
92
|
+
const property = checker.getPropertyOfType(type, name);
|
|
93
|
+
const declaration = property?.declarations?.[0];
|
|
94
|
+
if (!property || !declaration)
|
|
95
|
+
continue;
|
|
96
|
+
const propertyType = checker.getNonNullableType(checker.getTypeOfSymbolAtLocation(property, declaration));
|
|
97
|
+
const codes = statusCodesOfType(propertyType);
|
|
98
|
+
if (codes)
|
|
99
|
+
return codes;
|
|
100
|
+
if (propertyType.flags & ts.TypeFlags.Number)
|
|
101
|
+
return 'variable';
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
/** The status a value states, classified as a send's status argument is. */
|
|
106
|
+
export function valueStatus(checker, value) {
|
|
107
|
+
if (!value)
|
|
108
|
+
return 'undecided';
|
|
109
|
+
const read = statedStatusCodes(checker, peel(value));
|
|
110
|
+
if (read === 'variable')
|
|
111
|
+
return 'variable';
|
|
112
|
+
if (!read)
|
|
113
|
+
return 'undecided';
|
|
114
|
+
const kind = classifyStatusCodes(read);
|
|
115
|
+
return kind === 'mixed' ? 'variable' : kind;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The status an earlier call of a send's own chain states: the receiver
|
|
119
|
+
* calls `call` is invoked on (`a.status(401).json(body)` has `a.status(401)`),
|
|
120
|
+
* innermost last. Each receiver call's first argument is read by the rule
|
|
121
|
+
* an argument past a body is, so a numeric literal or a literal-typed value
|
|
122
|
+
* counts and a plain `number` is `variable`. `undefined` when no receiver
|
|
123
|
+
* call states a status. No method name is consulted: a receiver call that
|
|
124
|
+
* is handed no status-shaped value says nothing.
|
|
125
|
+
*/
|
|
126
|
+
export function chainStatus(checker, call) {
|
|
127
|
+
if (!ts.isCallExpression(call))
|
|
128
|
+
return undefined;
|
|
129
|
+
let callee = call.expression;
|
|
130
|
+
while (ts.isPropertyAccessExpression(callee)) {
|
|
131
|
+
const receiver = peel(callee.expression);
|
|
132
|
+
if (!ts.isCallExpression(receiver))
|
|
133
|
+
return undefined;
|
|
134
|
+
const status = valueStatus(checker, receiver.arguments[0]);
|
|
135
|
+
if (status !== 'undecided')
|
|
136
|
+
return status;
|
|
137
|
+
callee = receiver.expression;
|
|
138
|
+
}
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* `Promise<X>` / `PromiseLike<X>` / the surfaced `Awaited<X>` alias, peeled to
|
|
143
|
+
* `X` by name. Any other type is returned unchanged.
|
|
144
|
+
*/
|
|
145
|
+
function unwrapPromise(checker, type) {
|
|
146
|
+
// Bounded recursion guard: a pathological self-referential alias must not
|
|
147
|
+
// loop. Real promise nesting is shallow; 16 is far more than enough.
|
|
148
|
+
let current = type;
|
|
149
|
+
for (let depth = 0; depth < 16; depth++) {
|
|
150
|
+
const symbolName = current.getSymbol()?.getName();
|
|
151
|
+
const aliasName = current.aliasSymbol?.getName();
|
|
152
|
+
if (symbolName === 'Promise' || symbolName === 'PromiseLike') {
|
|
153
|
+
const args = current.objectFlags & ts.ObjectFlags.Reference
|
|
154
|
+
? checker.getTypeArguments(current)
|
|
155
|
+
: [];
|
|
156
|
+
if (args.length === 1) {
|
|
157
|
+
current = args[0];
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
return current;
|
|
161
|
+
}
|
|
162
|
+
if (aliasName === 'Awaited') {
|
|
163
|
+
const aliasArgs = current.aliasTypeArguments ?? [];
|
|
164
|
+
if (aliasArgs.length === 1) {
|
|
165
|
+
current = aliasArgs[0];
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
return current;
|
|
169
|
+
}
|
|
170
|
+
return current;
|
|
171
|
+
}
|
|
172
|
+
return current;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Status codes a send call's result type records: the members (of the result,
|
|
176
|
+
* or of each part of an intersection result) typed as a status literal or a
|
|
177
|
+
* union of them. `undefined` when none is.
|
|
178
|
+
*/
|
|
179
|
+
export function resultTypeStatusCodes(checker, call) {
|
|
180
|
+
const result = unwrapPromise(checker, checker.getTypeAtLocation(call));
|
|
181
|
+
const parts = result.flags & ts.TypeFlags.Intersection ? result.types : [result];
|
|
182
|
+
const codes = [];
|
|
183
|
+
for (const part of parts) {
|
|
184
|
+
if (part.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown) || !(part.flags & ts.TypeFlags.Object)) {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
for (const property of checker.getPropertiesOfType(part)) {
|
|
188
|
+
const codesHere = statusCodesOfType(checker.getTypeOfSymbolAtLocation(property, call));
|
|
189
|
+
if (codesHere)
|
|
190
|
+
codes.push(...codesHere);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return codes.length > 0 ? codes : undefined;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* What a reply states about its HTTP status (carrick#1161).
|
|
197
|
+
*
|
|
198
|
+
* Read in order, and the first that states anything decides:
|
|
199
|
+
*
|
|
200
|
+
* 1. an argument past the body that carries a status: a numeric literal
|
|
201
|
+
* (`send(body, 404)`), an options object (`{ status: 401 }`), or any
|
|
202
|
+
* expression whose TYPE is a status literal or a union of them
|
|
203
|
+
* (`let code: 400 | 500`). A numeric argument whose type is a plain
|
|
204
|
+
* `number` states a status the source does not fix, which is
|
|
205
|
+
* `variable`: the branch fails open into the union and is logged.
|
|
206
|
+
* 2. the call's own chain, when `readChain` is set (carrick#2005):
|
|
207
|
+
* `reply.status(401).json(body)`.
|
|
208
|
+
* 3. the call's own RESULT type, when a member of it is typed as a status
|
|
209
|
+
* literal. A framework that types its sends records the status there, and
|
|
210
|
+
* a redirect helper's default `302` is only visible there. A result whose
|
|
211
|
+
* status member spans success and error codes (the default of a typed
|
|
212
|
+
* `json(body, status?)`) says nothing either way.
|
|
213
|
+
*
|
|
214
|
+
* A status in the FIRST argument is a field of the body and is never read.
|
|
215
|
+
* No method or framework name is consulted anywhere.
|
|
216
|
+
*/
|
|
217
|
+
export function replyStatus(checker, expression, opts) {
|
|
218
|
+
const call = peel(expression);
|
|
219
|
+
if (!ts.isCallExpression(call) && !ts.isNewExpression(call)) {
|
|
220
|
+
return 'undecided';
|
|
221
|
+
}
|
|
222
|
+
const args = (call.arguments ?? []).map((a) => peel(a));
|
|
223
|
+
const stated = [];
|
|
224
|
+
let variable = false;
|
|
225
|
+
for (const arg of args.slice(1)) {
|
|
226
|
+
const read = statedStatusCodes(checker, arg);
|
|
227
|
+
if (read === 'variable') {
|
|
228
|
+
variable = true;
|
|
229
|
+
}
|
|
230
|
+
else if (read) {
|
|
231
|
+
stated.push(...read);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (stated.length > 0) {
|
|
235
|
+
const kind = classifyStatusCodes(stated);
|
|
236
|
+
return kind === 'mixed' ? 'variable' : kind;
|
|
237
|
+
}
|
|
238
|
+
if (variable)
|
|
239
|
+
return 'variable';
|
|
240
|
+
// carrick#2005: a status an earlier call of the same chain states. Read only
|
|
241
|
+
// where the caller asks for it; the return walk does not read it yet
|
|
242
|
+
// (carrick#1962).
|
|
243
|
+
if (opts.readChain) {
|
|
244
|
+
const chained = chainStatus(checker, call);
|
|
245
|
+
if (chained)
|
|
246
|
+
return chained;
|
|
247
|
+
}
|
|
248
|
+
const typed = resultTypeStatusCodes(checker, call);
|
|
249
|
+
if (typed) {
|
|
250
|
+
const kind = classifyStatusCodes(typed);
|
|
251
|
+
if (kind !== 'mixed')
|
|
252
|
+
return kind;
|
|
253
|
+
}
|
|
254
|
+
return 'undecided';
|
|
255
|
+
}
|
|
@@ -137,6 +137,40 @@ export declare function exactLocatorText(text: string): string;
|
|
|
137
137
|
* A string literal reads the same in any quotes (carrick#2056).
|
|
138
138
|
*/
|
|
139
139
|
export declare function normalizeLocatorText(text: string): string;
|
|
140
|
+
/** The 1-based, inclusive lines of the handler a route request is for. */
|
|
141
|
+
export interface HandlerLines {
|
|
142
|
+
start_line: number;
|
|
143
|
+
end_line: number;
|
|
144
|
+
}
|
|
145
|
+
/** The nodes a value type can be read at: the capture's own target rule. */
|
|
146
|
+
export declare function isPreferredTarget(node: ts.Node): boolean;
|
|
147
|
+
/** A located node is the row's own, or why none is. */
|
|
148
|
+
export type HandlerAnchor = {
|
|
149
|
+
node: ts.Node;
|
|
150
|
+
} | {
|
|
151
|
+
reason: string;
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* The node a route row reads, given the node a text or line locator found
|
|
155
|
+
* (stale-position T2). One rule for the inferrer and the capture; a span
|
|
156
|
+
* locator is the scanner's own and is not checked.
|
|
157
|
+
*
|
|
158
|
+
* The row's own functions are the handler (the function on its stamped
|
|
159
|
+
* lines) and every function its return reaches (`functionsAnsweringFor`). A
|
|
160
|
+
* request or response body is the row's own anywhere inside one of them; a
|
|
161
|
+
* `function_return`, whose answer is the return of the function around the
|
|
162
|
+
* node, only when that function is one of them (so a `.map` callback's
|
|
163
|
+
* return is not).
|
|
164
|
+
*
|
|
165
|
+
* A node found elsewhere is looked for again by the locator's text (or its
|
|
166
|
+
* own) inside those functions, the handler first, then by hop: where every
|
|
167
|
+
* match gives one answer (one function's return; one type for a body), the
|
|
168
|
+
* first match is read, else the row abstains. Never the stale node, the
|
|
169
|
+
* registration or a function outside the reach set. With no function on the
|
|
170
|
+
* stamped lines a body keeps the line bound and a return abstains. The
|
|
171
|
+
* reason carries lines only, never a path.
|
|
172
|
+
*/
|
|
173
|
+
export declare function anchorInHandler(checker: ts.TypeChecker, sourceFile: ts.SourceFile, node: ts.Node, lines: HandlerLines, inferKind: string | undefined, expressionText: string | undefined): HandlerAnchor;
|
|
140
174
|
/**
|
|
141
175
|
* The capture's locator: the first node in pre-order that `accept`s, whose
|
|
142
176
|
* text equals the locator's (`exactLocatorText`), and that ends on or after
|
|
@@ -339,6 +339,264 @@ const LOCATOR_SEARCH_RADIUS = 5;
|
|
|
339
339
|
function startLine(sourceFile, node) {
|
|
340
340
|
return sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1;
|
|
341
341
|
}
|
|
342
|
+
function isFunctionNode(node) {
|
|
343
|
+
return (ts.isArrowFunction(node) ||
|
|
344
|
+
ts.isFunctionExpression(node) ||
|
|
345
|
+
ts.isFunctionDeclaration(node) ||
|
|
346
|
+
ts.isMethodDeclaration(node));
|
|
347
|
+
}
|
|
348
|
+
/** `node` itself or its nearest enclosing function. */
|
|
349
|
+
function functionAtOrAbove(node) {
|
|
350
|
+
let current = node;
|
|
351
|
+
while (current && !isFunctionNode(current))
|
|
352
|
+
current = current.parent;
|
|
353
|
+
return current;
|
|
354
|
+
}
|
|
355
|
+
/** The 1-based lines `node` starts and ends on. */
|
|
356
|
+
function linesOf(sourceFile, node) {
|
|
357
|
+
return {
|
|
358
|
+
start_line: startLine(sourceFile, node),
|
|
359
|
+
end_line: sourceFile.getLineAndCharacterOfPosition(node.getEnd()).line + 1,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Whether `fn` is handed to a call the way a registration hands in its
|
|
364
|
+
* handler: the last of at least two arguments (a path or a middleware, then
|
|
365
|
+
* the handler). A loop callback that registers (`kinds.forEach((k) =>
|
|
366
|
+
* app.get(...))`) or a transaction callback is a call's only argument.
|
|
367
|
+
*/
|
|
368
|
+
function handedInLast(fn) {
|
|
369
|
+
const call = fn.parent;
|
|
370
|
+
return (!!call &&
|
|
371
|
+
ts.isCallExpression(call) &&
|
|
372
|
+
call.arguments.length >= 2 &&
|
|
373
|
+
call.arguments[call.arguments.length - 1] === fn);
|
|
374
|
+
}
|
|
375
|
+
/** Every function in `sourceFile`, by the lines it starts and ends on. */
|
|
376
|
+
const functionsByLines = new WeakMap();
|
|
377
|
+
function linesKey(lines) {
|
|
378
|
+
return `${lines.start_line}:${lines.end_line}`;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* The function the scanner stamped `lines` for, among the functions in
|
|
382
|
+
* `sourceFile` that start and end on exactly those lines: the outermost one
|
|
383
|
+
* handed in last to a call of two or more arguments, as the registration
|
|
384
|
+
* hands in its handler, else the outermost of them. Several literals can
|
|
385
|
+
* share one line: a callback inside its handler, or a handler inside the
|
|
386
|
+
* loop callback that registers it. The file is walked once per source file.
|
|
387
|
+
*/
|
|
388
|
+
function handlerOnLines(sourceFile, lines) {
|
|
389
|
+
let index = functionsByLines.get(sourceFile);
|
|
390
|
+
if (!index) {
|
|
391
|
+
const built = new Map();
|
|
392
|
+
const visit = (node) => {
|
|
393
|
+
if (isFunctionNode(node)) {
|
|
394
|
+
const key = linesKey(linesOf(sourceFile, node));
|
|
395
|
+
const held = built.get(key);
|
|
396
|
+
if (held)
|
|
397
|
+
held.push(node);
|
|
398
|
+
else
|
|
399
|
+
built.set(key, [node]);
|
|
400
|
+
}
|
|
401
|
+
ts.forEachChild(node, visit);
|
|
402
|
+
};
|
|
403
|
+
ts.forEachChild(sourceFile, visit);
|
|
404
|
+
functionsByLines.set(sourceFile, built);
|
|
405
|
+
index = built;
|
|
406
|
+
}
|
|
407
|
+
const onLines = index.get(linesKey(lines)) ?? [];
|
|
408
|
+
return onLines.find(handedInLast) ?? onLines[0];
|
|
409
|
+
}
|
|
410
|
+
/** The expressions `fn` returns: its expression body, or its own `return`s. */
|
|
411
|
+
function returnedExpressions(fn) {
|
|
412
|
+
const body = fn.body;
|
|
413
|
+
if (!body)
|
|
414
|
+
return [];
|
|
415
|
+
if (!ts.isBlock(body))
|
|
416
|
+
return [body];
|
|
417
|
+
const returned = [];
|
|
418
|
+
const visit = (node) => {
|
|
419
|
+
if (ts.isFunctionLike(node) || ts.isClassLike(node))
|
|
420
|
+
return;
|
|
421
|
+
if (ts.isReturnStatement(node) && node.expression)
|
|
422
|
+
returned.push(node.expression);
|
|
423
|
+
ts.forEachChild(node, visit);
|
|
424
|
+
};
|
|
425
|
+
ts.forEachChild(body, visit);
|
|
426
|
+
return returned;
|
|
427
|
+
}
|
|
428
|
+
function unwrapTransparent(expression) {
|
|
429
|
+
let current = expression;
|
|
430
|
+
while (wrapsValueTransparently(current))
|
|
431
|
+
current = current.expression;
|
|
432
|
+
return current;
|
|
433
|
+
}
|
|
434
|
+
/** A type every type relates to both ways: no evidence two types are one. */
|
|
435
|
+
const VACUOUS = ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.Never | ts.TypeFlags.TypeParameter;
|
|
436
|
+
/** Whether the checker holds `a` and `b` for one type (each assignable to the other). */
|
|
437
|
+
function sameAnswer(checker, a, b) {
|
|
438
|
+
if (!a || !b || a.flags & VACUOUS || b.flags & VACUOUS)
|
|
439
|
+
return false;
|
|
440
|
+
return checker.isTypeAssignableTo(a, b) && checker.isTypeAssignableTo(b, a);
|
|
441
|
+
}
|
|
442
|
+
function awaitedReturn(checker, fn) {
|
|
443
|
+
const returned = checker.getSignatureFromDeclaration(fn)?.getReturnType();
|
|
444
|
+
return returned && (checker.getAwaitedType(returned) ?? returned);
|
|
445
|
+
}
|
|
446
|
+
/** Hops a handler's return is followed through calls (helper, wrapper, callback). */
|
|
447
|
+
const RETURN_REACH_HOPS = 3;
|
|
448
|
+
/**
|
|
449
|
+
* The functions whose return is `handler`'s own answer: the handler, and
|
|
450
|
+
* every function its return reaches. A function returns (directly, awaited or
|
|
451
|
+
* through a transparent wrapper) a call; that call reaches the declaration it
|
|
452
|
+
* resolves to, and each function literal passed to it, on any callee form,
|
|
453
|
+
* whose awaited return is the call's awaited type (the checker decides: each
|
|
454
|
+
* assignable to the other, neither `any`, `unknown`, `never` nor a type
|
|
455
|
+
* parameter). So `db.transaction(cb)` reaches `cb`, whose return is the
|
|
456
|
+
* call's result, and `items.map(cb)` does not, its result being a list of
|
|
457
|
+
* `cb`'s return.
|
|
458
|
+
*/
|
|
459
|
+
function functionsAnsweringFor(checker, handler) {
|
|
460
|
+
const held = reachSets.get(handler);
|
|
461
|
+
if (held && held.checker === checker)
|
|
462
|
+
return held.reached;
|
|
463
|
+
const reached = new Set([handler]);
|
|
464
|
+
let frontier = [handler];
|
|
465
|
+
for (let hop = 0; hop < RETURN_REACH_HOPS && frontier.length > 0; hop += 1) {
|
|
466
|
+
const next = [];
|
|
467
|
+
const reach = (fn) => {
|
|
468
|
+
if (!fn || !isFunctionNode(fn) || !fn.body || reached.has(fn))
|
|
469
|
+
return;
|
|
470
|
+
reached.add(fn);
|
|
471
|
+
next.push(fn);
|
|
472
|
+
};
|
|
473
|
+
for (const fn of frontier) {
|
|
474
|
+
for (const returned of returnedExpressions(fn)) {
|
|
475
|
+
const call = unwrapTransparent(returned);
|
|
476
|
+
if (!ts.isCallExpression(call))
|
|
477
|
+
continue;
|
|
478
|
+
reach(checker.getResolvedSignature(call)?.getDeclaration());
|
|
479
|
+
const result = checker.getTypeAtLocation(call);
|
|
480
|
+
const answered = checker.getAwaitedType(result) ?? result;
|
|
481
|
+
for (const argument of call.arguments.map(unwrapTransparent)) {
|
|
482
|
+
if ((ts.isArrowFunction(argument) || ts.isFunctionExpression(argument)) &&
|
|
483
|
+
sameAnswer(checker, awaitedReturn(checker, argument), answered)) {
|
|
484
|
+
reach(argument);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
frontier = next;
|
|
490
|
+
}
|
|
491
|
+
reachSets.set(handler, { checker, reached });
|
|
492
|
+
return reached;
|
|
493
|
+
}
|
|
494
|
+
/** Each handler's reach set, per checker (the inferrer and the capture hold two programs). */
|
|
495
|
+
const reachSets = new WeakMap();
|
|
496
|
+
/** The nodes a value type can be read at: the capture's own target rule. */
|
|
497
|
+
export function isPreferredTarget(node) {
|
|
498
|
+
return (ts.isExpression(node) ||
|
|
499
|
+
ts.isVariableDeclaration(node) ||
|
|
500
|
+
ts.isParameter(node) ||
|
|
501
|
+
ts.isPropertyAssignment(node));
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Whether `node` is a member or key name, not the value its text names:
|
|
505
|
+
* `payload` in `reply.payload`, `{ payload: true }` or a class or function
|
|
506
|
+
* member's name. A variable's or parameter's own name stays: it names the
|
|
507
|
+
* value the text does, and it is where the source states its type.
|
|
508
|
+
*/
|
|
509
|
+
function isMemberName(node) {
|
|
510
|
+
const parent = node.parent;
|
|
511
|
+
if (!parent)
|
|
512
|
+
return false;
|
|
513
|
+
return ((ts.isPropertyAccessExpression(parent) ||
|
|
514
|
+
ts.isPropertyAssignment(parent) ||
|
|
515
|
+
ts.isPropertyDeclaration(parent) ||
|
|
516
|
+
ts.isMethodDeclaration(parent) ||
|
|
517
|
+
ts.isFunctionDeclaration(parent)) &&
|
|
518
|
+
parent.name === node);
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* The node a route row reads, given the node a text or line locator found
|
|
522
|
+
* (stale-position T2). One rule for the inferrer and the capture; a span
|
|
523
|
+
* locator is the scanner's own and is not checked.
|
|
524
|
+
*
|
|
525
|
+
* The row's own functions are the handler (the function on its stamped
|
|
526
|
+
* lines) and every function its return reaches (`functionsAnsweringFor`). A
|
|
527
|
+
* request or response body is the row's own anywhere inside one of them; a
|
|
528
|
+
* `function_return`, whose answer is the return of the function around the
|
|
529
|
+
* node, only when that function is one of them (so a `.map` callback's
|
|
530
|
+
* return is not).
|
|
531
|
+
*
|
|
532
|
+
* A node found elsewhere is looked for again by the locator's text (or its
|
|
533
|
+
* own) inside those functions, the handler first, then by hop: where every
|
|
534
|
+
* match gives one answer (one function's return; one type for a body), the
|
|
535
|
+
* first match is read, else the row abstains. Never the stale node, the
|
|
536
|
+
* registration or a function outside the reach set. With no function on the
|
|
537
|
+
* stamped lines a body keeps the line bound and a return abstains. The
|
|
538
|
+
* reason carries lines only, never a path.
|
|
539
|
+
*/
|
|
540
|
+
export function anchorInHandler(checker, sourceFile, node, lines, inferKind, expressionText) {
|
|
541
|
+
const line = startLine(sourceFile, node);
|
|
542
|
+
const where = `the route's handler (lines ${lines.start_line}-${lines.end_line})`;
|
|
543
|
+
const returns = inferKind === 'function_return';
|
|
544
|
+
const handler = handlerOnLines(sourceFile, lines);
|
|
545
|
+
if (!handler) {
|
|
546
|
+
if (returns)
|
|
547
|
+
return { reason: `anchor_outside_handler: no function starts and ends on ${where}` };
|
|
548
|
+
return line >= lines.start_line && line <= lines.end_line
|
|
549
|
+
? { node }
|
|
550
|
+
: { reason: `anchor_outside_handler: the located node starts at line ${line}, outside ${where}` };
|
|
551
|
+
}
|
|
552
|
+
const reached = functionsAnsweringFor(checker, handler);
|
|
553
|
+
const ownFunction = (candidate) => {
|
|
554
|
+
if (returns) {
|
|
555
|
+
const around = functionAtOrAbove(candidate);
|
|
556
|
+
return around && reached.has(around) ? around : undefined;
|
|
557
|
+
}
|
|
558
|
+
for (let current = candidate; current; current = current.parent) {
|
|
559
|
+
if (isFunctionNode(current) && reached.has(current))
|
|
560
|
+
return current;
|
|
561
|
+
}
|
|
562
|
+
return undefined;
|
|
563
|
+
};
|
|
564
|
+
if (ownFunction(node))
|
|
565
|
+
return { node };
|
|
566
|
+
const outside = `anchor_outside_handler: the located node (line ${line}) is outside ${where} and ` +
|
|
567
|
+
'every function its return reaches';
|
|
568
|
+
const wanted = normalizeLocatorText(expressionText ?? node.getText(sourceFile));
|
|
569
|
+
if (!wanted)
|
|
570
|
+
return { reason: outside };
|
|
571
|
+
const matches = [];
|
|
572
|
+
const seen = new Set();
|
|
573
|
+
for (const fn of reached) {
|
|
574
|
+
const visit = (candidate) => {
|
|
575
|
+
if (!seen.has(candidate) &&
|
|
576
|
+
isPreferredTarget(candidate) &&
|
|
577
|
+
!isMemberName(candidate) &&
|
|
578
|
+
normalizeLocatorText(candidate.getText(sourceFile)) === wanted &&
|
|
579
|
+
ownFunction(candidate)) {
|
|
580
|
+
matches.push(candidate);
|
|
581
|
+
}
|
|
582
|
+
seen.add(candidate);
|
|
583
|
+
ts.forEachChild(candidate, visit);
|
|
584
|
+
};
|
|
585
|
+
if (fn.body)
|
|
586
|
+
ts.forEachChild(fn.body, visit);
|
|
587
|
+
}
|
|
588
|
+
if (matches.length === 0) {
|
|
589
|
+
return { reason: `${outside}, and its text is not inside them` };
|
|
590
|
+
}
|
|
591
|
+
// One answer: a return is its function's, a body is its node's type.
|
|
592
|
+
const answers = new Set(matches.map((match) => (returns ? ownFunction(match) : checker.getTypeAtLocation(match))));
|
|
593
|
+
if (answers.size > 1) {
|
|
594
|
+
return {
|
|
595
|
+
reason: `${outside}, and its text names ${answers.size} different answers inside them`,
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
return { node: matches[0] };
|
|
599
|
+
}
|
|
342
600
|
/**
|
|
343
601
|
* The capture's locator: the first node in pre-order that `accept`s, whose
|
|
344
602
|
* text equals the locator's (`exactLocatorText`), and that ends on or after
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs_sha256":"
|
|
1
|
+
{"inputs_sha256":"d1f02f1a4fc4d6ca56af32b5700a01da7d78dfcacdb1942aaf2b2151de1871ee"}
|