@cplieger/deadset-ts 8.0.0-dev.2 → 8.0.0-dev.4
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 +61 -10
- package/dist/src/augmentations.js +63 -1
- package/dist/src/build-scripts.js +437 -0
- package/dist/src/conformance-record.js +1 -1
- package/dist/src/contextual-uses.js +56 -9
- package/dist/src/convention-rows.js +39 -2
- package/dist/src/conventions.js +15 -0
- package/dist/src/dependencies.js +98 -1
- package/dist/src/discover.js +14 -82
- package/dist/src/entry-points.js +9 -18
- package/dist/src/exempt.js +8 -0
- package/dist/src/findings/completion.js +66 -28
- package/dist/src/findings/intra-function.js +4 -2
- package/dist/src/findings/reads-and-writes.js +7 -6
- package/dist/src/findings/unused-declarations.js +4 -1
- package/dist/src/graph.js +5 -3
- package/dist/src/intra-function-parts.js +45 -17
- package/dist/src/kinds.js +8 -8
- package/dist/src/manifest.js +18 -0
- package/dist/src/matrix.js +38 -4
- package/dist/src/query.js +1 -0
- package/dist/src/report.js +23 -4
- package/dist/src/reporters/annotations.js +15 -8
- package/dist/src/reporters/sarif.js +12 -8
- package/dist/src/roots.js +197 -42
- package/dist/src/serialization-contract.js +590 -12
- package/dist/src/sweep.js +7 -9
- package/dist/src/test-globals.js +234 -0
- package/dist/src/type-errors.js +11 -3
- package/dist/src/unseen-loads.js +107 -0
- package/dist/src/verbs/analyze.js +24 -3
- package/dist/src/workflow-steps.js +32 -15
- package/dist/src/yaml-document.js +355 -0
- package/jsr.json +1 -1
- package/package.json +2 -2
- package/src/analysis.ts +109 -12
- package/src/augmentations.ts +77 -2
- package/src/build-scripts.ts +564 -0
- package/src/conformance-record.ts +1 -1
- package/src/contextual-uses.ts +64 -12
- package/src/convention-rows.ts +47 -2
- package/src/conventions.ts +16 -0
- package/src/dependencies.ts +110 -1
- package/src/discover.ts +14 -94
- package/src/entry-points.ts +10 -25
- package/src/exempt.ts +16 -0
- package/src/file-facts.ts +3 -0
- package/src/findings/boundary.ts +2 -0
- package/src/findings/completion.ts +76 -30
- package/src/findings/intra-function.ts +4 -2
- package/src/findings/reads-and-writes.ts +7 -6
- package/src/findings/unused-declarations.ts +6 -1
- package/src/graph.ts +5 -3
- package/src/intra-function-parts.ts +55 -16
- package/src/kinds.ts +8 -8
- package/src/manifest.ts +21 -3
- package/src/matrix.ts +51 -4
- package/src/query.ts +11 -0
- package/src/report.ts +56 -16
- package/src/reporters/annotations.ts +21 -10
- package/src/reporters/reporter.ts +1 -3
- package/src/reporters/sarif.ts +16 -8
- package/src/roots.ts +226 -46
- package/src/serialization-contract.ts +758 -12
- package/src/sweep.ts +23 -9
- package/src/test-globals.ts +275 -0
- package/src/type-errors.ts +14 -3
- package/src/unseen-loads.ts +145 -0
- package/src/verbs/analyze.ts +28 -3
- package/src/workflow-steps.ts +35 -17
- package/src/yaml-document.ts +377 -0
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* the project handing such a parameter to a destination is one, to a fixpoint, and a
|
|
7
7
|
* function invoked through `call`, `apply` or `bind` is the callee.
|
|
8
8
|
*/
|
|
9
|
-
import { isArrayLiteralExpression, isArrowFunction, isAsExpression, isCallExpression, isClassDeclaration, isClassExpression, isClassStaticBlockDeclaration, isFunctionDeclaration, isFunctionExpression, isGetAccessorDeclaration, isIdentifier, isInterfaceDeclaration, isMethodDeclaration, isNoSubstitutionTemplateLiteral, isNumericLiteral, isObjectLiteralExpression, isParenthesizedExpression, isPropertyAccessExpression, isPropertyDeclaration, isSatisfiesExpression, isSetAccessorDeclaration, isSpreadElement, isStringLiteral, isTypeLiteralNode, isVariableDeclaration, ModifierFlags, NodeFlags, SyntaxKind, } from "@typescript/native/ast";
|
|
9
|
+
import { isArrayLiteralExpression, isArrowFunction, isAsExpression, isAwaitExpression, isCallExpression, isClassDeclaration, isClassExpression, isClassStaticBlockDeclaration, isElementAccessExpression, isExpressionStatement, isFunctionDeclaration, isFunctionExpression, isFunctionLikeDeclaration, isGetAccessorDeclaration, isIdentifier, isInterfaceDeclaration, isMethodDeclaration, isNonNullExpression, isNoSubstitutionTemplateLiteral, isNumericLiteral, isObjectLiteralExpression, isParenthesizedExpression, isForInStatement, isPropertyAccessExpression, isPropertyDeclaration, isReturnStatement, isSatisfiesExpression, isSetAccessorDeclaration, isSourceFile, isSpreadAssignment, isSpreadElement, isStringLiteral, isTypeAssertion, isTypeLiteralNode, isVariableDeclaration, ModifierFlags, NodeFlags, SyntaxKind, } from "@typescript/native/ast";
|
|
10
10
|
import { SignatureKind, SymbolFlags, TypeFlags, } from "@typescript/native/sync";
|
|
11
11
|
import { aliasChains } from "./alias-chain.js";
|
|
12
12
|
import { calleeName, resolvedTargets, valuesPassed } from "./calls.js";
|
|
@@ -14,7 +14,7 @@ import { classMembers, memberComponent, typesAt } from "./class-members.js";
|
|
|
14
14
|
import { names, namesADeclaration, resolveEntries, spelledEntry, } from "./configured-declarations.js";
|
|
15
15
|
import { nodeKey } from "./inventory.js";
|
|
16
16
|
import { renderPosition } from "./position.js";
|
|
17
|
-
import { must } from "./query.js";
|
|
17
|
+
import { must, Unanswerable } from "./query.js";
|
|
18
18
|
import { erasedStores } from "./erased-stores.js";
|
|
19
19
|
import { checkedTypes, CONVERSION_MEMBERS, valueReach } from "./value-reach.js";
|
|
20
20
|
/** The serializer every run reads, whatever the configuration names. */
|
|
@@ -25,6 +25,8 @@ const CONVERSIONS = new Set(["toJSON", "toString"]);
|
|
|
25
25
|
const SERIALIZED_CONVERSION = "toJSON";
|
|
26
26
|
/** The parameter types that keep nothing of the value they are given. */
|
|
27
27
|
const ERASED = TypeFlags.Any | TypeFlags.Unknown;
|
|
28
|
+
/** The standard library's promise types, whose value is their one type argument. */
|
|
29
|
+
const PROMISES = new Set(["Promise", "PromiseLike"]);
|
|
28
30
|
/** How deep a callee's result is searched for a type parameter: `Promise<Box<T>>` is two levels. */
|
|
29
31
|
const MENTION_DEPTH = 3;
|
|
30
32
|
/** The wider of two reaches, where either may be absent. */
|
|
@@ -135,9 +137,16 @@ function throughMethod(call) {
|
|
|
135
137
|
function walkProject(project, held) {
|
|
136
138
|
const sites = [];
|
|
137
139
|
const parameters = [];
|
|
140
|
+
const initialized = new Set();
|
|
138
141
|
for (const file of project.ownSourceFiles()) {
|
|
139
142
|
const visit = (node, self) => {
|
|
140
143
|
let inner = self;
|
|
144
|
+
if (isPropertyDeclaration(node) && node.initializer !== undefined) {
|
|
145
|
+
const id = held.declarations.get(nodeKey(file, node));
|
|
146
|
+
if (id !== undefined) {
|
|
147
|
+
initialized.add(id);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
141
150
|
if (isClassDeclaration(node) || isClassExpression(node)) {
|
|
142
151
|
inner = held.declarations.get(nodeKey(file, node));
|
|
143
152
|
}
|
|
@@ -171,11 +180,33 @@ function walkProject(project, held) {
|
|
|
171
180
|
visit(child, undefined);
|
|
172
181
|
});
|
|
173
182
|
}
|
|
174
|
-
return { sites, parameters };
|
|
183
|
+
return { sites, parameters, initialized };
|
|
175
184
|
}
|
|
185
|
+
/** The calls of `Object` that read every own property of their argument by name. */
|
|
186
|
+
const ENUMERATIONS = new Set([
|
|
187
|
+
"keys",
|
|
188
|
+
"entries",
|
|
189
|
+
"values",
|
|
190
|
+
"getOwnPropertyNames",
|
|
191
|
+
"assign",
|
|
192
|
+
]);
|
|
176
193
|
/**
|
|
177
|
-
*
|
|
178
|
-
* `any`,
|
|
194
|
+
* Whether a parameter type keeps no member name of the value it is given beyond the
|
|
195
|
+
* properties it names: `unknown`, `any`, or an object type of a string index signature,
|
|
196
|
+
* under which every other member of the value is held.
|
|
197
|
+
*/
|
|
198
|
+
function keepsNoName(project, type) {
|
|
199
|
+
if ((type.flags & ERASED) !== 0) {
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
if ((type.flags & TypeFlags.Object) === 0) {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
return must(project.queries.indexInfosOf(type)).some((info) => (info.keyType.flags & TypeFlags.String) !== 0);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* The parameters that keep no member name of the value they are given, as `keepsNoName`
|
|
209
|
+
* reads it, and a rest parameter whose elements keep none, each by its symbol.
|
|
179
210
|
*/
|
|
180
211
|
function erasedParameters(project, parameters) {
|
|
181
212
|
const types = typesAt(project, parameters.map((parameter) => parameter.name));
|
|
@@ -185,10 +216,10 @@ function erasedParameters(project, parameters) {
|
|
|
185
216
|
return false;
|
|
186
217
|
}
|
|
187
218
|
if (!parameter.rest) {
|
|
188
|
-
return (type
|
|
219
|
+
return keepsNoName(project, type);
|
|
189
220
|
}
|
|
190
221
|
const [element] = must(project.queries.typeArguments(type));
|
|
191
|
-
return element !== undefined && (element
|
|
222
|
+
return element !== undefined && keepsNoName(project, element);
|
|
192
223
|
});
|
|
193
224
|
const bySymbol = new Map();
|
|
194
225
|
project
|
|
@@ -202,6 +233,103 @@ function erasedParameters(project, parameters) {
|
|
|
202
233
|
});
|
|
203
234
|
return bySymbol;
|
|
204
235
|
}
|
|
236
|
+
/** Whether one node is a function, whose body runs only where something calls it. */
|
|
237
|
+
function isFunctionNode(node) {
|
|
238
|
+
return (isFunctionDeclaration(node) ||
|
|
239
|
+
isFunctionExpression(node) ||
|
|
240
|
+
isArrowFunction(node) ||
|
|
241
|
+
isMethodDeclaration(node) ||
|
|
242
|
+
isGetAccessorDeclaration(node) ||
|
|
243
|
+
isSetAccessorDeclaration(node));
|
|
244
|
+
}
|
|
245
|
+
/** The name a function nested in another is called by: its own, or the variable it initializes. */
|
|
246
|
+
function nestedName(fn) {
|
|
247
|
+
if (isFunctionDeclaration(fn)) {
|
|
248
|
+
return fn.name?.text;
|
|
249
|
+
}
|
|
250
|
+
let at = fn.parent;
|
|
251
|
+
while (isParenthesizedExpression(at) || isAsExpression(at) || isSatisfiesExpression(at)) {
|
|
252
|
+
at = at.parent;
|
|
253
|
+
}
|
|
254
|
+
return isVariableDeclaration(at) && isIdentifier(at.name) ? at.name.text : undefined;
|
|
255
|
+
}
|
|
256
|
+
/** The names one function's body writes as identifiers, other than the names its declarations bind. */
|
|
257
|
+
function namesWritten(fn) {
|
|
258
|
+
const found = new Set();
|
|
259
|
+
const visit = (node) => {
|
|
260
|
+
const declares = (isFunctionDeclaration(node.parent) || isVariableDeclaration(node.parent)) &&
|
|
261
|
+
node.parent.name === node;
|
|
262
|
+
if (isIdentifier(node) && !declares) {
|
|
263
|
+
found.add(node.text);
|
|
264
|
+
}
|
|
265
|
+
node.forEachChild(visit);
|
|
266
|
+
};
|
|
267
|
+
fn.forEachChild(visit);
|
|
268
|
+
return found;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* The erased parameters their own function enumerates: one an `Object.keys`, `entries`,
|
|
272
|
+
* `values`, `getOwnPropertyNames` or `assign` call takes, `Object` the standard library's,
|
|
273
|
+
* or a `for…in` statement walks, which reads every property of the value by its name. A
|
|
274
|
+
* nested function is read as the function's own only where its name is written in it, or
|
|
275
|
+
* it has none, a callback or a function called where it is written.
|
|
276
|
+
*/
|
|
277
|
+
function enumeratedParameters(project, erased) {
|
|
278
|
+
const asked = [];
|
|
279
|
+
for (const parameter of erased.values()) {
|
|
280
|
+
const fn = parameter.name.parent.parent;
|
|
281
|
+
const spelled = parameter.name.getText();
|
|
282
|
+
const written = namesWritten(fn);
|
|
283
|
+
const visit = (node) => {
|
|
284
|
+
if (isFunctionNode(node)) {
|
|
285
|
+
const name = nestedName(node);
|
|
286
|
+
if (name !== undefined && !written.has(name)) {
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
const read = [];
|
|
291
|
+
let object;
|
|
292
|
+
if (isCallExpression(node)) {
|
|
293
|
+
const callee = node.expression;
|
|
294
|
+
if (isPropertyAccessExpression(callee) &&
|
|
295
|
+
isIdentifier(callee.expression) &&
|
|
296
|
+
callee.expression.text === "Object" &&
|
|
297
|
+
ENUMERATIONS.has(callee.name.text)) {
|
|
298
|
+
object = callee.expression;
|
|
299
|
+
// `Object.assign` reads its sources, every argument after the target.
|
|
300
|
+
read.push(...node.arguments.slice(...(callee.name.text === "assign" ? [1] : [0, 1])));
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
else if (isForInStatement(node)) {
|
|
304
|
+
read.push(node.expression);
|
|
305
|
+
}
|
|
306
|
+
for (const value of read.map(evaluated)) {
|
|
307
|
+
if (isIdentifier(value) && value.text === spelled) {
|
|
308
|
+
asked.push({ node: value, parameter, object });
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
node.forEachChild(visit);
|
|
312
|
+
};
|
|
313
|
+
fn.forEachChild(visit);
|
|
314
|
+
}
|
|
315
|
+
const symbols = project.symbolsAt(asked.map((one) => project.handle(one.node))).map(must);
|
|
316
|
+
const objects = asked.flatMap((one) => (one.object === undefined ? [] : [one.object]));
|
|
317
|
+
const own = project.ownPaths();
|
|
318
|
+
const standard = new Set(project
|
|
319
|
+
.symbolsAt(objects.map((node) => project.handle(node)))
|
|
320
|
+
.map(must)
|
|
321
|
+
.flatMap((symbol, at) => symbol?.declarations.every((handle) => !own.has(handle.path)) === true ? [objects[at]] : []));
|
|
322
|
+
return [
|
|
323
|
+
...new Set(asked.flatMap((one, at) => {
|
|
324
|
+
const symbol = symbols[at];
|
|
325
|
+
return symbol !== undefined &&
|
|
326
|
+
erased.get(symbol.id) === one.parameter &&
|
|
327
|
+
(one.object === undefined || standard.has(one.object))
|
|
328
|
+
? [one.parameter]
|
|
329
|
+
: [];
|
|
330
|
+
})),
|
|
331
|
+
];
|
|
332
|
+
}
|
|
205
333
|
/** Each place a call's argument passes one of the erased parameters it can see. */
|
|
206
334
|
function handedOn(project, sites, erased, invocationOf) {
|
|
207
335
|
const spellings = new Set([...erased.values()].map((parameter) => parameter.name.getText()));
|
|
@@ -285,8 +413,8 @@ function implementedOwners(project, inAnalysis) {
|
|
|
285
413
|
* callee is a serializer an entry names; at the arguments its resolved signature types
|
|
286
414
|
* `unknown` or `any` where its callee is declared outside the analysis or only ambiently
|
|
287
415
|
* with no implementation in it; and, where its callee is a function of the project, at each
|
|
288
|
-
* parameter the function hands on to a destination, holding back the widest
|
|
289
|
-
*
|
|
416
|
+
* parameter the function enumerates or hands on to a destination, holding back the widest
|
|
417
|
+
* of what those hold back, settled by widening until no parameter moves.
|
|
290
418
|
*/
|
|
291
419
|
function destinations(input, chains, walked) {
|
|
292
420
|
const { project, held } = input;
|
|
@@ -478,6 +606,9 @@ function destinations(input, chains, walked) {
|
|
|
478
606
|
}
|
|
479
607
|
return undefined;
|
|
480
608
|
};
|
|
609
|
+
for (const parameter of enumeratedParameters(project, erased)) {
|
|
610
|
+
forwards.set(keyOf(parameter), "properties");
|
|
611
|
+
}
|
|
481
612
|
const forwarded = handedOn(project, walked.sites, erased, invocation);
|
|
482
613
|
for (let moved = true; moved;) {
|
|
483
614
|
moved = false;
|
|
@@ -551,6 +682,14 @@ function flowsOf(project, held, sites, to, stores) {
|
|
|
551
682
|
},
|
|
552
683
|
];
|
|
553
684
|
});
|
|
685
|
+
const generic = typeParameterFlows(project, reach, sites, arguments_.flatMap((one) => {
|
|
686
|
+
const type = typeOf.get(one.value);
|
|
687
|
+
const parameters = type === undefined ? [] : typeParametersOf(project, type, 0);
|
|
688
|
+
const destination = parameters.length === 0 ? undefined : to.at(one.site.call, one.argument);
|
|
689
|
+
return destination === undefined
|
|
690
|
+
? []
|
|
691
|
+
: parameters.map((parameter) => ({ parameter, to: destination }));
|
|
692
|
+
}));
|
|
554
693
|
const left = stores.outside.flatMap((store) => {
|
|
555
694
|
const reached = reach.reachedFrom(store.type);
|
|
556
695
|
return reached.classes.size === 0 && reached.members.length === 0
|
|
@@ -565,7 +704,155 @@ function flowsOf(project, held, sites, to, stores) {
|
|
|
565
704
|
},
|
|
566
705
|
];
|
|
567
706
|
});
|
|
568
|
-
return [...passed, ...left];
|
|
707
|
+
return [...passed, ...generic, ...left];
|
|
708
|
+
}
|
|
709
|
+
/** The type parameters one type names, through constituents and type arguments. */
|
|
710
|
+
function typeParametersOf(project, type, depth) {
|
|
711
|
+
if (type.isTypeParameter()) {
|
|
712
|
+
return [type];
|
|
713
|
+
}
|
|
714
|
+
if (depth >= MENTION_DEPTH) {
|
|
715
|
+
return [];
|
|
716
|
+
}
|
|
717
|
+
const { queries } = project;
|
|
718
|
+
return [
|
|
719
|
+
...(type.isUnionType() || type.isIntersectionType() ? must(queries.constituents(type)) : []),
|
|
720
|
+
...must(queries.typeArguments(type)),
|
|
721
|
+
].flatMap((one) => typeParametersOf(project, one, depth + 1));
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* The type parameters one type names, by identifier: through constituents, type arguments,
|
|
725
|
+
* an alias's arguments and the parameters and results of its call signatures, so a callback
|
|
726
|
+
* parameter's result names the parameter its callee's result does.
|
|
727
|
+
*/
|
|
728
|
+
function namedTypeParameters(project, type, depth) {
|
|
729
|
+
const { queries } = project;
|
|
730
|
+
const aliased = type.aliasTypeArguments;
|
|
731
|
+
if (type.isTypeParameter()) {
|
|
732
|
+
return [type.id];
|
|
733
|
+
}
|
|
734
|
+
if (depth >= MENTION_DEPTH) {
|
|
735
|
+
return aliased ?? [];
|
|
736
|
+
}
|
|
737
|
+
return [
|
|
738
|
+
...(aliased ?? []),
|
|
739
|
+
...[
|
|
740
|
+
...(type.isUnionType() || type.isIntersectionType() ? must(queries.constituents(type)) : []),
|
|
741
|
+
...must(queries.typeArguments(type)),
|
|
742
|
+
...must(queries.signaturesOf(type, SignatureKind.Call)).flatMap((signature) => [
|
|
743
|
+
...signature.parameters.flatMap((_parameter, at) => [
|
|
744
|
+
must(queries.typeAtPosition(signature, at)),
|
|
745
|
+
]),
|
|
746
|
+
...[must(queries.returnTypeOf(signature))].filter((one) => one !== undefined),
|
|
747
|
+
]),
|
|
748
|
+
].flatMap((one) => namedTypeParameters(project, one, depth + 1)),
|
|
749
|
+
];
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* The types an instantiated type puts where a declared type names `parameter`: matched
|
|
753
|
+
* through type arguments, and in a union every constituent the declared union does not
|
|
754
|
+
* itself name.
|
|
755
|
+
*/
|
|
756
|
+
function bound(project, declared, instantiated, parameter, depth) {
|
|
757
|
+
if (declared.id === parameter.id) {
|
|
758
|
+
return [instantiated];
|
|
759
|
+
}
|
|
760
|
+
if (depth >= MENTION_DEPTH) {
|
|
761
|
+
return [];
|
|
762
|
+
}
|
|
763
|
+
const { queries } = project;
|
|
764
|
+
if (declared.isUnionType()) {
|
|
765
|
+
const parts = must(queries.constituents(declared));
|
|
766
|
+
if (parts.some((part) => part.id === parameter.id)) {
|
|
767
|
+
const kept = new Set(parts.map((part) => part.id));
|
|
768
|
+
const given = instantiated.isUnionType()
|
|
769
|
+
? must(queries.constituents(instantiated))
|
|
770
|
+
: [instantiated];
|
|
771
|
+
return given.filter((part) => !kept.has(part.id));
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
const declaredArguments = must(queries.typeArguments(declared));
|
|
775
|
+
const givenArguments = must(queries.typeArguments(instantiated));
|
|
776
|
+
return declaredArguments.length !== givenArguments.length
|
|
777
|
+
? []
|
|
778
|
+
: declaredArguments.flatMap((one, at) => {
|
|
779
|
+
const given = givenArguments[at];
|
|
780
|
+
return given === undefined ? [] : bound(project, one, given, parameter, depth + 1);
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* The flows a type parameter of a function of the project carries where a value its type
|
|
785
|
+
* names reaches a destination: each call of the function passes its type argument there,
|
|
786
|
+
* written or inferred from the arguments, and a type argument that is a type parameter
|
|
787
|
+
* of the calling function is followed on to that function's calls, to a fixpoint.
|
|
788
|
+
*/
|
|
789
|
+
function typeParameterFlows(project, reach, sites, pending) {
|
|
790
|
+
const { queries } = project;
|
|
791
|
+
const byName = new Map();
|
|
792
|
+
for (const site of sites) {
|
|
793
|
+
const name = calleeName(site.call.expression)?.getText();
|
|
794
|
+
if (name !== undefined) {
|
|
795
|
+
byName.set(name, [...(byName.get(name) ?? []), site]);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
const found = [];
|
|
799
|
+
const seen = new Set();
|
|
800
|
+
for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
|
|
801
|
+
const { parameter, to } = next;
|
|
802
|
+
if (seen.has(parameter.id)) {
|
|
803
|
+
continue;
|
|
804
|
+
}
|
|
805
|
+
seen.add(parameter.id);
|
|
806
|
+
const symbol = must(queries.symbolOfType(parameter));
|
|
807
|
+
const [handle] = symbol?.declarations ?? [];
|
|
808
|
+
const declaration = handle === undefined ? undefined : project.declarationAt(handle)?.node;
|
|
809
|
+
const fn = declaration?.parent;
|
|
810
|
+
const index = fn === undefined || !isFunctionLikeDeclaration(fn)
|
|
811
|
+
? -1
|
|
812
|
+
: (fn.typeParameters ?? []).findIndex((one) => one === declaration);
|
|
813
|
+
const name = fn === undefined
|
|
814
|
+
? undefined
|
|
815
|
+
: isMethodDeclaration(fn) && isIdentifier(fn.name)
|
|
816
|
+
? fn.name.text
|
|
817
|
+
: nestedName(fn);
|
|
818
|
+
if (symbol === undefined || fn === undefined || index < 0 || name === undefined) {
|
|
819
|
+
continue;
|
|
820
|
+
}
|
|
821
|
+
for (const site of byName.get(name) ?? []) {
|
|
822
|
+
const resolved = must(queries.resolvedSignature(site.call));
|
|
823
|
+
const declared = resolved === undefined ? undefined : must(queries.genericOf(resolved));
|
|
824
|
+
const called = declared?.declaration === undefined
|
|
825
|
+
? undefined
|
|
826
|
+
: project.declarationAt(declared.declaration)?.node;
|
|
827
|
+
if (resolved === undefined || declared === undefined || called !== fn) {
|
|
828
|
+
continue;
|
|
829
|
+
}
|
|
830
|
+
const written = site.call.typeArguments?.[index];
|
|
831
|
+
const given = written === undefined
|
|
832
|
+
? site.call.arguments.flatMap((_argument, at) => {
|
|
833
|
+
const from = must(queries.parameterType(declared, at));
|
|
834
|
+
const into = must(queries.parameterType(resolved, at));
|
|
835
|
+
return from === undefined || into === undefined
|
|
836
|
+
? []
|
|
837
|
+
: bound(project, from, into, parameter, 0);
|
|
838
|
+
})
|
|
839
|
+
: typesAt(project, [written]).filter((type) => type !== undefined);
|
|
840
|
+
for (const type of given) {
|
|
841
|
+
const reached = reach.reachedFrom(type);
|
|
842
|
+
if (reached.classes.size > 0 || reached.members.length > 0) {
|
|
843
|
+
found.push({
|
|
844
|
+
site: site.call,
|
|
845
|
+
classes: reached.classes,
|
|
846
|
+
members: reached.members,
|
|
847
|
+
to,
|
|
848
|
+
detail: `passed to ${to.callee} through the type parameter ${symbol.name}`,
|
|
849
|
+
});
|
|
850
|
+
}
|
|
851
|
+
pending.push(...typeParametersOf(project, type, 0).map((one) => ({ parameter: one, to })));
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
return found;
|
|
569
856
|
}
|
|
570
857
|
/**
|
|
571
858
|
* The serialization-contract detector: each data member and `toJSON` of each class,
|
|
@@ -588,6 +875,10 @@ export function serializationContract(input) {
|
|
|
588
875
|
const holdsBack = (flags, name, to) => (flags & SymbolFlags.Property) !== 0 ||
|
|
589
876
|
((flags & CONVERSION_MEMBERS) !== 0 &&
|
|
590
877
|
(name === SERIALIZED_CONVERSION || (to.reach === "conversions" && CONVERSIONS.has(name))));
|
|
878
|
+
// The receiver reads only what the program wrote, so a data member holds while written.
|
|
879
|
+
const evidence = (id, flags, detail, site) => (flags & SymbolFlags.Property) !== 0 && !walked.initialized.has(id)
|
|
880
|
+
? { id, detail, site, whileWritten: true }
|
|
881
|
+
: { id, detail, site };
|
|
591
882
|
const found = [];
|
|
592
883
|
for (const flow of flows) {
|
|
593
884
|
const site = renderPosition(flow.site.getSourceFile(), targetRoot, flow.site.getStart());
|
|
@@ -596,16 +887,303 @@ export function serializationContract(input) {
|
|
|
596
887
|
const symbol = byId.get(member.id);
|
|
597
888
|
const name = symbol === undefined ? "" : (memberComponent(symbol, byId) ?? "");
|
|
598
889
|
if (holdsBack(member.flags, name, flow.to)) {
|
|
599
|
-
found.push(
|
|
890
|
+
found.push(evidence(member.id, member.flags, detail, site));
|
|
600
891
|
}
|
|
601
892
|
}
|
|
602
893
|
for (const member of flow.members) {
|
|
603
894
|
if (holdsBack(member.flags, member.name, flow.to)) {
|
|
604
895
|
for (const id of chains.declarationsOf(member)) {
|
|
605
|
-
found.push(
|
|
896
|
+
found.push(evidence(id, member.flags, detail, site));
|
|
606
897
|
}
|
|
607
898
|
}
|
|
608
899
|
}
|
|
609
900
|
}
|
|
610
901
|
return found;
|
|
611
902
|
}
|
|
903
|
+
/**
|
|
904
|
+
* The data members the decodes and copies of `files` fill, as {@link Decoded} names them.
|
|
905
|
+
* A generic declared outside the analysis, or only ambiently, fills the type that a type
|
|
906
|
+
* parameter of its own no parameter names binds, where the awaited result carries that
|
|
907
|
+
* type through its data members rather than only naming it, as a store's methods do: no
|
|
908
|
+
* argument hands the value over, so it fills it where the analysis cannot see, and may
|
|
909
|
+
* read it. Where the checker leaves a question unanswered, every data member is `unanswered`.
|
|
910
|
+
*/
|
|
911
|
+
export function decodedMembers(project, held, files, consumers) {
|
|
912
|
+
try {
|
|
913
|
+
return { ...decodedOf(project, held, files, consumers), unanswered: [] };
|
|
914
|
+
}
|
|
915
|
+
catch (error) {
|
|
916
|
+
if (!(error instanceof Unanswerable)) {
|
|
917
|
+
throw error;
|
|
918
|
+
}
|
|
919
|
+
return {
|
|
920
|
+
filled: [],
|
|
921
|
+
parsed: [],
|
|
922
|
+
copied: [],
|
|
923
|
+
unanswered: held.symbols
|
|
924
|
+
.filter((symbol) => symbol.kind === "class-member" || symbol.kind === "type-member")
|
|
925
|
+
.map((symbol) => symbol.id),
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
function decodedOf(project, held, files, consumers) {
|
|
930
|
+
const { queries } = project;
|
|
931
|
+
const own = project.ownPaths();
|
|
932
|
+
const inAnalysis = (path) => own.has(path) || consumers.some((root) => path === root || path.startsWith(`${root}/`));
|
|
933
|
+
const calls = [];
|
|
934
|
+
const untyped = [];
|
|
935
|
+
const spreads = [];
|
|
936
|
+
const noteRead = (node) => {
|
|
937
|
+
const at = readAs(node);
|
|
938
|
+
if (at.tops.length > 0 || at.asserted.length > 0) {
|
|
939
|
+
untyped.push(node);
|
|
940
|
+
}
|
|
941
|
+
};
|
|
942
|
+
const visit = (node) => {
|
|
943
|
+
if (isCallExpression(node)) {
|
|
944
|
+
calls.push(node);
|
|
945
|
+
noteRead(node);
|
|
946
|
+
}
|
|
947
|
+
else if (isPropertyAccessExpression(node) || isElementAccessExpression(node)) {
|
|
948
|
+
noteRead(node);
|
|
949
|
+
}
|
|
950
|
+
else if (isSpreadAssignment(node)) {
|
|
951
|
+
spreads.push(node);
|
|
952
|
+
}
|
|
953
|
+
node.forEachChild(visit);
|
|
954
|
+
};
|
|
955
|
+
for (const file of files) {
|
|
956
|
+
file.forEachChild(visit);
|
|
957
|
+
}
|
|
958
|
+
const filled = [];
|
|
959
|
+
const resolved = queries.resolvedSignatures(calls).map(must);
|
|
960
|
+
for (const signature of resolved) {
|
|
961
|
+
const declared = signature?.target === undefined ? undefined : must(queries.genericOf(signature));
|
|
962
|
+
const declaration = declared?.declaration;
|
|
963
|
+
const generic = declared?.typeParameters ?? [];
|
|
964
|
+
if (signature === undefined || declared === undefined || generic.length === 0) {
|
|
965
|
+
continue;
|
|
966
|
+
}
|
|
967
|
+
const node = declaration === undefined ? undefined : project.declarationAt(declaration)?.node;
|
|
968
|
+
if (declaration === undefined ||
|
|
969
|
+
(inAnalysis(declaration.path) &&
|
|
970
|
+
(node === undefined || (node.flags & NodeFlags.Ambient) === 0))) {
|
|
971
|
+
continue;
|
|
972
|
+
}
|
|
973
|
+
const result = must(queries.returnTypeOf(declared));
|
|
974
|
+
const given = must(queries.returnTypeOf(signature));
|
|
975
|
+
if (result === undefined || given === undefined) {
|
|
976
|
+
continue;
|
|
977
|
+
}
|
|
978
|
+
const passed = new Set(declared.parameters.flatMap((_parameter, at) => {
|
|
979
|
+
const type = must(queries.parameterType(declared, at));
|
|
980
|
+
return type === undefined ? [] : namedTypeParameters(project, type, 0);
|
|
981
|
+
}));
|
|
982
|
+
const types = typeParametersOf(project, result, 0)
|
|
983
|
+
.filter((parameter) => generic.includes(parameter.id) && !passed.has(parameter.id))
|
|
984
|
+
.flatMap((parameter) => bound(project, result, given, parameter, 0));
|
|
985
|
+
if (types.length > 0) {
|
|
986
|
+
filled.push({ carried: awaited(project, given), types });
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
const read = readTypes(project, untyped);
|
|
990
|
+
const copies = copiedNames(project, spreads, calls);
|
|
991
|
+
if (filled.length === 0 &&
|
|
992
|
+
read.parsed.length === 0 &&
|
|
993
|
+
read.made.length === 0 &&
|
|
994
|
+
copies.length === 0) {
|
|
995
|
+
return { filled: [], parsed: [], copied: [] };
|
|
996
|
+
}
|
|
997
|
+
const reach = valueReach(project, held);
|
|
998
|
+
const chains = aliasChains(project, held);
|
|
999
|
+
const members = (type) => {
|
|
1000
|
+
const reached = reach.reachedFrom(type);
|
|
1001
|
+
const ofClasses = classMembers(project, held, reached.classes, "instance");
|
|
1002
|
+
return [
|
|
1003
|
+
...[...reached.classes].flatMap((id) => (ofClasses.get(id) ?? []).flatMap((member) => (member.flags & SymbolFlags.Property) !== 0 ? [member.id] : [])),
|
|
1004
|
+
...reached.members.flatMap((member) => (member.flags & SymbolFlags.Property) !== 0 ? chains.declarationsOf(member) : []),
|
|
1005
|
+
];
|
|
1006
|
+
};
|
|
1007
|
+
return {
|
|
1008
|
+
filled: [
|
|
1009
|
+
...new Set([
|
|
1010
|
+
...filled.flatMap(({ carried, types }) => {
|
|
1011
|
+
const carries = new Set(members(carried));
|
|
1012
|
+
return types.flatMap((type) => members(type).filter((id) => carries.has(id)));
|
|
1013
|
+
}),
|
|
1014
|
+
...read.made.flatMap(members),
|
|
1015
|
+
]),
|
|
1016
|
+
],
|
|
1017
|
+
parsed: [...new Set(read.parsed.flatMap(members))],
|
|
1018
|
+
copied: [
|
|
1019
|
+
...new Set(copies.flatMap(({ into, names }) => must(queries.constituents(into)).flatMap((part) => must(queries.propertiesOf(part)).flatMap((property) => (property.flags & SymbolFlags.Property) !== 0 && names.has(property.name)
|
|
1020
|
+
? chains.declarationsOf(property)
|
|
1021
|
+
: [])))),
|
|
1022
|
+
],
|
|
1023
|
+
};
|
|
1024
|
+
}
|
|
1025
|
+
/**
|
|
1026
|
+
* Where one value is read as a type: each `as` and angle-bracket assertion around it,
|
|
1027
|
+
* through parentheses, `await` and `!`, and the outermost expression unless a statement
|
|
1028
|
+
* of its own or the object or callee of a further access holds it. A `satisfies` check
|
|
1029
|
+
* is stepped through and types nothing, its expression's type being its operand's. A
|
|
1030
|
+
* callback's result is read as well where the call taking the callback is, which may
|
|
1031
|
+
* hand it on inside its result, as `map` does.
|
|
1032
|
+
*/
|
|
1033
|
+
function readAs(value, depth = 0) {
|
|
1034
|
+
const asserted = [];
|
|
1035
|
+
let at = value;
|
|
1036
|
+
for (;;) {
|
|
1037
|
+
const parent = at.parent;
|
|
1038
|
+
if (isAsExpression(parent) || isTypeAssertion(parent)) {
|
|
1039
|
+
asserted.push(parent.type);
|
|
1040
|
+
}
|
|
1041
|
+
else if (!isParenthesizedExpression(parent) &&
|
|
1042
|
+
!isAwaitExpression(parent) &&
|
|
1043
|
+
!isNonNullExpression(parent) &&
|
|
1044
|
+
!isSatisfiesExpression(parent)) {
|
|
1045
|
+
break;
|
|
1046
|
+
}
|
|
1047
|
+
at = parent;
|
|
1048
|
+
}
|
|
1049
|
+
const parent = at.parent;
|
|
1050
|
+
const unused = isExpressionStatement(parent) ||
|
|
1051
|
+
((isPropertyAccessExpression(parent) ||
|
|
1052
|
+
isElementAccessExpression(parent) ||
|
|
1053
|
+
isCallExpression(parent)) &&
|
|
1054
|
+
parent.expression === at);
|
|
1055
|
+
const call = depth < MENTION_DEPTH ? callTaking(at) : undefined;
|
|
1056
|
+
const outer = call === undefined ? undefined : readAs(call, depth + 1);
|
|
1057
|
+
return {
|
|
1058
|
+
asserted: [...asserted, ...(outer?.asserted ?? [])],
|
|
1059
|
+
tops: [...(unused ? [] : [at]), ...(outer?.tops ?? [])],
|
|
1060
|
+
};
|
|
1061
|
+
}
|
|
1062
|
+
/** The call one value is returned to as a callback's result, an argument of that call. */
|
|
1063
|
+
function callTaking(value) {
|
|
1064
|
+
const parent = value.parent;
|
|
1065
|
+
let callback;
|
|
1066
|
+
if (isArrowFunction(parent) && parent.body === value) {
|
|
1067
|
+
callback = parent;
|
|
1068
|
+
}
|
|
1069
|
+
else if (isReturnStatement(parent)) {
|
|
1070
|
+
let at = parent.parent;
|
|
1071
|
+
while (!isFunctionLikeDeclaration(at) && !isSourceFile(at)) {
|
|
1072
|
+
at = at.parent;
|
|
1073
|
+
}
|
|
1074
|
+
callback = isArrowFunction(at) || isFunctionExpression(at) ? at : undefined;
|
|
1075
|
+
}
|
|
1076
|
+
let at = callback?.parent;
|
|
1077
|
+
while (at !== undefined && isParenthesizedExpression(at)) {
|
|
1078
|
+
at = at.parent;
|
|
1079
|
+
}
|
|
1080
|
+
return at !== undefined && isCallExpression(at) && at.expression !== callback ? at : undefined;
|
|
1081
|
+
}
|
|
1082
|
+
/** The global the standard library declares one name of `JSON` or `Object` as. */
|
|
1083
|
+
function standardGlobal(callee, global, name) {
|
|
1084
|
+
return isPropertyAccessExpression(callee) &&
|
|
1085
|
+
isIdentifier(callee.expression) &&
|
|
1086
|
+
callee.expression.text === global &&
|
|
1087
|
+
callee.name.text === name
|
|
1088
|
+
? callee.expression
|
|
1089
|
+
: undefined;
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
* The types each value of `values` typed `any` or `unknown` is read as ({@link readAs}):
|
|
1093
|
+
* `parsed` for a `JSON.parse` call of the standard library, `made` for any other call or
|
|
1094
|
+
* property, whose maker the analysis cannot see. `JSON` must be the standard library's
|
|
1095
|
+
* global.
|
|
1096
|
+
*/
|
|
1097
|
+
function readTypes(project, values) {
|
|
1098
|
+
if (values.length === 0) {
|
|
1099
|
+
return { parsed: [], made: [] };
|
|
1100
|
+
}
|
|
1101
|
+
const own = project.ownPaths();
|
|
1102
|
+
const parses = values.map((value) => isCallExpression(value) ? standardGlobal(value.expression, "JSON", "parse") : undefined);
|
|
1103
|
+
const globals = parses.flatMap((global) => (global === undefined ? [] : [global]));
|
|
1104
|
+
const standard = new Set(project
|
|
1105
|
+
.symbolsAt(globals.map((global) => project.handle(global)))
|
|
1106
|
+
.map(must)
|
|
1107
|
+
.flatMap((symbol, at) => symbol?.declarations.every((handle) => !own.has(handle.path)) === true ? [globals[at]] : []));
|
|
1108
|
+
// An `await` around the value reads the value it resolves to.
|
|
1109
|
+
const valueTypes = typesAt(project, values.map((value) => (isAwaitExpression(value.parent) ? value.parent : value)));
|
|
1110
|
+
const sites = values.flatMap((value, at) => {
|
|
1111
|
+
const parse = parses[at];
|
|
1112
|
+
const parsed = parse !== undefined && standard.has(parse);
|
|
1113
|
+
const type = valueTypes[at];
|
|
1114
|
+
const untyped = type !== undefined && (type.flags & (TypeFlags.Any | TypeFlags.Unknown)) !== 0;
|
|
1115
|
+
return parsed || untyped ? [{ ...readAs(value), parsed }] : [];
|
|
1116
|
+
});
|
|
1117
|
+
// Each type node and each outermost expression, with whether a `JSON.parse` call is read as it.
|
|
1118
|
+
const typed = sites.flatMap((site) => site.asserted.map((node) => ({ node, parsed: site.parsed })));
|
|
1119
|
+
const placed = sites.flatMap((site) => site.tops.map((node) => ({ node, parsed: site.parsed })));
|
|
1120
|
+
const read = [
|
|
1121
|
+
...typesAt(project, typed.map((one) => one.node)).map((type, at) => ({ type, parsed: typed[at]?.parsed })),
|
|
1122
|
+
...project.queries
|
|
1123
|
+
.contextualTypes(placed.map((one) => one.node))
|
|
1124
|
+
.map(must)
|
|
1125
|
+
.map((type, at) => ({ type, parsed: placed[at]?.parsed })),
|
|
1126
|
+
];
|
|
1127
|
+
const typesWhere = (parsed) => read.flatMap((one) => (one.type !== undefined && one.parsed === parsed ? [one.type] : []));
|
|
1128
|
+
return { parsed: typesWhere(true), made: typesWhere(false) };
|
|
1129
|
+
}
|
|
1130
|
+
/**
|
|
1131
|
+
* The copies the spreads and calls of `files` make: for each spread in an object literal,
|
|
1132
|
+
* the type the literal is read as, and for each `Object.assign` call of the standard
|
|
1133
|
+
* library, its first argument's type, with the member names of the copied values' types.
|
|
1134
|
+
*/
|
|
1135
|
+
function copiedNames(project, spreads, calls) {
|
|
1136
|
+
const own = project.ownPaths();
|
|
1137
|
+
const assigns = calls.flatMap((call) => {
|
|
1138
|
+
const global = standardGlobal(call.expression, "Object", "assign");
|
|
1139
|
+
const [target, ...sources] = call.arguments;
|
|
1140
|
+
return global === undefined || target === undefined || sources.length === 0
|
|
1141
|
+
? []
|
|
1142
|
+
: [{ global, target, sources: sources.filter((source) => !isSpreadElement(source)) }];
|
|
1143
|
+
});
|
|
1144
|
+
const standard = project
|
|
1145
|
+
.symbolsAt(assigns.map((one) => project.handle(one.global)))
|
|
1146
|
+
.map(must)
|
|
1147
|
+
.map((symbol) => symbol?.declarations.every((handle) => !own.has(handle.path)) === true);
|
|
1148
|
+
const assigned = assigns.filter((_one, at) => standard[at] === true);
|
|
1149
|
+
const literals = spreads.flatMap((spread) => isObjectLiteralExpression(spread.parent)
|
|
1150
|
+
? [{ literal: spread.parent, source: spread.expression }]
|
|
1151
|
+
: []);
|
|
1152
|
+
const contexts = project.queries.contextualTypes(literals.map((one) => one.literal)).map(must);
|
|
1153
|
+
const targets = typesAt(project, assigned.map((one) => one.target));
|
|
1154
|
+
const spreadTypes = typesAt(project, literals.map((one) => one.source));
|
|
1155
|
+
const sourceTypes = assigned.map((one) => typesAt(project, one.sources));
|
|
1156
|
+
const namesOf = (types) => new Set(types.flatMap((type) => type === undefined
|
|
1157
|
+
? []
|
|
1158
|
+
: must(project.queries.propertiesOf(type)).map((property) => property.name)));
|
|
1159
|
+
const found = [];
|
|
1160
|
+
const add = (into, names) => {
|
|
1161
|
+
if (into !== undefined && names.size > 0) {
|
|
1162
|
+
found.push({ into, names });
|
|
1163
|
+
}
|
|
1164
|
+
};
|
|
1165
|
+
contexts.forEach((into, at) => {
|
|
1166
|
+
add(into, namesOf([spreadTypes[at]]));
|
|
1167
|
+
});
|
|
1168
|
+
targets.forEach((into, at) => {
|
|
1169
|
+
add(into, namesOf(sourceTypes[at] ?? []));
|
|
1170
|
+
});
|
|
1171
|
+
return found;
|
|
1172
|
+
}
|
|
1173
|
+
/** The value a promise of the standard library resolves to, through nested promises. */
|
|
1174
|
+
function awaited(project, type) {
|
|
1175
|
+
const { queries } = project;
|
|
1176
|
+
let at = type;
|
|
1177
|
+
for (let depth = 0; depth < MENTION_DEPTH; depth += 1) {
|
|
1178
|
+
const symbol = must(queries.symbolOfType(at));
|
|
1179
|
+
const [inner] = must(queries.typeArguments(at));
|
|
1180
|
+
const standard = symbol !== undefined &&
|
|
1181
|
+
PROMISES.has(symbol.name) &&
|
|
1182
|
+
symbol.declarations.every((handle) => project.program.getSourceFileMetadata(handle.path)?.isDefaultLibrary === true);
|
|
1183
|
+
if (!standard || inner === undefined) {
|
|
1184
|
+
break;
|
|
1185
|
+
}
|
|
1186
|
+
at = inner;
|
|
1187
|
+
}
|
|
1188
|
+
return at;
|
|
1189
|
+
}
|