@cplieger/deadset-ts 8.0.0-dev.20 → 8.0.0-dev.21

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.
Files changed (68) hide show
  1. package/dist/src/alias-chain.js +33 -2
  2. package/dist/src/analysis.js +18 -77
  3. package/dist/src/conformance-record.js +2 -2
  4. package/dist/src/contract/contract.generated.js +4 -3
  5. package/dist/src/dependencies.js +1 -1
  6. package/dist/src/exemptions/argument-places.js +131 -0
  7. package/dist/src/{calls.js → exemptions/calls.js} +126 -179
  8. package/dist/src/{exempt-classes.js → exemptions/classes.js} +1 -1
  9. package/dist/src/{decorator.js → exemptions/decorator.js} +19 -30
  10. package/dist/src/exemptions/detectors.js +48 -0
  11. package/dist/src/{enum-group.js → exemptions/enum-group.js} +7 -7
  12. package/dist/src/{erased-stores.js → exemptions/erased-stores.js} +4 -6
  13. package/dist/src/{framework-lifecycle.js → exemptions/framework-lifecycle.js} +31 -36
  14. package/dist/src/{generated-file.js → exemptions/generated-file.js} +4 -4
  15. package/dist/src/{injection-container.js → exemptions/injection-container.js} +19 -25
  16. package/dist/src/exemptions/installed-globals.js +173 -0
  17. package/dist/src/{interface-satisfaction.js → exemptions/interface-satisfaction.js} +52 -121
  18. package/dist/src/exemptions/lifecycle.js +150 -0
  19. package/dist/src/{reflective-lookup.js → exemptions/reflective-lookup.js} +3 -3
  20. package/dist/src/{serialization-contract.js → exemptions/serialization-contract.js} +415 -329
  21. package/dist/src/exemptions/sites.js +182 -0
  22. package/dist/src/{template-field.js → exemptions/template-field.js} +7 -7
  23. package/dist/src/{value-reach.js → exemptions/value-reach.js} +14 -7
  24. package/dist/src/index/sites.js +1 -1
  25. package/dist/src/index/values.js +124 -0
  26. package/dist/src/liveness/sweep.js +2 -2
  27. package/dist/src/query.js +48 -0
  28. package/dist/src/roots/configured-declarations.js +1 -1
  29. package/dist/src/verbs/print-retained.js +23 -1
  30. package/jsr.json +1 -1
  31. package/package.json +1 -1
  32. package/src/alias-chain.ts +38 -2
  33. package/src/analysis.ts +19 -100
  34. package/src/bindings.ts +1 -1
  35. package/src/conformance-record.ts +2 -2
  36. package/src/contract/contract.generated.ts +4 -3
  37. package/src/dependencies.ts +1 -1
  38. package/src/exemptions/argument-places.ts +198 -0
  39. package/src/{calls.ts → exemptions/calls.ts} +190 -224
  40. package/src/{exempt-classes.ts → exemptions/classes.ts} +1 -1
  41. package/src/exemptions/decorator.ts +56 -0
  42. package/src/exemptions/detectors.ts +75 -0
  43. package/src/{enum-group.ts → exemptions/enum-group.ts} +9 -9
  44. package/src/{erased-stores.ts → exemptions/erased-stores.ts} +5 -8
  45. package/src/{framework-lifecycle.ts → exemptions/framework-lifecycle.ts} +39 -48
  46. package/src/{generated-file.ts → exemptions/generated-file.ts} +6 -6
  47. package/src/{injection-container.ts → exemptions/injection-container.ts} +28 -33
  48. package/src/exemptions/installed-globals.ts +255 -0
  49. package/src/{interface-satisfaction.ts → exemptions/interface-satisfaction.ts} +69 -160
  50. package/src/exemptions/lifecycle.ts +286 -0
  51. package/src/{reflective-lookup.ts → exemptions/reflective-lookup.ts} +5 -5
  52. package/src/{serialization-contract.ts → exemptions/serialization-contract.ts} +563 -429
  53. package/src/exemptions/sites.ts +293 -0
  54. package/src/{template-field.ts → exemptions/template-field.ts} +11 -11
  55. package/src/{value-reach.ts → exemptions/value-reach.ts} +16 -8
  56. package/src/facts.ts +1 -1
  57. package/src/index/sites.ts +1 -1
  58. package/src/index/values.ts +170 -0
  59. package/src/liveness/sweep.ts +2 -2
  60. package/src/query.ts +109 -10
  61. package/src/roots/configured-declarations.ts +1 -2
  62. package/src/stores.ts +1 -1
  63. package/src/verbs/print-retained.ts +29 -1
  64. package/dist/src/exempt.js +0 -164
  65. package/dist/src/value-flow.js +0 -74
  66. package/src/decorator.ts +0 -78
  67. package/src/exempt.ts +0 -295
  68. package/src/value-flow.ts +0 -104
@@ -6,14 +6,14 @@
6
6
  * elements and each `ts.lifecycle_contracts` entry. A component's members are its type's,
7
7
  * so a member a base class declares answers for every subclass.
8
8
  */
9
- import { isClassDeclaration, isClassExpression, isHeritageClause, SyntaxKind, } from "@typescript/native/ast";
10
- import { aliasChains } from "./alias-chain.js";
11
- import { calleeName, callsOf, classesPassed, decoratedClasses, decoratorCallee } from "./calls.js";
12
- import { classMembers, memberComponent, typesAt } from "./class-members.js";
13
- import { entriesAt, names, namesADeclaration, resolveEntries, spelledEntry, } from "./roots/configured-declarations.js";
14
- import { nodeKey } from "./inventory.js";
15
- import { renderPosition } from "./position.js";
16
- import { must } from "./query.js";
9
+ import { isHeritageClause, SyntaxKind, } from "@typescript/native/ast";
10
+ import { aliasChains } from "../alias-chain.js";
11
+ import { nameOf } from "../index/values.js";
12
+ import { classesPassed, decoratorCallee, decoratorsOf } from "./calls.js";
13
+ import { classMembers, memberComponent, typesAt } from "../class-members.js";
14
+ import { entriesAt, names, namesADeclaration, resolveEntries, spelledEntry, } from "../roots/configured-declarations.js";
15
+ import { renderPosition } from "../position.js";
16
+ import { must } from "../query.js";
17
17
  /**
18
18
  * The platform's own contract (references-ts.38): `CustomElementRegistry.define` reads
19
19
  * these members off the class it registers, its constructor's and its prototype's
@@ -57,16 +57,20 @@ function resolveContracts(project, held, contracts) {
57
57
  }));
58
58
  }
59
59
  /** The classes a decorator or a call the contracts' components name makes a component. */
60
- function byComponents(project, held, chains, contracts) {
60
+ function byComponents(project, held, sites, chains, contracts) {
61
61
  const routed = contracts.filter((one) => one.components.length > 0);
62
62
  if (routed.length === 0) {
63
63
  return [];
64
64
  }
65
65
  const contractOf = new Map(routed.flatMap((one) => one.components.map((entry) => [entry, one.contract])));
66
- const calls = callsOf(project);
67
- const decorated = decoratedClasses(project, held);
66
+ const calls = sites.calls.map((one) => one.call);
67
+ const invoked = (call) => nameOf(sites.invocations.of(call).callee);
68
+ const decorated = sites.classes.flatMap(({ id, node }) => {
69
+ const decorators = decoratorsOf(node);
70
+ return decorators.length === 0 ? [] : [{ id, decorators }];
71
+ });
68
72
  const named = entriesAt(project, chains, [
69
- ...calls.flatMap((call) => calleeName(call.expression) ?? []),
73
+ ...calls.flatMap((call) => invoked(call) ?? []),
70
74
  ...decorated.flatMap((one) => one.decorators.flatMap((decorator) => decoratorCallee(decorator) ?? [])),
71
75
  ], [...contractOf.keys()]);
72
76
  const naming = (node) => (node === undefined ? [] : (named.get(node) ?? [])).flatMap((entry) => {
@@ -83,12 +87,12 @@ function byComponents(project, held, chains, contracts) {
83
87
  }
84
88
  const matched = new Map();
85
89
  for (const call of calls) {
86
- const routes = naming(calleeName(call.expression));
90
+ const routes = naming(invoked(call));
87
91
  if (routes.length > 0) {
88
92
  matched.set(call, routes);
89
93
  }
90
94
  }
91
- for (const [call, uses] of classesPassed(project, held, chains, [...matched.keys()])) {
95
+ for (const [call, uses] of classesPassed(project, held, chains, sites, [...matched.keys()])) {
92
96
  for (const route of matched.get(call) ?? []) {
93
97
  for (const use of uses) {
94
98
  found.push({
@@ -102,32 +106,23 @@ function byComponents(project, held, chains, contracts) {
102
106
  }
103
107
  return found;
104
108
  }
105
- /** The class declarations of the target that extend another class. */
106
- function extendingClasses(project, held) {
107
- const found = [];
108
- for (const file of project.ownSourceFiles()) {
109
- const visit = (node) => {
110
- if ((isClassDeclaration(node) || isClassExpression(node)) && node.name !== undefined) {
111
- const id = held.declarations.get(nodeKey(file, node));
112
- const clause = (node.heritageClauses ?? []).find((one) => isHeritageClause(one) && one.token === SyntaxKind.ExtendsKeyword);
113
- const extended = clause !== undefined && isHeritageClause(clause) ? clause.types[0] : undefined;
114
- if (id !== undefined && extended !== undefined) {
115
- found.push({ id, name: node.name, extended });
116
- }
117
- }
118
- node.forEachChild(visit);
119
- };
120
- file.forEachChild(visit);
121
- }
122
- return found;
109
+ /** Each named class of the target that extends another, with its name and the class its `extends` names. */
110
+ function extendingClasses(sites) {
111
+ return sites.classes.flatMap(({ id, node }) => {
112
+ const clause = (node.heritageClauses ?? []).find((one) => isHeritageClause(one) && one.token === SyntaxKind.ExtendsKeyword);
113
+ const extended = clause !== undefined && isHeritageClause(clause) ? clause.types[0] : undefined;
114
+ return node.name === undefined || extended === undefined
115
+ ? []
116
+ : [{ id, name: node.name, extended }];
117
+ });
123
118
  }
124
119
  /** The classes that extend, directly or through a chain, a class the contracts' bases name. */
125
- function byBases(project, held, chains, contracts) {
120
+ function byBases(project, held, sites, chains, contracts) {
126
121
  const based = contracts.filter((one) => one.bases.length > 0);
127
122
  if (based.length === 0) {
128
123
  return [];
129
124
  }
130
- const classes = extendingClasses(project, held);
125
+ const classes = extendingClasses(sites);
131
126
  if (classes.length === 0) {
132
127
  return [];
133
128
  }
@@ -191,8 +186,8 @@ export function frameworkLifecycle(input) {
191
186
  }
192
187
  const chains = aliasChains(project, held);
193
188
  const components = [
194
- ...byComponents(project, held, chains, contracts),
195
- ...byBases(project, held, chains, contracts),
189
+ ...byComponents(project, held, input.sites, chains, contracts),
190
+ ...byBases(project, held, input.sites, chains, contracts),
196
191
  ];
197
192
  if (components.length === 0) {
198
193
  return [];
@@ -5,10 +5,10 @@
5
5
  * generation. The same decision is each `generated` entry of the facts document.
6
6
  */
7
7
  import { isIdentifier, isImportDeclaration, isPropertyAssignment, isStringLiteralLikeNode, isVariableStatement, NodeFlags, } from "@typescript/native/ast";
8
- import { GENERATORS } from "./contract/contract.generated.js";
9
- import { minifiedBy } from "./javascript-file.js";
10
- import { bytewise } from "./text/compare.js";
11
- import { satisfies } from "./version-range.js";
8
+ import { GENERATORS } from "../contract/contract.generated.js";
9
+ import { minifiedBy } from "../javascript-file.js";
10
+ import { bytewise } from "../text/compare.js";
11
+ import { satisfies } from "../version-range.js";
12
12
  /** The generated-code header, matched against each line of a leading comment. */
13
13
  const HEADER_LINE = /^\/\/ Code generated .* DO NOT EDIT\.$/u;
14
14
  /** The documentation tag `@generated` as a word of a comment. */
@@ -6,13 +6,14 @@
6
6
  * parameter property is injected, decorated or not. A constructor is no declaration of
7
7
  * the inventory, so the class stands for it and the registration referencing it keeps it.
8
8
  */
9
- import { isClassDeclaration, isClassExpression, isConstructorDeclaration, isGetAccessorDeclaration, isPropertyDeclaration, isSetAccessorDeclaration, } from "@typescript/native/ast";
10
- import { aliasChains } from "./alias-chain.js";
11
- import { calleeName, callsOf, classesPassed, decoratorsOf, spelledDecorator } from "./calls.js";
12
- import { classMembers } from "./class-members.js";
13
- import { entriesAt, namesADeclaration, resolveEntries, spelledEntry, } from "./roots/configured-declarations.js";
14
- import { isParameterProperty, nodeKey } from "./inventory.js";
15
- import { renderPosition } from "./position.js";
9
+ import { isConstructorDeclaration, isGetAccessorDeclaration, isPropertyDeclaration, isSetAccessorDeclaration, } from "@typescript/native/ast";
10
+ import { aliasChains } from "../alias-chain.js";
11
+ import { nameOf } from "../index/values.js";
12
+ import { classesPassed, decoratorsOf, spelledDecorator } from "./calls.js";
13
+ import { classMembers } from "../class-members.js";
14
+ import { entriesAt, namesADeclaration, resolveEntries, spelledEntry, } from "../roots/configured-declarations.js";
15
+ import { isParameterProperty } from "../inventory.js";
16
+ import { renderPosition } from "../position.js";
16
17
  /** Whether one member declaration is a parameter property, or a property or accessor carrying a decorator. */
17
18
  function isInjected(node) {
18
19
  return (isParameterProperty(node) ||
@@ -23,23 +24,24 @@ function isInjected(node) {
23
24
  }
24
25
  /** The classes of the target a call whose callee an entry names passes, with that call's entry. */
25
26
  function registrations(input) {
26
- const { project, held } = input;
27
+ const { project, held, sites } = input;
27
28
  const resolved = resolveEntries(project, held, input.ts.injection_registrations);
28
29
  if (!resolved.some(namesADeclaration)) {
29
30
  return [];
30
31
  }
31
32
  const chains = aliasChains(project, held);
32
- const calls = callsOf(project);
33
- const named = entriesAt(project, chains, calls.flatMap((call) => calleeName(call.expression) ?? []), resolved);
33
+ const calls = sites.calls.map((one) => one.call);
34
+ const invoked = (call) => nameOf(sites.invocations.of(call).callee);
35
+ const named = entriesAt(project, chains, calls.flatMap((call) => invoked(call) ?? []), resolved);
34
36
  const matched = new Map();
35
37
  for (const call of calls) {
36
- const name = calleeName(call.expression);
38
+ const name = invoked(call);
37
39
  const [entry] = name === undefined ? [] : (named.get(name) ?? []);
38
40
  if (entry !== undefined) {
39
41
  matched.set(call, entry);
40
42
  }
41
43
  }
42
- return [...classesPassed(project, held, chains, [...matched.keys()])].flatMap(([call, uses]) => {
44
+ return [...classesPassed(project, held, chains, sites, [...matched.keys()])].flatMap(([call, uses]) => {
43
45
  const entry = matched.get(call);
44
46
  const detail = entry === undefined ? "" : `registered by ${spelledEntry(entry.entry, held)}`;
45
47
  return uses.map((use) => ({ id: use.id, site: use.site, detail }));
@@ -50,20 +52,12 @@ function registrations(input) {
50
52
  * with the first such decorator.
51
53
  */
52
54
  function constructedForParameters(input) {
53
- const { project, held } = input;
54
55
  const found = new Map();
55
- for (const file of project.ownSourceFiles()) {
56
- const visit = (node) => {
57
- if (isClassDeclaration(node) || isClassExpression(node)) {
58
- const id = held.declarations.get(nodeKey(file, node));
59
- const [asks] = node.members.flatMap((member) => isConstructorDeclaration(member) ? member.parameters.flatMap(decoratorsOf) : []);
60
- if (id !== undefined && asks !== undefined) {
61
- found.set(id, asks);
62
- }
63
- }
64
- node.forEachChild(visit);
65
- };
66
- file.forEachChild(visit);
56
+ for (const { id, node } of input.sites.classes) {
57
+ const [asks] = node.members.flatMap((member) => isConstructorDeclaration(member) ? member.parameters.flatMap(decoratorsOf) : []);
58
+ if (asks !== undefined) {
59
+ found.set(id, asks);
60
+ }
67
61
  }
68
62
  return found;
69
63
  }
@@ -0,0 +1,173 @@
1
+ /**
2
+ * The classes a program installs as globals: a class value written into a property of
3
+ * the global object, which code outside the analysis that names the global constructs.
4
+ * The checker finds each write: the receiver's type is the global object's, the callee is
5
+ * the standard library's `Object.defineProperty` or a test runner's stub, and the
6
+ * property is the one of the key's name that `typeof globalThis` declares.
7
+ */
8
+ import { isIdentifier, isNoSubstitutionTemplateLiteral, isObjectLiteralExpression, isPropertyAssignment, isShorthandPropertyAssignment, isStringLiteral, } from "@typescript/native/ast";
9
+ import { SignatureKind, SymbolFlags, } from "@typescript/native/sync";
10
+ import { typesAt } from "../class-members.js";
11
+ import { packageOfPath } from "../dependencies.js";
12
+ import { must } from "../query.js";
13
+ import { parenthesesRemoved, unwrapped } from "../unwrap.js";
14
+ import { spansOf } from "./argument-places.js";
15
+ import { classesCarried } from "./calls.js";
16
+ /**
17
+ * The test runners' stubs of a global, by method name and the packages that declare it:
18
+ * `vi.stubGlobal` of vitest, and `jest.replaceProperty`, which `@jest/environment` types
19
+ * for `@jest/globals` and `@types/jest` declares for the `jest` global.
20
+ */
21
+ const STUBS = new Map([
22
+ [
23
+ "stubGlobal",
24
+ { packages: new Set(["vitest"]), object: undefined, key: 0, value: 1, described: false },
25
+ ],
26
+ [
27
+ "replaceProperty",
28
+ {
29
+ packages: new Set(["@jest/environment", "@jest/globals", "@types/jest"]),
30
+ object: 0,
31
+ key: 1,
32
+ value: 2,
33
+ described: false,
34
+ },
35
+ ],
36
+ ]);
37
+ /** `Object.defineProperty(object, key, descriptor)`. */
38
+ const DEFINE = { object: 0, key: 1, value: 2, described: true };
39
+ /** The text of a key written as a string literal. */
40
+ function keyText(node) {
41
+ const key = node === undefined ? undefined : parenthesesRemoved(node);
42
+ return key !== undefined && (isStringLiteral(key) || isNoSubstitutionTemplateLiteral(key))
43
+ ? key.text
44
+ : undefined;
45
+ }
46
+ /** Whether a property's name is `value`. */
47
+ function named(name) {
48
+ return (isIdentifier(name) || isStringLiteral(name)) && name.text === "value";
49
+ }
50
+ /** The value a property descriptor written as an object literal gives. */
51
+ function describedValue(node) {
52
+ const descriptor = node === undefined ? undefined : parenthesesRemoved(node);
53
+ if (descriptor === undefined || !isObjectLiteralExpression(descriptor)) {
54
+ return undefined;
55
+ }
56
+ for (const property of descriptor.properties) {
57
+ if (isPropertyAssignment(property) && named(property.name)) {
58
+ return property.initializer;
59
+ }
60
+ if (isShorthandPropertyAssignment(property) &&
61
+ isIdentifier(property.name) &&
62
+ named(property.name)) {
63
+ return property.name;
64
+ }
65
+ }
66
+ return undefined;
67
+ }
68
+ /** How a call's callee writes a property: the standard library's `Object.defineProperty` or a runner's stub. */
69
+ function writerOf(project, method) {
70
+ if (method.name === "defineProperty") {
71
+ const standard = method.declarations.some((handle) => project.program.getSourceFileMetadata(handle.path)?.isDefaultLibrary === true);
72
+ return standard && must(project.queries.parentOf(method))?.name === "ObjectConstructor"
73
+ ? DEFINE
74
+ : undefined;
75
+ }
76
+ const stub = STUBS.get(method.name);
77
+ return stub !== undefined &&
78
+ method.declarations.some((handle) => stub.packages.has(packageOfPath(handle.path) ?? ""))
79
+ ? stub
80
+ : undefined;
81
+ }
82
+ /** Each write of one project's own files that may install a value as a global, read from syntax and the callee. */
83
+ function writesOf(project, sites) {
84
+ const found = sites.stores.map((store) => ({
85
+ site: store.store,
86
+ receiver: store.receiver,
87
+ key: store.key,
88
+ value: store.value,
89
+ }));
90
+ for (const { call } of sites.calls) {
91
+ const method = sites.invocations.callee(call);
92
+ const writer = method === undefined ? undefined : writerOf(project, method);
93
+ if (writer === undefined) {
94
+ continue;
95
+ }
96
+ const invoked = sites.invocations.of(call);
97
+ // With no types asked, a spread has no known length, so only a place no spread
98
+ // comes at or before is read.
99
+ const spans = spansOf(project, invoked.arguments, invoked.listed, []);
100
+ const at = (place) => {
101
+ const span = spans.find((one) => !one.open && one.first === place);
102
+ return span === undefined ? undefined : invoked.arguments[span.argument];
103
+ };
104
+ const key = keyText(at(writer.key));
105
+ const receiver = writer.object === undefined ? undefined : at(writer.object);
106
+ const value = writer.described ? describedValue(at(writer.value)) : at(writer.value);
107
+ if (key !== undefined &&
108
+ value !== undefined &&
109
+ (writer.object === undefined || receiver !== undefined)) {
110
+ found.push({ site: call, receiver, key, value });
111
+ }
112
+ }
113
+ return found;
114
+ }
115
+ /**
116
+ * The classes one project installs as globals. A write installs one where its object is
117
+ * the global object, as the checker types it (`window` and `self` are intersections that
118
+ * hold `typeof globalThis`, and an object asserted to `any` is read inside the
119
+ * assertion), and where the global object declares a property of the key's name with a
120
+ * construct signature, whose instances code outside the analysis makes.
121
+ */
122
+ export function installedGlobals(project, held, chains, sites) {
123
+ const { queries } = project;
124
+ const writes = writesOf(project, sites);
125
+ if (writes.length === 0) {
126
+ return [];
127
+ }
128
+ const global = must(queries.resolveName("globalThis", SymbolFlags.Value, undefined, false));
129
+ const globalType = global === undefined ? undefined : must(queries.typeOfSymbol(global));
130
+ if (global === undefined || globalType === undefined) {
131
+ return [];
132
+ }
133
+ const properties = new Map(must(queries.propertiesOf(globalType)).map((property) => [property.name, property]));
134
+ const keyed = writes.filter((write) => properties.has(write.key));
135
+ const objects = keyed.flatMap((write) => write.receiver === undefined ? [] : [unwrapped(write.receiver)]);
136
+ const objectTypes = new Map(typesAt(project, objects).map((type, at) => [objects[at], type]));
137
+ const globals = new Map();
138
+ const isGlobal = (type) => {
139
+ if (type === undefined) {
140
+ return false;
141
+ }
142
+ let known = globals.get(type.id);
143
+ if (known === undefined) {
144
+ const parts = type.isUnionType() || type.isIntersectionType() ? must(queries.constituents(type)) : [type];
145
+ known = parts.some((part) => must(queries.symbolOfType(part))?.id === global.id);
146
+ globals.set(type.id, known);
147
+ }
148
+ return known;
149
+ };
150
+ const installs = keyed.filter((write) => write.receiver === undefined || isGlobal(objectTypes.get(unwrapped(write.receiver))));
151
+ if (installs.length === 0) {
152
+ return [];
153
+ }
154
+ const instances = new Map();
155
+ const instancesOf = (key) => {
156
+ let made = instances.get(key);
157
+ if (made === undefined) {
158
+ const property = properties.get(key);
159
+ const type = property === undefined ? undefined : must(queries.typeOfSymbol(property));
160
+ made = (type === undefined ? [] : must(queries.signaturesOf(type, SignatureKind.Construct))).flatMap((signature) => must(queries.returnTypeOf(signature)) ?? []);
161
+ instances.set(key, made);
162
+ }
163
+ return made;
164
+ };
165
+ const carried = classesCarried(project, held, chains, sites, installs.map((write) => [write.value]));
166
+ return installs.flatMap((write, at) => {
167
+ const uses = carried[at] ?? [];
168
+ const made = uses.length === 0 ? [] : instancesOf(write.key);
169
+ return made.length === 0
170
+ ? []
171
+ : uses.map((use) => ({ id: use.id, site: write.site, global: write.key, instances: made }));
172
+ });
173
+ }
@@ -3,85 +3,41 @@
3
3
  * reaches a position typed as an interface, or an object type a type alias names, that
4
4
  * requires the member: an `implements` entry, a typed initializer, an assignment, an
5
5
  * argument, a return, an array element, an object property or an `as` or `satisfies`
6
- * operand. `this` is a value of its class; a type parameter reaches its constraint. The
6
+ * operand. `this` is a value of its class; a type parameter reaches its constraint. A
7
+ * class installed as a global reaches the instance type the global constructs, whether
8
+ * or not it is assignable to it, because a stub answers only what its caller uses. The
7
9
  * contextual lookup has no batch form, so it is asked only of a value of a target class.
8
10
  */
9
- import { isCallExpression, isClassDeclaration, isClassExpression, isClassStaticBlockDeclaration, isFunctionDeclaration, isFunctionExpression, isGetAccessorDeclaration, isHeritageClause, isMethodDeclaration, isNewExpression, isObjectLiteralExpression, isSetAccessorDeclaration, isTypeLiteralNode, ModifierFlags, SyntaxKind, } from "@typescript/native/ast";
11
+ import { isCallExpression, isHeritageClause, isNewExpression, isTypeLiteralNode, SyntaxKind, } from "@typescript/native/ast";
10
12
  import { SymbolFlags } from "@typescript/native/sync";
11
- import { aliasChains } from "./alias-chain.js";
12
- import { emptyImplementation } from "./findings/implementations.js";
13
- import { nodeKey } from "./inventory.js";
14
- import { renderPosition } from "./position.js";
15
- import { must } from "./query.js";
16
- import { handedOn, valuesOf } from "./value-flow.js";
13
+ import { aliasChains } from "../alias-chain.js";
14
+ import { emptyImplementation } from "../findings/implementations.js";
15
+ import { installedGlobals } from "./installed-globals.js";
16
+ import { renderPosition } from "../position.js";
17
+ import { must } from "../query.js";
17
18
  /** Whether a type alias names one type: the client's type object carries the alias it was written through. */
18
19
  function aliased(type) {
19
20
  return type.aliasSymbol !== undefined;
20
21
  }
21
- /** Whether one node declares a static member, whose `this` is the class itself. */
22
- function isStatic(node) {
23
- const flags = node.modifierFlags ?? 0;
24
- return (flags & ModifierFlags.Static) !== 0;
25
- }
26
- /** Whether one node is a method or an accessor of an object literal, whose `this` is the literal. */
27
- function isObjectLiteralMethod(node) {
28
- return ((isMethodDeclaration(node) ||
29
- isGetAccessorDeclaration(node) ||
30
- isSetAccessorDeclaration(node)) &&
31
- isObjectLiteralExpression(node.parent));
32
- }
33
22
  /**
34
- * The classes, the `implements` entries and the flowing values of one project's own
35
- * files. `this` is the class an instance member belongs to, so a function that is not
36
- * an arrow, a static member and a static block each stop it.
23
+ * The named classes, the `implements` entries and the flowing values of one project's own
24
+ * files, a value `this` standing for the class it is an instance of.
37
25
  */
38
- function walk(files, declarations) {
39
- const classes = [];
40
- const clauses = [];
41
- const flows = [];
42
- for (const file of files) {
43
- const visit = (node, self) => {
44
- let inner = self;
45
- if (isClassDeclaration(node) || isClassExpression(node)) {
46
- const id = declarations.get(nodeKey(file, node));
47
- const name = node.name;
48
- const owner = id === undefined || name === undefined
49
- ? undefined
50
- : { id, name, generic: node.typeParameters !== undefined };
51
- if (owner !== undefined) {
52
- classes.push(owner);
53
- for (const clause of node.heritageClauses ?? []) {
54
- if (isHeritageClause(clause) && clause.token === SyntaxKind.ImplementsKeyword) {
55
- clauses.push(...clause.types.map((entry) => ({ owner, entry })));
56
- }
57
- }
58
- }
59
- inner = owner;
60
- }
61
- else if (isFunctionDeclaration(node) ||
62
- isFunctionExpression(node) ||
63
- isClassStaticBlockDeclaration(node) ||
64
- isStatic(node) ||
65
- isObjectLiteralMethod(node)) {
66
- inner = undefined;
67
- }
68
- for (const written of handedOn(node)) {
69
- for (const value of valuesOf(written)) {
70
- flows.push({
71
- value,
72
- self: value.kind === SyntaxKind.ThisKeyword ? inner : undefined,
73
- });
74
- }
75
- }
76
- node.forEachChild((child) => {
77
- visit(child, inner);
78
- });
79
- };
80
- file.forEachChild((child) => {
81
- visit(child, undefined);
82
- });
83
- }
84
- return { classes, clauses, flows };
26
+ function walked(sites) {
27
+ const classes = sites.classes.flatMap(({ id, node }) => node.name === undefined
28
+ ? []
29
+ : [{ id, name: node.name, generic: node.typeParameters !== undefined, node }]);
30
+ const byId = new Map(classes.map((owner) => [owner.id, owner]));
31
+ return {
32
+ classes,
33
+ clauses: classes.flatMap((owner) => (owner.node.heritageClauses ?? []).flatMap((clause) => isHeritageClause(clause) && clause.token === SyntaxKind.ImplementsKeyword
34
+ ? clause.types.map((entry) => ({ owner, entry }))
35
+ : [])),
36
+ flows: sites.flows.map((flow) => ({
37
+ value: flow.value,
38
+ self: flow.self === undefined ? undefined : byId.get(flow.self),
39
+ })),
40
+ };
85
41
  }
86
42
  /**
87
43
  * The interface-satisfaction detector: for each class type that reaches an interface
@@ -91,8 +47,8 @@ function walk(files, declarations) {
91
47
  export function interfaceSatisfaction(input) {
92
48
  const { project, held, targetRoot } = input;
93
49
  const queries = project.queries;
94
- const files = project.ownSourceFiles();
95
- const { classes, clauses, flows } = walk(files, held.declarations);
50
+ const chains = aliasChains(project, held);
51
+ const { classes, clauses, flows } = walked(input.sites);
96
52
  if (classes.length === 0) {
97
53
  return [];
98
54
  }
@@ -197,13 +153,15 @@ export function interfaceSatisfaction(input) {
197
153
  const pairs = new Map();
198
154
  const reach = (owner, source, target, name, site) => {
199
155
  const key = `${String(source.id)}:${String(target.id)}`;
200
- const pair = pairs.get(key);
156
+ let pair = pairs.get(key);
201
157
  if (pair === undefined) {
202
- pairs.set(key, { owner, source, target, name, sites: [site] });
158
+ pair = { owner, source, target, name, sites: [site] };
159
+ pairs.set(key, pair);
203
160
  }
204
161
  else {
205
162
  pair.sites.push(site);
206
163
  }
164
+ return pair;
207
165
  };
208
166
  for (const clause of clauses) {
209
167
  const source = instanceOf.get(clause.owner.id);
@@ -237,14 +195,26 @@ export function interfaceSatisfaction(input) {
237
195
  }
238
196
  }
239
197
  });
240
- const chains = aliasChains(project, held);
198
+ const byId = new Map(classes.map((owner) => [owner.id, owner]));
199
+ for (const one of installedGlobals(project, held, chains, input.sites)) {
200
+ const owner = byId.get(one.id);
201
+ const source = instanceOf.get(one.id);
202
+ if (owner === undefined || source === undefined) {
203
+ continue;
204
+ }
205
+ for (const instance of one.instances) {
206
+ reach(owner, source, instance, one.global, one.site).installed = one.global;
207
+ }
208
+ }
241
209
  const required = new Map();
242
210
  const members = new Map();
243
211
  const found = [];
244
- const asking = [...pairs.values()];
245
- const assignable = queries.assignableEach(asking);
246
- for (const [index, pair] of asking.entries()) {
247
- if (!must(assignable[index] ?? false)) {
212
+ const asking = [...pairs.values()].filter((pair) => pair.installed === undefined);
213
+ const assignable = new Set(queries
214
+ .assignableEach(asking)
215
+ .flatMap((answer, index) => (must(answer) ? [asking[index]] : [])));
216
+ for (const pair of pairs.values()) {
217
+ if (pair.installed === undefined && !assignable.has(pair)) {
248
218
  continue;
249
219
  }
250
220
  let names = required.get(pair.target.id);
@@ -267,7 +237,9 @@ export function interfaceSatisfaction(input) {
267
237
  ]));
268
238
  members.set(pair.owner.id, answering);
269
239
  }
270
- const detail = `satisfies ${pair.name}`;
240
+ const detail = pair.installed === undefined
241
+ ? `satisfies ${pair.name}`
242
+ : `installed as the global ${pair.installed}`;
271
243
  for (const [name, methods] of names) {
272
244
  const member = answering.get(name);
273
245
  for (const id of member?.ids ?? []) {
@@ -284,44 +256,3 @@ export function interfaceSatisfaction(input) {
284
256
  }
285
257
  return found;
286
258
  }
287
- /**
288
- * Each project's records with every record dropped whose member implements only interface
289
- * methods nothing calls, unless every implementation of such a method is empty, and a
290
- * reference from each such method to the member, which then falls with the method.
291
- * `references` is every reference of the run, a consumer's included.
292
- */
293
- export function settledSatisfaction(projects, references) {
294
- const methods = new Set(projects.flatMap((one) => one.held.symbols.filter((symbol) => symbol.kind === "interface-method").map((one) => one.id)));
295
- const called = new Set(references.map((reference) => reference.to));
296
- const bodies = new Map();
297
- for (const record of projects.flatMap((one) => one.evidence)) {
298
- for (const method of record.implementing?.methods ?? []) {
299
- bodies.set(method, (bodies.get(method) ?? true) && record.implementing?.empty === true);
300
- }
301
- }
302
- const uncalled = (method) => methods.has(method) && !called.has(method) && bodies.get(method) !== true;
303
- return projects.map(({ evidence }) => {
304
- const kept = [];
305
- const added = [];
306
- for (const record of evidence) {
307
- const implemented = record.implementing?.methods ?? [];
308
- if (record.class !== "interface-satisfaction" ||
309
- implemented.length === 0 ||
310
- !implemented.every(uncalled)) {
311
- kept.push(record);
312
- continue;
313
- }
314
- for (const method of implemented) {
315
- added.push({
316
- from: method,
317
- to: record.id,
318
- position: record.site,
319
- use: "read",
320
- resolution: "override",
321
- test: false,
322
- });
323
- }
324
- }
325
- return { evidence: kept, references: added };
326
- });
327
- }