@cplieger/deadset-ts 3.10.0 → 3.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -13
- package/dist/src/analysis.js +56 -16
- package/dist/src/config.js +6 -0
- package/dist/src/conformance-record.js +2 -12
- package/dist/src/consumers.js +49 -0
- package/dist/src/discover.js +59 -23
- package/dist/src/findings/completion.js +49 -17
- package/dist/src/findings/non-code-artifacts.js +9 -2
- package/dist/src/findings/reads-and-writes.js +3 -1
- package/dist/src/findings-pass.js +3 -5
- package/dist/src/graph.js +8 -5
- package/dist/src/matrix.js +14 -3
- package/dist/src/references.js +21 -9
- package/dist/src/report.js +4 -1
- package/dist/src/reporters/annotations.js +36 -0
- package/dist/src/reporters/fingerprint.js +61 -0
- package/dist/src/reporters/reporter.js +7 -1
- package/dist/src/reporters/reporters.js +18 -0
- package/dist/src/reporters/rules.js +178 -0
- package/dist/src/reporters/sarif.js +235 -0
- package/dist/src/reporters/sha256.js +101 -0
- package/dist/src/reporters/template.js +877 -0
- package/dist/src/run.js +4 -10
- package/dist/src/session.js +15 -0
- package/dist/src/sweep.js +11 -0
- package/dist/src/verbs/analyze.js +48 -16
- package/dist/src/verbs/explain.js +317 -0
- package/jsr.json +1 -1
- package/package.json +1 -1
- package/src/analysis.ts +123 -29
- package/src/config.ts +8 -0
- package/src/conformance-record.ts +2 -14
- package/src/consumers.ts +86 -0
- package/src/discover.ts +67 -23
- package/src/findings/completion.ts +56 -20
- package/src/findings/non-code-artifacts.ts +9 -2
- package/src/findings/reads-and-writes.ts +3 -1
- package/src/findings-pass.ts +5 -7
- package/src/graph.ts +14 -5
- package/src/matrix.ts +14 -5
- package/src/references.ts +48 -9
- package/src/report.ts +17 -2
- package/src/reporters/annotations.ts +67 -0
- package/src/reporters/fingerprint.ts +69 -0
- package/src/reporters/json.ts +2 -2
- package/src/reporters/reporter.ts +21 -2
- package/src/reporters/reporters.ts +18 -1
- package/src/reporters/rules.ts +195 -0
- package/src/reporters/sarif.ts +287 -0
- package/src/reporters/sha256.ts +120 -0
- package/src/reporters/template.ts +989 -0
- package/src/reporters/text.ts +2 -3
- package/src/run.ts +5 -11
- package/src/session.ts +19 -0
- package/src/sweep.ts +13 -1
- package/src/verbs/analyze.ts +60 -19
- package/src/verbs/explain.ts +380 -0
package/src/reporters/text.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { WireTotals } from "../report.ts";
|
|
2
|
-
import type { Reporter } from "./reporter.ts";
|
|
1
|
+
import type { Report, WireTotals } from "../report.ts";
|
|
3
2
|
|
|
4
3
|
/** The kind token and the confidence a stale suppression is rendered with, both fixed. */
|
|
5
4
|
const STALE_KIND = "suppression";
|
|
@@ -30,7 +29,7 @@ function summary(totals: WireTotals): string {
|
|
|
30
29
|
* summary is deliberately not in that shape, so a filter for finding lines yields exactly
|
|
31
30
|
* them. Nothing else is written: no timestamp, duration or host detail.
|
|
32
31
|
*/
|
|
33
|
-
export const text
|
|
32
|
+
export const text = (report: Report): string => {
|
|
34
33
|
const lines = report.findings.map(
|
|
35
34
|
(found) =>
|
|
36
35
|
`${found.position.path}:${String(found.position.line)}:${String(found.position.column)}: ${found.symbol.kind} ${found.symbol.name}: ${found.message} [${found.confidence}] (${found.code})`,
|
package/src/run.ts
CHANGED
|
@@ -11,6 +11,7 @@ import { printRetainedVerb } from "./verbs/print-retained.ts";
|
|
|
11
11
|
import { printRootsVerb } from "./verbs/print-roots.ts";
|
|
12
12
|
import { ANALYZE_OPTIONS, analyzeVerb } from "./verbs/analyze.ts";
|
|
13
13
|
import { describeVerb } from "./verbs/describe.ts";
|
|
14
|
+
import { EXPLAIN_OPTIONS, explainVerb } from "./verbs/explain.ts";
|
|
14
15
|
import { EXIT_FAILURE, EXIT_USAGE, type Verb, type VerbOptions } from "./verbs/verb.ts";
|
|
15
16
|
import { versionVerb } from "./verbs/version.ts";
|
|
16
17
|
|
|
@@ -19,17 +20,14 @@ export interface Writer {
|
|
|
19
20
|
write(text: string): void;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
/**
|
|
23
|
-
* Every verb this command answers, in the order the usage text lists them. A verb
|
|
24
|
-
* with no `run` is listed, and invoking it is a usage error.
|
|
25
|
-
*/
|
|
23
|
+
/** Every verb this command answers, in the order the usage text lists them. */
|
|
26
24
|
const VERBS: readonly {
|
|
27
25
|
readonly name: string;
|
|
28
|
-
readonly run
|
|
26
|
+
readonly run: Verb;
|
|
29
27
|
readonly options?: VerbOptions;
|
|
30
28
|
}[] = [
|
|
31
29
|
{ name: "analyze", run: analyzeVerb, options: ANALYZE_OPTIONS },
|
|
32
|
-
{ name: "explain" },
|
|
30
|
+
{ name: "explain", run: explainVerb, options: EXPLAIN_OPTIONS },
|
|
33
31
|
{ name: "print-config", run: printConfigVerb },
|
|
34
32
|
{ name: "print-projects", run: printProjectsVerb },
|
|
35
33
|
{ name: "print-roots", run: printRootsVerb },
|
|
@@ -219,7 +217,7 @@ export function run(
|
|
|
219
217
|
|
|
220
218
|
const name = args[0];
|
|
221
219
|
const verb = VERBS.find((listed) => listed.name === name);
|
|
222
|
-
if (verb
|
|
220
|
+
if (verb !== undefined) {
|
|
223
221
|
return invoke(
|
|
224
222
|
verb.run,
|
|
225
223
|
verb.options ?? NO_OPTIONS,
|
|
@@ -228,10 +226,6 @@ export function run(
|
|
|
228
226
|
openClient,
|
|
229
227
|
);
|
|
230
228
|
}
|
|
231
|
-
if (verb !== undefined) {
|
|
232
|
-
err.write(`deadset-ts: ${verb.name} is not implemented\n`);
|
|
233
|
-
return EXIT_USAGE;
|
|
234
|
-
}
|
|
235
229
|
if (name !== undefined) {
|
|
236
230
|
err.write(`deadset-ts: unknown verb ${JSON.stringify(name)}\n`);
|
|
237
231
|
}
|
package/src/session.ts
CHANGED
|
@@ -193,6 +193,25 @@ function viewOf<Brand>(project: Project): ProjectView<Brand> {
|
|
|
193
193
|
};
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
+
/**
|
|
197
|
+
* The same project, its own source files narrowed to the ones `keep` admits. A consumer's
|
|
198
|
+
* program holds the target's files it imports beside its own, and a pass that enumerates
|
|
199
|
+
* the target's declarations in that program reads those files alone.
|
|
200
|
+
*/
|
|
201
|
+
export function narrowedTo<Brand>(
|
|
202
|
+
project: ProjectView<Brand>,
|
|
203
|
+
keep: (file: SourceFile) => boolean,
|
|
204
|
+
): ProjectView<Brand> {
|
|
205
|
+
let kept: readonly SourceFile[] | undefined;
|
|
206
|
+
return {
|
|
207
|
+
...project,
|
|
208
|
+
ownSourceFiles: () => {
|
|
209
|
+
kept ??= project.ownSourceFiles().filter(keep);
|
|
210
|
+
return kept;
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
196
215
|
/** Every diagnostic set a project is refused for carrying an error in. */
|
|
197
216
|
export interface ProjectDiagnostics {
|
|
198
217
|
readonly configFile: string;
|
package/src/sweep.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type { TSExemptionClass } from "./exempt-classes.ts";
|
|
10
|
-
import { namesACaller, OUTSIDE, type Graph } from "./graph.ts";
|
|
10
|
+
import { namesACaller, OUTSIDE, type Edge, type Graph } from "./graph.ts";
|
|
11
11
|
import type { Position } from "./position.ts";
|
|
12
12
|
|
|
13
13
|
/** The liveness relation that decided a declaration, as a finding spells it. */
|
|
@@ -156,6 +156,14 @@ export function sweep(graph: Graph, input: SweepInput): Liveness {
|
|
|
156
156
|
return { liveUnder: live, candidates };
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
+
/**
|
|
160
|
+
* The references a loaded consumer makes that the mode counts. A production mode counts
|
|
161
|
+
* none a consumer's test file made, as it counts none of the target's.
|
|
162
|
+
*/
|
|
163
|
+
function callsOf(graph: Graph, mode: Mode): readonly Edge[] {
|
|
164
|
+
return graph.consumed.filter((edge) => !mode.production || !edge.test);
|
|
165
|
+
}
|
|
166
|
+
|
|
159
167
|
/** Per declaration of the graph, whether one of the identifiers names it. */
|
|
160
168
|
function flagged(graph: Graph, ids: readonly string[]): boolean[] {
|
|
161
169
|
const flags = graph.symbols.map(() => false);
|
|
@@ -218,6 +226,10 @@ function reachable(graph: Graph, held: readonly boolean[], mode: Mode): readonly
|
|
|
218
226
|
for (const root of graph.rooted) {
|
|
219
227
|
enter(root.at, false);
|
|
220
228
|
}
|
|
229
|
+
// A consumer's reference reaches what it names as one of the target's own does.
|
|
230
|
+
for (const edge of callsOf(graph, mode)) {
|
|
231
|
+
enter(edge.to, !edge.evaluation);
|
|
232
|
+
}
|
|
221
233
|
walk(false);
|
|
222
234
|
return reached;
|
|
223
235
|
}
|
package/src/verbs/analyze.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { runAnalysis, type RunProject } from "../analysis.ts";
|
|
2
2
|
import { ConfigError, type Format } from "../config.ts";
|
|
3
3
|
import { CONFORMANCE, DECLARED_GAPS } from "../conformance.ts";
|
|
4
|
+
import { moduleIdentity } from "../consumers.ts";
|
|
4
5
|
import { analyzerProvenance, recordedFindings, verdictOf } from "../findings-pass.ts";
|
|
5
|
-
import {
|
|
6
|
-
import { normalizePath, relativePath, resolvePath } from "../paths.ts";
|
|
6
|
+
import { joinPath, normalizePath, relativePath, resolvePath } from "../paths.ts";
|
|
7
7
|
import type { Host } from "../host.ts";
|
|
8
8
|
import {
|
|
9
9
|
buildReport,
|
|
@@ -14,15 +14,15 @@ import {
|
|
|
14
14
|
} from "../report.ts";
|
|
15
15
|
import { json } from "../reporters/json.ts";
|
|
16
16
|
import { RENDERINGS } from "../reporters/reporters.ts";
|
|
17
|
+
import { parseTemplate, TemplateError, type Template } from "../reporters/template.ts";
|
|
17
18
|
import { resolve } from "../resolve.ts";
|
|
18
|
-
import type { Module } from "../scope.ts";
|
|
19
19
|
import { writeBaseline, type Recorded } from "../suppress-file.ts";
|
|
20
20
|
import { CONTRACT_VERSION } from "../version.ts";
|
|
21
21
|
import { EXIT_CLEAN, EXIT_FAILURE, type Verb, type VerbOptions } from "./verb.ts";
|
|
22
22
|
|
|
23
23
|
/** The options `analyze` takes beside the ones every verb takes. */
|
|
24
24
|
export const ANALYZE_OPTIONS: VerbOptions = {
|
|
25
|
-
plain: ["report", "baseline-write", "exit-code"],
|
|
25
|
+
plain: ["report", "baseline-write", "exit-code", "template"],
|
|
26
26
|
repeatable: ["format"],
|
|
27
27
|
};
|
|
28
28
|
|
|
@@ -30,9 +30,6 @@ export const ANALYZE_OPTIONS: VerbOptions = {
|
|
|
30
30
|
const EXIT_CODE_ON = "on";
|
|
31
31
|
const EXIT_CODE_OFF = "off";
|
|
32
32
|
|
|
33
|
-
/** Why a declared consumer is not loaded, which every report names it with. */
|
|
34
|
-
const NOT_LOADED = "this analyzer loads no consumer beside the target";
|
|
35
|
-
|
|
36
33
|
/** A path of the run no report can name, which ends the run with the failure code. */
|
|
37
34
|
class ReportPathError extends Error {
|
|
38
35
|
constructor(message: string) {
|
|
@@ -100,16 +97,52 @@ function configurationsOf(
|
|
|
100
97
|
});
|
|
101
98
|
}
|
|
102
99
|
|
|
103
|
-
/** The name one module publishes itself under: the scope's, else its manifest's. */
|
|
104
|
-
function identityOf(module: Module, host: Host): string {
|
|
105
|
-
return module.id === "" ? packageScope(host, module.path)("index.ts").package : module.id;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
100
|
/** A count with its noun, so a message reads for one record as well as for several. */
|
|
109
101
|
function counted(n: number, noun: string): string {
|
|
110
102
|
return `${String(n)} ${noun}${n === 1 ? "" : "s"}`;
|
|
111
103
|
}
|
|
112
104
|
|
|
105
|
+
/**
|
|
106
|
+
* The template `--template` names, read and parsed before any analysis, so a template that
|
|
107
|
+
* cannot be read or does not parse refuses the invocation. The template format with no
|
|
108
|
+
* template named is refused the same way.
|
|
109
|
+
*/
|
|
110
|
+
function templateOf(
|
|
111
|
+
host: Host,
|
|
112
|
+
invoked: string,
|
|
113
|
+
named: string | undefined,
|
|
114
|
+
formats: readonly Format[],
|
|
115
|
+
): Template | undefined {
|
|
116
|
+
if (named === undefined || named === "") {
|
|
117
|
+
if (formats.includes("template")) {
|
|
118
|
+
throw new ConfigError(
|
|
119
|
+
"malformed",
|
|
120
|
+
"",
|
|
121
|
+
"the template format renders the template --template names, and none was named",
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
let source: string;
|
|
127
|
+
try {
|
|
128
|
+
source = host.readFile(resolvePath(invoked, named));
|
|
129
|
+
} catch (error: unknown) {
|
|
130
|
+
throw new ConfigError(
|
|
131
|
+
"malformed",
|
|
132
|
+
"",
|
|
133
|
+
`--template=${named}: ${error instanceof Error ? error.message : String(error)}`,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
return parseTemplate(source);
|
|
138
|
+
} catch (error: unknown) {
|
|
139
|
+
if (!(error instanceof TemplateError)) {
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
throw new ConfigError("malformed", "", `--template=${named}: ${error.message}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
113
146
|
/** Every document one invocation asked for: the report, each rendering beside it, a baseline. */
|
|
114
147
|
interface Documents {
|
|
115
148
|
readonly report: string;
|
|
@@ -121,8 +154,9 @@ interface Documents {
|
|
|
121
154
|
* Analyzes the scope under the configuration and writes the JSON report to the path
|
|
122
155
|
* `--report` names, each rendering `--format` names beside it at the report's path with the
|
|
123
156
|
* format's suffix appended, and a baseline of every finding where `--baseline-write` names a
|
|
124
|
-
* path.
|
|
125
|
-
* verdict is the exit code, or 0
|
|
157
|
+
* path. The template format renders the template `--template` names. Nothing is written to
|
|
158
|
+
* the output stream; diagnostics go to the error stream and the verdict is the exit code, or 0
|
|
159
|
+
* under `--exit-code=off`, and a rendering that cannot be written ends the run with 3.
|
|
126
160
|
*/
|
|
127
161
|
export const analyzeVerb: Verb = ({ err, host, inputs, scope, openClient, option, repeated }) => {
|
|
128
162
|
const reportPath = option("report");
|
|
@@ -153,6 +187,7 @@ export const analyzeVerb: Verb = ({ err, host, inputs, scope, openClient, option
|
|
|
153
187
|
formats: formatsOf(repeated("format"), config.reporters.formats),
|
|
154
188
|
baseline: baseline === undefined ? undefined : resolvePath(invoked, baseline),
|
|
155
189
|
};
|
|
190
|
+
const template = templateOf(host, invoked, option("template"), documents.formats);
|
|
156
191
|
const scoped = scope();
|
|
157
192
|
const targetRoot = scoped.target.path;
|
|
158
193
|
|
|
@@ -177,12 +212,13 @@ export const analyzeVerb: Verb = ({ err, host, inputs, scope, openClient, option
|
|
|
177
212
|
version,
|
|
178
213
|
conformance: CONFORMANCE,
|
|
179
214
|
declaredGaps: DECLARED_GAPS,
|
|
180
|
-
target: { kind, root, identity:
|
|
215
|
+
target: { kind, root, identity: moduleIdentity(host, scoped.target) },
|
|
181
216
|
configurations: configurationsOf(run.projects, targetRoot),
|
|
182
|
-
|
|
183
|
-
id:
|
|
184
|
-
|
|
217
|
+
loaded: run.consumers.map((consumer) => ({
|
|
218
|
+
id: consumer.id,
|
|
219
|
+
path: runRelative(invoked, consumer.path),
|
|
185
220
|
})),
|
|
221
|
+
unavailable: [],
|
|
186
222
|
result,
|
|
187
223
|
testFileRules: run.testFileRules,
|
|
188
224
|
});
|
|
@@ -201,10 +237,15 @@ export const analyzeVerb: Verb = ({ err, host, inputs, scope, openClient, option
|
|
|
201
237
|
|
|
202
238
|
try {
|
|
203
239
|
host.writeFile(documents.report, json(report));
|
|
240
|
+
const options = {
|
|
241
|
+
failOn: config.reporters.failOn,
|
|
242
|
+
readSource: (path: string) => host.readFile(joinPath(targetRoot, path)),
|
|
243
|
+
template,
|
|
244
|
+
};
|
|
204
245
|
for (const format of documents.formats) {
|
|
205
246
|
const rendering = RENDERINGS.get(format);
|
|
206
247
|
if (rendering !== undefined) {
|
|
207
|
-
host.writeFile(documents.report + rendering.suffix, rendering.render(report));
|
|
248
|
+
host.writeFile(documents.report + rendering.suffix, rendering.render(report, options));
|
|
208
249
|
}
|
|
209
250
|
}
|
|
210
251
|
if (documents.baseline !== undefined) {
|
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import { runAnalysis, type RunAnalysis } from "../analysis.ts";
|
|
2
|
+
import { listing } from "../components.ts";
|
|
3
|
+
import type { Cascade } from "../config.ts";
|
|
4
|
+
import { ConfigError } from "../config.ts";
|
|
5
|
+
import type { CompletedFinding } from "../finding.ts";
|
|
6
|
+
import { OUTSIDE, swept as judged } from "../graph.ts";
|
|
7
|
+
import type { InventorySymbol } from "../inventory.ts";
|
|
8
|
+
import { referenceKey, type Configured } from "../matrix.ts";
|
|
9
|
+
import { positionKey } from "../position.ts";
|
|
10
|
+
import type { Reference } from "../references.ts";
|
|
11
|
+
import { resolve } from "../resolve.ts";
|
|
12
|
+
import type { Root } from "../roots.ts";
|
|
13
|
+
import { EXIT_CLEAN, EXIT_USAGE, type Verb, type VerbOptions } from "./verb.ts";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The three spellings of an explanation request, each naming the symbol to explain. They are
|
|
17
|
+
* one request, because exactly one state holds for a symbol and the answer is that state.
|
|
18
|
+
*/
|
|
19
|
+
const WHY_OPTIONS = ["why", "why-live", "why-not"] as const;
|
|
20
|
+
|
|
21
|
+
/** The options `explain` takes beside the ones every verb takes. */
|
|
22
|
+
export const EXPLAIN_OPTIONS: VerbOptions = { plain: [...WHY_OPTIONS], repeatable: [] };
|
|
23
|
+
|
|
24
|
+
/** The most partial matches a refused request prints. */
|
|
25
|
+
const MAX_PARTIAL_MATCHES = 20;
|
|
26
|
+
|
|
27
|
+
/** A count with its noun, so a line reads for one as well as for several. */
|
|
28
|
+
function counted(n: number, noun: string): string {
|
|
29
|
+
return `${String(n)} ${noun}${n === 1 ? "" : "s"}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A list for a line a maintainer reads, which says none rather than leaving the field blank. */
|
|
33
|
+
function listed(items: readonly string[]): string {
|
|
34
|
+
return items.length === 0 ? "none" : items.join(" ");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The one symbol a request names, from whichever of the three options names it. */
|
|
38
|
+
function namedSymbol(option: (name: string) => string | undefined): string {
|
|
39
|
+
const named = WHY_OPTIONS.flatMap((name) => {
|
|
40
|
+
const value = option(name);
|
|
41
|
+
return value === undefined || value === "" ? [] : [{ name, value }];
|
|
42
|
+
});
|
|
43
|
+
const [only, ...more] = named;
|
|
44
|
+
if (only === undefined) {
|
|
45
|
+
throw new ConfigError(
|
|
46
|
+
"malformed",
|
|
47
|
+
"",
|
|
48
|
+
"explain explains the symbol --why, --why-live or --why-not names, and none was named",
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
if (more.length > 0) {
|
|
52
|
+
throw new ConfigError(
|
|
53
|
+
"malformed",
|
|
54
|
+
"",
|
|
55
|
+
`explain explains one symbol, and ${named.map((one) => `--${one.name}`).join(", ")} each name one`,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
return only.value;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The part of a reference after its separator: the `Type.member` or bare name a maintainer types. */
|
|
62
|
+
function fragmentOf(symbol: InventorySymbol): string {
|
|
63
|
+
const at = symbol.ref.indexOf("#");
|
|
64
|
+
return at < 0 ? "" : symbol.ref.slice(at + 1);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function described(symbol: InventorySymbol): string {
|
|
68
|
+
return `${symbol.ref}\t${positionKey(symbol.position)}`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The one declaration a request names, or the candidates where it names none or several.
|
|
73
|
+
* The reference, the fragment of it a maintainer types and the display name are tried in
|
|
74
|
+
* order, each an exact match; a key several declarations answer is no answer.
|
|
75
|
+
*/
|
|
76
|
+
function subjectOf(
|
|
77
|
+
symbols: readonly InventorySymbol[],
|
|
78
|
+
named: string,
|
|
79
|
+
): { readonly subject?: InventorySymbol; readonly candidates: readonly string[] } {
|
|
80
|
+
const keys: ((symbol: InventorySymbol) => string)[] = [
|
|
81
|
+
(symbol) => symbol.ref,
|
|
82
|
+
fragmentOf,
|
|
83
|
+
(symbol) => symbol.name,
|
|
84
|
+
];
|
|
85
|
+
for (const key of keys) {
|
|
86
|
+
const matched = symbols.filter((symbol) => key(symbol) === named);
|
|
87
|
+
const [only] = matched;
|
|
88
|
+
if (only !== undefined && matched.length === 1) {
|
|
89
|
+
return { subject: only, candidates: [] };
|
|
90
|
+
}
|
|
91
|
+
if (matched.length > 1) {
|
|
92
|
+
return { candidates: matched.map(described) };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const folded = named.toLowerCase();
|
|
96
|
+
const partial = symbols
|
|
97
|
+
.filter(
|
|
98
|
+
(symbol) =>
|
|
99
|
+
symbol.ref.toLowerCase().includes(folded) || symbol.name.toLowerCase().includes(folded),
|
|
100
|
+
)
|
|
101
|
+
.map(described)
|
|
102
|
+
.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
103
|
+
return { candidates: partial.slice(0, MAX_PARTIAL_MATCHES) };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Every reference of the run once, a reference several configurations make counted once. */
|
|
107
|
+
function referencesOf(configured: readonly Configured[]): readonly Reference[] {
|
|
108
|
+
const seen = new Set<string>();
|
|
109
|
+
const held: Reference[] = [];
|
|
110
|
+
for (const reference of configured.flatMap((one) => one.references)) {
|
|
111
|
+
const key = referenceKey(reference);
|
|
112
|
+
if (!seen.has(key)) {
|
|
113
|
+
seen.add(key);
|
|
114
|
+
held.push(reference);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return held;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Every root of the run once per reason, in the order the projects name them. */
|
|
121
|
+
function rootsOf(configured: readonly Configured[]): readonly Root[] {
|
|
122
|
+
const seen = new Set<string>();
|
|
123
|
+
return configured
|
|
124
|
+
.flatMap((one) => one.roots)
|
|
125
|
+
.filter((root) => {
|
|
126
|
+
const key = [root.id, root.kind, root.source].join("\u0000");
|
|
127
|
+
if (seen.has(key)) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
seen.add(key);
|
|
131
|
+
return true;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* One step of a path: a reference, or a reference that reads a module and so reaches a
|
|
137
|
+
* declaration the module exports.
|
|
138
|
+
*/
|
|
139
|
+
type Hop =
|
|
140
|
+
| { readonly k: "reference"; readonly reference: Reference }
|
|
141
|
+
| { readonly k: "export"; readonly reference: Reference; readonly to: string };
|
|
142
|
+
|
|
143
|
+
/** Where a path starts: a root of the run, or a reference a loaded consumer makes. */
|
|
144
|
+
type Origin =
|
|
145
|
+
| { readonly k: "root"; readonly root: Root }
|
|
146
|
+
| { readonly k: "consumer"; readonly consumer: string };
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The shortest path from a root or a loaded consumer to one declaration, walked breadth first
|
|
150
|
+
* from every root at once in the order the run names them, a consumer's reference being the
|
|
151
|
+
* first hop of a path of its own, and following the references the sweep follows: a
|
|
152
|
+
* reference that reads a module reaches what the module exports, and an evaluation or a root
|
|
153
|
+
* reaches the module alone. Under `production` no reference a test file made is followed,
|
|
154
|
+
* which is the set the report's sweep counts.
|
|
155
|
+
*/
|
|
156
|
+
function shortestPath(
|
|
157
|
+
analysis: RunAnalysis,
|
|
158
|
+
to: string,
|
|
159
|
+
production: boolean,
|
|
160
|
+
): { readonly origin: Origin; readonly path: readonly Hop[] } | undefined {
|
|
161
|
+
const graph = analysis.swept.matrix.union;
|
|
162
|
+
const out = new Map<string, Reference[]>();
|
|
163
|
+
const consumed: (Reference & { readonly consumer: string })[] = [];
|
|
164
|
+
for (const reference of referencesOf(analysis.configured)) {
|
|
165
|
+
if (production && reference.test) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const { consumer } = reference;
|
|
169
|
+
if (consumer !== undefined) {
|
|
170
|
+
consumed.push({ ...reference, consumer });
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const held = out.get(reference.from);
|
|
174
|
+
if (held === undefined) {
|
|
175
|
+
out.set(reference.from, [reference]);
|
|
176
|
+
} else {
|
|
177
|
+
held.push(reference);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
interface Step {
|
|
181
|
+
readonly at: string;
|
|
182
|
+
readonly hop: Hop | undefined;
|
|
183
|
+
readonly previous: number;
|
|
184
|
+
readonly origin: Origin;
|
|
185
|
+
}
|
|
186
|
+
const steps: Step[] = [];
|
|
187
|
+
const seen = new Set<string>();
|
|
188
|
+
const expanded = new Set<string>();
|
|
189
|
+
const enter = (step: Step): void => {
|
|
190
|
+
if (!seen.has(step.at)) {
|
|
191
|
+
seen.add(step.at);
|
|
192
|
+
steps.push(step);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
const follow = (reference: Reference, previous: number, origin: Origin): void => {
|
|
196
|
+
enter({ at: reference.to, hop: { k: "reference", reference }, previous, origin });
|
|
197
|
+
const target = graph.at(reference.to);
|
|
198
|
+
if (reference.use === "evaluation" || target === OUTSIDE || expanded.has(reference.to)) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
expanded.add(reference.to);
|
|
202
|
+
for (const exported of graph.exportsOf[target] ?? []) {
|
|
203
|
+
const id = graph.symbols[exported]?.id;
|
|
204
|
+
if (id !== undefined) {
|
|
205
|
+
enter({ at: id, hop: { k: "export", reference, to: id }, previous, origin });
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
for (const root of rootsOf(analysis.configured)) {
|
|
210
|
+
enter({ at: root.id, hop: undefined, previous: -1, origin: { k: "root", root } });
|
|
211
|
+
}
|
|
212
|
+
// A consumer's reference is one hop long, so it joins the walk where the roots' first
|
|
213
|
+
// hops do.
|
|
214
|
+
const firstHops = steps.length;
|
|
215
|
+
for (let head = 0; ; head += 1) {
|
|
216
|
+
if (head === firstHops) {
|
|
217
|
+
for (const reference of consumed) {
|
|
218
|
+
follow(reference, -1, { k: "consumer", consumer: reference.consumer });
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const step = steps[head];
|
|
222
|
+
if (step === undefined) {
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
if (step.at === to) {
|
|
226
|
+
const path: Hop[] = [];
|
|
227
|
+
for (let at: Step | undefined = step; at?.hop !== undefined; at = steps[at.previous]) {
|
|
228
|
+
path.unshift(at.hop);
|
|
229
|
+
}
|
|
230
|
+
return { origin: step.origin, path };
|
|
231
|
+
}
|
|
232
|
+
for (const reference of out.get(step.at) ?? []) {
|
|
233
|
+
follow(reference, head, step.origin);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** What made a reference: a declaration of the target, or a loaded consumer. */
|
|
240
|
+
function madeBy(reference: Reference): string {
|
|
241
|
+
return reference.consumer === undefined ? reference.from : `consumer ${reference.consumer}`;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function referenceLine(reference: Reference, tail: string): string {
|
|
245
|
+
return ` reference: ${positionKey(reference.position)}\t${reference.use}\t${tail}${reference.test ? "\tfrom a test file" : ""}\n`;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Why a declaration no finding names and no exemption held back is live, or how it fell. */
|
|
249
|
+
function liveness(analysis: RunAnalysis, subject: InventorySymbol, cascade: Cascade): string {
|
|
250
|
+
const { sweep } = analysis.swept;
|
|
251
|
+
const candidate = sweep.candidates.find((one) => one.id === subject.id);
|
|
252
|
+
if (candidate === undefined && !judged(subject.kind)) {
|
|
253
|
+
return `answer: unjudged\n unjudged: neither liveness relation judges a ${subject.kind}\n`;
|
|
254
|
+
}
|
|
255
|
+
let text = `answer: ${candidate === undefined ? "live" : "dead"}\n`;
|
|
256
|
+
text += ` live under: ${listed(sweep.liveUnder.get(subject.id) ?? [])}\n`;
|
|
257
|
+
for (const root of rootsOf(analysis.configured).filter((one) => one.id === subject.id)) {
|
|
258
|
+
text += ` root: ${root.kind}${root.source === "" ? "" : `\tnamed by ${root.source}`}\n`;
|
|
259
|
+
}
|
|
260
|
+
const found =
|
|
261
|
+
shortestPath(analysis, subject.id, true) ?? shortestPath(analysis, subject.id, false);
|
|
262
|
+
if (found !== undefined && found.path.length > 0) {
|
|
263
|
+
const { origin } = found;
|
|
264
|
+
text += ` reached from: ${origin.k === "root" ? `root ${origin.root.kind} ${origin.root.id}` : `consumer ${origin.consumer}`}\n`;
|
|
265
|
+
for (const hop of found.path) {
|
|
266
|
+
text += referenceLine(hop.reference, `${madeBy(hop.reference)} -> ${hop.reference.to}`);
|
|
267
|
+
if (hop.k === "export") {
|
|
268
|
+
text += ` export: ${hop.reference.to} -> ${hop.to}\n`;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
} else if (found === undefined) {
|
|
272
|
+
const references = referencesOf(analysis.configured).filter((one) => one.to === subject.id);
|
|
273
|
+
text += " unreachable: no path of references reaches it from a root or a loaded consumer\n";
|
|
274
|
+
text += ` references: ${String(references.length)}\n`;
|
|
275
|
+
for (const reference of references) {
|
|
276
|
+
text += referenceLine(reference, `from ${madeBy(reference)}`);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (candidate !== undefined) {
|
|
280
|
+
const component = sweep.components.find((one) => one.members.includes(subject.id));
|
|
281
|
+
if (component !== undefined) {
|
|
282
|
+
const listed = listing(component, cascade);
|
|
283
|
+
text += ` component: ${component.id}, ${counted(listed.symbolCount, "symbol")} and ${counted(listed.deletableLines, "deletable line")} fall with it, listed at cascade ${cascade}\n`;
|
|
284
|
+
for (const root of listed.roots) {
|
|
285
|
+
text += ` component root: ${root}\n`;
|
|
286
|
+
}
|
|
287
|
+
for (const member of listed.members) {
|
|
288
|
+
text += ` component member: ${member}\n`;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
text += ` candidate: dead under ${candidate.relation}, with ${String(candidate.productionRefs)} production and ${String(candidate.testRefs)} test references\n`;
|
|
292
|
+
}
|
|
293
|
+
return text;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function reported(found: CompletedFinding): string {
|
|
297
|
+
return [
|
|
298
|
+
`answer: reported`,
|
|
299
|
+
` code: ${found.code} ${found.kind}`,
|
|
300
|
+
` message: ${found.message}`,
|
|
301
|
+
` class: ${found.reachabilityClass}`,
|
|
302
|
+
` confidence: ${found.confidence}`,
|
|
303
|
+
...(found.livenessRelation === undefined
|
|
304
|
+
? []
|
|
305
|
+
: [` liveness relation: ${found.livenessRelation}`]),
|
|
306
|
+
` configurations: ${listed(found.configurations)}`,
|
|
307
|
+
` loaded consumers: ${listed(found.consumersLoaded)}`,
|
|
308
|
+
...found.retainedBy.map((one) => ` retained by: ${one}`),
|
|
309
|
+
"",
|
|
310
|
+
].join("\n");
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Every finding of the run that names the declaration by its reference and reports at
|
|
315
|
+
* another position: a part of it, or a suppression record naming it. Each is published under
|
|
316
|
+
* the reference asked about, so an explanation leaving it out would disagree with the report.
|
|
317
|
+
*/
|
|
318
|
+
function namedElsewhere(findings: readonly CompletedFinding[], subject: InventorySymbol): string {
|
|
319
|
+
return findings
|
|
320
|
+
.filter(
|
|
321
|
+
(found) => found.symbol.ref === subject.ref && positionKey(found.position) !== subject.id,
|
|
322
|
+
)
|
|
323
|
+
.map(
|
|
324
|
+
(found) =>
|
|
325
|
+
`also reported: ${found.code} ${found.kind}\t${positionKey(found.position)}\t${found.symbol.kind} ${found.symbol.name}\n`,
|
|
326
|
+
)
|
|
327
|
+
.join("");
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** The one explanation that applies to a declaration, built whole before it is written. */
|
|
331
|
+
function explanation(analysis: RunAnalysis, subject: InventorySymbol, cascade: Cascade): string {
|
|
332
|
+
const graph = analysis.swept.matrix.union;
|
|
333
|
+
const at = graph.at(subject.id);
|
|
334
|
+
let text = `symbol: ${subject.ref}\n`;
|
|
335
|
+
text += `declaration: ${subject.kind} ${subject.name}\n`;
|
|
336
|
+
text += `position: ${subject.id}\n`;
|
|
337
|
+
text += `configurations: ${listed(analysis.swept.matrix.heldIn[at] ?? [])}\n`;
|
|
338
|
+
const found = analysis.result.findings.find((one) => positionKey(one.position) === subject.id);
|
|
339
|
+
const retained = analysis.swept.retained.filter((one) => one.id === subject.id);
|
|
340
|
+
if (found !== undefined) {
|
|
341
|
+
text += reported(found);
|
|
342
|
+
} else if (retained.length > 0) {
|
|
343
|
+
text += `answer: retained\n held back by: ${counted(retained.length, "exemption class")}\n`;
|
|
344
|
+
for (const one of retained) {
|
|
345
|
+
text += ` class: ${one.class}\t${positionKey(one.site)}\t${one.detail}\n`;
|
|
346
|
+
}
|
|
347
|
+
} else {
|
|
348
|
+
text += liveness(analysis, subject, cascade);
|
|
349
|
+
}
|
|
350
|
+
return text + namedElsewhere(analysis.result.findings, subject);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Explains one symbol from the analysis the report is built from, so an explanation cannot
|
|
355
|
+
* disagree with a finding: why a reported symbol is reported, which exemption classes held a
|
|
356
|
+
* retained one back, and for any other declaration the relations that hold it live and the
|
|
357
|
+
* shortest path of references from a root or a loaded consumer, or the references it has where
|
|
358
|
+
* no path reaches it. A request naming no one declaration exits with the usage code and names
|
|
359
|
+
* what partially matches it.
|
|
360
|
+
*/
|
|
361
|
+
export const explainVerb: Verb = ({ out, err, host, inputs, scope, openClient, option }) => {
|
|
362
|
+
const named = namedSymbol(option);
|
|
363
|
+
const { config, provenance } = resolve(inputs());
|
|
364
|
+
const analysis = runAnalysis(openClient(false), host, scope(), config, provenance, {
|
|
365
|
+
production: true,
|
|
366
|
+
});
|
|
367
|
+
const { subject, candidates } = subjectOf(analysis.swept.matrix.union.symbols, named);
|
|
368
|
+
if (subject === undefined) {
|
|
369
|
+
err.write(`deadset-ts: ${JSON.stringify(named)} names no one symbol of the target\n`);
|
|
370
|
+
for (const candidate of candidates) {
|
|
371
|
+
err.write(` ${candidate}\n`);
|
|
372
|
+
}
|
|
373
|
+
if (candidates.length === 0) {
|
|
374
|
+
err.write(" no symbol of the target partially matches it either\n");
|
|
375
|
+
}
|
|
376
|
+
return EXIT_USAGE;
|
|
377
|
+
}
|
|
378
|
+
out.write(explanation(analysis, subject, config.reporters.cascade));
|
|
379
|
+
return EXIT_CLEAN;
|
|
380
|
+
};
|