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

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 (72) 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 +5 -4
  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/findings/intra-function.js +50 -37
  25. package/dist/src/index/sites.js +1 -1
  26. package/dist/src/index/values.js +124 -0
  27. package/dist/src/intra-function-parts.js +1149 -222
  28. package/dist/src/liveness/sweep.js +2 -2
  29. package/dist/src/query.js +48 -0
  30. package/dist/src/roots/configured-declarations.js +1 -1
  31. package/dist/src/verbs/print-retained.js +23 -1
  32. package/jsr.json +1 -1
  33. package/package.json +2 -2
  34. package/src/alias-chain.ts +38 -2
  35. package/src/analysis.ts +19 -100
  36. package/src/bindings.ts +1 -1
  37. package/src/conformance-record.ts +2 -2
  38. package/src/contract/contract.generated.ts +5 -4
  39. package/src/dependencies.ts +1 -1
  40. package/src/exemptions/argument-places.ts +198 -0
  41. package/src/{calls.ts → exemptions/calls.ts} +190 -224
  42. package/src/{exempt-classes.ts → exemptions/classes.ts} +1 -1
  43. package/src/exemptions/decorator.ts +56 -0
  44. package/src/exemptions/detectors.ts +75 -0
  45. package/src/{enum-group.ts → exemptions/enum-group.ts} +9 -9
  46. package/src/{erased-stores.ts → exemptions/erased-stores.ts} +5 -8
  47. package/src/{framework-lifecycle.ts → exemptions/framework-lifecycle.ts} +39 -48
  48. package/src/{generated-file.ts → exemptions/generated-file.ts} +6 -6
  49. package/src/{injection-container.ts → exemptions/injection-container.ts} +28 -33
  50. package/src/exemptions/installed-globals.ts +255 -0
  51. package/src/{interface-satisfaction.ts → exemptions/interface-satisfaction.ts} +69 -160
  52. package/src/exemptions/lifecycle.ts +286 -0
  53. package/src/{reflective-lookup.ts → exemptions/reflective-lookup.ts} +5 -5
  54. package/src/{serialization-contract.ts → exemptions/serialization-contract.ts} +563 -429
  55. package/src/exemptions/sites.ts +293 -0
  56. package/src/{template-field.ts → exemptions/template-field.ts} +11 -11
  57. package/src/{value-reach.ts → exemptions/value-reach.ts} +16 -8
  58. package/src/facts.ts +1 -1
  59. package/src/findings/intra-function.ts +59 -37
  60. package/src/index/sites.ts +1 -1
  61. package/src/index/values.ts +170 -0
  62. package/src/intra-function-parts.ts +1435 -247
  63. package/src/liveness/sweep.ts +2 -2
  64. package/src/query.ts +109 -10
  65. package/src/roots/configured-declarations.ts +1 -2
  66. package/src/stores.ts +1 -1
  67. package/src/verbs/print-retained.ts +29 -1
  68. package/dist/src/exempt.js +0 -164
  69. package/dist/src/value-flow.js +0 -74
  70. package/src/decorator.ts +0 -78
  71. package/src/exempt.ts +0 -295
  72. package/src/value-flow.ts +0 -104
@@ -0,0 +1,293 @@
1
+ /**
2
+ * The one walk of a project's own files every detector shares: its calls and the class
3
+ * `this` stands for at each, its classes, the values it hands to positions typed by
4
+ * their context, its functions' parameters, its decorated declarations and its
5
+ * assignments to properties. The walk reads syntax only; what a call invokes through a
6
+ * method of `Function.prototype` asks the checker, once per project, at the first
7
+ * detector that needs it.
8
+ */
9
+
10
+ import {
11
+ isArrowFunction,
12
+ isBinaryExpression,
13
+ isClassDeclaration,
14
+ isClassExpression,
15
+ isClassStaticBlockDeclaration,
16
+ isElementAccessExpression,
17
+ isFunctionDeclaration,
18
+ isFunctionExpression,
19
+ isGetAccessorDeclaration,
20
+ isIdentifier,
21
+ isMethodDeclaration,
22
+ isNoSubstitutionTemplateLiteral,
23
+ isObjectLiteralExpression,
24
+ isPropertyAccessExpression,
25
+ isPropertyDeclaration,
26
+ isSetAccessorDeclaration,
27
+ isStringLiteral,
28
+ isVariableDeclaration,
29
+ ModifierFlags,
30
+ SyntaxKind,
31
+ type CallExpression,
32
+ type ClassLikeDeclaration,
33
+ type Expression,
34
+ type Node,
35
+ type ParameterDeclaration,
36
+ } from "@typescript/native/ast";
37
+ import { handedOn, valuesOf } from "../index/values.ts";
38
+ import { isParameterProperty, nodeKey, type Inventory } from "../inventory.ts";
39
+ import type { ProjectView } from "../session.ts";
40
+ import { parenthesesRemoved } from "../unwrap.ts";
41
+ import { decoratorsOf, invocations, type Invocations } from "./calls.ts";
42
+
43
+ /** One call of the project's own files, and the class `this` is a value of where it is written. */
44
+ export interface CallSite {
45
+ readonly call: CallExpression;
46
+ /** The inventory's identifier of the class, where `this` is an instance of one. */
47
+ readonly self: string | undefined;
48
+ }
49
+
50
+ /** One class the inventory holds, declared or written as an expression. */
51
+ interface ClassSite {
52
+ readonly id: string;
53
+ readonly node: ClassLikeDeclaration;
54
+ }
55
+
56
+ /** One value the project hands to a position the checker types from its context. */
57
+ interface FlowSite {
58
+ readonly value: Expression;
59
+ /** The class `this` stands for, where the value is `this` inside an instance member. */
60
+ readonly self: string | undefined;
61
+ }
62
+
63
+ /** One identifier-named parameter of a function, method or function-valued variable of the project. */
64
+ export interface ParameterSite {
65
+ /** The inventory's identifier of the function's declaration. */
66
+ readonly fn: string;
67
+ /** Its place among the arguments a call binds, a `this` parameter taking none. */
68
+ readonly index: number;
69
+ readonly rest: boolean;
70
+ readonly name: Node;
71
+ }
72
+
73
+ /** One assignment of a value to a property a key written as a name or a string names. */
74
+ interface StoreSite {
75
+ readonly store: Expression;
76
+ readonly receiver: Expression;
77
+ readonly key: string;
78
+ readonly value: Expression;
79
+ }
80
+
81
+ /** The property one assignment's left side names on its receiver, where a written key names it. */
82
+ function storeOf(node: Node): StoreSite | undefined {
83
+ if (!isBinaryExpression(node) || node.operatorToken.kind !== SyntaxKind.EqualsToken) {
84
+ return undefined;
85
+ }
86
+ const left = parenthesesRemoved(node.left);
87
+ if (isPropertyAccessExpression(left) && isIdentifier(left.name)) {
88
+ return { store: node, receiver: left.expression, key: left.name.text, value: node.right };
89
+ }
90
+ if (
91
+ isElementAccessExpression(left) &&
92
+ (isStringLiteral(left.argumentExpression) ||
93
+ isNoSubstitutionTemplateLiteral(left.argumentExpression))
94
+ ) {
95
+ return {
96
+ store: node,
97
+ receiver: left.expression,
98
+ key: left.argumentExpression.text,
99
+ value: node.right,
100
+ };
101
+ }
102
+ return undefined;
103
+ }
104
+
105
+ /** One declaration the inventory holds that carries a decorator. */
106
+ interface DecoratedSite {
107
+ readonly id: string;
108
+ readonly node: Node;
109
+ }
110
+
111
+ /** What one walk of a project's own files found. */
112
+ interface Walked {
113
+ readonly calls: readonly CallSite[];
114
+ readonly classes: readonly ClassSite[];
115
+ readonly flows: readonly FlowSite[];
116
+ readonly parameters: readonly ParameterSite[];
117
+ readonly decorated: readonly DecoratedSite[];
118
+ /** The assignments to a property a written key names. */
119
+ readonly stores: readonly StoreSite[];
120
+ /** The parameter properties the inventory holds, by identifier. */
121
+ readonly parameterProperties: ReadonlySet<string>;
122
+ /** The class properties whose declaration writes an initializer, by identifier. */
123
+ readonly initialized: ReadonlySet<string>;
124
+ }
125
+
126
+ /** Whether one node declares a static member, whose `this` is the class itself. */
127
+ function isStatic(node: Node): boolean {
128
+ const flags = (node as { readonly modifierFlags?: number }).modifierFlags ?? 0;
129
+ return (flags & ModifierFlags.Static) !== 0;
130
+ }
131
+
132
+ /**
133
+ * Whether one node opens a body whose `this` is no instance of the enclosing class: a
134
+ * function that is not an arrow, a static block, a static member, and a method or an
135
+ * accessor of an object literal, whose `this` is the literal.
136
+ */
137
+ function resetsThis(node: Node): boolean {
138
+ return (
139
+ isFunctionDeclaration(node) ||
140
+ isFunctionExpression(node) ||
141
+ isClassStaticBlockDeclaration(node) ||
142
+ isStatic(node) ||
143
+ ((isMethodDeclaration(node) ||
144
+ isGetAccessorDeclaration(node) ||
145
+ isSetAccessorDeclaration(node)) &&
146
+ isObjectLiteralExpression(node.parent))
147
+ );
148
+ }
149
+
150
+ /** The parameters and declaring node of one function-like declaration of the project. */
151
+ function functionOf(
152
+ node: Node,
153
+ ): { readonly declared: Node; readonly parameters: readonly ParameterDeclaration[] } | undefined {
154
+ if (
155
+ isFunctionDeclaration(node) ||
156
+ (isMethodDeclaration(node) && !isObjectLiteralExpression(node.parent))
157
+ ) {
158
+ return { declared: node, parameters: node.parameters };
159
+ }
160
+ if (
161
+ isVariableDeclaration(node) &&
162
+ node.initializer !== undefined &&
163
+ (isArrowFunction(node.initializer) || isFunctionExpression(node.initializer))
164
+ ) {
165
+ return { declared: node, parameters: node.initializer.parameters };
166
+ }
167
+ return undefined;
168
+ }
169
+
170
+ function walk<Brand>(project: ProjectView<Brand>, held: Inventory): Walked {
171
+ const calls: CallSite[] = [];
172
+ const classes: ClassSite[] = [];
173
+ const flows: FlowSite[] = [];
174
+ const parameters: ParameterSite[] = [];
175
+ const decorated: DecoratedSite[] = [];
176
+ const stores: StoreSite[] = [];
177
+ const parameterProperties = new Set<string>();
178
+ const initialized = new Set<string>();
179
+ for (const file of project.ownSourceFiles()) {
180
+ const visit = (node: Node, self: string | undefined): void => {
181
+ const id = held.declarations.get(nodeKey(file, node));
182
+ let inner = self;
183
+ if (isClassDeclaration(node) || isClassExpression(node)) {
184
+ inner = id;
185
+ if (id !== undefined) {
186
+ classes.push({ id, node });
187
+ }
188
+ } else if (resetsThis(node)) {
189
+ inner = undefined;
190
+ }
191
+ if (id !== undefined) {
192
+ if (isParameterProperty(node)) {
193
+ parameterProperties.add(id);
194
+ }
195
+ if (decoratorsOf(node).length > 0) {
196
+ decorated.push({ id, node });
197
+ }
198
+ if (isPropertyDeclaration(node) && node.initializer !== undefined) {
199
+ initialized.add(id);
200
+ }
201
+ }
202
+ if (node.kind === SyntaxKind.CallExpression) {
203
+ calls.push({ call: node as CallExpression, self: inner });
204
+ }
205
+ const store = storeOf(node);
206
+ if (store !== undefined) {
207
+ stores.push(store);
208
+ }
209
+ for (const written of handedOn(node)) {
210
+ for (const value of valuesOf(written)) {
211
+ flows.push({ value, self: value.kind === SyntaxKind.ThisKeyword ? inner : undefined });
212
+ }
213
+ }
214
+ const fn = functionOf(node);
215
+ const fnId = fn === undefined ? undefined : held.declarations.get(nodeKey(file, fn.declared));
216
+ if (fn !== undefined && fnId !== undefined) {
217
+ fn.parameters
218
+ .filter((parameter) => !(isIdentifier(parameter.name) && parameter.name.text === "this"))
219
+ .forEach((parameter, index) => {
220
+ if (isIdentifier(parameter.name)) {
221
+ parameters.push({
222
+ fn: fnId,
223
+ index,
224
+ rest: parameter.dotDotDotToken !== undefined,
225
+ name: parameter.name,
226
+ });
227
+ }
228
+ });
229
+ }
230
+ node.forEachChild((child) => {
231
+ visit(child, inner);
232
+ });
233
+ };
234
+ file.forEachChild((child) => {
235
+ visit(child, undefined);
236
+ });
237
+ }
238
+ return {
239
+ calls,
240
+ classes,
241
+ flows,
242
+ parameters,
243
+ decorated,
244
+ stores,
245
+ parameterProperties,
246
+ initialized,
247
+ };
248
+ }
249
+
250
+ /** One project's sites, walked at the first detector that reads them. */
251
+ export interface ProjectSites extends Walked {
252
+ /** What each call invokes, a call through `call`, `apply` or `bind` included. */
253
+ readonly invocations: Invocations;
254
+ }
255
+
256
+ /** The sites of one project's own files, each walk and each checker question made once. */
257
+ export function projectSites<Brand>(project: ProjectView<Brand>, held: Inventory): ProjectSites {
258
+ let walked: Walked | undefined;
259
+ let invoked: Invocations | undefined;
260
+ const sites = (): Walked => (walked ??= walk(project, held));
261
+ return {
262
+ get calls() {
263
+ return sites().calls;
264
+ },
265
+ get classes() {
266
+ return sites().classes;
267
+ },
268
+ get flows() {
269
+ return sites().flows;
270
+ },
271
+ get parameters() {
272
+ return sites().parameters;
273
+ },
274
+ get decorated() {
275
+ return sites().decorated;
276
+ },
277
+ get stores() {
278
+ return sites().stores;
279
+ },
280
+ get parameterProperties() {
281
+ return sites().parameterProperties;
282
+ },
283
+ get initialized() {
284
+ return sites().initialized;
285
+ },
286
+ get invocations() {
287
+ return (invoked ??= invocations(
288
+ project,
289
+ sites().calls.map((one) => one.call),
290
+ ));
291
+ },
292
+ };
293
+ }
@@ -12,17 +12,17 @@
12
12
  * type, because nothing ties the name to a type.
13
13
  */
14
14
 
15
- import { readComponent } from "./component-files.ts";
16
- import type { ResolvedConfig, TemplateDelimiters } from "./config.ts";
17
- import type { DetectorInput, Evidence } from "./exempt.ts";
18
- import type { Host } from "./host.ts";
19
- import type { SymbolKind } from "./inventory.ts";
20
- import { memberNames } from "./member-names.ts";
21
- import { extensionOf, isAbsolutePath, joinPath, normalizePath, relativePath } from "./paths.ts";
22
- import { isComponentFile, type Position } from "./position.ts";
23
- import { bytewise } from "./text/compare.ts";
24
- import { filesBelow } from "./fs/tree.ts";
25
- import { UsageError } from "./usage.ts";
15
+ import { readComponent } from "../component-files.ts";
16
+ import type { ResolvedConfig, TemplateDelimiters } from "../config.ts";
17
+ import type { DetectorInput, Evidence } from "./lifecycle.ts";
18
+ import type { Host } from "../host.ts";
19
+ import type { SymbolKind } from "../inventory.ts";
20
+ import { memberNames } from "../member-names.ts";
21
+ import { extensionOf, isAbsolutePath, joinPath, normalizePath, relativePath } from "../paths.ts";
22
+ import { isComponentFile, type Position } from "../position.ts";
23
+ import { bytewise } from "../text/compare.ts";
24
+ import { filesBelow } from "../fs/tree.ts";
25
+ import { UsageError } from "../usage.ts";
26
26
 
27
27
  /** One template, by its path below the target root. */
28
28
  interface TemplateFile {
@@ -2,9 +2,10 @@
2
2
  * The classes of the target a value of one type carries, and the members of the other
3
3
  * object types the target writes that it carries, as a serializer walking the value reaches
4
4
  * them: through constituents, array and tuple elements, the data members and index
5
- * signatures of the types the target writes, and the values `stored` gives for a type where
6
- * it erases them. The walk stops at `any`, `unknown`, a type parameter, a function and an
7
- * interface or class of another program, so an instance held only there is missed.
5
+ * signatures of the types the target writes, a class's polymorphic `this` type, and the values
6
+ * `stored` gives for a type where it erases them. The walk stops at `any`, `unknown`, any
7
+ * other type parameter, a function and an interface or class of another program, so an
8
+ * instance held only there is missed.
8
9
  */
9
10
 
10
11
  import {
@@ -17,14 +18,15 @@ import {
17
18
  import {
18
19
  ObjectFlags,
19
20
  SymbolFlags,
21
+ TypeFlags,
20
22
  type ObjectType,
21
23
  type Symbol as TSSymbol,
22
24
  type Type,
23
25
  } from "@typescript/native/sync";
24
- import { classNames, typesAt } from "./class-members.ts";
25
- import type { Inventory } from "./inventory.ts";
26
- import { must } from "./query.ts";
27
- import type { ProjectView } from "./session.ts";
26
+ import { classNames, typesAt } from "../class-members.ts";
27
+ import type { Inventory } from "../inventory.ts";
28
+ import { must } from "../query.ts";
29
+ import type { ProjectView } from "../session.ts";
28
30
 
29
31
  /**
30
32
  * The object types whose members the walk reads whatever declares them. A reverse-mapped
@@ -53,7 +55,10 @@ interface ValueReach {
53
55
  }
54
56
 
55
57
  /** The two conversion methods a serializer or a formatter resolves on a value's own shape. */
56
- const CONVERSION_NAMES: ReadonlySet<string> = new Set(["toJSON", "toString"]);
58
+ export const CONVERSION_NAMES: ReadonlySet<string> = new Set(["toJSON", "toString"]);
59
+
60
+ /** The types that keep nothing of a value they are given, or that a decode gives a value. */
61
+ export const ERASED = TypeFlags.Any | TypeFlags.Unknown;
57
62
 
58
63
  /**
59
64
  * The members that can answer a conversion: a serializer reads the name with `[[Get]]`
@@ -208,6 +213,9 @@ export function valueReach<Brand>(
208
213
  let found: readonly Type[] = [];
209
214
  if (type.isUnionType() || type.isIntersectionType()) {
210
215
  found = must(queries.constituents(type));
216
+ } else if (type.isTypeParameter() && type.isThisType === true) {
217
+ // A value of a class's polymorphic `this` type is an instance of the class.
218
+ found = [must(queries.constraintOf(type))].filter((one) => one !== undefined);
211
219
  } else if (type.isObjectType()) {
212
220
  const target = targetOf(type);
213
221
  const isList =
package/src/facts.ts CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  import { FACTS_VERSION } from "./contract/contract.generated.ts";
8
8
  import type { BindingRecord } from "./bindings.ts";
9
- import type { GeneratedFact } from "./generated-file.ts";
9
+ import type { GeneratedFact } from "./exemptions/generated-file.ts";
10
10
  import { LANGUAGE, PRODUCT } from "./product.ts";
11
11
  import { bytewise } from "./text/compare.ts";
12
12
 
@@ -2,10 +2,25 @@ import { CODE, type Confidence } from "../contract/contract.generated.ts";
2
2
  import type { Finding } from "../finding.ts";
3
3
  import { KINDS } from "../kinds.ts";
4
4
  import { parameterRef } from "../ref.ts";
5
- import { type PartFact } from "../intra-function-parts.ts";
5
+ import type { PartFact, PartKind } from "../intra-function-parts.ts";
6
6
  import { reachedWithTheTests } from "../liveness/sweep.ts";
7
7
  import type { Emitter, EmitterInput } from "./emitter.ts";
8
8
 
9
+ /** The code each part kind is reported under, and what its finding says before the overlap clause. */
10
+ const PART_KINDS: Readonly<Record<PartKind, { code: string; says: (name: string) => string }>> = {
11
+ parameter: {
12
+ code: CODE.unusedParameter,
13
+ says: (name) => `parameter ${name} is never read in the body`,
14
+ },
15
+ result: { code: CODE.unusedResult, says: () => "result is discarded at every call site" },
16
+ store: { code: CODE.deadStore, says: (name) => `value written to ${name} is never read` },
17
+ statement: { code: CODE.unreachableStatement, says: () => "statement is unreachable" },
18
+ case: {
19
+ code: CODE.unreachableCase,
20
+ says: (name) => `case ${name} is covered by an earlier case of the same switch`,
21
+ },
22
+ };
23
+
9
24
  /** A list of rules as a sentence names them. */
10
25
  function spelled(rules: readonly string[]): string {
11
26
  if (rules.length < 2) {
@@ -38,21 +53,29 @@ type ParameterVerdict =
38
53
  | undefined;
39
54
 
40
55
  /**
41
- * Whether one unread parameter is reported, and how. Of a free signature it is deleted. A
42
- * caller may pass a function used as a value more arguments than it declares, so there one
43
- * a read parameter follows is renamed to an underscore name, which the compilers read as
44
- * declared unread, and one no read parameter follows is deleted: kept where the call's
45
- * inference may read its annotation, at `probable` where nothing reads the annotation or
46
- * where it is a name a contextually typed object pattern binds.
56
+ * Whether one unread parameter is reported, and how (DS1401's precondition). Of a free
57
+ * signature it is deleted. Of a function used as a value, one a read parameter follows is
58
+ * renamed to an underscore name, and any other is deleted, kept where the call's inference
59
+ * may read its annotation. A deletion is `probable` where an unread annotation or a
60
+ * contextual pattern may matter to the caller, or where `length` counts the parameter of
61
+ * a function used as a value or of a root, whose holder may count it.
47
62
  */
48
63
  function parameterVerdict(
49
64
  part: PartFact,
50
65
  free: ReadonlySet<string>,
51
66
  freeButValued: ReadonlySet<string>,
67
+ rooted: ReadonlySet<string>,
52
68
  ): ParameterVerdict {
53
69
  const place = part.parameter;
70
+ const counted = place?.counted === true;
54
71
  if (place?.inValue !== true && free.has(part.declaration)) {
55
- return place?.annotated === undefined ? { fix: "delete", confidence: "certain" } : undefined;
72
+ if (place?.annotated !== undefined) {
73
+ return undefined;
74
+ }
75
+ return {
76
+ fix: "delete",
77
+ confidence: counted && rooted.has(part.declaration) ? "probable" : "certain",
78
+ };
56
79
  }
57
80
  const valued = place?.inValue === true || freeButValued.has(part.declaration);
58
81
  if (!valued) {
@@ -64,7 +87,7 @@ function parameterVerdict(
64
87
  if (place?.annotated === "steers") {
65
88
  return undefined;
66
89
  }
67
- const unseen = place?.annotated === "free" || place?.contextualPattern === true;
90
+ const unseen = place?.annotated === "free" || place?.contextualPattern === true || counted;
68
91
  return { fix: "delete", confidence: unseen ? "probable" : "certain" };
69
92
  }
70
93
 
@@ -78,16 +101,14 @@ function parameterVerdict(
78
101
  function reported(
79
102
  input: EmitterInput,
80
103
  part: PartFact,
104
+ rooted: ReadonlySet<string>,
81
105
  callersUnknown: ReadonlySet<string>,
82
106
  ): boolean {
83
- if (part.used) {
84
- return false;
85
- }
86
107
  const { free, freeButValued, discardedEverywhere } = input.intraFunction;
87
- switch (part.code) {
88
- case CODE.unusedParameter:
89
- return parameterVerdict(part, free, freeButValued) !== undefined;
90
- case CODE.unusedResult:
108
+ switch (part.kind) {
109
+ case "parameter":
110
+ return parameterVerdict(part, free, freeButValued, rooted) !== undefined;
111
+ case "result":
91
112
  return (
92
113
  free.has(part.declaration) &&
93
114
  discardedEverywhere.has(part.declaration) &&
@@ -120,49 +141,50 @@ export const intraFunction: Emitter = (input) => {
120
141
  const testCode = union.test[at] === true || union.support[at] === true;
121
142
  return !testCode || (reached[at] === true && !testsOfDeadCode.has(id));
122
143
  };
123
- const unknown = new Set(
144
+ const rooted: ReadonlySet<string> = new Set(
124
145
  union.rooted.flatMap((root) => {
125
146
  const symbol = union.symbols[root.at];
126
147
  return symbol === undefined ? [] : [symbol.id];
127
148
  }),
128
149
  );
150
+ const unknown = new Set(rooted);
129
151
  const { declared, loaded } = input.boundary.consumers;
130
152
  const closed = input.config.consumers.complete && declared.every((id) => loaded.includes(id));
131
- const published = (id: string): boolean => {
132
- const symbol = byId.get(id);
133
- return (
134
- symbol !== undefined &&
135
- symbol.kind !== "file" &&
136
- (symbol.exported || published(symbol.parent))
137
- );
138
- };
139
153
  if (!closed) {
140
- for (const part of input.intraFunction.parts) {
141
- if (published(part.declaration)) {
142
- unknown.add(part.declaration);
143
- }
154
+ for (const id of input.intraFunction.published) {
155
+ unknown.add(id);
144
156
  }
145
157
  }
146
158
  const found: Finding[] = [];
147
159
  for (const part of input.intraFunction.parts) {
148
160
  const declaration = byId.get(part.declaration);
149
- const overlap = KINDS.get(part.code)?.overlap?.ts ?? [];
150
- if (declaration === undefined || !judged(declaration.id) || !reported(input, part, unknown)) {
161
+ const { code, says } = PART_KINDS[part.kind];
162
+ const overlap = KINDS.get(code)?.overlap?.ts ?? [];
163
+ if (
164
+ declaration === undefined ||
165
+ !judged(declaration.id) ||
166
+ !reported(input, part, rooted, unknown)
167
+ ) {
151
168
  continue;
152
169
  }
153
170
  const verdict =
154
- part.code === CODE.unusedParameter
155
- ? parameterVerdict(part, input.intraFunction.free, input.intraFunction.freeButValued)
171
+ part.kind === "parameter"
172
+ ? parameterVerdict(
173
+ part,
174
+ input.intraFunction.free,
175
+ input.intraFunction.freeButValued,
176
+ rooted,
177
+ )
156
178
  : undefined;
157
179
  const message =
158
180
  verdict?.fix === "rename"
159
- ? `${part.message}, and a read parameter follows it, so rename it to _${part.name}`
160
- : part.message;
181
+ ? `${says(part.name)}, and a read parameter follows it, so rename it to _${part.name}`
182
+ : says(part.name);
161
183
  found.push({
162
- code: part.code,
184
+ code,
163
185
  position: part.position,
164
186
  symbol: {
165
- ...(part.code === CODE.unusedParameter
187
+ ...(part.kind === "parameter"
166
188
  ? { ref: parameterRef(declaration.ref, part.name), holder: declaration.ref }
167
189
  : { ref: declaration.ref }),
168
190
  kind: part.kind,
@@ -56,7 +56,7 @@ import type { Use } from "../references.ts";
56
56
  import { isLoadCall, type LoadCall } from "../roots/unseen-loads.ts";
57
57
  import { keyNameOf, type KeyedAccess } from "../symbol-keys.ts";
58
58
  import { parenthesesRemoved } from "../unwrap.ts";
59
- import { handedOn, valuesOf } from "../value-flow.ts";
59
+ import { handedOn, valuesOf } from "./values.ts";
60
60
 
61
61
  /**
62
62
  * The operators whose left side one assignment stores into. A compound assignment is