carrick 0.3.136 → 0.3.137
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/scripts/install-scanned-deps.sh +142 -2
- package/sidecar/dist/src/capture/api.d.ts +7 -0
- package/sidecar/dist/src/capture/check.d.ts +33 -0
- package/sidecar/dist/src/capture/check.js +102 -10
- package/sidecar/dist/src/index.js +1 -1
- package/sidecar/dist/src/library-claims.d.ts +25 -1
- package/sidecar/dist/src/library-claims.js +1348 -94
- package/sidecar/dist/src/result-candidates.d.ts +7 -0
- package/sidecar/dist/src/result-candidates.js +12 -5
- package/sidecar/dist/src/runtime-emitter.d.ts +39 -0
- package/sidecar/dist/src/runtime-emitter.js +65 -0
- package/sidecar/dist/src/server-labels.js +2 -2
- package/sidecar/dist/src/stated-params.d.ts +73 -3
- package/sidecar/dist/src/stated-params.js +292 -9
- package/sidecar/dist/src/type-inferrer.d.ts +17 -35
- package/sidecar/dist/src/type-inferrer.js +81 -82
- package/sidecar/dist/src/types.d.ts +125 -13
- package/sidecar/dist/src/validators.d.ts +1422 -162
- package/sidecar/dist/src/validators.js +39 -5
|
@@ -24,5 +24,12 @@
|
|
|
24
24
|
import { ts } from 'ts-morph';
|
|
25
25
|
import type { ResultShape, SurfaceResultCandidates } from './types.js';
|
|
26
26
|
export declare function resultCandidatesOf(checker: ts.TypeChecker, signature: ts.Signature): SurfaceResultCandidates | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Rule R over a return type read against `parameters`: the type parameters
|
|
29
|
+
* that stand for the caller's value. A handler a route takes returns a type
|
|
30
|
+
* the route's own parameters decide (carrick#3047), so those are the ones
|
|
31
|
+
* its return is read against.
|
|
32
|
+
*/
|
|
33
|
+
export declare function resultCandidatesOfReturn(checker: ts.TypeChecker, returned: ts.Type, parameters: readonly ts.Type[]): SurfaceResultCandidates | undefined;
|
|
27
34
|
/** Whether `shape` names one of `candidates`. `whole` is always one; a decided whole is the only one. */
|
|
28
35
|
export declare function holdsResult(candidates: SurfaceResultCandidates, shape: ResultShape): boolean;
|
|
@@ -29,8 +29,16 @@ const OPEN = ts.TypeFlags.Any | ts.TypeFlags.Unknown;
|
|
|
29
29
|
const isTypeParameter = (type) => (type.flags & ts.TypeFlags.TypeParameter) !== 0;
|
|
30
30
|
const byCodeUnit = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
31
31
|
export function resultCandidatesOf(checker, signature) {
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
return resultCandidatesOfReturn(checker, checker.getReturnTypeOfSignature(signature), signature.getTypeParameters() ?? []);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Rule R over a return type read against `parameters`: the type parameters
|
|
36
|
+
* that stand for the caller's value. A handler a route takes returns a type
|
|
37
|
+
* the route's own parameters decide (carrick#3047), so those are the ones
|
|
38
|
+
* its return is read against.
|
|
39
|
+
*/
|
|
40
|
+
export function resultCandidatesOfReturn(checker, returned, parameters) {
|
|
41
|
+
return new ResultReader(checker).candidates(returned, parameters);
|
|
34
42
|
}
|
|
35
43
|
/** Whether `shape` names one of `candidates`. `whole` is always one; a decided whole is the only one. */
|
|
36
44
|
export function holdsResult(candidates, shape) {
|
|
@@ -47,11 +55,10 @@ class ResultReader {
|
|
|
47
55
|
constructor(checker) {
|
|
48
56
|
this.checker = checker;
|
|
49
57
|
}
|
|
50
|
-
candidates(
|
|
51
|
-
const raw = this.checker.getReturnTypeOfSignature(signature);
|
|
58
|
+
candidates(raw, parameters) {
|
|
52
59
|
if (raw.flags & NOTHING)
|
|
53
60
|
return undefined;
|
|
54
|
-
const params = new Set(
|
|
61
|
+
const params = new Set(parameters);
|
|
55
62
|
let awaited = this.awaited(raw);
|
|
56
63
|
for (let hop = 0; hop < MAX_DEPTH && isTypeParameter(awaited) && params.has(awaited); hop++) {
|
|
57
64
|
const fallback = this.checker.getDefaultFromTypeParameter(awaited);
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The event names Node's runtime gives every `EventEmitter` (carrick#3032).
|
|
3
|
+
*
|
|
4
|
+
* No declaration of the runtime's emitter states them: `error` is declared
|
|
5
|
+
* nowhere in the runtime types package, and `newListener` and
|
|
6
|
+
* `removeListener` are declared only from 25 on, through a conditional type
|
|
7
|
+
* the lister does not read. A built-in of the language runtime is a fact of
|
|
8
|
+
* the platform, so it is stated once here. The lister marks the members it
|
|
9
|
+
* applies to (`runtime_events`) and the verifier accepts a `reserved` claim
|
|
10
|
+
* for the names on those members, from this same table.
|
|
11
|
+
*/
|
|
12
|
+
import { ts } from 'ts-morph';
|
|
13
|
+
/**
|
|
14
|
+
* The runtime's own type packages. A type they declare (the runtime's event
|
|
15
|
+
* emitter above all) is never part of another package's own declarations,
|
|
16
|
+
* even when that package's maker returns it: a member only the runtime
|
|
17
|
+
* declares is the runtime's, not the package's. When the named package is
|
|
18
|
+
* one of these, it is its own.
|
|
19
|
+
*/
|
|
20
|
+
export declare const RUNTIME_TYPE_PACKAGES: ReadonlySet<string>;
|
|
21
|
+
/**
|
|
22
|
+
* The event names Node's runtime emits on, or treats specially for, every
|
|
23
|
+
* `EventEmitter` (Node's events documentation: "Error events",
|
|
24
|
+
* "Event: 'newListener'", "Event: 'removeListener'"). An emitter emits
|
|
25
|
+
* `newListener` before it adds a listener and `removeListener` after it
|
|
26
|
+
* removes one, and emitting `error` with no listener throws.
|
|
27
|
+
*/
|
|
28
|
+
export declare const RUNTIME_EMITTER_EVENTS: readonly string[];
|
|
29
|
+
/**
|
|
30
|
+
* Whether `property` is a member of the runtime's own emitter: some
|
|
31
|
+
* declaration of it sits in `events.d.ts` at the root of a runtime types
|
|
32
|
+
* package, in a class or interface named `EventEmitter`, and takes a
|
|
33
|
+
* parameter named `eventName`. A member another package declares (its own
|
|
34
|
+
* `EventEmitter`, a class that redeclares `on`) never qualifies, nor does a
|
|
35
|
+
* runtime member that takes no event name (`setMaxListeners`).
|
|
36
|
+
*
|
|
37
|
+
* `packageOf` names the package a source file is installed in.
|
|
38
|
+
*/
|
|
39
|
+
export declare function isRuntimeEmitterMember(property: ts.Symbol, packageOf: (file: ts.SourceFile) => string | undefined): boolean;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The event names Node's runtime gives every `EventEmitter` (carrick#3032).
|
|
3
|
+
*
|
|
4
|
+
* No declaration of the runtime's emitter states them: `error` is declared
|
|
5
|
+
* nowhere in the runtime types package, and `newListener` and
|
|
6
|
+
* `removeListener` are declared only from 25 on, through a conditional type
|
|
7
|
+
* the lister does not read. A built-in of the language runtime is a fact of
|
|
8
|
+
* the platform, so it is stated once here. The lister marks the members it
|
|
9
|
+
* applies to (`runtime_events`) and the verifier accepts a `reserved` claim
|
|
10
|
+
* for the names on those members, from this same table.
|
|
11
|
+
*/
|
|
12
|
+
import * as path from 'node:path';
|
|
13
|
+
import { ts } from 'ts-morph';
|
|
14
|
+
/**
|
|
15
|
+
* The runtime's own type packages. A type they declare (the runtime's event
|
|
16
|
+
* emitter above all) is never part of another package's own declarations,
|
|
17
|
+
* even when that package's maker returns it: a member only the runtime
|
|
18
|
+
* declares is the runtime's, not the package's. When the named package is
|
|
19
|
+
* one of these, it is its own.
|
|
20
|
+
*/
|
|
21
|
+
export const RUNTIME_TYPE_PACKAGES = new Set([
|
|
22
|
+
'@types/node',
|
|
23
|
+
'@types/bun',
|
|
24
|
+
'bun-types',
|
|
25
|
+
'@types/deno',
|
|
26
|
+
]);
|
|
27
|
+
/**
|
|
28
|
+
* The event names Node's runtime emits on, or treats specially for, every
|
|
29
|
+
* `EventEmitter` (Node's events documentation: "Error events",
|
|
30
|
+
* "Event: 'newListener'", "Event: 'removeListener'"). An emitter emits
|
|
31
|
+
* `newListener` before it adds a listener and `removeListener` after it
|
|
32
|
+
* removes one, and emitting `error` with no listener throws.
|
|
33
|
+
*/
|
|
34
|
+
export const RUNTIME_EMITTER_EVENTS = ['error', 'newListener', 'removeListener'];
|
|
35
|
+
/** The file, relative to its package, that declares the runtime's emitter. */
|
|
36
|
+
const EMITTER_FILE = 'events.d.ts';
|
|
37
|
+
/**
|
|
38
|
+
* Whether `property` is a member of the runtime's own emitter: some
|
|
39
|
+
* declaration of it sits in `events.d.ts` at the root of a runtime types
|
|
40
|
+
* package, in a class or interface named `EventEmitter`, and takes a
|
|
41
|
+
* parameter named `eventName`. A member another package declares (its own
|
|
42
|
+
* `EventEmitter`, a class that redeclares `on`) never qualifies, nor does a
|
|
43
|
+
* runtime member that takes no event name (`setMaxListeners`).
|
|
44
|
+
*
|
|
45
|
+
* `packageOf` names the package a source file is installed in.
|
|
46
|
+
*/
|
|
47
|
+
export function isRuntimeEmitterMember(property, packageOf) {
|
|
48
|
+
return (property.getDeclarations() ?? []).some(declaration => {
|
|
49
|
+
if (!ts.isMethodDeclaration(declaration) && !ts.isMethodSignature(declaration))
|
|
50
|
+
return false;
|
|
51
|
+
const owner = declaration.parent;
|
|
52
|
+
if (!(ts.isClassLike(owner) || ts.isInterfaceDeclaration(owner)) || owner.name?.text !== 'EventEmitter')
|
|
53
|
+
return false;
|
|
54
|
+
if (!declaration.parameters.some(parameter => ts.isIdentifier(parameter.name) && parameter.name.text === 'eventName')) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
const file = declaration.getSourceFile();
|
|
58
|
+
const pkg = packageOf(file);
|
|
59
|
+
if (pkg === undefined || !RUNTIME_TYPE_PACKAGES.has(pkg))
|
|
60
|
+
return false;
|
|
61
|
+
const name = file.fileName.split(path.sep).join('/');
|
|
62
|
+
const marker = `/node_modules/${pkg}/`;
|
|
63
|
+
return name.slice(name.lastIndexOf(marker) + marker.length) === EMITTER_FILE;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
@@ -14,7 +14,7 @@ import path from 'node:path';
|
|
|
14
14
|
import { LibraryClaimsVerifier } from './library-claims.js';
|
|
15
15
|
import { settledServerChecks } from './validators.js';
|
|
16
16
|
/** No label: the inferrer reads nothing new. */
|
|
17
|
-
export const NO_LABELS = { sends: new Map(), bodies: new Set() };
|
|
17
|
+
export const NO_LABELS = { sends: new Map(), sets: new Map(), returns: new Map(), bodies: new Set() };
|
|
18
18
|
/**
|
|
19
19
|
* The members `claims` name in the program behind `key`. A claim the schema
|
|
20
20
|
* refuses is dropped; a read that throws answers no labels, so every item of
|
|
@@ -34,7 +34,7 @@ export function readServerLabels(source, key, claims, budgetMs, log) {
|
|
|
34
34
|
resolveModule: (name, from, options) => source.resolveModule(name, from, options),
|
|
35
35
|
});
|
|
36
36
|
const labels = verifier.labelledMembers(fromDir, checks, budgetMs, claims.scan_root, claims.type_packages);
|
|
37
|
-
log(`Read ${labels.sends.size} labelled send and ${labels.bodies.size} body member(s) for handler parameters ` +
|
|
37
|
+
log(`Read ${labels.sends.size} labelled send, ${labels.sets.size} assigned reply, ${labels.returns.size} returned reply and ${labels.bodies.size} body member(s) for handler parameters ` +
|
|
38
38
|
`from ${checks.length} claim(s) in ${fromDir}: ${JSON.stringify(labels.outcomes ?? {})}`);
|
|
39
39
|
return labels;
|
|
40
40
|
}
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
*/
|
|
18
18
|
import { Node, type ArrowFunction, type CallExpression, type FunctionDeclaration, type FunctionExpression, type MethodDeclaration, type ParameterDeclaration, type Type } from 'ts-morph';
|
|
19
19
|
import { type LabelledMembers } from './library-claims.js';
|
|
20
|
+
import type { ClaimSlot } from './types.js';
|
|
20
21
|
/** A function a handler can be: what the inferrer follows a registration to. */
|
|
21
22
|
export type HandlerFunction = FunctionDeclaration | ArrowFunction | FunctionExpression | MethodDeclaration;
|
|
22
23
|
/** What a handler's parameters state about the request body. */
|
|
@@ -32,22 +33,30 @@ export type StatedRequest = {
|
|
|
32
33
|
/** The reply a handler's parameters state: one or more success variants. */
|
|
33
34
|
export interface StatedReply {
|
|
34
35
|
variants: Type[];
|
|
35
|
-
at:
|
|
36
|
+
at: Node;
|
|
36
37
|
}
|
|
37
38
|
/** The status a send's own chain states, as the inferrer reads it. */
|
|
38
39
|
export type ChainStatus = (call: CallExpression) => 'success' | 'error' | 'redirect' | 'variable' | 'undecided' | undefined;
|
|
40
|
+
/** The status a value states, read as a send's status argument is: `undecided` when it states none. */
|
|
41
|
+
export type ValueStatus = (value: Node) => 'success' | 'error' | 'redirect' | 'variable' | 'undecided';
|
|
42
|
+
/** The node a value is read from: `JSON.stringify(x)` is read as `x`. */
|
|
43
|
+
export type PayloadNode = (value: Node) => Node;
|
|
39
44
|
export declare class StatedParams {
|
|
40
45
|
private readonly labels;
|
|
41
46
|
/** True for a file the repo declares itself (not a library's or the runtime's). */
|
|
42
47
|
private readonly isRepoFile;
|
|
43
48
|
private readonly chainStatus;
|
|
49
|
+
private readonly valueStatus;
|
|
50
|
+
private readonly payloadNode;
|
|
44
51
|
constructor(labels: LabelledMembers,
|
|
45
52
|
/** True for a file the repo declares itself (not a library's or the runtime's). */
|
|
46
|
-
isRepoFile: (file: Node) => boolean, chainStatus: ChainStatus);
|
|
53
|
+
isRepoFile: (file: Node) => boolean, chainStatus: ChainStatus, valueStatus: ValueStatus, payloadNode: PayloadNode);
|
|
47
54
|
/** No label names a member: every reading answers nothing. */
|
|
48
55
|
get isEmpty(): boolean;
|
|
49
|
-
/** Whether some label names a reply's send member. */
|
|
56
|
+
/** Whether some label names a reply's send member, called or set by assignment. */
|
|
50
57
|
get readsReplies(): boolean;
|
|
58
|
+
/** Whether some label says a route's handler returns the reply (carrick#3054). */
|
|
59
|
+
get readsReturns(): boolean;
|
|
51
60
|
/**
|
|
52
61
|
* The request body the handler's parameters state, the first parameter
|
|
53
62
|
* whose type has a labelled body member deciding. Undefined when no
|
|
@@ -100,6 +109,26 @@ export declare class StatedParams {
|
|
|
100
109
|
* success send through the parameter carries.
|
|
101
110
|
*/
|
|
102
111
|
reply(handler: HandlerFunction): StatedReply | undefined;
|
|
112
|
+
/**
|
|
113
|
+
* The reply a route's handler returns (carrick#3054): the registration
|
|
114
|
+
* hands the handler in at the slot a `returns` label names on the route
|
|
115
|
+
* member it calls. The handler's return, awaited, without its `void`,
|
|
116
|
+
* `undefined`, `null` and `never` parts; with `result`, the part of it
|
|
117
|
+
* that label names. Undefined when no label names the route, the handler
|
|
118
|
+
* is not at the slot, nothing is left, a part is `any` or `unknown`, the
|
|
119
|
+
* named member is missing from a part, or the label reads a parse method
|
|
120
|
+
* (not read here). The caller reads a labelled send first: a handler that
|
|
121
|
+
* sends does not also reply by return.
|
|
122
|
+
*/
|
|
123
|
+
returned(registration: Node, handler: HandlerFunction): StatedReply | undefined;
|
|
124
|
+
/** The `returns` label of the route member a registration call calls, by its declarations. */
|
|
125
|
+
private returnsLabelOf;
|
|
126
|
+
private unions;
|
|
127
|
+
/** The parts of `type`, each read through its promise. */
|
|
128
|
+
private settled;
|
|
129
|
+
/** A promise read through to what it resolves to, at most four hops. */
|
|
130
|
+
private awaited;
|
|
131
|
+
private isNothing;
|
|
103
132
|
/**
|
|
104
133
|
* The payload the labelled send members the handler calls through the
|
|
105
134
|
* parameter state: `none` when it calls none, `abstain` when they state
|
|
@@ -110,6 +139,8 @@ export declare class StatedParams {
|
|
|
110
139
|
private statedPayload;
|
|
111
140
|
/** The payload slot a labelled send member takes, by any of its declarations. */
|
|
112
141
|
private sendSlotOf;
|
|
142
|
+
/** The payload slot a member set by assignment takes, by any of its declarations. */
|
|
143
|
+
private setSlotOf;
|
|
113
144
|
/**
|
|
114
145
|
* The type parameters of the type that declares the member that its
|
|
115
146
|
* payload is declared through: a slot each route fills. None for a payload
|
|
@@ -119,12 +150,51 @@ export declare class StatedParams {
|
|
|
119
150
|
private payloadTypeParameters;
|
|
120
151
|
/** The type the call signature takes at the payload slot, read at `at`. */
|
|
121
152
|
private payloadAt;
|
|
153
|
+
/**
|
|
154
|
+
* What the labels say about one call (carrick#2472 C2): the payload slot
|
|
155
|
+
* when the called member's declaration is a labelled send; `'setter'` when
|
|
156
|
+
* the receiver's type has a labelled send member but this member is not
|
|
157
|
+
* one (a header, cookie or status setter that returns the reply too);
|
|
158
|
+
* `undefined` when no label speaks, so the caller keeps its own reading.
|
|
159
|
+
* Matched by declaration, never by name; the receiver's properties are read
|
|
160
|
+
* as `statedPayload` reads them, so an inherited send counts.
|
|
161
|
+
*/
|
|
162
|
+
sendAt(call: Node): ClaimSlot | 'setter' | undefined;
|
|
163
|
+
/**
|
|
164
|
+
* The payload slot of a labelled `new` send (carrick#3053): the constructed
|
|
165
|
+
* expression's construct signatures are keyed by the declaration the
|
|
166
|
+
* verifier read (an inherited constructor is the base's). A signature with
|
|
167
|
+
* no declaration (a class that writes no constructor and has no base to
|
|
168
|
+
* borrow one from) has no key; the verifier never labels one with a payload
|
|
169
|
+
* slot either, so it reads as no label and the structural reading decides.
|
|
170
|
+
*/
|
|
171
|
+
private constructSlotOf;
|
|
122
172
|
/**
|
|
123
173
|
* The calls of a labelled send member the handler makes on `parameter`, or
|
|
124
174
|
* on a call chain rooted at it (`res.status(201).json(body)`), each with
|
|
125
175
|
* its payload slot and the keys of the member's declarations.
|
|
126
176
|
*/
|
|
127
177
|
private sendsThrough;
|
|
178
|
+
/**
|
|
179
|
+
* The assignments `<root chain>.<member> = value` the handler makes through
|
|
180
|
+
* the parameter to a member a `set` label names, each with the keys of the
|
|
181
|
+
* member's declarations.
|
|
182
|
+
*/
|
|
183
|
+
private setsThrough;
|
|
184
|
+
/**
|
|
185
|
+
* The status a reply set by assignment states (carrick#3052): the nearest
|
|
186
|
+
* earlier statement of the same block that assigns a number to another
|
|
187
|
+
* member of the same root and states a status. `undecided` when none does.
|
|
188
|
+
* No member name is consulted. An assignment of a number to any member
|
|
189
|
+
* counts, so `ctx.length = 200` before a body reads as a 200 status.
|
|
190
|
+
*/
|
|
191
|
+
private assignedStatus;
|
|
192
|
+
/**
|
|
193
|
+
* The success values the handler assigns: each right side's type, the ones
|
|
194
|
+
* after an error or redirect status dropped. `abstain` when a value is
|
|
195
|
+
* typed `any` or `unknown`, which says nothing of what is sent.
|
|
196
|
+
*/
|
|
197
|
+
private assignedPayloads;
|
|
128
198
|
/**
|
|
129
199
|
* The payloads the sends carry whose chain states a success status or
|
|
130
200
|
* none. A value typed `any` or `unknown` is assignable to every variant, so
|
|
@@ -44,20 +44,31 @@ export class StatedParams {
|
|
|
44
44
|
labels;
|
|
45
45
|
isRepoFile;
|
|
46
46
|
chainStatus;
|
|
47
|
+
valueStatus;
|
|
48
|
+
payloadNode;
|
|
47
49
|
constructor(labels,
|
|
48
50
|
/** True for a file the repo declares itself (not a library's or the runtime's). */
|
|
49
|
-
isRepoFile, chainStatus) {
|
|
51
|
+
isRepoFile, chainStatus, valueStatus, payloadNode) {
|
|
50
52
|
this.labels = labels;
|
|
51
53
|
this.isRepoFile = isRepoFile;
|
|
52
54
|
this.chainStatus = chainStatus;
|
|
55
|
+
this.valueStatus = valueStatus;
|
|
56
|
+
this.payloadNode = payloadNode;
|
|
53
57
|
}
|
|
54
58
|
/** No label names a member: every reading answers nothing. */
|
|
55
59
|
get isEmpty() {
|
|
56
|
-
return this.labels.sends.size === 0 &&
|
|
60
|
+
return (this.labels.sends.size === 0 &&
|
|
61
|
+
this.labels.sets.size === 0 &&
|
|
62
|
+
this.labels.bodies.size === 0 &&
|
|
63
|
+
this.labels.returns.size === 0);
|
|
57
64
|
}
|
|
58
|
-
/** Whether some label names a reply's send member. */
|
|
65
|
+
/** Whether some label names a reply's send member, called or set by assignment. */
|
|
59
66
|
get readsReplies() {
|
|
60
|
-
return this.labels.sends.size > 0;
|
|
67
|
+
return this.labels.sends.size > 0 || this.labels.sets.size > 0;
|
|
68
|
+
}
|
|
69
|
+
/** Whether some label says a route's handler returns the reply (carrick#3054). */
|
|
70
|
+
get readsReturns() {
|
|
71
|
+
return this.labels.returns.size > 0;
|
|
61
72
|
}
|
|
62
73
|
// ===========================================================================
|
|
63
74
|
// Request
|
|
@@ -301,13 +312,20 @@ export class StatedParams {
|
|
|
301
312
|
* success send through the parameter carries.
|
|
302
313
|
*/
|
|
303
314
|
reply(handler) {
|
|
304
|
-
if (this.
|
|
315
|
+
if (!this.readsReplies)
|
|
305
316
|
return undefined;
|
|
306
317
|
for (const parameter of handler.getParameters()) {
|
|
307
318
|
const sends = this.sendsThrough(handler, parameter);
|
|
308
|
-
const
|
|
309
|
-
|
|
319
|
+
const sets = this.setsThrough(handler, parameter);
|
|
320
|
+
const stated = this.statedPayload(parameter, new Set([...sends, ...sets].flatMap((send) => send.keys)));
|
|
321
|
+
if (stated === 'none') {
|
|
322
|
+
const assigned = this.assignedPayloads(sets);
|
|
323
|
+
if (assigned === 'abstain')
|
|
324
|
+
return undefined;
|
|
325
|
+
if (assigned.length > 0)
|
|
326
|
+
return { variants: assigned, at: parameter };
|
|
310
327
|
continue;
|
|
328
|
+
}
|
|
311
329
|
if (stated === 'abstain')
|
|
312
330
|
return undefined;
|
|
313
331
|
if (stated.isAny() || stated.isUnknown() || stated.isNever())
|
|
@@ -319,12 +337,111 @@ export class StatedParams {
|
|
|
319
337
|
const defined = stated.isUnion() ? stated.getUnionTypes().filter((t) => !t.isUndefined()) : [stated];
|
|
320
338
|
return defined.length > 0 ? { variants: defined, at: parameter } : undefined;
|
|
321
339
|
}
|
|
322
|
-
const
|
|
340
|
+
const assigned = this.assignedPayloads(sets);
|
|
341
|
+
const carried = [...this.successPayloads(sends), ...(assigned === 'abstain' ? [] : assigned)];
|
|
323
342
|
const kept = variants.filter((variant) => carried.some((sent) => sent.isAssignableTo(variant)));
|
|
324
343
|
return kept.length > 0 ? { variants: kept, at: parameter } : undefined;
|
|
325
344
|
}
|
|
326
345
|
return undefined;
|
|
327
346
|
}
|
|
347
|
+
/**
|
|
348
|
+
* The reply a route's handler returns (carrick#3054): the registration
|
|
349
|
+
* hands the handler in at the slot a `returns` label names on the route
|
|
350
|
+
* member it calls. The handler's return, awaited, without its `void`,
|
|
351
|
+
* `undefined`, `null` and `never` parts; with `result`, the part of it
|
|
352
|
+
* that label names. Undefined when no label names the route, the handler
|
|
353
|
+
* is not at the slot, nothing is left, a part is `any` or `unknown`, the
|
|
354
|
+
* named member is missing from a part, or the label reads a parse method
|
|
355
|
+
* (not read here). The caller reads a labelled send first: a handler that
|
|
356
|
+
* sends does not also reply by return.
|
|
357
|
+
*/
|
|
358
|
+
returned(registration, handler) {
|
|
359
|
+
if (this.labels.returns.size === 0 || !Node.isCallExpression(registration))
|
|
360
|
+
return undefined;
|
|
361
|
+
const label = this.returnsLabelOf(registration);
|
|
362
|
+
if (!label)
|
|
363
|
+
return undefined;
|
|
364
|
+
const at = registration.getArguments()[label.handler.arg];
|
|
365
|
+
if (!at || (at !== handler && this.repoFunction(at) !== handler))
|
|
366
|
+
return undefined;
|
|
367
|
+
const parts = [];
|
|
368
|
+
for (const part of this.settled(handler.getReturnType())) {
|
|
369
|
+
if (this.isNothing(part))
|
|
370
|
+
continue;
|
|
371
|
+
parts.push(part);
|
|
372
|
+
}
|
|
373
|
+
let held = parts;
|
|
374
|
+
const shape = label.result;
|
|
375
|
+
if (shape !== undefined) {
|
|
376
|
+
if ('parse' in shape)
|
|
377
|
+
return undefined;
|
|
378
|
+
if ('member' in shape) {
|
|
379
|
+
held = [];
|
|
380
|
+
for (const part of parts) {
|
|
381
|
+
const member = part.getProperty(shape.member[0]);
|
|
382
|
+
if (!member)
|
|
383
|
+
return undefined;
|
|
384
|
+
let type;
|
|
385
|
+
try {
|
|
386
|
+
type = member.getTypeAtLocation(handler);
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
return undefined;
|
|
390
|
+
}
|
|
391
|
+
for (const each of this.settled(type)) {
|
|
392
|
+
if (!this.isNothing(each))
|
|
393
|
+
held.push(each);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if (held.length === 0 || held.some((part) => part.isAny() || part.isUnknown()))
|
|
399
|
+
return undefined;
|
|
400
|
+
return { variants: held, at: handler };
|
|
401
|
+
}
|
|
402
|
+
/** The `returns` label of the route member a registration call calls, by its declarations. */
|
|
403
|
+
returnsLabelOf(call) {
|
|
404
|
+
const declarations = [];
|
|
405
|
+
try {
|
|
406
|
+
const declaration = call.getProject().getTypeChecker().getResolvedSignature(call)?.getDeclaration();
|
|
407
|
+
if (declaration)
|
|
408
|
+
declarations.push(declaration);
|
|
409
|
+
}
|
|
410
|
+
catch {
|
|
411
|
+
// The call resolves no signature: the member's own symbol still names it.
|
|
412
|
+
}
|
|
413
|
+
const callee = call.getExpression();
|
|
414
|
+
const named = Node.isPropertyAccessExpression(callee) ? callee.getNameNode() : callee;
|
|
415
|
+
declarations.push(...(named.getSymbol()?.getDeclarations() ?? []));
|
|
416
|
+
for (const declaration of declarations) {
|
|
417
|
+
const label = this.labels.returns.get(declarationKey(declaration.compilerNode));
|
|
418
|
+
if (label)
|
|
419
|
+
return label;
|
|
420
|
+
}
|
|
421
|
+
return undefined;
|
|
422
|
+
}
|
|
423
|
+
unions(type) {
|
|
424
|
+
return type.isUnion() ? type.getUnionTypes() : [type];
|
|
425
|
+
}
|
|
426
|
+
/** The parts of `type`, each read through its promise. */
|
|
427
|
+
settled(type) {
|
|
428
|
+
return this.unions(type).flatMap((part) => this.unions(this.awaited(part)));
|
|
429
|
+
}
|
|
430
|
+
/** A promise read through to what it resolves to, at most four hops. */
|
|
431
|
+
awaited(type) {
|
|
432
|
+
let current = type;
|
|
433
|
+
for (let hop = 0; hop < 4; hop++) {
|
|
434
|
+
const name = current.getSymbol()?.getName();
|
|
435
|
+
const inner = current.getTypeArguments()[0];
|
|
436
|
+
if ((name !== 'Promise' && name !== 'PromiseLike') || !inner)
|
|
437
|
+
break;
|
|
438
|
+
current = inner;
|
|
439
|
+
}
|
|
440
|
+
return current;
|
|
441
|
+
}
|
|
442
|
+
isNothing(type) {
|
|
443
|
+
return type.isUndefined() || type.isNull() || (type.getFlags() & (ts.TypeFlags.Void | ts.TypeFlags.Never)) !== 0;
|
|
444
|
+
}
|
|
328
445
|
/**
|
|
329
446
|
* The payload the labelled send members the handler calls through the
|
|
330
447
|
* parameter state: `none` when it calls none, `abstain` when they state
|
|
@@ -346,7 +463,9 @@ export class StatedParams {
|
|
|
346
463
|
const keys = property.getDeclarations().map((d) => declarationKey(d.compilerNode));
|
|
347
464
|
if (!keys.some((key) => called.has(key)))
|
|
348
465
|
continue;
|
|
349
|
-
const
|
|
466
|
+
const declarations = property.getDeclarations().map((d) => d.compilerNode);
|
|
467
|
+
const assigned = this.setSlotOf(declarations);
|
|
468
|
+
const slot = assigned ?? this.sendSlotOf(declarations);
|
|
350
469
|
if (!slot)
|
|
351
470
|
continue;
|
|
352
471
|
found = true;
|
|
@@ -365,6 +484,17 @@ export class StatedParams {
|
|
|
365
484
|
catch {
|
|
366
485
|
continue;
|
|
367
486
|
}
|
|
487
|
+
if (assigned) {
|
|
488
|
+
// A member set by assignment is its own payload: the type the route
|
|
489
|
+
// filled, or the declared default when it filled nothing.
|
|
490
|
+
if (defaults.has(member.compilerType))
|
|
491
|
+
continue;
|
|
492
|
+
if (payload === undefined)
|
|
493
|
+
payload = member;
|
|
494
|
+
else if (payload.compilerType !== member.compilerType)
|
|
495
|
+
return 'abstain';
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
368
498
|
for (const signature of member.getCallSignatures()) {
|
|
369
499
|
// A payload the member's own type parameter types is whatever each
|
|
370
500
|
// call passes: the parameter states nothing.
|
|
@@ -396,6 +526,15 @@ export class StatedParams {
|
|
|
396
526
|
}
|
|
397
527
|
return undefined;
|
|
398
528
|
}
|
|
529
|
+
/** The payload slot a member set by assignment takes, by any of its declarations. */
|
|
530
|
+
setSlotOf(declarations) {
|
|
531
|
+
for (const declaration of declarations) {
|
|
532
|
+
const slot = this.labels.sets.get(declarationKey(declaration));
|
|
533
|
+
if (slot)
|
|
534
|
+
return slot;
|
|
535
|
+
}
|
|
536
|
+
return undefined;
|
|
537
|
+
}
|
|
399
538
|
/**
|
|
400
539
|
* The type parameters of the type that declares the member that its
|
|
401
540
|
* payload is declared through: a slot each route fills. None for a payload
|
|
@@ -461,6 +600,67 @@ export class StatedParams {
|
|
|
461
600
|
}
|
|
462
601
|
return type;
|
|
463
602
|
}
|
|
603
|
+
/**
|
|
604
|
+
* What the labels say about one call (carrick#2472 C2): the payload slot
|
|
605
|
+
* when the called member's declaration is a labelled send; `'setter'` when
|
|
606
|
+
* the receiver's type has a labelled send member but this member is not
|
|
607
|
+
* one (a header, cookie or status setter that returns the reply too);
|
|
608
|
+
* `undefined` when no label speaks, so the caller keeps its own reading.
|
|
609
|
+
* Matched by declaration, never by name; the receiver's properties are read
|
|
610
|
+
* as `statedPayload` reads them, so an inherited send counts.
|
|
611
|
+
*/
|
|
612
|
+
sendAt(call) {
|
|
613
|
+
if (this.labels.sends.size === 0)
|
|
614
|
+
return undefined;
|
|
615
|
+
if (Node.isNewExpression(call))
|
|
616
|
+
return this.constructSlotOf(call);
|
|
617
|
+
if (!Node.isCallExpression(call))
|
|
618
|
+
return undefined;
|
|
619
|
+
const callee = call.getExpression();
|
|
620
|
+
if (!Node.isPropertyAccessExpression(callee))
|
|
621
|
+
return undefined;
|
|
622
|
+
const slot = this.sendSlotOf((callee.getNameNode().getSymbol()?.getDeclarations() ?? []).map((d) => d.compilerNode));
|
|
623
|
+
if (slot)
|
|
624
|
+
return slot;
|
|
625
|
+
let receiver;
|
|
626
|
+
try {
|
|
627
|
+
receiver = callee.getExpression().getType().getNonNullableType();
|
|
628
|
+
}
|
|
629
|
+
catch {
|
|
630
|
+
return undefined;
|
|
631
|
+
}
|
|
632
|
+
const repliesHere = receiver
|
|
633
|
+
.getProperties()
|
|
634
|
+
.some((property) => this.sendSlotOf(property.getDeclarations().map((d) => d.compilerNode)) !== undefined);
|
|
635
|
+
return repliesHere ? 'setter' : undefined;
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* The payload slot of a labelled `new` send (carrick#3053): the constructed
|
|
639
|
+
* expression's construct signatures are keyed by the declaration the
|
|
640
|
+
* verifier read (an inherited constructor is the base's). A signature with
|
|
641
|
+
* no declaration (a class that writes no constructor and has no base to
|
|
642
|
+
* borrow one from) has no key; the verifier never labels one with a payload
|
|
643
|
+
* slot either, so it reads as no label and the structural reading decides.
|
|
644
|
+
*/
|
|
645
|
+
constructSlotOf(construct) {
|
|
646
|
+
if (!Node.isNewExpression(construct))
|
|
647
|
+
return undefined;
|
|
648
|
+
let declarations;
|
|
649
|
+
try {
|
|
650
|
+
declarations = construct
|
|
651
|
+
.getExpression()
|
|
652
|
+
.getType()
|
|
653
|
+
.getConstructSignatures()
|
|
654
|
+
.flatMap((signature) => {
|
|
655
|
+
const declaration = signature.compilerSignature.getDeclaration();
|
|
656
|
+
return declaration ? [declaration] : [];
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
catch {
|
|
660
|
+
return undefined;
|
|
661
|
+
}
|
|
662
|
+
return this.sendSlotOf(declarations);
|
|
663
|
+
}
|
|
464
664
|
/**
|
|
465
665
|
* The calls of a labelled send member the handler makes on `parameter`, or
|
|
466
666
|
* on a call chain rooted at it (`res.status(201).json(body)`), each with
|
|
@@ -487,6 +687,89 @@ export class StatedParams {
|
|
|
487
687
|
}
|
|
488
688
|
return sends;
|
|
489
689
|
}
|
|
690
|
+
/**
|
|
691
|
+
* The assignments `<root chain>.<member> = value` the handler makes through
|
|
692
|
+
* the parameter to a member a `set` label names, each with the keys of the
|
|
693
|
+
* member's declarations.
|
|
694
|
+
*/
|
|
695
|
+
setsThrough(handler, parameter) {
|
|
696
|
+
const name = parameter.getNameNode();
|
|
697
|
+
const symbol = Node.isIdentifier(name) ? name.getSymbol() : undefined;
|
|
698
|
+
const body = handler.getBody();
|
|
699
|
+
if (!symbol || !body || this.labels.sets.size === 0)
|
|
700
|
+
return [];
|
|
701
|
+
const sets = [];
|
|
702
|
+
for (const assignment of body.getDescendantsOfKind(SyntaxKind.BinaryExpression)) {
|
|
703
|
+
if (assignment.getOperatorToken().getKind() !== SyntaxKind.EqualsToken)
|
|
704
|
+
continue;
|
|
705
|
+
const left = assignment.getLeft();
|
|
706
|
+
if (!Node.isPropertyAccessExpression(left))
|
|
707
|
+
continue;
|
|
708
|
+
const declarations = (left.getNameNode().getSymbol()?.getDeclarations() ?? []).map((d) => d.compilerNode);
|
|
709
|
+
if (!this.setSlotOf(declarations))
|
|
710
|
+
continue;
|
|
711
|
+
if (this.rootSymbol(left.getExpression()) !== symbol)
|
|
712
|
+
continue;
|
|
713
|
+
sets.push({ assignment, keys: declarations.map(declarationKey) });
|
|
714
|
+
}
|
|
715
|
+
return sets;
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* The status a reply set by assignment states (carrick#3052): the nearest
|
|
719
|
+
* earlier statement of the same block that assigns a number to another
|
|
720
|
+
* member of the same root and states a status. `undecided` when none does.
|
|
721
|
+
* No member name is consulted. An assignment of a number to any member
|
|
722
|
+
* counts, so `ctx.length = 200` before a body reads as a 200 status.
|
|
723
|
+
*/
|
|
724
|
+
assignedStatus(assignment) {
|
|
725
|
+
const statement = assignment.getParent();
|
|
726
|
+
if (!Node.isExpressionStatement(statement))
|
|
727
|
+
return 'undecided';
|
|
728
|
+
const root = this.rootSymbol(assignment.getLeft());
|
|
729
|
+
if (!root)
|
|
730
|
+
return 'undecided';
|
|
731
|
+
for (const earlier of [...statement.getPreviousSiblings()].reverse()) {
|
|
732
|
+
if (!Node.isExpressionStatement(earlier))
|
|
733
|
+
continue;
|
|
734
|
+
const set = earlier.getExpression();
|
|
735
|
+
if (!Node.isBinaryExpression(set) || set.getOperatorToken().getKind() !== SyntaxKind.EqualsToken)
|
|
736
|
+
continue;
|
|
737
|
+
const left = set.getLeft();
|
|
738
|
+
if (!Node.isPropertyAccessExpression(left) || this.rootSymbol(left) !== root)
|
|
739
|
+
continue;
|
|
740
|
+
if (this.setSlotOf((left.getNameNode().getSymbol()?.getDeclarations() ?? []).map((d) => d.compilerNode)))
|
|
741
|
+
continue;
|
|
742
|
+
const value = set.getRight();
|
|
743
|
+
if ((value.getType().getFlags() & ts.TypeFlags.NumberLike) === 0)
|
|
744
|
+
continue;
|
|
745
|
+
const status = this.valueStatus(value);
|
|
746
|
+
if (status !== 'undecided')
|
|
747
|
+
return status;
|
|
748
|
+
}
|
|
749
|
+
return 'undecided';
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* The success values the handler assigns: each right side's type, the ones
|
|
753
|
+
* after an error or redirect status dropped. `abstain` when a value is
|
|
754
|
+
* typed `any` or `unknown`, which says nothing of what is sent.
|
|
755
|
+
*/
|
|
756
|
+
assignedPayloads(sets) {
|
|
757
|
+
const sent = [];
|
|
758
|
+
for (const { assignment } of sets) {
|
|
759
|
+
const status = this.assignedStatus(assignment);
|
|
760
|
+
if (status === 'error' || status === 'redirect')
|
|
761
|
+
continue;
|
|
762
|
+
const type = this.payloadNode(assignment.getRight()).getType();
|
|
763
|
+
if (type.isAny() || type.isUnknown())
|
|
764
|
+
return 'abstain';
|
|
765
|
+
// No body: clearing the reply states no payload.
|
|
766
|
+
if (type.isNull() || type.isUndefined())
|
|
767
|
+
continue;
|
|
768
|
+
if (!sent.some((each) => each.compilerType === type.compilerType))
|
|
769
|
+
sent.push(type);
|
|
770
|
+
}
|
|
771
|
+
return sent;
|
|
772
|
+
}
|
|
490
773
|
/**
|
|
491
774
|
* The payloads the sends carry whose chain states a success status or
|
|
492
775
|
* none. A value typed `any` or `unknown` is assignable to every variant, so
|