@cplieger/deadset-ts 3.3.1 → 3.4.0
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/README.md +1 -1
- package/dist/src/analysis.js +8 -1
- package/dist/src/calls.js +154 -0
- package/dist/src/class-members.js +92 -0
- package/dist/src/config.js +3 -0
- package/dist/src/configured-declarations.js +232 -0
- package/dist/src/decorator.js +3 -10
- package/dist/src/framework-lifecycle.js +192 -0
- package/dist/src/injection-container.js +101 -0
- package/dist/src/print.js +18 -0
- package/dist/src/resolve.js +98 -0
- package/dist/src/schema.js +18 -1
- package/dist/src/serialization-contract.js +315 -0
- package/dist/src/value-reach.js +120 -0
- package/dist/src/version.js +1 -1
- package/jsr.json +1 -1
- package/package.json +2 -2
- package/src/analysis.ts +8 -1
- package/src/calls.ts +224 -0
- package/src/class-members.ts +136 -0
- package/src/config.ts +31 -0
- package/src/configured-declarations.ts +308 -0
- package/src/decorator.ts +4 -16
- package/src/exempt.ts +5 -1
- package/src/framework-lifecycle.ts +280 -0
- package/src/injection-container.ts +141 -0
- package/src/print.ts +20 -1
- package/src/resolve.ts +155 -0
- package/src/schema.ts +19 -1
- package/src/serialization-contract.ts +492 -0
- package/src/value-reach.ts +152 -0
- package/src/version.ts +1 -1
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The serialization-contract class: the properties of a class whose values reach
|
|
3
|
+
* `JSON.stringify` or a call `ts.serializers` names, and those and `toJSON` and
|
|
4
|
+
* `toString` where a value is passed to an `unknown` or `any` parameter of a function
|
|
5
|
+
* outside the analysis. A function of the project handing such a parameter of its own
|
|
6
|
+
* to a destination is one, holding back what that destination does, to a fixpoint.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
isArrowFunction,
|
|
11
|
+
isClassDeclaration,
|
|
12
|
+
isClassExpression,
|
|
13
|
+
isClassStaticBlockDeclaration,
|
|
14
|
+
isFunctionDeclaration,
|
|
15
|
+
isFunctionExpression,
|
|
16
|
+
isGetAccessorDeclaration,
|
|
17
|
+
isIdentifier,
|
|
18
|
+
isMethodDeclaration,
|
|
19
|
+
isNoSubstitutionTemplateLiteral,
|
|
20
|
+
isNumericLiteral,
|
|
21
|
+
isObjectLiteralExpression,
|
|
22
|
+
isParenthesizedExpression,
|
|
23
|
+
isSetAccessorDeclaration,
|
|
24
|
+
isSpreadElement,
|
|
25
|
+
isStringLiteral,
|
|
26
|
+
isVariableDeclaration,
|
|
27
|
+
ModifierFlags,
|
|
28
|
+
SyntaxKind,
|
|
29
|
+
type CallExpression,
|
|
30
|
+
type Expression,
|
|
31
|
+
type Node,
|
|
32
|
+
type ParameterDeclaration,
|
|
33
|
+
} from "@typescript/native/unstable/ast";
|
|
34
|
+
import {
|
|
35
|
+
SymbolFlags,
|
|
36
|
+
TypeFlags,
|
|
37
|
+
type Symbol as TSSymbol,
|
|
38
|
+
type Type,
|
|
39
|
+
} from "@typescript/native/unstable/sync";
|
|
40
|
+
import { aliasChains, type AliasChains } from "./alias-chain.ts";
|
|
41
|
+
import { calleeName, resolvedTargets, valuesPassed } from "./calls.ts";
|
|
42
|
+
import { classMembers, memberComponent, typesAt } from "./class-members.ts";
|
|
43
|
+
import type { DeclarationEntry } from "./config.ts";
|
|
44
|
+
import {
|
|
45
|
+
names,
|
|
46
|
+
namesADeclaration,
|
|
47
|
+
resolveEntries,
|
|
48
|
+
spelledEntry,
|
|
49
|
+
} from "./configured-declarations.ts";
|
|
50
|
+
import type { DetectorInput, Evidence } from "./exempt.ts";
|
|
51
|
+
import { nodeKey, type Inventory } from "./inventory.ts";
|
|
52
|
+
import { renderPosition } from "./position.ts";
|
|
53
|
+
import type { ProjectView } from "./session.ts";
|
|
54
|
+
import { valueReach } from "./value-reach.ts";
|
|
55
|
+
|
|
56
|
+
/** The serializer every run reads, whatever the configuration names. */
|
|
57
|
+
const JSON_STRINGIFY: DeclarationEntry = { shape: "global", global: "JSON.stringify" };
|
|
58
|
+
|
|
59
|
+
/** The methods a serializer and a formatter resolve by name on a value that left the analysis. */
|
|
60
|
+
const CONVERSIONS: ReadonlySet<string> = new Set(["toJSON", "toString"]);
|
|
61
|
+
|
|
62
|
+
/** The parameter types that keep nothing of the value they are given. */
|
|
63
|
+
const ERASED = TypeFlags.Any | TypeFlags.Unknown;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* What a destination holds back of a class: its properties, or its properties and its
|
|
67
|
+
* two conversion methods. The second holds back everything the first does.
|
|
68
|
+
*/
|
|
69
|
+
type Reach = "properties" | "conversions";
|
|
70
|
+
|
|
71
|
+
/** One call of the project's own files, and the class `this` is a value of where it is written. */
|
|
72
|
+
interface Site {
|
|
73
|
+
readonly call: CallExpression;
|
|
74
|
+
readonly self: string | undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** One parameter of a function of the project that may hand on what it is given. */
|
|
78
|
+
interface Parameter {
|
|
79
|
+
/** The inventory's identifier of the function's declaration. */
|
|
80
|
+
readonly fn: string;
|
|
81
|
+
readonly index: number;
|
|
82
|
+
readonly rest: boolean;
|
|
83
|
+
readonly name: Node;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** One place a function hands a parameter of its own to the argument of a call. */
|
|
87
|
+
interface Forwarded {
|
|
88
|
+
readonly parameter: Parameter;
|
|
89
|
+
readonly site: Site;
|
|
90
|
+
readonly argument: number;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** A destination at one argument: what it holds back, and the callee a record names. */
|
|
94
|
+
interface Destination {
|
|
95
|
+
readonly reach: Reach;
|
|
96
|
+
readonly callee: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The wider of two reaches, where either may be absent. */
|
|
100
|
+
function wider(a: Reach | undefined, b: Reach | undefined): Reach | undefined {
|
|
101
|
+
return a === "conversions" || b === "conversions" ? "conversions" : (a ?? b);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Whether one node declares a static member, whose `this` is the class itself. */
|
|
105
|
+
function isStatic(node: Node): boolean {
|
|
106
|
+
const flags = (node as { readonly modifierFlags?: number }).modifierFlags ?? 0;
|
|
107
|
+
return (flags & ModifierFlags.Static) !== 0;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Whether one node opens a body whose `this` is not an instance of the enclosing class. */
|
|
111
|
+
function resetsThis(node: Node): boolean {
|
|
112
|
+
return (
|
|
113
|
+
isFunctionDeclaration(node) ||
|
|
114
|
+
isFunctionExpression(node) ||
|
|
115
|
+
isClassStaticBlockDeclaration(node) ||
|
|
116
|
+
isStatic(node) ||
|
|
117
|
+
((isMethodDeclaration(node) ||
|
|
118
|
+
isGetAccessorDeclaration(node) ||
|
|
119
|
+
isSetAccessorDeclaration(node)) &&
|
|
120
|
+
isObjectLiteralExpression(node.parent))
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The parameters and declaring node of one function-like declaration of the project. */
|
|
125
|
+
function functionOf(
|
|
126
|
+
node: Node,
|
|
127
|
+
): { readonly declared: Node; readonly parameters: readonly ParameterDeclaration[] } | undefined {
|
|
128
|
+
if (
|
|
129
|
+
isFunctionDeclaration(node) ||
|
|
130
|
+
(isMethodDeclaration(node) && !isObjectLiteralExpression(node.parent))
|
|
131
|
+
) {
|
|
132
|
+
return { declared: node, parameters: node.parameters };
|
|
133
|
+
}
|
|
134
|
+
if (
|
|
135
|
+
isVariableDeclaration(node) &&
|
|
136
|
+
node.initializer !== undefined &&
|
|
137
|
+
(isArrowFunction(node.initializer) || isFunctionExpression(node.initializer))
|
|
138
|
+
) {
|
|
139
|
+
return { declared: node, parameters: node.initializer.parameters };
|
|
140
|
+
}
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Whether one parameter is a `this` parameter, which types `this` and binds no argument. */
|
|
145
|
+
function isThisParameter(parameter: ParameterDeclaration): boolean {
|
|
146
|
+
return isIdentifier(parameter.name) && parameter.name.text === "this";
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Whether an argument is written in a form that holds no class instance, which saves a type. */
|
|
150
|
+
function holdsNoInstance(argument: Expression): boolean {
|
|
151
|
+
return (
|
|
152
|
+
isArrowFunction(argument) ||
|
|
153
|
+
isFunctionExpression(argument) ||
|
|
154
|
+
isStringLiteral(argument) ||
|
|
155
|
+
isNumericLiteral(argument) ||
|
|
156
|
+
isNoSubstitutionTemplateLiteral(argument) ||
|
|
157
|
+
argument.kind === SyntaxKind.TemplateExpression
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The expression an argument evaluates, through parentheses and a spread. */
|
|
162
|
+
function evaluated(argument: Expression): Expression {
|
|
163
|
+
const value = isSpreadElement(argument) ? argument.expression : argument;
|
|
164
|
+
return isParenthesizedExpression(value) ? evaluated(value.expression) : value;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** What one walk of the project's own files found: its calls and its functions' parameters. */
|
|
168
|
+
interface Walked {
|
|
169
|
+
readonly sites: readonly Site[];
|
|
170
|
+
readonly parameters: readonly Parameter[];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Every call of the project's own files with the class `this` stands for there, and
|
|
175
|
+
* every identifier-named parameter of every function, method and function-valued
|
|
176
|
+
* variable the project declares, at the position of the argument it binds.
|
|
177
|
+
*/
|
|
178
|
+
function walkProject<Brand>(project: ProjectView<Brand>, held: Inventory): Walked {
|
|
179
|
+
const sites: Site[] = [];
|
|
180
|
+
const parameters: Parameter[] = [];
|
|
181
|
+
for (const file of project.ownSourceFiles()) {
|
|
182
|
+
const visit = (node: Node, self: string | undefined): void => {
|
|
183
|
+
let inner = self;
|
|
184
|
+
if (isClassDeclaration(node) || isClassExpression(node)) {
|
|
185
|
+
inner = held.declarations.get(nodeKey(file, node));
|
|
186
|
+
} else if (resetsThis(node)) {
|
|
187
|
+
inner = undefined;
|
|
188
|
+
}
|
|
189
|
+
if (node.kind === SyntaxKind.CallExpression) {
|
|
190
|
+
sites.push({ call: node as CallExpression, self });
|
|
191
|
+
}
|
|
192
|
+
const fn = functionOf(node);
|
|
193
|
+
const id = fn === undefined ? undefined : held.declarations.get(nodeKey(file, fn.declared));
|
|
194
|
+
if (fn !== undefined && id !== undefined) {
|
|
195
|
+
fn.parameters
|
|
196
|
+
.filter((parameter) => !isThisParameter(parameter))
|
|
197
|
+
.forEach((parameter, index) => {
|
|
198
|
+
if (isIdentifier(parameter.name)) {
|
|
199
|
+
parameters.push({
|
|
200
|
+
fn: id,
|
|
201
|
+
index,
|
|
202
|
+
rest: parameter.dotDotDotToken !== undefined,
|
|
203
|
+
name: parameter.name,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
node.forEachChild((child) => {
|
|
209
|
+
visit(child, inner);
|
|
210
|
+
});
|
|
211
|
+
};
|
|
212
|
+
file.forEachChild((child) => {
|
|
213
|
+
visit(child, undefined);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
return { sites, parameters };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* The parameters that keep nothing of the value they are given: one typed `unknown` or
|
|
221
|
+
* `any`, and a rest parameter whose elements are, each by its symbol.
|
|
222
|
+
*/
|
|
223
|
+
function erasedParameters<Brand>(
|
|
224
|
+
project: ProjectView<Brand>,
|
|
225
|
+
parameters: readonly Parameter[],
|
|
226
|
+
): ReadonlyMap<number, Parameter> {
|
|
227
|
+
const types = typesAt(
|
|
228
|
+
project,
|
|
229
|
+
parameters.map((parameter) => parameter.name),
|
|
230
|
+
);
|
|
231
|
+
const erased = parameters.filter((parameter, index) => {
|
|
232
|
+
const type = types[index];
|
|
233
|
+
if (type === undefined) {
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
if (!parameter.rest) {
|
|
237
|
+
return (type.flags & ERASED) !== 0;
|
|
238
|
+
}
|
|
239
|
+
const [element] = type.isTypeReference() ? project.checker.getTypeArguments(type) : [];
|
|
240
|
+
return element !== undefined && (element.flags & ERASED) !== 0;
|
|
241
|
+
});
|
|
242
|
+
const bySymbol = new Map<number, Parameter>();
|
|
243
|
+
project
|
|
244
|
+
.symbolsAt(erased.map((parameter) => project.handle(parameter.name)))
|
|
245
|
+
.forEach((symbol, index) => {
|
|
246
|
+
const parameter = erased[index];
|
|
247
|
+
if (symbol !== undefined && parameter !== undefined) {
|
|
248
|
+
bySymbol.set(symbol.id, parameter);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
return bySymbol;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Each place a call's argument passes one of the erased parameters it can see. */
|
|
255
|
+
function handedOn<Brand>(
|
|
256
|
+
project: ProjectView<Brand>,
|
|
257
|
+
sites: readonly Site[],
|
|
258
|
+
erased: ReadonlyMap<number, Parameter>,
|
|
259
|
+
): readonly Forwarded[] {
|
|
260
|
+
const spellings = new Set([...erased.values()].map((parameter) => parameter.name.getText()));
|
|
261
|
+
const candidates = sites.flatMap((site) =>
|
|
262
|
+
site.call.arguments.flatMap((argument, index) =>
|
|
263
|
+
valuesPassed(argument)
|
|
264
|
+
.filter((value) => spellings.has(value.name.getText()))
|
|
265
|
+
.map((value) => ({ site, argument: index, value })),
|
|
266
|
+
),
|
|
267
|
+
);
|
|
268
|
+
const plain = candidates.filter((one) => !one.value.shorthand);
|
|
269
|
+
const resolved = project.symbolsAt(plain.map((one) => project.handle(one.value.name)));
|
|
270
|
+
const symbolOf = new Map(plain.map((one, index) => [one, resolved[index]]));
|
|
271
|
+
return candidates.flatMap((one) => {
|
|
272
|
+
const symbol = one.value.shorthand
|
|
273
|
+
? project.shorthandValueAt(project.handle(one.value.name))
|
|
274
|
+
: symbolOf.get(one);
|
|
275
|
+
const parameter = symbol === undefined ? undefined : erased.get(symbol.id);
|
|
276
|
+
return parameter === undefined ? [] : [{ parameter, site: one.site, argument: one.argument }];
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** The destination of each call's arguments, with the project's forwarding functions settled. */
|
|
281
|
+
interface Destinations {
|
|
282
|
+
/** The destination one argument of one call is, if it is one. */
|
|
283
|
+
at(call: CallExpression, argument: number): Destination | undefined;
|
|
284
|
+
/** Whether a call can be a destination at any argument, read without a round trip. */
|
|
285
|
+
mayReceive(call: CallExpression): boolean;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* The destinations of one project's calls. A call is one at every argument where its
|
|
290
|
+
* callee is a serializer an entry names; at the arguments its resolved signature types
|
|
291
|
+
* `unknown` or `any` where its callee is declared outside the analysis; and, where its
|
|
292
|
+
* callee is a function of the project, at each parameter the function hands on to a
|
|
293
|
+
* destination, holding back the widest of what those destinations hold back, which is
|
|
294
|
+
* settled by widening until no parameter moves.
|
|
295
|
+
*/
|
|
296
|
+
function destinations<Brand>(
|
|
297
|
+
input: DetectorInput<Brand>,
|
|
298
|
+
chains: AliasChains,
|
|
299
|
+
walked: Walked,
|
|
300
|
+
): Destinations {
|
|
301
|
+
const { project, held } = input;
|
|
302
|
+
const checker = project.checker;
|
|
303
|
+
const own = new Set(project.ownSourceFiles().map((file) => file.fileName));
|
|
304
|
+
const named = resolveEntries(project, held, [JSON_STRINGIFY, ...input.ts.serializers]).filter(
|
|
305
|
+
namesADeclaration,
|
|
306
|
+
);
|
|
307
|
+
const callees = resolvedTargets(
|
|
308
|
+
project,
|
|
309
|
+
walked.sites.flatMap((site) => calleeName(site.call.expression) ?? []),
|
|
310
|
+
);
|
|
311
|
+
const calleeOf = (call: CallExpression): TSSymbol | undefined => {
|
|
312
|
+
const name = calleeName(call.expression);
|
|
313
|
+
return name === undefined ? undefined : callees.get(name);
|
|
314
|
+
};
|
|
315
|
+
const erased = erasedParameters(project, walked.parameters);
|
|
316
|
+
const byFunction = new Map<string, Parameter[]>();
|
|
317
|
+
for (const parameter of erased.values()) {
|
|
318
|
+
byFunction.set(parameter.fn, [...(byFunction.get(parameter.fn) ?? []), parameter]);
|
|
319
|
+
}
|
|
320
|
+
const outside = (target: TSSymbol): boolean =>
|
|
321
|
+
target.declarations.length > 0 &&
|
|
322
|
+
target.declarations.every(
|
|
323
|
+
(handle) =>
|
|
324
|
+
!own.has(handle.path) &&
|
|
325
|
+
!input.consumers.some((root) => handle.path === root || handle.path.startsWith(`${root}/`)),
|
|
326
|
+
);
|
|
327
|
+
const erasedArguments = new Map<CallExpression, ReadonlySet<number>>();
|
|
328
|
+
const erasedAt = (call: CallExpression, argument: number): boolean => {
|
|
329
|
+
let indexes = erasedArguments.get(call);
|
|
330
|
+
if (indexes === undefined) {
|
|
331
|
+
const signature = checker.getResolvedSignature(call);
|
|
332
|
+
indexes = new Set(
|
|
333
|
+
call.arguments.flatMap((_unused, index) => {
|
|
334
|
+
const type =
|
|
335
|
+
signature === undefined ? undefined : checker.getParameterType(signature, index);
|
|
336
|
+
return type !== undefined && (type.flags & ERASED) !== 0 ? [index] : [];
|
|
337
|
+
}),
|
|
338
|
+
);
|
|
339
|
+
erasedArguments.set(call, indexes);
|
|
340
|
+
}
|
|
341
|
+
return indexes.has(argument);
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
const forwards = new Map<string, Reach>();
|
|
345
|
+
const keyOf = (parameter: Parameter): string => `${parameter.fn}\u0000${String(parameter.index)}`;
|
|
346
|
+
const at = (call: CallExpression, argument: number): Destination | undefined => {
|
|
347
|
+
const target = calleeOf(call);
|
|
348
|
+
if (target === undefined) {
|
|
349
|
+
return undefined;
|
|
350
|
+
}
|
|
351
|
+
const entry = named.find((one) => names(one, target, chains));
|
|
352
|
+
if (entry !== undefined) {
|
|
353
|
+
return { reach: "properties", callee: spelledEntry(entry.entry, held) };
|
|
354
|
+
}
|
|
355
|
+
const declared = chains.declarationsOf(target);
|
|
356
|
+
if (declared.length === 0) {
|
|
357
|
+
return outside(target) && erasedAt(call, argument)
|
|
358
|
+
? { reach: "conversions", callee: call.expression.getText().replace(/\s+/gu, "") }
|
|
359
|
+
: undefined;
|
|
360
|
+
}
|
|
361
|
+
for (const fn of declared) {
|
|
362
|
+
const parameter = (byFunction.get(fn) ?? []).find(
|
|
363
|
+
(one) => one.index === argument || (one.rest && one.index <= argument),
|
|
364
|
+
);
|
|
365
|
+
const reach = parameter === undefined ? undefined : forwards.get(keyOf(parameter));
|
|
366
|
+
if (reach !== undefined) {
|
|
367
|
+
return { reach, callee: held.symbols.find((symbol) => symbol.id === fn)?.name ?? fn };
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return undefined;
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
const forwarded = handedOn(project, walked.sites, erased);
|
|
374
|
+
for (let moved = true; moved;) {
|
|
375
|
+
moved = false;
|
|
376
|
+
for (const one of forwarded) {
|
|
377
|
+
const key = keyOf(one.parameter);
|
|
378
|
+
const before = forwards.get(key);
|
|
379
|
+
const after = wider(before, at(one.site.call, one.argument)?.reach);
|
|
380
|
+
if (after !== undefined && after !== before) {
|
|
381
|
+
forwards.set(key, after);
|
|
382
|
+
moved = true;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const mayReceive = (call: CallExpression): boolean => {
|
|
388
|
+
const target = calleeOf(call);
|
|
389
|
+
if (target === undefined) {
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
const declared = chains.declarationsOf(target);
|
|
393
|
+
if (named.some((one) => names(one, target, chains))) {
|
|
394
|
+
return true;
|
|
395
|
+
}
|
|
396
|
+
if (declared.length === 0) {
|
|
397
|
+
return outside(target);
|
|
398
|
+
}
|
|
399
|
+
return declared.some((fn) =>
|
|
400
|
+
(byFunction.get(fn) ?? []).some((parameter) => forwards.has(keyOf(parameter))),
|
|
401
|
+
);
|
|
402
|
+
};
|
|
403
|
+
return { at, mayReceive };
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** One argument whose value carries classes of the target into a destination. */
|
|
407
|
+
interface Flow {
|
|
408
|
+
readonly site: Node;
|
|
409
|
+
readonly classes: ReadonlySet<string>;
|
|
410
|
+
readonly to: Destination;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** Each argument of a call that can receive one whose value carries a class of the target. */
|
|
414
|
+
function flowsOf<Brand>(
|
|
415
|
+
project: ProjectView<Brand>,
|
|
416
|
+
held: Inventory,
|
|
417
|
+
sites: readonly Site[],
|
|
418
|
+
to: Destinations,
|
|
419
|
+
): readonly Flow[] {
|
|
420
|
+
const reach = valueReach(project, held);
|
|
421
|
+
const arguments_ = sites.flatMap((site) =>
|
|
422
|
+
to.mayReceive(site.call)
|
|
423
|
+
? site.call.arguments.flatMap((argument, index) => {
|
|
424
|
+
const value = evaluated(argument);
|
|
425
|
+
return holdsNoInstance(value) ? [] : [{ site, argument: index, value }];
|
|
426
|
+
})
|
|
427
|
+
: [],
|
|
428
|
+
);
|
|
429
|
+
const asked = arguments_.filter((one) => one.value.kind !== SyntaxKind.ThisKeyword);
|
|
430
|
+
const types = typesAt(
|
|
431
|
+
project,
|
|
432
|
+
asked.map((one) => one.value),
|
|
433
|
+
);
|
|
434
|
+
const typeOf = new Map<Expression, Type | undefined>(
|
|
435
|
+
asked.map((one, index) => [one.value, types[index]]),
|
|
436
|
+
);
|
|
437
|
+
return arguments_.flatMap((one) => {
|
|
438
|
+
const type = typeOf.get(one.value);
|
|
439
|
+
let classes: ReadonlySet<string> = new Set<string>();
|
|
440
|
+
if (one.value.kind === SyntaxKind.ThisKeyword) {
|
|
441
|
+
classes = new Set(one.site.self === undefined ? [] : [one.site.self]);
|
|
442
|
+
} else if (type !== undefined) {
|
|
443
|
+
classes = reach.classesOf(type);
|
|
444
|
+
}
|
|
445
|
+
const destination = classes.size === 0 ? undefined : to.at(one.site.call, one.argument);
|
|
446
|
+
return destination === undefined ? [] : [{ site: one.value, classes, to: destination }];
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* The serialization-contract detector: each property of each class whose values reach
|
|
452
|
+
* a destination, and the two conversion methods where the destination is outside the
|
|
453
|
+
* analysis, recorded at the argument with the callee named.
|
|
454
|
+
*/
|
|
455
|
+
export function serializationContract<Brand>(input: DetectorInput<Brand>): readonly Evidence[] {
|
|
456
|
+
const { project, held, targetRoot } = input;
|
|
457
|
+
const walked = walkProject(project, held);
|
|
458
|
+
const flows = flowsOf(
|
|
459
|
+
project,
|
|
460
|
+
held,
|
|
461
|
+
walked.sites,
|
|
462
|
+
destinations(input, aliasChains(project, held), walked),
|
|
463
|
+
);
|
|
464
|
+
if (flows.length === 0) {
|
|
465
|
+
return [];
|
|
466
|
+
}
|
|
467
|
+
const members = classMembers(
|
|
468
|
+
project,
|
|
469
|
+
held,
|
|
470
|
+
new Set(flows.flatMap((flow) => [...flow.classes])),
|
|
471
|
+
"instance",
|
|
472
|
+
);
|
|
473
|
+
const byId = new Map(held.symbols.map((symbol) => [symbol.id, symbol]));
|
|
474
|
+
const found: Evidence[] = [];
|
|
475
|
+
for (const flow of flows) {
|
|
476
|
+
const site = renderPosition(flow.site.getSourceFile(), targetRoot, flow.site.getStart());
|
|
477
|
+
const detail = `passed to ${flow.to.callee}`;
|
|
478
|
+
for (const member of [...flow.classes].flatMap((id) => members.get(id) ?? [])) {
|
|
479
|
+
const symbol = byId.get(member.id);
|
|
480
|
+
const isProperty = (member.flags & SymbolFlags.Property) !== 0;
|
|
481
|
+
const converts =
|
|
482
|
+
flow.to.reach === "conversions" &&
|
|
483
|
+
(member.flags & SymbolFlags.Method) !== 0 &&
|
|
484
|
+
symbol !== undefined &&
|
|
485
|
+
CONVERSIONS.has(memberComponent(symbol, byId) ?? "");
|
|
486
|
+
if (isProperty || converts) {
|
|
487
|
+
found.push({ id: member.id, detail, site });
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return found;
|
|
492
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The classes of the target a value of one type carries, as a serializer walking the
|
|
3
|
+
* value reaches them: through union and intersection constituents, array and tuple
|
|
4
|
+
* elements, and the data members of the target's classes and of object types the
|
|
5
|
+
* target writes. The walk stops at `any`, `unknown`, a type parameter, a function and
|
|
6
|
+
* an interface or class of another program, so an instance held only there is missed.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
ObjectFlags,
|
|
11
|
+
SymbolFlags,
|
|
12
|
+
type ObjectType,
|
|
13
|
+
type Symbol as TSSymbol,
|
|
14
|
+
type Type,
|
|
15
|
+
} from "@typescript/native/unstable/sync";
|
|
16
|
+
import { classNames, typesAt } from "./class-members.ts";
|
|
17
|
+
import type { Inventory } from "./inventory.ts";
|
|
18
|
+
import type { ProjectView } from "./session.ts";
|
|
19
|
+
|
|
20
|
+
/** The object types whose members the walk reads whatever declares them. */
|
|
21
|
+
const WRITTEN = ObjectFlags.ObjectLiteral | ObjectFlags.Mapped;
|
|
22
|
+
|
|
23
|
+
/** The symbols of an anonymous object type the target writes as a type. */
|
|
24
|
+
const LITERAL_SYMBOLS = SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral;
|
|
25
|
+
|
|
26
|
+
/** The classes a type's values carry, each asked of the checker once. */
|
|
27
|
+
export interface ValueReach {
|
|
28
|
+
/** The identifiers of the target's classes a value of `type` carries. */
|
|
29
|
+
classesOf(type: Type): ReadonlySet<string>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The global array types, whose element type is what an array value carries. */
|
|
33
|
+
function arrayTargets<Brand>(project: ProjectView<Brand>): ReadonlySet<number> {
|
|
34
|
+
const checker = project.checker;
|
|
35
|
+
return new Set(
|
|
36
|
+
["Array", "ReadonlyArray"].flatMap((name) => {
|
|
37
|
+
const symbol = checker.resolveName(name, SymbolFlags.Type, undefined, false);
|
|
38
|
+
return symbol === undefined ? [] : [checker.getDeclaredTypeOfSymbol(symbol).id];
|
|
39
|
+
}),
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The walk over one project's types, against the classes of `held`, the same
|
|
45
|
+
* project's inventory.
|
|
46
|
+
*/
|
|
47
|
+
export function valueReach<Brand>(project: ProjectView<Brand>, held: Inventory): ValueReach {
|
|
48
|
+
const checker = project.checker;
|
|
49
|
+
const names = [...classNames(project, held)];
|
|
50
|
+
const declared = typesAt(
|
|
51
|
+
project,
|
|
52
|
+
names.map(([, node]) => node),
|
|
53
|
+
);
|
|
54
|
+
const classOfTarget = new Map<number, string>();
|
|
55
|
+
names.forEach(([id], index) => {
|
|
56
|
+
const type = declared[index];
|
|
57
|
+
if (type !== undefined) {
|
|
58
|
+
classOfTarget.set(type.id, id);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
const arrays = arrayTargets(project);
|
|
62
|
+
const own = new Set(project.ownSourceFiles().map((file) => file.fileName));
|
|
63
|
+
const children = new Map<number, readonly Type[]>();
|
|
64
|
+
|
|
65
|
+
/** The generic target of a type reference, and the type itself otherwise. */
|
|
66
|
+
const targetOf = (type: ObjectType): Type =>
|
|
67
|
+
(type.objectFlags & ObjectFlags.Reference) !== 0 && type.isTypeReference()
|
|
68
|
+
? type.getTarget()
|
|
69
|
+
: type;
|
|
70
|
+
|
|
71
|
+
const classOf = (type: Type): string | undefined => {
|
|
72
|
+
if (!type.isObjectType()) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
return classOfTarget.get(type.id) ?? classOfTarget.get(targetOf(type).id);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** Whether the target writes the object type, so its members are what the value holds. */
|
|
79
|
+
const isWritten = (type: ObjectType): boolean => {
|
|
80
|
+
if ((type.objectFlags & WRITTEN) !== 0) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
const symbol: TSSymbol | undefined = type.getSymbol();
|
|
84
|
+
if (symbol === undefined) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
if ((type.objectFlags & ObjectFlags.Anonymous) !== 0) {
|
|
88
|
+
return (symbol.flags & LITERAL_SYMBOLS) !== 0;
|
|
89
|
+
}
|
|
90
|
+
return (
|
|
91
|
+
(symbol.flags & SymbolFlags.Interface) !== 0 &&
|
|
92
|
+
symbol.declarations.every((handle) => own.has(handle.path))
|
|
93
|
+
);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** The types a value of one object type holds in its data members. */
|
|
97
|
+
const memberTypes = (type: Type): readonly Type[] => {
|
|
98
|
+
const properties = checker
|
|
99
|
+
.getPropertiesOfType(type)
|
|
100
|
+
.filter((property) => (property.flags & SymbolFlags.Property) !== 0);
|
|
101
|
+
const types = properties.length === 0 ? [] : checker.getTypeOfSymbol(properties);
|
|
102
|
+
return [
|
|
103
|
+
...types.filter((held): held is Type => held !== undefined),
|
|
104
|
+
...checker.getIndexInfosOfType(type).map((info) => info.valueType),
|
|
105
|
+
];
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/** The types a value of one type holds, read once per type. */
|
|
109
|
+
const childrenOf = (type: Type): readonly Type[] => {
|
|
110
|
+
const known = children.get(type.id);
|
|
111
|
+
if (known !== undefined) {
|
|
112
|
+
return known;
|
|
113
|
+
}
|
|
114
|
+
let found: readonly Type[] = [];
|
|
115
|
+
if (type.isUnionType() || type.isIntersectionType()) {
|
|
116
|
+
found = type.getTypes();
|
|
117
|
+
} else if (type.isObjectType()) {
|
|
118
|
+
const target = targetOf(type);
|
|
119
|
+
const isList =
|
|
120
|
+
type.isTypeReference() &&
|
|
121
|
+
(arrays.has(target.id) ||
|
|
122
|
+
(target.isObjectType() && (target.objectFlags & ObjectFlags.Tuple) !== 0));
|
|
123
|
+
if (classOf(type) !== undefined || (!isList && isWritten(type))) {
|
|
124
|
+
found = memberTypes(type);
|
|
125
|
+
} else if (isList && type.isTypeReference()) {
|
|
126
|
+
found = checker.getTypeArguments(type);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
children.set(type.id, found);
|
|
130
|
+
return found;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
classesOf: (type) => {
|
|
135
|
+
const found = new Set<string>();
|
|
136
|
+
const seen = new Set<number>();
|
|
137
|
+
const pending: Type[] = [type];
|
|
138
|
+
for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
|
|
139
|
+
if (seen.has(next.id)) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
seen.add(next.id);
|
|
143
|
+
const owner = classOf(next);
|
|
144
|
+
if (owner !== undefined) {
|
|
145
|
+
found.add(owner);
|
|
146
|
+
}
|
|
147
|
+
pending.push(...childrenOf(next));
|
|
148
|
+
}
|
|
149
|
+
return found;
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
package/src/version.ts
CHANGED