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