@cplieger/deadset-ts 8.0.0-dev.16 → 8.0.0-dev.18

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 (161) hide show
  1. package/dist/src/alias-chain.js +0 -5
  2. package/dist/src/amd.js +81 -0
  3. package/dist/src/analysis.js +82 -91
  4. package/dist/src/augmentations.js +24 -25
  5. package/dist/src/bindings.js +382 -0
  6. package/dist/src/calls.js +279 -43
  7. package/dist/src/commands/tools.js +5 -9
  8. package/dist/src/component-files.js +3 -2
  9. package/dist/src/conformance-record.js +1 -1
  10. package/dist/src/consumers.js +6 -9
  11. package/dist/src/contextual-uses.js +1 -17
  12. package/dist/src/contract/config.generated.js +4 -1
  13. package/dist/src/contract/contract.generated.js +2 -2
  14. package/dist/src/contract/report.generated.js +6 -2
  15. package/dist/src/dependencies.js +25 -29
  16. package/dist/src/destructuring.js +1 -19
  17. package/dist/src/element-access.js +179 -164
  18. package/dist/src/entries/build-scripts.js +2 -2
  19. package/dist/src/entries/configuration-files.js +3 -7
  20. package/dist/src/entries/conventions.js +2 -2
  21. package/dist/src/entries/html-entries.js +7 -10
  22. package/dist/src/entries/mdx-entries.js +2 -2
  23. package/dist/src/entries/static-module.js +2 -1
  24. package/dist/src/exempt.js +0 -26
  25. package/dist/src/exposure.js +400 -0
  26. package/dist/src/facts.js +4 -4
  27. package/dist/src/findings/completion.js +44 -21
  28. package/dist/src/findings/emitters.js +22 -33
  29. package/dist/src/{implementations.js → findings/implementations.js} +3 -3
  30. package/dist/src/{in-operands.js → findings/in-operands.js} +2 -2
  31. package/dist/src/findings/intra-function.js +3 -3
  32. package/dist/src/findings/reads-and-writes.js +71 -15
  33. package/dist/src/findings/unused-declarations.js +2 -22
  34. package/dist/src/findings/visibility-narrowing.js +14 -20
  35. package/dist/src/framework-lifecycle.js +41 -14
  36. package/dist/src/import-bindings.js +73 -28
  37. package/dist/src/index/sites.js +557 -0
  38. package/dist/src/indexed-access.js +523 -478
  39. package/dist/src/interface-satisfaction.js +1 -1
  40. package/dist/src/inventory.js +18 -0
  41. package/dist/src/keyof-comparisons.js +124 -103
  42. package/dist/src/{components.js → liveness/components.js} +1 -1
  43. package/dist/src/{graph.js → liveness/graph.js} +45 -20
  44. package/dist/src/liveness/matrix.js +99 -0
  45. package/dist/src/liveness/sweep.js +558 -0
  46. package/dist/src/load/expected-module.js +2 -2
  47. package/dist/src/load/specifiers.js +2 -4
  48. package/dist/src/load/synthesize.js +5 -0
  49. package/dist/src/load/workspace-resolution.js +2 -2
  50. package/dist/src/module-declarations.js +70 -85
  51. package/dist/src/overrides.js +134 -112
  52. package/dist/src/paths.js +46 -7
  53. package/dist/src/platform-calls.js +2 -2
  54. package/dist/src/platform-symbols.js +13 -19
  55. package/dist/src/query.js +46 -0
  56. package/dist/src/references/read-rule.js +1 -0
  57. package/dist/src/references/read-rules.js +48 -0
  58. package/dist/src/references/sink.js +237 -0
  59. package/dist/src/references/sources.js +450 -0
  60. package/dist/src/references.js +110 -1285
  61. package/dist/src/roots/configured-declarations.js +3 -0
  62. package/dist/src/roots/published-reach.js +4 -4
  63. package/dist/src/roots/root-set.js +1 -1
  64. package/dist/src/roots/unseen-loads.js +38 -10
  65. package/dist/src/serialization-contract.js +8 -3
  66. package/dist/src/stores.js +4 -2
  67. package/dist/src/symbol-keys.js +89 -83
  68. package/dist/src/tests/file-class.js +76 -0
  69. package/dist/src/{outside-tests.js → tests/outside.js} +7 -7
  70. package/dist/src/{test-support.js → tests/support.js} +2 -11
  71. package/dist/src/type-parameter-reads.js +178 -139
  72. package/dist/src/unimplemented-callees.js +262 -242
  73. package/dist/src/verbs/analyze.js +3 -3
  74. package/dist/src/verbs/explain.js +75 -132
  75. package/dist/src/verbs/print-retained.js +3 -3
  76. package/jsr.json +1 -1
  77. package/package.json +2 -2
  78. package/src/alias-chain.ts +0 -7
  79. package/src/amd.ts +130 -0
  80. package/src/analysis.ts +125 -128
  81. package/src/augmentations.ts +42 -28
  82. package/src/bindings.ts +460 -0
  83. package/src/calls.ts +338 -46
  84. package/src/commands/tools.ts +5 -11
  85. package/src/component-files.ts +4 -2
  86. package/src/conformance-record.ts +1 -1
  87. package/src/consumers.ts +8 -11
  88. package/src/contextual-uses.ts +2 -22
  89. package/src/contract/config.generated.ts +4 -1
  90. package/src/contract/contract.generated.ts +2 -2
  91. package/src/contract/report.generated.ts +6 -2
  92. package/src/dependencies.ts +27 -28
  93. package/src/destructuring.ts +1 -31
  94. package/src/element-access.ts +188 -182
  95. package/src/entries/build-scripts.ts +2 -2
  96. package/src/entries/configuration-files.ts +11 -8
  97. package/src/entries/conventions.ts +2 -1
  98. package/src/entries/html-entries.ts +15 -14
  99. package/src/entries/mdx-entries.ts +2 -2
  100. package/src/entries/static-module.ts +2 -1
  101. package/src/exempt.ts +31 -46
  102. package/src/exposure.ts +492 -0
  103. package/src/facts.ts +5 -3
  104. package/src/finding.ts +1 -1
  105. package/src/findings/boundary.ts +9 -0
  106. package/src/findings/completion.ts +48 -23
  107. package/src/findings/emitter.ts +1 -1
  108. package/src/findings/emitters.ts +23 -41
  109. package/src/{implementations.ts → findings/implementations.ts} +4 -4
  110. package/src/{in-operands.ts → findings/in-operands.ts} +3 -3
  111. package/src/findings/interfaces.ts +3 -3
  112. package/src/findings/intra-function.ts +3 -3
  113. package/src/findings/reads-and-writes.ts +99 -26
  114. package/src/findings/unused-declarations.ts +4 -28
  115. package/src/findings/visibility-narrowing.ts +25 -26
  116. package/src/framework-lifecycle.ts +42 -14
  117. package/src/import-bindings.ts +98 -29
  118. package/src/index/sites.ts +781 -0
  119. package/src/indexed-access.ts +570 -527
  120. package/src/interface-satisfaction.ts +2 -2
  121. package/src/inventory.ts +19 -0
  122. package/src/keyof-comparisons.ts +131 -116
  123. package/src/{components.ts → liveness/components.ts} +3 -3
  124. package/src/{graph.ts → liveness/graph.ts} +93 -27
  125. package/src/liveness/matrix.ts +162 -0
  126. package/src/liveness/sweep.ts +855 -0
  127. package/src/load/expected-module.ts +8 -2
  128. package/src/load/specifiers.ts +8 -5
  129. package/src/load/synthesize.ts +6 -0
  130. package/src/load/workspace-resolution.ts +9 -2
  131. package/src/module-declarations.ts +74 -98
  132. package/src/overrides.ts +162 -134
  133. package/src/paths.ts +54 -8
  134. package/src/platform-calls.ts +3 -3
  135. package/src/platform-symbols.ts +11 -27
  136. package/src/query.ts +81 -0
  137. package/src/references/read-rule.ts +60 -0
  138. package/src/references/read-rules.ts +55 -0
  139. package/src/references/sink.ts +314 -0
  140. package/src/references/sources.ts +632 -0
  141. package/src/references.ts +175 -1733
  142. package/src/report.ts +1 -1
  143. package/src/roots/configured-declarations.ts +3 -0
  144. package/src/roots/published-reach.ts +9 -4
  145. package/src/roots/root-set.ts +1 -1
  146. package/src/roots/unseen-loads.ts +49 -11
  147. package/src/serialization-contract.ts +8 -3
  148. package/src/stores.ts +15 -3
  149. package/src/symbol-keys.ts +102 -102
  150. package/src/tests/file-class.ts +135 -0
  151. package/src/{outside-tests.ts → tests/outside.ts} +16 -10
  152. package/src/{test-support.ts → tests/support.ts} +9 -20
  153. package/src/type-parameter-reads.ts +187 -158
  154. package/src/unimplemented-callees.ts +297 -280
  155. package/src/verbs/analyze.ts +3 -2
  156. package/src/verbs/explain.ts +83 -158
  157. package/src/verbs/print-retained.ts +3 -3
  158. package/dist/src/matrix.js +0 -273
  159. package/dist/src/sweep.js +0 -222
  160. package/src/matrix.ts +0 -463
  161. package/src/sweep.ts +0 -361
package/src/sweep.ts DELETED
@@ -1,361 +0,0 @@
1
- /**
2
- * Which declarations of one graph are dead, and under which liveness relation.
3
- * Reference counting holds a declaration live when any declaration references it,
4
- * whatever that one's own liveness, or when it is a root of a kind that names a caller;
5
- * reachability holds it live when a path of references reaches it from a root. So a
6
- * declaration only unreachable ones reference is live under the first, dead under the second.
7
- */
8
-
9
- import type { TSExemptionClass } from "./exempt-classes.ts";
10
- import { namesACaller, OUTSIDE, type Edge, type Graph } from "./graph.ts";
11
- import type { InventorySymbol } from "./inventory.ts";
12
- import type { Position } from "./position.ts";
13
-
14
- /** The liveness relation that decided a declaration, as a finding spells it. */
15
- export type Relation = "reference-counting" | "reachability";
16
-
17
- /** One declaration an exemption class holds back, and the evidence it was found by. */
18
- export interface Exemption {
19
- /** The identifier of the declaration held back. */
20
- readonly id: string;
21
- readonly class: TSExemptionClass;
22
- /** One clause naming the relation, then the thing it relates to: `passed to JSON.stringify`. */
23
- readonly detail: string;
24
- /** Where the evidence is written. */
25
- readonly site: Position;
26
- /**
27
- * The component file, below the target root, whose markup holds the evidence. The
28
- * record holds only while that file is live.
29
- */
30
- readonly whileLive?: string;
31
- /**
32
- * The interface methods the retained member implements, and whether its body is empty.
33
- * The record holds only while one of them is called through its interface.
34
- */
35
- readonly implementing?: Implementing;
36
- /** Whether the record holds only where a write the mode counts stores into the declaration. */
37
- readonly whileWritten?: true;
38
- }
39
-
40
- /** The interface methods one retained class member implements, and whether its body is empty. */
41
- export interface Implementing {
42
- /** The declarations of the interface's methods the member answers, by identifier. */
43
- readonly methods: readonly string[];
44
- readonly empty: boolean;
45
- }
46
-
47
- /**
48
- * The run's reference mode, decided once by the composition and read by every stage
49
- * that counts references.
50
- */
51
- export interface Mode {
52
- /**
53
- * Counts no reference a test file made, under either relation. Every reference a root
54
- * in a test file makes is such a reference, so nothing only a test reaches is live.
55
- */
56
- readonly production: boolean;
57
- }
58
-
59
- /** What one sweep runs over besides the graph. */
60
- export interface SweepInput {
61
- /**
62
- * The declarations a matched suppression names. A mark holds its declaration live
63
- * under both relations and seeds reachability through every reference it makes, so
64
- * nothing only a marked declaration references is dead.
65
- */
66
- readonly marked: readonly string[];
67
- /**
68
- * The exemptions the run computed. An exempt declaration is never a candidate, and it
69
- * seeds reachability as a mark does, so nothing it references is dead; it is live
70
- * under reference counting only where something references it. Absent, the sweep
71
- * holds nothing back.
72
- */
73
- readonly exempt?: readonly Exemption[];
74
- /**
75
- * The declarations a question the checker did not answer could have kept live. Each
76
- * is held as an exempt declaration is. Absent, the sweep holds nothing back for one.
77
- */
78
- readonly unanswered?: readonly string[];
79
- readonly mode: Mode;
80
- }
81
-
82
- /** One dead declaration, the relation that found it, and the counts a kind reads. */
83
- export interface Candidate {
84
- readonly id: string;
85
- /** The references a production file made to the declaration, whatever the mode counts. */
86
- readonly productionRefs: number;
87
- /** The references a test file made to it. */
88
- readonly testRefs: number;
89
- /**
90
- * Reference counting where the mode counts no reference to the declaration, and
91
- * reachability otherwise, so a declaration dead under both carries the stronger claim.
92
- */
93
- readonly relation: Relation;
94
- /**
95
- * Whether the test-of-dead-code rule admitted the declaration: a test declaration whose
96
- * set of referenced production declarations is non-empty and wholly dead.
97
- */
98
- readonly testOfDeadCode: boolean;
99
- }
100
-
101
- /** One graph's answer: what each relation holds live, and what is dead. */
102
- export interface Liveness {
103
- /**
104
- * Per declaration, the relations that hold it live, reference counting first. A
105
- * declaration neither holds live has no entry.
106
- */
107
- readonly liveUnder: ReadonlyMap<string, readonly Relation[]>;
108
- /** The dead declarations, in the order the inventory holds them. */
109
- readonly candidates: readonly Candidate[];
110
- }
111
-
112
- /**
113
- * Answers which declarations of one graph are dead under the input's mode.
114
- *
115
- * The order is the order the answers depend on: the callers and the marks are live
116
- * before either relation runs, the candidate set is every judged declaration at least
117
- * one relation does not hold live and no exemption holds back, and the tests of dead
118
- * code join it.
119
- */
120
- export function sweep(graph: Graph, input: SweepInput): Liveness {
121
- const marked = flagged(graph, input.marked);
122
- const exempt = flagged(graph, [
123
- ...(input.exempt ?? []).map((record) => record.id),
124
- ...(input.unanswered ?? []),
125
- ]);
126
- const held = marked.map((mark, at) => mark || exempt[at] === true);
127
- // Every root, every mark and every exemption seeds reachability. A root of a kind that
128
- // names a caller, and a mark, are live under reference counting as well; a root that
129
- // only supposes a caller is not, and neither is an exemption.
130
- const called = graph.symbols.map(() => false);
131
- for (const root of graph.rooted) {
132
- if (namesACaller(root.kind)) {
133
- called[root.at] = true;
134
- }
135
- }
136
-
137
- const counted = (at: number): number => {
138
- const made = graph.made[at];
139
- if (made === undefined) {
140
- return 0;
141
- }
142
- return input.mode.production ? made.production : made.production + made.test;
143
- };
144
- const reached = reachable(graph, held, input.mode);
145
- const liveUnder = graph.symbols.map((_symbol, at): readonly Relation[] => {
146
- const relations: Relation[] = [];
147
- if (marked[at] === true || called[at] === true || counted(at) > 0) {
148
- relations.push("reference-counting");
149
- }
150
- if (reached[at] === true) {
151
- relations.push("reachability");
152
- }
153
- return relations;
154
- });
155
-
156
- const dead = graph.symbols.map(
157
- (_symbol, at) =>
158
- graph.subject[at] === true && exempt[at] !== true && (liveUnder[at]?.length ?? 0) < 2,
159
- );
160
- const testOfDeadCode = admitTestsOfDeadCode(graph, dead, held);
161
-
162
- const candidates: Candidate[] = [];
163
- const live = new Map<string, readonly Relation[]>();
164
- graph.symbols.forEach((symbol, at) => {
165
- const relations = liveUnder[at] ?? [];
166
- if (relations.length > 0) {
167
- live.set(symbol.id, relations);
168
- }
169
- if (dead[at] !== true) {
170
- return;
171
- }
172
- candidates.push({
173
- id: symbol.id,
174
- productionRefs: graph.made[at]?.production ?? 0,
175
- testRefs: graph.made[at]?.test ?? 0,
176
- relation: counted(at) === 0 ? "reference-counting" : "reachability",
177
- testOfDeadCode: testOfDeadCode[at] === true,
178
- });
179
- });
180
-
181
- return { liveUnder: live, candidates };
182
- }
183
-
184
- /**
185
- * The directory a test runner reads a manual mock from, whose exports it puts in place of
186
- * the module's: https://jestjs.io/docs/manual-mocks
187
- */
188
- const MANUAL_MOCKS = "__mocks__";
189
-
190
- /** Whether a test runner calls or reads one export of a test file: a function, or any export of a manual mock. */
191
- export function runnerCalls(symbol: InventorySymbol): boolean {
192
- return symbol.kind === "function" || symbol.position.path.split("/").includes(MANUAL_MOCKS);
193
- }
194
-
195
- /**
196
- * Per declaration of the graph, whether the run that counts test references reaches it.
197
- * That run's roots are the graph's own and, of each test file, its top level and every
198
- * function it exports, which a test runner may call, and every export of a manual mock,
199
- * which the runner reads; any other export of a test file, and test-support code, which
200
- * runs where a test imports it, are judged as any file's are. `held` seeds it as a mark
201
- * seeds a sweep; with `namespaces` false a file read reaches none of the file's exports.
202
- */
203
- export function reachedWithTheTests(
204
- graph: Graph,
205
- held: readonly string[],
206
- namespaces = true,
207
- ): readonly boolean[] {
208
- const tests = graph.symbols.flatMap((symbol, at) => {
209
- const exported = symbol.exported && graph.symbols[graph.parent[at] ?? OUTSIDE]?.kind === "file";
210
- const called = exported && runnerCalls(symbol);
211
- return graph.test[at] === true &&
212
- graph.support[at] !== true &&
213
- (symbol.kind === "file" || called)
214
- ? [{ at, kind: "test-runner" as const }]
215
- : [];
216
- });
217
- return reachable(
218
- { ...graph, rooted: [...graph.rooted, ...tests] },
219
- flagged(graph, held),
220
- { production: false },
221
- namespaces,
222
- );
223
- }
224
-
225
- /**
226
- * The references a loaded consumer makes that the mode counts. A production mode counts
227
- * none a consumer's test file made, as it counts none of the target's.
228
- */
229
- function callsOf(graph: Graph, mode: Mode): readonly Edge[] {
230
- return graph.consumed.filter((edge) => !mode.production || !edge.test);
231
- }
232
-
233
- /** Per declaration of the graph, whether one of the identifiers names it. */
234
- function flagged(graph: Graph, ids: readonly string[]): boolean[] {
235
- const flags = graph.symbols.map(() => false);
236
- for (const id of ids) {
237
- const at = graph.at(id);
238
- if (at !== OUTSIDE) {
239
- flags[at] = true;
240
- }
241
- }
242
- return flags;
243
- }
244
-
245
- /**
246
- * Per declaration, whether a seed reaches it. The two seeds reach through different
247
- * reference sets: a held declaration, marked or exempt, is live by a mechanism the
248
- * analysis cannot see, so no mode withholds a reference it makes, a test declaration's
249
- * included, while a root reaches through the references the mode counts. The held seed
250
- * runs first, so what it reaches is expanded under the wider rule.
251
- */
252
- function reachable(
253
- graph: Graph,
254
- held: readonly boolean[],
255
- mode: Mode,
256
- namespaces = true,
257
- ): readonly boolean[] {
258
- const reached = graph.symbols.map(() => false);
259
- const expanded = graph.symbols.map(() => false);
260
- const queue: number[] = [];
261
-
262
- // Reaching a file reaches its top level: the references written outside every
263
- // declaration it holds. A reference that reads a file reads the module's namespace,
264
- // which reaches what the module exports as well. An evaluation runs the top level
265
- // and reads no export, and a root does not expand either: a file a runtime runs
266
- // without reading its exports is a root by itself.
267
- const enter = (at: number, expands: boolean): void => {
268
- if (!reached[at]) {
269
- reached[at] = true;
270
- queue.push(at);
271
- }
272
- if (!expands || expanded[at] === true) {
273
- return;
274
- }
275
- expanded[at] = true;
276
- for (const exported of graph.exportsOf[at] ?? []) {
277
- enter(exported, false);
278
- }
279
- };
280
- const walk = (holding: boolean): void => {
281
- for (let at = queue.pop(); at !== undefined; at = queue.pop()) {
282
- for (const edge of graph.out[at] ?? []) {
283
- if (!holding && mode.production && edge.test) {
284
- continue;
285
- }
286
- enter(edge.to, namespaces && !edge.evaluation);
287
- }
288
- }
289
- };
290
-
291
- held.forEach((hold, at) => {
292
- if (hold) {
293
- enter(at, false);
294
- }
295
- });
296
- walk(true);
297
- for (const root of graph.rooted) {
298
- enter(root.at, false);
299
- }
300
- // A consumer's reference reaches what it names as one of the target's own does.
301
- for (const edge of callsOf(graph, mode)) {
302
- enter(edge.to, namespaces && !edge.evaluation);
303
- }
304
- walk(false);
305
- return reached;
306
- }
307
-
308
- /**
309
- * Admits every test declaration whose set of referenced production declarations is
310
- * non-empty and wholly dead, and returns which it admitted. An ambient declaration describes
311
- * code the target does not implement, so it is no production declaration here. The set is
312
- * read from every reference the test makes, which a production sweep does not count. A
313
- * test-support declaration a test referencing a live target also references is a live
314
- * target of every test referencing it. A marked or exempt test is never admitted.
315
- */
316
- function admitTestsOfDeadCode(
317
- graph: Graph,
318
- dead: boolean[],
319
- held: readonly boolean[],
320
- ): readonly boolean[] {
321
- const testCode = (at: number): boolean => graph.test[at] === true || graph.support[at] === true;
322
- const judged = graph.symbols.map((_symbol, at) => {
323
- let targets = 0;
324
- let live = 0;
325
- if (testCode(at) && graph.subject[at] === true && held[at] !== true) {
326
- for (const edge of graph.out[at] ?? []) {
327
- if (
328
- testCode(edge.to) ||
329
- graph.subject[edge.to] !== true ||
330
- graph.ambient[edge.to] === true
331
- ) {
332
- continue;
333
- }
334
- targets += 1;
335
- if (dead[edge.to] !== true) {
336
- live += 1;
337
- }
338
- }
339
- }
340
- return { targets, live };
341
- });
342
- const shared = new Set<number>();
343
- judged.forEach(({ live }, at) => {
344
- if (live > 0) {
345
- for (const edge of graph.out[at] ?? []) {
346
- if (graph.support[edge.to] === true) {
347
- shared.add(edge.to);
348
- }
349
- }
350
- }
351
- });
352
- const admitted = graph.symbols.map(() => false);
353
- judged.forEach(({ targets, live }, at) => {
354
- const sharing = (graph.out[at] ?? []).some((edge) => shared.has(edge.to) && edge.to !== at);
355
- if (targets > 0 && live === 0 && !sharing) {
356
- dead[at] = true;
357
- admitted[at] = true;
358
- }
359
- });
360
- return admitted;
361
- }