@cplieger/deadset-ts 8.0.0-dev.17 → 8.0.0-dev.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/analysis.js +26 -71
- package/dist/src/augmentations.js +1 -1
- package/dist/src/calls.js +267 -48
- package/dist/src/conformance-record.js +1 -1
- package/dist/src/contract/contract.generated.js +2 -2
- package/dist/src/exempt.js +0 -26
- package/dist/src/exposure.js +400 -0
- package/dist/src/findings/completion.js +4 -17
- package/dist/src/findings/emitters.js +22 -33
- package/dist/src/findings/intra-function.js +3 -3
- package/dist/src/findings/reads-and-writes.js +71 -15
- package/dist/src/findings/unused-declarations.js +2 -22
- package/dist/src/findings/visibility-narrowing.js +14 -20
- package/dist/src/inventory.js +18 -0
- package/dist/src/{components.js → liveness/components.js} +1 -1
- package/dist/src/{graph.js → liveness/graph.js} +38 -21
- package/dist/src/liveness/matrix.js +99 -0
- package/dist/src/liveness/sweep.js +558 -0
- package/dist/src/query.js +46 -0
- package/dist/src/references/sink.js +87 -1
- package/dist/src/references.js +1 -1
- package/dist/src/roots/published-reach.js +4 -4
- package/dist/src/verbs/explain.js +73 -152
- package/dist/src/verbs/print-retained.js +1 -1
- package/jsr.json +1 -1
- package/package.json +1 -1
- package/src/analysis.ts +41 -107
- package/src/augmentations.ts +1 -1
- package/src/calls.ts +326 -52
- package/src/conformance-record.ts +1 -1
- package/src/contract/contract.generated.ts +2 -2
- package/src/exempt.ts +31 -46
- package/src/exposure.ts +492 -0
- package/src/finding.ts +1 -1
- package/src/findings/boundary.ts +7 -0
- package/src/findings/completion.ts +5 -19
- package/src/findings/emitters.ts +23 -41
- package/src/findings/interfaces.ts +3 -3
- package/src/findings/intra-function.ts +3 -3
- package/src/findings/reads-and-writes.ts +99 -26
- package/src/findings/unused-declarations.ts +4 -28
- package/src/findings/visibility-narrowing.ts +25 -26
- package/src/interface-satisfaction.ts +1 -1
- package/src/inventory.ts +19 -0
- package/src/{components.ts → liveness/components.ts} +3 -3
- package/src/{graph.ts → liveness/graph.ts} +74 -26
- package/src/liveness/matrix.ts +162 -0
- package/src/liveness/sweep.ts +855 -0
- package/src/query.ts +81 -0
- package/src/references/sink.ts +101 -0
- package/src/references.ts +8 -1
- package/src/roots/published-reach.ts +9 -4
- package/src/stores.ts +2 -1
- package/src/tests/file-class.ts +19 -2
- package/src/verbs/explain.ts +81 -199
- package/src/verbs/print-retained.ts +1 -1
- package/dist/src/matrix.js +0 -275
- package/dist/src/sweep.js +0 -234
- package/src/matrix.ts +0 -466
- package/src/sweep.ts +0 -384
package/dist/src/analysis.js
CHANGED
|
@@ -12,6 +12,7 @@ import { consumerReferences } from "./consumers.js";
|
|
|
12
12
|
import { configuredDeclarations, configuredRef, declarationsNamed, } from "./roots/configured-declarations.js";
|
|
13
13
|
import { decorator } from "./decorator.js";
|
|
14
14
|
import { configurationNeeds, dependenciesOf, projectNeeds } from "./dependencies.js";
|
|
15
|
+
import { carriersOf, NO_CARRIERS } from "./exposure.js";
|
|
15
16
|
import { deprecatedDeclarations } from "./deprecation.js";
|
|
16
17
|
import { moduleIdentity } from "./load/discover.js";
|
|
17
18
|
import { loadRun } from "./load/load.js";
|
|
@@ -23,7 +24,7 @@ import { bindingsOf, distinctBindings } from "./bindings.js";
|
|
|
23
24
|
import { generatedFile, generatedFilesOf } from "./generated-file.js";
|
|
24
25
|
import { inOperandNames } from "./findings/in-operands.js";
|
|
25
26
|
import { sourceFilesOf } from "./source-files.js";
|
|
26
|
-
import { evidenceRecords, heldRecords, disabledClasses, exemptionsOf,
|
|
27
|
+
import { evidenceRecords, heldRecords, disabledClasses, exemptionsOf, isUnansweredRecord, writtenRecords, } from "./exempt.js";
|
|
27
28
|
import { heldByInclusion, sourcePaths, sourceTree } from "./file-facts.js";
|
|
28
29
|
import { findingsPass, recordedFindings, rowKey } from "./findings-pass.js";
|
|
29
30
|
import { unmatchedRootFindings } from "./findings/self-check.js";
|
|
@@ -35,7 +36,7 @@ import { intraFunctionFacts, projectParts } from "./intra-function-parts.js";
|
|
|
35
36
|
import {} from "./inventory.js";
|
|
36
37
|
import { inventory } from "./inventory/enumerate.js";
|
|
37
38
|
import { readManifest } from "./roots/manifest.js";
|
|
38
|
-
import {
|
|
39
|
+
import { matrixOf, referenceKey } from "./liveness/matrix.js";
|
|
39
40
|
import { ancestors, dirnamePath, extensionOf, joinPath, relativePath, resolvePath, } from "./paths.js";
|
|
40
41
|
import { byPosition, isComponentFile } from "./position.js";
|
|
41
42
|
import { skippedUnits } from "./type-errors.js";
|
|
@@ -52,6 +53,7 @@ import { accessorsOf, storesOf } from "./stores.js";
|
|
|
52
53
|
import { isPartKind } from "./suppress.js";
|
|
53
54
|
import { bindRows, readBaseline, readIgnoreFile } from "./suppress-file.js";
|
|
54
55
|
import { inlineDirectives, inlineSuppressions } from "./suppress-inline.js";
|
|
56
|
+
import { sweep } from "./liveness/sweep.js";
|
|
55
57
|
import { readTemplates, templateField } from "./template-field.js";
|
|
56
58
|
import { bytewise } from "./text/compare.js";
|
|
57
59
|
import { holdsManifest, manifestAt } from "./fs/manifest.js";
|
|
@@ -454,6 +456,7 @@ function readRun(engine, host, scope, config, mode, detectors, extra) {
|
|
|
454
456
|
],
|
|
455
457
|
projects,
|
|
456
458
|
consumers: read.plan.discovery.consumers,
|
|
459
|
+
declaresConsumers: scope.consumers.length > 0,
|
|
457
460
|
notBuilt: read.classified.notBuilt,
|
|
458
461
|
unanswered: read.unanswered,
|
|
459
462
|
componentsUnread: read.plan.workspace?.componentsUnread ?? [],
|
|
@@ -467,29 +470,6 @@ function readRun(engine, host, scope, config, mode, detectors, extra) {
|
|
|
467
470
|
workflows: read.result.workflows,
|
|
468
471
|
};
|
|
469
472
|
}
|
|
470
|
-
/**
|
|
471
|
-
* The run swept under one set of marks with the records that hold. A record a component
|
|
472
|
-
* file's markup holds counts only while the file is live, and what it holds may be what
|
|
473
|
-
* keeps the file live, so each file's markup joins only once a sweep without it finds the
|
|
474
|
-
* file live, until a sweep finds no further file live.
|
|
475
|
-
*/
|
|
476
|
-
function holdingSweep(read, input) {
|
|
477
|
-
const paths = new Set(read.exempt.flatMap((record) => record.whileLive ?? []));
|
|
478
|
-
let live = new Set();
|
|
479
|
-
for (;;) {
|
|
480
|
-
const swept = {
|
|
481
|
-
...input,
|
|
482
|
-
exempt: holdingWhile(read.exempt, live),
|
|
483
|
-
unanswered: read.unansweredHeld,
|
|
484
|
-
};
|
|
485
|
-
const result = sweepMatrix(read.matrix, swept);
|
|
486
|
-
const next = new Set(liveFilesAt(read.matrix, result, paths).map((one) => one.position.path));
|
|
487
|
-
if (next.size === live.size) {
|
|
488
|
-
return { swept, result };
|
|
489
|
-
}
|
|
490
|
-
live = next;
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
473
|
/**
|
|
494
474
|
* The test references exemption evidence written in a test file makes: each is a read of
|
|
495
475
|
* the declaration it would retain, made by the test file at the evidence's site.
|
|
@@ -547,55 +527,22 @@ function writtenOnly(read, mode) {
|
|
|
547
527
|
}
|
|
548
528
|
return new Set([...written].filter((id) => !readSome.has(id)));
|
|
549
529
|
}
|
|
550
|
-
/**
|
|
551
|
-
* The sweep of a run read once, under one set of marks. Where a question went unanswered,
|
|
552
|
-
* the run is swept once more without what that question could have kept live and without
|
|
553
|
-
* the references that stood in for its answer, and every declaration that sweep judges
|
|
554
|
-
* dead and this one judges live, or dead by another rule, is held by the gap too, as is
|
|
555
|
-
* every declaration that makes such a reference.
|
|
556
|
-
*/
|
|
530
|
+
/** The sweep of a run read once, under one set of marks. */
|
|
557
531
|
function sweptOf(read, input) {
|
|
558
|
-
const
|
|
559
|
-
const exempt = swept.exempt ?? [];
|
|
560
|
-
const held = new Set(read.unansweredHeld);
|
|
561
|
-
if (held.size > 0) {
|
|
562
|
-
// What a declaration reads where the checker did not answer is unknown, so a rule
|
|
563
|
-
// over what it reads, the test of dead code among them, cannot judge it.
|
|
564
|
-
for (const { configured } of read.projects) {
|
|
565
|
-
for (const reference of configured.references) {
|
|
566
|
-
if (reference.guessed === true) {
|
|
567
|
-
held.add(reference.from);
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
const dead = new Map(result.candidates.map((candidate) => [candidate.id, candidate]));
|
|
572
|
-
const answered = matrixOf(read.projects.map(({ configured }) => ({
|
|
573
|
-
...configured,
|
|
574
|
-
references: configured.references.filter((one) => one.guessed !== true),
|
|
575
|
-
})));
|
|
576
|
-
const bare = {
|
|
577
|
-
...swept,
|
|
578
|
-
exempt: exempt.filter((record) => !isUnansweredRecord(record)),
|
|
579
|
-
unanswered: [],
|
|
580
|
-
};
|
|
581
|
-
for (const candidate of sweepMatrix(answered, bare).candidates) {
|
|
582
|
-
const kept = dead.get(candidate.id);
|
|
583
|
-
if (kept?.testOfDeadCode !== candidate.testOfDeadCode ||
|
|
584
|
-
kept.relation !== candidate.relation) {
|
|
585
|
-
held.add(candidate.id);
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
}
|
|
532
|
+
const componentFiles = [...new Set(read.projects.flatMap((one) => one.componentFiles))].sort(bytewise);
|
|
589
533
|
return {
|
|
590
534
|
matrix: read.matrix,
|
|
591
|
-
sweep:
|
|
592
|
-
|
|
593
|
-
|
|
535
|
+
sweep: sweep(read.matrix, {
|
|
536
|
+
...input,
|
|
537
|
+
exempt: read.exempt,
|
|
538
|
+
unanswered: read.unansweredHeld,
|
|
539
|
+
componentFiles,
|
|
540
|
+
unseenCallers: !read.declaresConsumers,
|
|
541
|
+
writeOnly: writtenOnly(read, input.mode),
|
|
542
|
+
}),
|
|
594
543
|
notBuilt: read.notBuilt,
|
|
595
544
|
unanswered: read.unanswered,
|
|
596
|
-
heldByUnanswered: [...held],
|
|
597
545
|
skipped: read.projects.flatMap((one) => one.skipped),
|
|
598
|
-
componentFiles: [...new Set(read.projects.flatMap((one) => one.componentFiles))].sort(bytewise),
|
|
599
546
|
};
|
|
600
547
|
}
|
|
601
548
|
/**
|
|
@@ -631,6 +578,8 @@ export function readEmitterRun(engine, host, scope, config, mode, detectors) {
|
|
|
631
578
|
declarationsNamed: declarationsNamed(project, read.held, configuredDeclarations(config)),
|
|
632
579
|
parts: projectParts(project, read.held, references, targetRoot),
|
|
633
580
|
unseenLoads,
|
|
581
|
+
// Only a closed world reports an unnecessary export, which is all the carriers decide.
|
|
582
|
+
carriers: config.consumers.complete ? carriersOf(project, read.held) : NO_CARRIERS,
|
|
634
583
|
}));
|
|
635
584
|
// Whether a declaration carries the marker decides its code, so one whose answer went
|
|
636
585
|
// unread is reported by nothing.
|
|
@@ -694,7 +643,7 @@ export function emitterInputOver(host, scope, config, read, input, selfCheck) {
|
|
|
694
643
|
references: one.configured.references,
|
|
695
644
|
support: new Set(one.configured.supportFiles ?? []),
|
|
696
645
|
accessors: one.extra.accessors,
|
|
697
|
-
})), swept.exempt, input.mode),
|
|
646
|
+
})), swept.sweep.exempt, input.mode),
|
|
698
647
|
dependencies: dependenciesOf(host, targetRoot, read.workflows, [...projects.map((one) => one.extra.needs), configurationNeeds(read.conventionUses)], swept.sweep.candidates.map((candidate) => candidate.id), members.read, members.unbuilt, read.nestedDirs),
|
|
699
648
|
implementations: mergeImplementations(projects.map((one) => one.extra.implementations)),
|
|
700
649
|
files: {
|
|
@@ -713,9 +662,15 @@ export function emitterInputOver(host, scope, config, read, input, selfCheck) {
|
|
|
713
662
|
edges: readEdgeSides(host, targetRoot),
|
|
714
663
|
filledOutside: new Set(projects.flatMap((one) => one.filledOutside)),
|
|
715
664
|
possiblyLoaded: new Set(projects.flatMap((one) => one.possiblyLoaded)),
|
|
665
|
+
carriers: {
|
|
666
|
+
named: new Map([
|
|
667
|
+
...Map.groupBy(projects.flatMap((one) => [...one.extra.carriers.named]), ([id]) => id),
|
|
668
|
+
].map(([id, entries]) => [id, [...new Set(entries.flatMap(([, named]) => named))]])),
|
|
669
|
+
unread: new Set(projects.flatMap((one) => [...one.extra.carriers.unread])),
|
|
670
|
+
},
|
|
716
671
|
},
|
|
717
672
|
selfCheck,
|
|
718
|
-
intraFunction: intraFunctionFacts(projects.map((one) => one.extra.parts), projects.flatMap((one) => one.configured.references), new Set([...swept.exempt.map((one) => one.id), ...swept.
|
|
673
|
+
intraFunction: intraFunctionFacts(projects.map((one) => one.extra.parts), projects.flatMap((one) => one.configured.references), new Set([...swept.sweep.exempt.map((one) => one.id), ...swept.sweep.held])),
|
|
719
674
|
};
|
|
720
675
|
}
|
|
721
676
|
/**
|
|
@@ -828,7 +783,7 @@ export function runAnalysis(engine, host, scope, config, provenance, mode, detec
|
|
|
828
783
|
* declarations of its own the run holds for them.
|
|
829
784
|
*/
|
|
830
785
|
export function unansweredCounts(analysis) {
|
|
831
|
-
const held = new Set(analysis.swept.
|
|
786
|
+
const held = new Set(analysis.swept.sweep.held);
|
|
832
787
|
return analysis.run.projects.flatMap((project) => {
|
|
833
788
|
const questions = analysis.run.unanswered.filter((one) => one.configFile === project.configFile).length +
|
|
834
789
|
analysis.run.unread.filter((one) => one.kind === "manifest" && one.configFile === project.configFile).length +
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { isInterfaceDeclaration, isModuleBlock, isModuleDeclaration, } from "@typescript/native/ast";
|
|
10
10
|
import { nodeKey } from "./inventory.js";
|
|
11
|
-
import { referenceKey } from "./matrix.js";
|
|
11
|
+
import { referenceKey } from "./liveness/matrix.js";
|
|
12
12
|
/** Whether one declaration is a `declare global` block: a module-level namespace named `global`. */
|
|
13
13
|
export function isGlobalBlock(symbol, held, files) {
|
|
14
14
|
return (symbol.kind === "namespace" &&
|
package/dist/src/calls.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The calls and decorators of one project that the configured classes read: what each
|
|
3
3
|
* callee resolves to, and which of the target's classes a call passes or a decorator
|
|
4
|
-
* is attached to. A class is passed
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* is attached to. A class is passed by each value an argument can be, at any depth inside
|
|
5
|
+
* the array and object literals it writes, named directly or through variables, and a
|
|
6
|
+
* decorator written as a call is a call.
|
|
7
7
|
*/
|
|
8
|
-
import { isArrayLiteralExpression, isCallExpression, isClassDeclaration, isDecorator, isIdentifier, isObjectLiteralExpression, isParenthesizedExpression, isPropertyAccessExpression, isPropertyAssignment, isShorthandPropertyAssignment, isSpreadElement, isVariableDeclaration, } from "@typescript/native/ast";
|
|
9
|
-
import { SymbolFlags } from "@typescript/native/sync";
|
|
8
|
+
import { isArrayLiteralExpression, isArrowFunction, isBinaryExpression, isBindingElement, isCallExpression, isClassDeclaration, isConditionalExpression, isDecorator, isFunctionDeclaration, isFunctionExpression, isIdentifier, isMethodDeclaration, isObjectLiteralExpression, isParameterDeclaration, isParenthesizedExpression, isPropertyAccessExpression, isPropertyAssignment, isPropertyDeclaration, isShorthandPropertyAssignment, isOmittedExpression, isSpreadAssignment, isSpreadElement, isVariableDeclaration, SyntaxKind, } from "@typescript/native/ast";
|
|
9
|
+
import { ObjectFlags, SymbolFlags, } from "@typescript/native/sync";
|
|
10
10
|
import { nodeKey } from "./inventory.js";
|
|
11
11
|
import { must } from "./query.js";
|
|
12
12
|
import { unwrapped } from "./unwrap.js";
|
|
@@ -66,77 +66,296 @@ export function resolvedTargets(project, nodes) {
|
|
|
66
66
|
});
|
|
67
67
|
return targets;
|
|
68
68
|
}
|
|
69
|
-
/** The
|
|
70
|
-
function
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
if (isArrayLiteralExpression(expression)) {
|
|
76
|
-
for (const element of expression.elements) {
|
|
77
|
-
passedIn(isSpreadElement(element) ? element.expression : element, found);
|
|
78
|
-
}
|
|
79
|
-
return;
|
|
69
|
+
/** The declaration a shorthand property's value resolves to, an import followed to what it names. */
|
|
70
|
+
function shorthandTarget(project, node) {
|
|
71
|
+
const value = must(project.shorthandValueAt(project.handle(node)));
|
|
72
|
+
if (value === undefined || (value.flags & SymbolFlags.Alias) === 0) {
|
|
73
|
+
return value;
|
|
80
74
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
75
|
+
const aliased = must(project.queries.aliased(value));
|
|
76
|
+
return aliased.declarations.length === 0 ? undefined : aliased;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The values one expression passes: each value it can be ({@link leavesOf}), or what an
|
|
80
|
+
* array or object literal it can be holds, at any depth.
|
|
81
|
+
*/
|
|
82
|
+
function passedIn(written, found) {
|
|
83
|
+
for (const expression of leavesOf(written)) {
|
|
84
|
+
if (isArrayLiteralExpression(expression)) {
|
|
85
|
+
for (const element of expression.elements) {
|
|
86
|
+
passedIn(element, found);
|
|
85
87
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
+
}
|
|
89
|
+
else if (isObjectLiteralExpression(expression)) {
|
|
90
|
+
for (const property of expression.properties) {
|
|
91
|
+
if (isPropertyAssignment(property)) {
|
|
92
|
+
passedIn(property.initializer, found);
|
|
93
|
+
}
|
|
94
|
+
else if (isSpreadAssignment(property)) {
|
|
95
|
+
passedIn(property.expression, found);
|
|
96
|
+
}
|
|
97
|
+
else if (isShorthandPropertyAssignment(property)) {
|
|
98
|
+
found.push({ site: property.name, name: property, shorthand: true });
|
|
99
|
+
}
|
|
88
100
|
}
|
|
89
101
|
}
|
|
90
|
-
|
|
102
|
+
else if (!isOmittedExpression(expression)) {
|
|
103
|
+
found.push({ site: expression, name: calleeName(expression), shorthand: false });
|
|
104
|
+
}
|
|
91
105
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
106
|
+
}
|
|
107
|
+
/** The operators whose value is one of their operands. */
|
|
108
|
+
const CHOOSING = new Set([
|
|
109
|
+
SyntaxKind.QuestionQuestionToken,
|
|
110
|
+
SyntaxKind.BarBarToken,
|
|
111
|
+
SyntaxKind.AmpersandAmpersandToken,
|
|
112
|
+
]);
|
|
113
|
+
/**
|
|
114
|
+
* The expressions one value can be: itself, or each branch of a conditional, each operand
|
|
115
|
+
* of `??`, `||` and `&&` and the last of a comma, inside any wrapper that changes no value.
|
|
116
|
+
* Each is read alone because the checker reduces a union by subtype, so the type at the
|
|
117
|
+
* whole drops a class structurally equal to, or a subtype of, another branch's.
|
|
118
|
+
*/
|
|
119
|
+
function leavesOf(written) {
|
|
120
|
+
const expression = unwrapped(isSpreadElement(written) ? written.expression : written);
|
|
121
|
+
if (isConditionalExpression(expression)) {
|
|
122
|
+
return [...leavesOf(expression.whenTrue), ...leavesOf(expression.whenFalse)];
|
|
95
123
|
}
|
|
124
|
+
if (isBinaryExpression(expression)) {
|
|
125
|
+
const operator = expression.operatorToken.kind;
|
|
126
|
+
if (CHOOSING.has(operator)) {
|
|
127
|
+
return [...leavesOf(expression.left), ...leavesOf(expression.right)];
|
|
128
|
+
}
|
|
129
|
+
if (operator === SyntaxKind.CommaToken) {
|
|
130
|
+
return leavesOf(expression.right);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return [expression];
|
|
96
134
|
}
|
|
97
135
|
/** The values one argument passes, a spread argument's included. */
|
|
98
|
-
|
|
136
|
+
function leavesPassed(argument) {
|
|
99
137
|
const found = [];
|
|
100
|
-
passedIn(
|
|
138
|
+
passedIn(argument, found);
|
|
101
139
|
return found;
|
|
102
140
|
}
|
|
141
|
+
/** The values one argument passes that name their declaration. */
|
|
142
|
+
export function valuesPassed(argument) {
|
|
143
|
+
return leavesPassed(argument).filter((value) => value.name !== undefined);
|
|
144
|
+
}
|
|
103
145
|
/**
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
|
|
146
|
+
* What one passed value holds: the declaration its name resolves to, and the classes and
|
|
147
|
+
* functions the checker's type at it holds ({@link constructedAt}). `known` holds names
|
|
148
|
+
* already resolved in a batch.
|
|
149
|
+
*/
|
|
150
|
+
function heldBy(project, value, known) {
|
|
151
|
+
const { name } = value;
|
|
152
|
+
if (name === undefined) {
|
|
153
|
+
return { named: undefined, typed: constructedAt(project, value.site) };
|
|
154
|
+
}
|
|
155
|
+
if (value.shorthand) {
|
|
156
|
+
return { named: shorthandTarget(project, name), typed: [] };
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
named: known?.get(name) ?? resolvedTargets(project, [name]).get(name),
|
|
160
|
+
typed: constructedAt(project, value.site),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The classes of the target each call passes, by the call: each class a value an argument
|
|
165
|
+
* can be holds as a constructor, as the checker types it. A name whose type erases the
|
|
166
|
+
* class is followed through what its declaration binds ({@link boundTo}) and, for a
|
|
167
|
+
* parameter, through what each call of its function passes at its place, so a class handed
|
|
168
|
+
* to a wrapper is passed. A class is used at each value that passes it. A class of another
|
|
169
|
+
* program passes nothing.
|
|
108
170
|
*/
|
|
109
171
|
export function classesPassed(project, held, chains, calls) {
|
|
110
|
-
const passed = new Map(calls.map((call) => [call, call.arguments.flatMap(
|
|
111
|
-
const
|
|
112
|
-
const targets = resolvedTargets(project, all.filter((value) => !value.shorthand).map((value) => value.name));
|
|
172
|
+
const passed = new Map(calls.map((call) => [call, call.arguments.flatMap(leavesPassed)]));
|
|
173
|
+
const targets = resolvedTargets(project, [...passed.values()].flat().flatMap((value) => (value.shorthand ? [] : (value.name ?? []))));
|
|
113
174
|
const classes = new Set(held.symbols.filter((symbol) => symbol.kind === "class").map((symbol) => symbol.id));
|
|
175
|
+
let calling;
|
|
176
|
+
const callsOfFunction = (symbol) => {
|
|
177
|
+
calling ??= callsByDeclaration(project);
|
|
178
|
+
return symbol.declarations.flatMap((handle) => calling?.get(keyOf(handle)) ?? []);
|
|
179
|
+
};
|
|
180
|
+
const classesOf = (value, seen) => {
|
|
181
|
+
const { named, typed } = heldBy(project, value, targets);
|
|
182
|
+
return [
|
|
183
|
+
...typed.flatMap((symbol) => chains.declarationsOf(symbol).filter((id) => classes.has(id))),
|
|
184
|
+
...(named === undefined ? [] : reached(named, seen)),
|
|
185
|
+
];
|
|
186
|
+
};
|
|
114
187
|
const reached = (symbol, seen) => {
|
|
115
188
|
const ids = chains.declarationsOf(symbol).filter((id) => classes.has(id));
|
|
116
189
|
if (ids.length > 0 || seen.has(symbol.id)) {
|
|
117
190
|
return ids;
|
|
118
191
|
}
|
|
119
192
|
seen.add(symbol.id);
|
|
193
|
+
const through = (written) => leavesPassed(written).flatMap((value) => classesOf(value, seen));
|
|
120
194
|
return symbol.declarations.flatMap((handle) => {
|
|
121
195
|
const node = project.declarationAt(handle)?.node;
|
|
122
|
-
if (node === undefined
|
|
196
|
+
if (node === undefined) {
|
|
123
197
|
return [];
|
|
124
198
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
199
|
+
if (isParameterDeclaration(node)) {
|
|
200
|
+
return argumentsAt(project, node, callsOfFunction).flatMap(through);
|
|
201
|
+
}
|
|
202
|
+
const bound = boundTo(project, node);
|
|
203
|
+
return [
|
|
204
|
+
...(bound.written === undefined ? [] : through(bound.written)),
|
|
205
|
+
...bound.symbols.flatMap((next) => reached(next, seen)),
|
|
206
|
+
];
|
|
207
|
+
});
|
|
208
|
+
};
|
|
209
|
+
return new Map([...passed].map(([call, values]) => {
|
|
210
|
+
const uses = new Map();
|
|
211
|
+
for (const value of values) {
|
|
212
|
+
for (const id of classesOf(value, new Set())) {
|
|
213
|
+
uses.set(`${id}\u0000${String(value.site.pos)}`, { id, site: value.site });
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return [call, [...uses.values()]];
|
|
217
|
+
}));
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* The symbols of the classes and functions the checker's type at one expression holds as
|
|
221
|
+
* values, each member of a union: the static side of a class or a function is an
|
|
222
|
+
* anonymous type its declaration's symbol names, which an instance's type is not.
|
|
223
|
+
*/
|
|
224
|
+
function constructedAt(project, written) {
|
|
225
|
+
return heldIn(project, must(project.queries.typeAt(unwrapped(written))));
|
|
226
|
+
}
|
|
227
|
+
/** The classes and functions one type holds as values, as {@link constructedAt} reads them. */
|
|
228
|
+
function heldIn(project, type) {
|
|
229
|
+
const { queries } = project;
|
|
230
|
+
return (type === undefined ? [] : must(queries.constituents(type))).flatMap((part) => {
|
|
231
|
+
if (!part.isObjectType() || (part.objectFlags & ObjectFlags.Anonymous) === 0) {
|
|
232
|
+
return [];
|
|
233
|
+
}
|
|
234
|
+
return must(queries.symbolOfType(part)) ?? [];
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* What a variable, a property or a destructured binding binds its name to: a variable's
|
|
239
|
+
* or a property's initializer, the value a shorthand property names, or the property of
|
|
240
|
+
* the destructured value a binding element reads.
|
|
241
|
+
*/
|
|
242
|
+
function boundTo(project, node) {
|
|
243
|
+
if (isShorthandPropertyAssignment(node)) {
|
|
244
|
+
const value = shorthandTarget(project, node);
|
|
245
|
+
return { symbols: value === undefined ? [] : [value] };
|
|
246
|
+
}
|
|
247
|
+
if (isBindingElement(node)) {
|
|
248
|
+
const key = node.propertyName ?? node.name;
|
|
249
|
+
if (key === undefined || !isIdentifier(key)) {
|
|
250
|
+
return { symbols: [] };
|
|
251
|
+
}
|
|
252
|
+
const source = must(project.queries.typeAt(node.parent));
|
|
253
|
+
const property = source === undefined
|
|
254
|
+
? undefined
|
|
255
|
+
: must(project.queries.propertiesOf(source)).find((one) => one.name === key.text);
|
|
256
|
+
return { symbols: property === undefined ? [] : [property] };
|
|
257
|
+
}
|
|
258
|
+
const written = initializerOf(node);
|
|
259
|
+
return written === undefined ? { symbols: [] } : { written, symbols: [] };
|
|
260
|
+
}
|
|
261
|
+
/** The value a variable or a property declaration binds its name to, where it writes one. */
|
|
262
|
+
function initializerOf(node) {
|
|
263
|
+
return isVariableDeclaration(node) || isPropertyAssignment(node) || isPropertyDeclaration(node)
|
|
264
|
+
? node.initializer
|
|
265
|
+
: undefined;
|
|
266
|
+
}
|
|
267
|
+
/** The key of one declaration a symbol names, the same for each symbol the checker makes of it. */
|
|
268
|
+
function keyOf(handle) {
|
|
269
|
+
return `${handle.path}#${String(handle.index)}`;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Every call of the project by the declarations its callee can be: the function or the
|
|
273
|
+
* method it names, or else each function the checker's type at it holds, wherever that
|
|
274
|
+
* value came from. A name is also followed through what its binding copies
|
|
275
|
+
* ({@link boundTo}), which is how a call reaches a function an annotation erases.
|
|
276
|
+
* Only the target's own declarations are keys, as only its own functions are asked for.
|
|
277
|
+
*/
|
|
278
|
+
function callsByDeclaration(project) {
|
|
279
|
+
const calls = callsOf(project);
|
|
280
|
+
const own = project.ownPaths();
|
|
281
|
+
const callees = resolvedTargets(project, calls.flatMap((call) => calleeName(call.expression) ?? []));
|
|
282
|
+
const named = new Map();
|
|
283
|
+
const keysOf = (symbol, seen) => {
|
|
284
|
+
const known = named.get(symbol.id);
|
|
285
|
+
if (known !== undefined || seen.has(symbol.id)) {
|
|
286
|
+
return known ?? [];
|
|
287
|
+
}
|
|
288
|
+
seen.add(symbol.id);
|
|
289
|
+
const keys = symbol.declarations
|
|
290
|
+
.filter((handle) => own.has(handle.path))
|
|
291
|
+
.flatMap((handle) => {
|
|
292
|
+
const node = project.declarationAt(handle)?.node;
|
|
293
|
+
const bound = node === undefined ? { symbols: [] } : boundTo(project, node);
|
|
294
|
+
const copied = [
|
|
295
|
+
...bound.symbols,
|
|
296
|
+
...(bound.written === undefined ? [] : leavesPassed(bound.written)).flatMap((value) => {
|
|
297
|
+
const { named: next, typed } = heldBy(project, value);
|
|
298
|
+
return [...typed, ...(next === undefined ? [] : [next])];
|
|
299
|
+
}),
|
|
300
|
+
];
|
|
301
|
+
return [keyOf(handle), ...copied.flatMap((next) => keysOf(next, seen))];
|
|
128
302
|
});
|
|
303
|
+
named.set(symbol.id, keys);
|
|
304
|
+
return keys;
|
|
129
305
|
};
|
|
130
|
-
const
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
: targets.get(value.name);
|
|
134
|
-
return symbol === undefined ? [] : reached(symbol, new Set());
|
|
306
|
+
const calleeOf = (call) => {
|
|
307
|
+
const name = calleeName(call.expression);
|
|
308
|
+
return name === undefined ? undefined : callees.get(name);
|
|
135
309
|
};
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
310
|
+
const declared = (call) => ((calleeOf(call)?.flags ?? 0) & (SymbolFlags.Function | SymbolFlags.Method)) !== 0;
|
|
311
|
+
const typed = calls.filter((call) => !declared(call));
|
|
312
|
+
const types = new Map(project.queries
|
|
313
|
+
.typesAt(typed.map((call) => unwrapped(call.expression)))
|
|
314
|
+
.map((type, index) => [typed[index], must(type)]));
|
|
315
|
+
const found = new Map();
|
|
316
|
+
for (const call of calls) {
|
|
317
|
+
const callee = calleeOf(call);
|
|
318
|
+
const held = [
|
|
319
|
+
...(callee === undefined ? [] : [callee]),
|
|
320
|
+
...(types.has(call) ? heldIn(project, types.get(call)) : []),
|
|
321
|
+
];
|
|
322
|
+
for (const key of new Set(held.flatMap((symbol) => keysOf(symbol, new Set())))) {
|
|
323
|
+
found.set(key, [...(found.get(key) ?? []), call]);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return found;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* What each call of the function a parameter belongs to passes at the parameter's place,
|
|
330
|
+
* every argument from there on for a rest parameter. The function is the one the checker's
|
|
331
|
+
* type at its own declaration names, so a function with no name of its own is called too.
|
|
332
|
+
* A `this` parameter takes no place, and an argument at or after a spread argument passes
|
|
333
|
+
* at every place the spread's length can move it to.
|
|
334
|
+
*/
|
|
335
|
+
function argumentsAt(project, parameter, callsOfFunction) {
|
|
336
|
+
const owner = parameter.parent;
|
|
337
|
+
if (!isFunctionDeclaration(owner) &&
|
|
338
|
+
!isMethodDeclaration(owner) &&
|
|
339
|
+
!isArrowFunction(owner) &&
|
|
340
|
+
!isFunctionExpression(owner)) {
|
|
341
|
+
return [];
|
|
342
|
+
}
|
|
343
|
+
const calls = new Set(constructedAt(project, owner).flatMap(callsOfFunction));
|
|
344
|
+
const [first] = owner.parameters;
|
|
345
|
+
const erased = first !== undefined && isIdentifier(first.name) && first.name.text === "this";
|
|
346
|
+
const place = owner.parameters.indexOf(parameter) - (erased ? 1 : 0);
|
|
347
|
+
const rest = parameter.dotDotDotToken !== undefined;
|
|
348
|
+
return [...calls].flatMap((call) => {
|
|
349
|
+
let spreads = 0;
|
|
350
|
+
return call.arguments.flatMap((argument, index) => {
|
|
351
|
+
const spread = isSpreadElement(argument);
|
|
352
|
+
const earliest = index - spreads;
|
|
353
|
+
spreads += spread ? 1 : 0;
|
|
354
|
+
const open = spread || earliest < index;
|
|
355
|
+
const lands = open ? rest || earliest <= place : rest ? index >= place : index === place;
|
|
356
|
+
return lands ? [spread ? argument.expression : argument] : [];
|
|
357
|
+
});
|
|
358
|
+
});
|
|
140
359
|
}
|
|
141
360
|
/** Every class declaration of the target that carries a decorator. */
|
|
142
361
|
export function decoratedClasses(project, held) {
|
|
@@ -4,7 +4,7 @@ export const RECORD = {
|
|
|
4
4
|
conformance: {
|
|
5
5
|
corpusVersion: "6.0.0",
|
|
6
6
|
result: "pass",
|
|
7
|
-
digest: "sha256:
|
|
7
|
+
digest: "sha256:140f095d52fa8140de070f46d13a132b723c634ecf24a8b96e6a92c410c4b848",
|
|
8
8
|
},
|
|
9
9
|
gaps: [],
|
|
10
10
|
};
|
|
@@ -372,7 +372,7 @@ export const KIND_ROWS = [
|
|
|
372
372
|
subject: "declaration",
|
|
373
373
|
holdsLive: true,
|
|
374
374
|
rule: "An exported symbol whose every reference is inside the symbol's own package or module, reported as a candidate for unexporting. The subject is a package-level declaration or a method. Three subjects are excluded. An interface method is excluded because its exportedness is the contract of the interface that declares it. A struct field is excluded because an encoder reads it by name. A method that satisfies an interface some symbol uses as a type is excluded because it cannot be unexported without its type ceasing to satisfy that interface, and so is a method whose name and signature are those of a method of an exported interface type declared by a package outside the target that the import closure holds, because that interface fixes them whether or not the program converts the method's type to it. The finding names the narrower visibility the references support. A declared cross-language edge counts as an out-of-package reference. Where the edge's other side is unknown to the analyzer, the finding is emitted pending. A reference from a Go external test package, a test file whose package clause names its directory's package with the suffix _test, is a reference from outside the package, because that test builds as another package and needs the export.",
|
|
375
|
-
precondition: "Closed world only. A Go main package, and an internal/ directory tree for importers outside its parent, are closed worlds whatever the configuration declares, because no other program can import them, so this finding is reported there always. A published package is a closed world only when the configuration declares the consumer set complete and every declared consumer loads. A library with no consumer loaded and no complete consumer set declared gets this finding on its internal/ tree and its main packages and never on its published API. A symbol the rule of DS1301 holds for is not reported under this code, whatever severity the configuration gives DS1301, because the write-only finding names the defect to fix first. A type
|
|
375
|
+
precondition: "Closed world only. A Go main package, and an internal/ directory tree for importers outside its parent, are closed worlds whatever the configuration declares, because no other program can import them, so this finding is reported there always. A published package is a closed world only when the configuration declares the consumer set complete and every declared consumer loads. A library with no consumer loaded and no complete consumer set declared gets this finding on its internal/ tree and its main packages and never on its published API. A symbol the rule of DS1301 holds for is not reported under this code, whatever severity the configuration gives DS1301, because the write-only finding names the defect to fix first. A type is not reported when a reference from outside the package reaches its values through its own package's exported declarations: a reference to an exported function or method whose parameters or results name the type, to an exported variable or constant whose type names it, to an exported field or property whose type names it, an embedded field included, or to an exported member of the type itself, a field promoted through an embedding included. A type a reference from outside the package names, or whose values such a reference reaches, passes the reach on to each type its definition names other than through an unexported member, whether it is exported or not, so an unexported type an exported function returns passes it to the types its exported fields name. A type names another through every composite that spells it: a pointer, an array, a slice, a map key or value, a channel, a function signature and a type argument. The referencing code holds values of the type, so unexporting it would leave an exported declaration naming a type its users cannot name. A package-level declaration whose name a string literal spells carries the class grammar/analysis/confidence.md gives it.",
|
|
376
376
|
details: { narrower_visibility: "required", name_literal: "optional" },
|
|
377
377
|
defaultEnabled: true,
|
|
378
378
|
defaultSeverity: "warn",
|
|
@@ -387,7 +387,7 @@ export const KIND_ROWS = [
|
|
|
387
387
|
subject: "declaration",
|
|
388
388
|
holdsLive: true,
|
|
389
389
|
rule: "An exported symbol of a non-internal package whose every reference is inside the target module, reported as a candidate for relocation behind an internal boundary. The subject is a package-level declaration or a method. Three subjects are excluded. An interface method is excluded because its exportedness is the contract of the interface that declares it. A struct field is excluded because an encoder reads it by name. A method that satisfies an interface some symbol uses as a type is excluded because it cannot be unexported without its type ceasing to satisfy that interface, and so is a method whose name and signature are those of a method of an exported interface type declared by a package outside the target that the import closure holds, because that interface fixes them whether or not the program converts the method's type to it. The finding names the narrower visibility the references support. A declared cross-language edge counts as an out-of-package reference. Where the edge's other side is unknown to the analyzer, the finding is emitted pending. A reference from a Go external test package, a test file whose package clause names its directory's package with the suffix _test, is a reference from outside the package, because that test builds as another package and needs the export.",
|
|
390
|
-
precondition: "Closed world only. Reported always for a main package and an internal/ directory tree, and for a published package only when the configuration declares the consumer set complete and every declared consumer loads. A library with no consumer loaded and no complete consumer set declared gets this finding on its internal/ tree and its main packages and never on its published API. A symbol the rule of DS1301 holds for is not reported under this code, whatever severity the configuration gives DS1301, because the write-only finding names the defect to fix first. A type
|
|
390
|
+
precondition: "Closed world only. Reported always for a main package and an internal/ directory tree, and for a published package only when the configuration declares the consumer set complete and every declared consumer loads. A library with no consumer loaded and no complete consumer set declared gets this finding on its internal/ tree and its main packages and never on its published API. A symbol the rule of DS1301 holds for is not reported under this code, whatever severity the configuration gives DS1301, because the write-only finding names the defect to fix first. A type is not reported when a reference from outside the target module reaches its values through its own package's exported declarations, as DS1501 defines that reach, because the referencing code holds values of the type, and moving it behind an internal boundary would leave an exported declaration naming a type its users cannot name.",
|
|
391
391
|
details: { narrower_visibility: "required", name_literal: "optional" },
|
|
392
392
|
defaultEnabled: true,
|
|
393
393
|
defaultSeverity: "warn",
|
package/dist/src/exempt.js
CHANGED
|
@@ -10,7 +10,6 @@ import { EXEMPTION_CLASSES } from "./contract/contract.generated.js";
|
|
|
10
10
|
import { TS_EXEMPTION_CLASSES, typescriptVisibilityOf, } from "./exempt-classes.js";
|
|
11
11
|
import { byPosition, positionKey } from "./position.js";
|
|
12
12
|
import { Unanswerable } from "./query.js";
|
|
13
|
-
import { sweep } from "./sweep.js";
|
|
14
13
|
import { bytewise } from "./text/compare.js";
|
|
15
14
|
/**
|
|
16
15
|
* The classes `exemptions.disabled` switches off. A class that runs on another language
|
|
@@ -130,13 +129,6 @@ export function exemptionsOf(found) {
|
|
|
130
129
|
return true;
|
|
131
130
|
});
|
|
132
131
|
}
|
|
133
|
-
/**
|
|
134
|
-
* The records that hold while the component files at `live` are live: every record that
|
|
135
|
-
* holds whatever is live, and each one a live file's markup holds, as a record of the run.
|
|
136
|
-
*/
|
|
137
|
-
export function holdingWhile(records, live) {
|
|
138
|
-
return exemptionsOf(records.flatMap(({ whileLive, ...record }) => whileLive === undefined || live.has(whileLive) ? [record] : []));
|
|
139
|
-
}
|
|
140
132
|
/** The records per declaration, each declaration's in the order given. */
|
|
141
133
|
function byDeclaration(records) {
|
|
142
134
|
const grouped = new Map();
|
|
@@ -151,24 +143,6 @@ function byDeclaration(records) {
|
|
|
151
143
|
}
|
|
152
144
|
return grouped;
|
|
153
145
|
}
|
|
154
|
-
/**
|
|
155
|
-
* The records that held back a declaration: one some configuration's graph, swept
|
|
156
|
-
* again with no exemption and its marks, judges a candidate, whatever another
|
|
157
|
-
* configuration answers, or one in `writeOnly`, which the run would report as written
|
|
158
|
-
* and never read without the record's read. A declaration a relation or a mark holds
|
|
159
|
-
* live in every configuration is held back by no record. The order is the
|
|
160
|
-
* declarations' site order, and for one declaration the input's.
|
|
161
|
-
*/
|
|
162
|
-
export function retainedIn(matrix, input, writeOnly = new Set()) {
|
|
163
|
-
const exempt = input.exempt ?? [];
|
|
164
|
-
if (exempt.length === 0) {
|
|
165
|
-
return [];
|
|
166
|
-
}
|
|
167
|
-
const bare = { marked: input.marked, mode: input.mode };
|
|
168
|
-
const dead = new Set(matrix.graphs.flatMap((graph) => sweep(graph, bare).candidates.map((candidate) => candidate.id)));
|
|
169
|
-
const held = byDeclaration(exempt.filter((record) => dead.has(record.id) || writeOnly.has(record.id)));
|
|
170
|
-
return matrix.union.symbols.flatMap((symbol) => held.get(symbol.id) ?? []);
|
|
171
|
-
}
|
|
172
146
|
/**
|
|
173
147
|
* One line per held-back declaration, in site order: its reference and position, then
|
|
174
148
|
* each record's class, site and detail, all separated by tabs.
|