@cplieger/deadset-ts 8.0.0-dev.2 → 8.0.0-dev.3
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 +27 -5
- package/dist/src/augmentations.js +62 -0
- package/dist/src/build-scripts.js +437 -0
- package/dist/src/conformance-record.js +1 -1
- package/dist/src/convention-rows.js +39 -2
- package/dist/src/conventions.js +15 -0
- package/dist/src/dependencies.js +89 -0
- package/dist/src/entry-points.js +9 -18
- package/dist/src/findings/completion.js +62 -26
- package/dist/src/findings/intra-function.js +4 -2
- package/dist/src/findings/reads-and-writes.js +7 -6
- package/dist/src/findings/unused-declarations.js +4 -1
- package/dist/src/graph.js +5 -3
- package/dist/src/intra-function-parts.js +45 -17
- package/dist/src/kinds.js +7 -7
- package/dist/src/manifest.js +18 -0
- package/dist/src/matrix.js +38 -4
- package/dist/src/report.js +23 -4
- package/dist/src/reporters/sarif.js +12 -8
- package/dist/src/roots.js +138 -11
- package/dist/src/serialization-contract.js +258 -8
- package/dist/src/sweep.js +7 -9
- package/dist/src/test-globals.js +234 -0
- package/dist/src/type-errors.js +11 -3
- package/dist/src/unseen-loads.js +107 -0
- package/dist/src/verbs/analyze.js +24 -2
- package/dist/src/yaml-document.js +355 -0
- package/jsr.json +1 -1
- package/package.json +1 -1
- package/src/analysis.ts +45 -7
- package/src/augmentations.ts +76 -1
- package/src/build-scripts.ts +564 -0
- package/src/conformance-record.ts +1 -1
- package/src/convention-rows.ts +47 -2
- package/src/conventions.ts +16 -0
- package/src/dependencies.ts +96 -0
- package/src/entry-points.ts +10 -25
- package/src/file-facts.ts +3 -0
- package/src/findings/completion.ts +72 -28
- package/src/findings/intra-function.ts +4 -2
- package/src/findings/reads-and-writes.ts +7 -6
- package/src/findings/unused-declarations.ts +6 -1
- package/src/graph.ts +5 -3
- package/src/intra-function-parts.ts +55 -16
- package/src/kinds.ts +7 -7
- package/src/manifest.ts +21 -3
- package/src/matrix.ts +51 -4
- package/src/report.ts +56 -16
- package/src/reporters/sarif.ts +16 -8
- package/src/roots.ts +162 -12
- package/src/serialization-contract.ts +309 -7
- package/src/sweep.ts +21 -9
- package/src/test-globals.ts +275 -0
- package/src/type-errors.ts +14 -3
- package/src/unseen-loads.ts +145 -0
- package/src/verbs/analyze.ts +28 -2
- package/src/yaml-document.ts +377 -0
|
@@ -137,6 +137,12 @@ interface ParameterPlace {
|
|
|
137
137
|
* so the annotation may steer the call's inference.
|
|
138
138
|
*/
|
|
139
139
|
readonly annotated?: "steers" | "free";
|
|
140
|
+
/**
|
|
141
|
+
* Whether the name is one an object binding pattern of a parameter binds, and the
|
|
142
|
+
* compiler gives its function a contextual type: the code handing the argument may act on
|
|
143
|
+
* the pattern's read of the property, which the program does not show.
|
|
144
|
+
*/
|
|
145
|
+
readonly contextualPattern?: true;
|
|
140
146
|
/** Whether the compiler checks the parameter's file, so its unused-parameter rule may report it. */
|
|
141
147
|
readonly checked: boolean;
|
|
142
148
|
}
|
|
@@ -1093,10 +1099,9 @@ function calleeSteers<Brand>(
|
|
|
1093
1099
|
|
|
1094
1100
|
/**
|
|
1095
1101
|
* The parts with each name of `contextual` judged where the compiler gives its function a
|
|
1096
|
-
* contextual type:
|
|
1097
|
-
*
|
|
1098
|
-
*
|
|
1099
|
-
* name read.
|
|
1102
|
+
* contextual type: a name an object pattern binds is marked as one, and an annotated
|
|
1103
|
+
* parameter with whether the call's inference may read its annotation. A question the
|
|
1104
|
+
* checker leaves unanswered keeps the name read.
|
|
1100
1105
|
*/
|
|
1101
1106
|
function withContextualRead<Brand>(
|
|
1102
1107
|
project: ProjectView<Brand>,
|
|
@@ -1118,15 +1123,17 @@ function withContextualRead<Brand>(
|
|
|
1118
1123
|
const place = one.part.parameter;
|
|
1119
1124
|
judged.set(
|
|
1120
1125
|
one.part,
|
|
1121
|
-
|
|
1126
|
+
answer === UNANSWERED || place === undefined
|
|
1122
1127
|
? { ...one.part, used: true }
|
|
1123
|
-
:
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
...
|
|
1127
|
-
|
|
1128
|
+
: one.pattern
|
|
1129
|
+
? { ...one.part, parameter: { ...place, contextualPattern: true } }
|
|
1130
|
+
: {
|
|
1131
|
+
...one.part,
|
|
1132
|
+
parameter: {
|
|
1133
|
+
...place,
|
|
1134
|
+
annotated: calleeSteers(project, one.signature, one.at) ? "steers" : "free",
|
|
1135
|
+
},
|
|
1128
1136
|
},
|
|
1129
|
-
},
|
|
1130
1137
|
);
|
|
1131
1138
|
}
|
|
1132
1139
|
return parts.map((part) => judged.get(part) ?? part);
|
|
@@ -1205,6 +1212,8 @@ interface Local {
|
|
|
1205
1212
|
readonly uses: readonly Identifier[];
|
|
1206
1213
|
/** A `var` of a nested block, whose function scope outlives the block's end. */
|
|
1207
1214
|
readonly outlives: boolean;
|
|
1215
|
+
/** A function declaration, written at the start of the block whatever its place. */
|
|
1216
|
+
readonly hoisted: boolean;
|
|
1208
1217
|
}
|
|
1209
1218
|
|
|
1210
1219
|
/**
|
|
@@ -1244,9 +1253,10 @@ function functionOf(block: Block): FunctionLikeDeclaration | undefined {
|
|
|
1244
1253
|
}
|
|
1245
1254
|
|
|
1246
1255
|
/**
|
|
1247
|
-
* The locals one block declares with `let`, `const
|
|
1248
|
-
* block that spell it. A `var` of a nested
|
|
1249
|
-
*
|
|
1256
|
+
* The locals one block declares with `let`, `const`, `var` or a function declaration
|
|
1257
|
+
* inside a function, each with the names of the block that spell it. A `var` of a nested
|
|
1258
|
+
* block is one where no nested function of its function names it, since the function scope
|
|
1259
|
+
* holds it past the block's end.
|
|
1250
1260
|
*/
|
|
1251
1261
|
function localsOf(block: Block): readonly Local[] {
|
|
1252
1262
|
const declared: {
|
|
@@ -1254,10 +1264,23 @@ function localsOf(block: Block): readonly Local[] {
|
|
|
1254
1264
|
statement: number;
|
|
1255
1265
|
initialized: boolean;
|
|
1256
1266
|
outlives: boolean;
|
|
1267
|
+
hoisted: boolean;
|
|
1257
1268
|
}[] = [];
|
|
1258
1269
|
const scope = functionOf(block);
|
|
1259
1270
|
const outlives = scope?.body !== block;
|
|
1260
1271
|
block.statements.forEach((statement, at) => {
|
|
1272
|
+
if (isFunctionDeclaration(statement)) {
|
|
1273
|
+
if (scope !== undefined && statement.name !== undefined && statement.body !== undefined) {
|
|
1274
|
+
declared.push({
|
|
1275
|
+
name: statement.name,
|
|
1276
|
+
statement: at,
|
|
1277
|
+
initialized: true,
|
|
1278
|
+
outlives: false,
|
|
1279
|
+
hoisted: true,
|
|
1280
|
+
});
|
|
1281
|
+
}
|
|
1282
|
+
return;
|
|
1283
|
+
}
|
|
1261
1284
|
if (!isVariableStatement(statement)) {
|
|
1262
1285
|
return;
|
|
1263
1286
|
}
|
|
@@ -1267,6 +1290,11 @@ function localsOf(block: Block): readonly Local[] {
|
|
|
1267
1290
|
return;
|
|
1268
1291
|
}
|
|
1269
1292
|
for (const declaration of list.declarations) {
|
|
1293
|
+
// A write to a local typed `never` is the compiler's exhaustiveness check, which
|
|
1294
|
+
// fails to compile once a case goes unhandled.
|
|
1295
|
+
if (declaration.type?.kind === SyntaxKind.NeverKeyword) {
|
|
1296
|
+
continue;
|
|
1297
|
+
}
|
|
1270
1298
|
for (const name of boundNames(declaration.name)) {
|
|
1271
1299
|
if (
|
|
1272
1300
|
!blockScoped &&
|
|
@@ -1282,6 +1310,7 @@ function localsOf(block: Block): readonly Local[] {
|
|
|
1282
1310
|
statement: at,
|
|
1283
1311
|
initialized: declaration.initializer !== undefined,
|
|
1284
1312
|
outlives: !blockScoped && outlives,
|
|
1313
|
+
hoisted: false,
|
|
1285
1314
|
});
|
|
1286
1315
|
}
|
|
1287
1316
|
}
|
|
@@ -1333,12 +1362,22 @@ function deadStores(
|
|
|
1333
1362
|
used: false,
|
|
1334
1363
|
});
|
|
1335
1364
|
for (const local of locals) {
|
|
1336
|
-
const
|
|
1365
|
+
const own = local.hoisted ? block.statements[local.statement] : undefined;
|
|
1366
|
+
// A function's reads of itself keep it no more than its absence would.
|
|
1367
|
+
const uses = local.uses.filter(
|
|
1368
|
+
(use) => readsBinding(use, local.name, ids) && (own === undefined || !within(use, own)),
|
|
1369
|
+
);
|
|
1337
1370
|
if (!local.outlives && uses.every(isPlainWrite)) {
|
|
1338
|
-
|
|
1371
|
+
// A declaration with no initializer writes `undefined`, the one store left to name.
|
|
1372
|
+
const writes = [...(local.initialized ? [local.name] : []), ...uses];
|
|
1373
|
+
const targets = writes.length === 0 ? [local.name] : writes;
|
|
1339
1374
|
found.push(...targets.map((target) => deadAt(local, target)));
|
|
1340
1375
|
continue;
|
|
1341
1376
|
}
|
|
1377
|
+
// A read anywhere in the block reads a function declaration, which is written first.
|
|
1378
|
+
if (local.hoisted) {
|
|
1379
|
+
continue;
|
|
1380
|
+
}
|
|
1342
1381
|
if (uses.some((use) => inNestedFunction(use, block))) {
|
|
1343
1382
|
continue;
|
|
1344
1383
|
}
|
package/src/kinds.ts
CHANGED
|
@@ -87,7 +87,7 @@ const ROWS: readonly KindRow[] = [
|
|
|
87
87
|
code: "DS1005",
|
|
88
88
|
name: "test-of-dead-code",
|
|
89
89
|
languages: ["go", "ts"],
|
|
90
|
-
rule: "A test symbol whose set of referenced target symbols is non-empty and every member of that set is reported dead. In TypeScript an ambient declaration, one a declaration file holds or one a `declare` modifier carries or a declaration carrying one holds, describes code the target does not implement, so it is no target symbol of that set. A test that references at least one live target symbol is never reported, no notion of a test's subject and no name matching enters the rule, and the message states the rule. The test joins the dead component of the symbols it references, and it and each of them count as referencing each other, so the test is a member of their cycle and never a dead symbol outside it.",
|
|
90
|
+
rule: "A test symbol whose set of referenced target symbols is non-empty and every member of that set is reported dead. In TypeScript an ambient declaration, one a declaration file holds or one a `declare` modifier carries or a declaration carrying one holds, describes code the target does not implement, so it is no target symbol of that set. A test that references at least one live target symbol is never reported, no notion of a test's subject and no name matching enters the rule, and the message states the rule. A production declaration whose reachability class is `possible` because programs outside the analysis may use it, being published API of a library whose run declares no consumer, and in Go a declaration of a package no build links or an export of a unit a dropped module requires, counts as a live target symbol of every test that references it, so such a test is no test of dead code. The declaration is still reported at `possible`. The test joins the dead component of the symbols it references, and it and each of them count as referencing each other, so the test is a member of their cycle and never a dead symbol outside it.",
|
|
91
91
|
defaultSeverity: "deny",
|
|
92
92
|
maxClass: "certain",
|
|
93
93
|
fixability: "deletable",
|
|
@@ -146,7 +146,7 @@ const ROWS: readonly KindRow[] = [
|
|
|
146
146
|
languages: ["ts"],
|
|
147
147
|
rule: "An exported declaration used only inside its own file, reported as a candidate for removing the export keyword. The finding names the narrower visibility the references support.",
|
|
148
148
|
precondition:
|
|
149
|
-
'Closed world only. Reported in a file that is not an entry file, a file only a configuration string or a script token names being none, because each runs the file and reads no name it exports, and a test file being one only for the exports its test runner calls, as grammar/analysis.md "Test code" states, and a file a Markdown or MDX document imports one only for the exports the import reads, as grammar/analysis.md "Markdown and MDX documents" states, and that either belongs to a project whose consumer set the configuration declares complete or is reached by no manifest export. A declared cross-language edge counts as a reference from outside the file. Where the edge\'s other side is unknown to the analyzer, the finding is emitted pending. 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.',
|
|
149
|
+
'Closed world only. Reported in a file that is not an entry file, a file only a configuration string or a script token names being none, because each runs the file and reads no name it exports, while a file a build script\'s bundler call names is one, because the bundle exposes what it exports, and a test file being one only for the exports its test runner calls, as grammar/analysis.md "Test code" states, and a file a Markdown or MDX document imports one only for the exports the import reads, as grammar/analysis.md "Markdown and MDX documents" states, and that either belongs to a project whose consumer set the configuration declares complete or is reached by no manifest export. A declared cross-language edge counts as a reference from outside the file. Where the edge\'s other side is unknown to the analyzer, the finding is emitted pending. 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.',
|
|
150
150
|
defaultSeverity: "warn",
|
|
151
151
|
maxClass: "certain",
|
|
152
152
|
fixability: "narrowable",
|
|
@@ -212,9 +212,9 @@ const ROWS: readonly KindRow[] = [
|
|
|
212
212
|
code: "DS1303",
|
|
213
213
|
name: "unused-type-parameter",
|
|
214
214
|
languages: ["go", "ts"],
|
|
215
|
-
rule: "A type parameter of a function
|
|
215
|
+
rule: "A type parameter of a function, a method or a TypeScript type alias that no part of the declaration's signature, body or aliased type names, at the certain class. The deletion is local to the declaration and the explicit instantiations the reference set already lists. A type parameter of a declaration that is itself dead falls with it and is reported under no code of its own.",
|
|
216
216
|
precondition:
|
|
217
|
-
"Function and method type parameters
|
|
217
|
+
"Function and method type parameters, and in TypeScript those of a type alias. A TypeScript alias names no type of its own, so two instantiations that differ only in a parameter its type never names are one type. A Go type declaration's type parameter is never reported, because a phantom type parameter such as `type ID[T any] int` makes two instantiations distinct types while naming the parameter nowhere, so deleting it changes the program. Nor is one of a TypeScript class or interface, which merges only with declarations of the same type parameters. A method signature an interface or an object type declares belongs to that type declaration, so its type parameters are never reported either.",
|
|
218
218
|
defaultSeverity: "deny",
|
|
219
219
|
maxClass: "certain",
|
|
220
220
|
fixability: "deletable",
|
|
@@ -332,7 +332,7 @@ const ROWS: readonly KindRow[] = [
|
|
|
332
332
|
languages: ["go", "ts"],
|
|
333
333
|
rule: "A parameter with no reference inside its function body, on a function whose signature is free to change.",
|
|
334
334
|
precondition:
|
|
335
|
-
"The signature must be free, defined as follows: the function is not a method retained by interface satisfaction, is not 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, is not used as a value, is not a go:linkname or cgo target, is not a go-fuzz entry point, is not a function the Go test driver runs (a test, benchmark or fuzz test of a test file, or TestMain), and is not a stub whose body is empty or only panics. A function literal or function expression that initializes a variable the program only calls, never passing, storing, returning or assigning it, is called by name and is not used as a value. In Go a function literal that is the callee of the call it is written in, the call of a `go` or a `defer` statement included, is not used as a value, wherever the literal is written. A parameter its body never names is dead whatever the callers, so a published declaration of a library is reported too, with the fixability the vocabulary gives the kind: the signature change is a breaking change. In TypeScript a caller may pass a function used as a value more arguments than it declares parameters, so in such a function whose signature is free but for that use, a parameter its body never reads is reported for deletion when its body reads no parameter after it, and an unread parameter before the last read one is reported for a rename to its name with a leading underscore, which keeps every argument in place and which the compilers' unused-parameter rules read as declared unread, unless its name already starts with one. A parameter of a TypeScript function expression or arrow function that the compiler gives a contextual type, and that carries a type annotation, is not reported for deletion where the inference of the call holding the function may read the annotation, being where the callee is generic and its parameter type at the function's place names a type parameter at the parameter's place, or where the function is written inside an object literal such a call is passed, and it is reported for deletion at `probable` where it carries an annotation no such inference reads, because nothing in the program shows that an outside reader ignores it.
|
|
335
|
+
"The signature must be free, defined as follows: the function is not a method retained by interface satisfaction, is not 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, is not used as a value, is not a go:linkname or cgo target, is not a go-fuzz entry point, is not a function the Go test driver runs (a test, benchmark or fuzz test of a test file, or TestMain), and is not a stub whose body is empty or only panics. A function literal or function expression that initializes a variable the program only calls, never passing, storing, returning or assigning it, is called by name and is not used as a value. In Go a function literal that is the callee of the call it is written in, the call of a `go` or a `defer` statement included, is not used as a value, wherever the literal is written. A parameter its body never names is dead whatever the callers, so a published declaration of a library is reported too, with the fixability the vocabulary gives the kind: the signature change is a breaking change. In TypeScript a caller may pass a function used as a value more arguments than it declares parameters, so in such a function whose signature is free but for that use, a parameter its body never reads is reported for deletion when its body reads no parameter after it, and an unread parameter before the last read one is reported for a rename to its name with a leading underscore, which keeps every argument in place and which the compilers' unused-parameter rules read as declared unread, unless its name already starts with one. A parameter of a TypeScript function expression or arrow function that the compiler gives a contextual type, and that carries a type annotation, is not reported for deletion where the inference of the call holding the function may read the annotation, being where the callee is generic and its parameter type at the function's place names a type parameter at the parameter's place, or where the function is written inside an object literal such a call is passed, and it is reported for deletion at `probable` where it carries an annotation no such inference reads, because nothing in the program shows that an outside reader ignores it. A name an object binding pattern of a parameter of such a function binds, annotated or not, that the body never reads is reported for deletion at `probable`, because the pattern reads the property it names from the argument and the code that hands the argument may act on that read, which nothing in the program shows. Any other TypeScript parameter that is a binding pattern is judged by the names it binds: each name the body never reads is reported wherever the pattern stands, because removing it from the pattern moves no argument, except a name beside a rest element of the same object pattern, whose removal changes what that element holds, and the pattern counts as a read parameter when the body reads any name it binds. A rest parameter is judged as the last parameter. A TypeScript `this` parameter declares the type of `this` and receives no argument, so it is never reported. A TypeScript constructor is used as a value where its class is used as a value other than as the callee of a `new` expression or the class an `extends` clause names, and its signature is not free where its class carries a decorator or a parameter of the constructor carries one, because the code that decorator or a container runs may construct the class. A parameter property is read by the write to its member the constructor makes, so it is never reported under this kind, and its member is judged under DS1301. A TypeScript name a parameter binds, whole or through a binding pattern, that starts with an underscore is never reported, because a leading underscore is the spelling unused-parameter rules read as declared unread, and no other name exempts a parameter. A Go parameter named by the blank identifier declares no name a body could read and is never reported.",
|
|
336
336
|
defaultSeverity: "warn",
|
|
337
337
|
maxClass: "certain",
|
|
338
338
|
fixability: "manual",
|
|
@@ -358,7 +358,7 @@ const ROWS: readonly KindRow[] = [
|
|
|
358
358
|
languages: ["go", "ts"],
|
|
359
359
|
rule: "A result value that no call site of the function uses, on a function whose signature is free to change.",
|
|
360
360
|
precondition:
|
|
361
|
-
"The same free-signature rule as unused-parameter, plus a closed-world condition: every call site of the function must be in the loaded graph, because an unknown caller may consume the result, so the kind reports only for a function whose callers are all visible. A call that is the operand of an await expression written as a statement of its own uses a result whose type has a member named then, a promise or another thenable, because the caller sequences on it, and discards any other result. A call written as a statement of its own, or as the operand of void, discards its result whatever its type.",
|
|
361
|
+
"The same free-signature rule as unused-parameter, plus a closed-world condition: every call site of the function must be in the loaded graph, because an unknown caller may consume the result, so the kind reports only for a function whose callers are all visible. A function a program run by naming the file names, a file under the `ignore` tag or a script of a dropped module, has call sites outside the loaded graph. A call that is the operand of an await expression written as a statement of its own uses a result whose type has a member named then, a promise or another thenable, because the caller sequences on it, and discards any other result. A call written as a statement of its own, or as the operand of void, discards its result whatever its type.",
|
|
362
362
|
defaultSeverity: "warn",
|
|
363
363
|
maxClass: "certain",
|
|
364
364
|
fixability: "manual",
|
|
@@ -382,7 +382,7 @@ const ROWS: readonly KindRow[] = [
|
|
|
382
382
|
languages: ["go", "ts"],
|
|
383
383
|
rule: "A write to a local variable with no read before the next write to it or the end of its scope, computed on the same read-and-write classification the write-only-symbol kind uses. The finding names the write position.",
|
|
384
384
|
precondition:
|
|
385
|
-
"In TypeScript the binding of a catch clause is a local variable the clause writes when it catches, so a binding that is a name and that the clause's block never reads is a dead store, positioned at the binding: the clause written with no binding behaves the same. A TypeScript local is a name a `let` or `const` declaration of a block binds, or a `var` declaration of a block of a function, a name an object or array binding pattern of one binds at any depth included, except a name with a default, whose removal skips the default's evaluation, and a name beside a rest element of its object pattern, whose removal changes what the rest holds. A `var` of a block nested in its function's body is judged in that block, its last write kept because the function scope reads on past the block's end, and is no subject where a nested function of its function names it. A local a nested function reads is no subject, because the function may read it at any time, and every write to a local nothing reads is dead, one a nested function makes included, each positioned at the name it writes. The variable a `let` or `const` declaration of a `for…of` or `for…in` statement binds is written at each iteration, so one the loop body never reads is a dead store positioned at the binding, except a name that starts with an underscore, because a loop must bind a name and that spelling declares it unread. In Go a function literal a package-level variable's initializer holds is a function body of its own, and a finding about one of its stores carries the reference of the variable the literal initializes, which for a blank variable is its package's, as `grammar/symbol-ref.md` states for a blank-identifier declaration. In Go a range statement stores its key and its value at the start of every iteration, and an empty range stores neither. In Go a function literal reads each variable it names where it is written, and a store that a literal naming its variable may precede on some path of the control-flow graph, the literal of an earlier iteration of a loop included, is not dead, because a call of the literal the graph does not order may read it. A store no such literal precedes is judged as any other. A literal that is the callee of the call it is written in, outside a `go` or `defer` statement, runs there, so it reads its variables at that call and precedes no later store.",
|
|
385
|
+
"In TypeScript the binding of a catch clause is a local variable the clause writes when it catches, so a binding that is a name and that the clause's block never reads is a dead store, positioned at the binding: the clause written with no binding behaves the same. A TypeScript local is a name a `let` or `const` declaration of a block binds, or a `var` declaration of a block of a function, a name an object or array binding pattern of one binds at any depth included, except a name with a default, whose removal skips the default's evaluation, and a name beside a rest element of its object pattern, whose removal changes what the rest holds. A function declaration of a block of a function is a TypeScript local too, written with its function when the block starts, wherever it stands in the block, so one that nothing outside its own body reads is a dead store positioned at its name, and a read anywhere else in the block reads it. A `let` or `var` declaration with no initializer writes `undefined`, so one that nothing reads and nothing writes is a dead store positioned at its name. A local whose declaration carries the type annotation `never` is no subject: a write to it is the compiler's exhaustiveness check, which fails to compile once a case goes unhandled, so deleting it changes what the program checks. A `var` of a block nested in its function's body is judged in that block, its last write kept because the function scope reads on past the block's end, and is no subject where a nested function of its function names it. A local a nested function reads is no subject, because the function may read it at any time, and every write to a local nothing reads is dead, one a nested function makes included, each positioned at the name it writes. The variable a `let` or `const` declaration of a `for…of` or `for…in` statement binds is written at each iteration, so one the loop body never reads is a dead store positioned at the binding, except a name that starts with an underscore, because a loop must bind a name and that spelling declares it unread. In Go a function literal a package-level variable's initializer holds is a function body of its own, and a finding about one of its stores carries the reference of the variable the literal initializes, which for a blank variable is its package's, as `grammar/symbol-ref.md` states for a blank-identifier declaration. In Go a range statement stores its key and its value at the start of every iteration, and an empty range stores neither. In Go a function literal reads each variable it names where it is written, and a store that a literal naming its variable may precede on some path of the control-flow graph, the literal of an earlier iteration of a loop included, is not dead, because a call of the literal the graph does not order may read it. A store no such literal precedes is judged as any other. A literal that is the callee of the call it is written in, outside a `go` or `defer` statement, runs there, so it reads its variables at that call and precedes no later store.",
|
|
386
386
|
defaultSeverity: "deny",
|
|
387
387
|
maxClass: "certain",
|
|
388
388
|
fixability: "deletable",
|
package/src/manifest.ts
CHANGED
|
@@ -35,8 +35,8 @@ export interface ManifestEntry {
|
|
|
35
35
|
readonly path: string;
|
|
36
36
|
readonly role: EntryRole;
|
|
37
37
|
/**
|
|
38
|
-
* Whether the member declares what a consumer may import, which is `exports`
|
|
39
|
-
* the three members that preceded
|
|
38
|
+
* Whether the member declares what a consumer may import, which is `exports`, `browser`
|
|
39
|
+
* and the three members that preceded `exports`. A command is published too, and is not this:
|
|
40
40
|
* `exports` is the surface a library's published API is read from.
|
|
41
41
|
*/
|
|
42
42
|
readonly published: boolean;
|
|
@@ -136,7 +136,7 @@ function manifestOf(host: Host, targetRoot: string): Record<string, unknown> | u
|
|
|
136
136
|
export interface Manifest {
|
|
137
137
|
/**
|
|
138
138
|
* Every entry point the manifest names, in the order the members are read: `main`,
|
|
139
|
-
* `module`, `types`, then each `bin` command, then every target of `exports`, then
|
|
139
|
+
* `module`, `types`, then `browser` and each file it maps a module to, then each `bin` command, then every target of `exports`, then
|
|
140
140
|
* every token of each script, which names a file only where one is at its path. Each
|
|
141
141
|
* path is absolute.
|
|
142
142
|
*/
|
|
@@ -171,6 +171,24 @@ export function readManifest(host: Host, targetRoot: string): Manifest {
|
|
|
171
171
|
}
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
+
// A `browser` string replaces `main` in a browser bundle, and an object replaces each
|
|
175
|
+
// module it maps with the file it names: https://github.com/defunctzombie/package-browser-field-spec
|
|
176
|
+
const browser = manifest["browser"];
|
|
177
|
+
if (typeof browser === "string" && isTarget(browser, false)) {
|
|
178
|
+
found.push({ member: "browser", path: browser, role: "import", published: true });
|
|
179
|
+
} else if (isRecord(browser)) {
|
|
180
|
+
for (const [replaced, value] of Object.entries(browser)) {
|
|
181
|
+
if (typeof value === "string" && isTarget(value, true)) {
|
|
182
|
+
found.push({
|
|
183
|
+
member: below("browser", replaced),
|
|
184
|
+
path: value,
|
|
185
|
+
role: "import",
|
|
186
|
+
published: true,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
174
192
|
const bin = manifest["bin"];
|
|
175
193
|
if (typeof bin === "string" && isTarget(bin, false)) {
|
|
176
194
|
found.push({ member: "bin", path: bin, role: "run", published: false });
|
package/src/matrix.ts
CHANGED
|
@@ -223,18 +223,32 @@ export function sweepMatrix(matrix: Matrix, input: SweepInput): SweepResult {
|
|
|
223
223
|
|
|
224
224
|
const union = matrix.union;
|
|
225
225
|
const heldBy: (string | undefined)[] = union.symbols.map(() => undefined);
|
|
226
|
-
const
|
|
226
|
+
const held = [
|
|
227
227
|
...input.marked,
|
|
228
228
|
...(input.exempt ?? []).map((record) => record.id),
|
|
229
229
|
...(input.unanswered ?? []),
|
|
230
|
-
]
|
|
231
|
-
const
|
|
230
|
+
];
|
|
231
|
+
const testReached = reachedWithTheTests(union, held);
|
|
232
|
+
// A test uses what it names: a file a test reads through a star re-export or a
|
|
233
|
+
// namespace keeps the exports nothing names at their own class.
|
|
234
|
+
const testNamed = reachedWithTheTests(union, held, false);
|
|
235
|
+
const everywhere = union.symbols.map((symbol, at): readonly Candidate[] | undefined => {
|
|
232
236
|
const held = matrix.heldIn[at] ?? [];
|
|
233
237
|
const found = held.map((configuration) => byConfiguration.get(configuration)?.get(symbol.id));
|
|
234
238
|
return held.length === 0 || found.some((candidate) => candidate === undefined)
|
|
235
239
|
? undefined
|
|
236
240
|
: found.filter((candidate): candidate is Candidate => candidate !== undefined);
|
|
237
241
|
});
|
|
242
|
+
const compiled = compiledReferences(
|
|
243
|
+
matrix,
|
|
244
|
+
new Set(
|
|
245
|
+
union.symbols.flatMap((symbol, at) => (everywhere[at] === undefined ? [] : [symbol.id])),
|
|
246
|
+
),
|
|
247
|
+
input.mode,
|
|
248
|
+
);
|
|
249
|
+
const judged = everywhere.map((found, at) =>
|
|
250
|
+
compiled.has(union.symbols[at]?.id ?? "") ? undefined : found,
|
|
251
|
+
);
|
|
238
252
|
// A configuration that does not hold a test's subject's caller finds the subject dead,
|
|
239
253
|
// so a test is of dead code only where every production subject it names is dead in
|
|
240
254
|
// every configuration that holds it.
|
|
@@ -252,7 +266,7 @@ export function sweepMatrix(matrix: Matrix, input: SweepInput): SweepResult {
|
|
|
252
266
|
const kept = keptByTests(union, testReached, admittedAt);
|
|
253
267
|
const testUsed = testUsedReach(
|
|
254
268
|
union,
|
|
255
|
-
union.symbols.map((_symbol, at) => matrix.testUsed[at] === true &&
|
|
269
|
+
union.symbols.map((_symbol, at) => matrix.testUsed[at] === true && testNamed[at] === true),
|
|
256
270
|
judged.map((one) => one !== undefined),
|
|
257
271
|
);
|
|
258
272
|
const candidates: MatrixCandidate[] = [];
|
|
@@ -296,6 +310,39 @@ export function sweepMatrix(matrix: Matrix, input: SweepInput): SweepResult {
|
|
|
296
310
|
};
|
|
297
311
|
}
|
|
298
312
|
|
|
313
|
+
/**
|
|
314
|
+
* Every declaration of `dead`, those dead in every configuration that holds them, that a
|
|
315
|
+
* configuration's graph references from a judged declaration outside `dead` or already
|
|
316
|
+
* kept, until none joins: deleting one would break that configuration's compile. A
|
|
317
|
+
* production run counts no reference a test file made.
|
|
318
|
+
*/
|
|
319
|
+
function compiledReferences(
|
|
320
|
+
matrix: Matrix,
|
|
321
|
+
dead: ReadonlySet<string>,
|
|
322
|
+
mode: SweepInput["mode"],
|
|
323
|
+
): ReadonlySet<string> {
|
|
324
|
+
const kept = new Set<string>();
|
|
325
|
+
for (let changed = true; changed;) {
|
|
326
|
+
changed = false;
|
|
327
|
+
for (const graph of matrix.graphs) {
|
|
328
|
+
graph.out.forEach((edges, from) => {
|
|
329
|
+
const id = graph.symbols[from]?.id ?? "";
|
|
330
|
+
if (graph.subject[from] !== true || (dead.has(id) && !kept.has(id))) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
for (const edge of edges) {
|
|
334
|
+
const to = graph.symbols[edge.to]?.id ?? "";
|
|
335
|
+
if ((!mode.production || !edge.test) && dead.has(to) && !kept.has(to)) {
|
|
336
|
+
kept.add(to);
|
|
337
|
+
changed = true;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return kept;
|
|
344
|
+
}
|
|
345
|
+
|
|
299
346
|
/** Per declaration of the union, whether one of `seeds` is it or reaches it through `dead` declarations. */
|
|
300
347
|
function testUsedReach(
|
|
301
348
|
union: Graph,
|
package/src/report.ts
CHANGED
|
@@ -81,11 +81,6 @@ export interface WireFinding {
|
|
|
81
81
|
readonly root: boolean;
|
|
82
82
|
readonly symbol_count: number;
|
|
83
83
|
readonly deletable_lines: number;
|
|
84
|
-
readonly members?: readonly {
|
|
85
|
-
readonly ref: string;
|
|
86
|
-
readonly name: string;
|
|
87
|
-
readonly position: WirePosition;
|
|
88
|
-
}[];
|
|
89
84
|
};
|
|
90
85
|
readonly retained_by: readonly string[];
|
|
91
86
|
readonly configurations: readonly string[];
|
|
@@ -96,6 +91,16 @@ export interface WireFinding {
|
|
|
96
91
|
readonly details: WireDetails;
|
|
97
92
|
}
|
|
98
93
|
|
|
94
|
+
/** One dead component a finding names, with its members, as the report lists it once. */
|
|
95
|
+
export interface WireComponent {
|
|
96
|
+
readonly id: string;
|
|
97
|
+
readonly members: readonly {
|
|
98
|
+
readonly ref: string;
|
|
99
|
+
readonly name: string;
|
|
100
|
+
readonly position: WirePosition;
|
|
101
|
+
}[];
|
|
102
|
+
}
|
|
103
|
+
|
|
99
104
|
interface WireEdgeEvaluation {
|
|
100
105
|
readonly edge: string;
|
|
101
106
|
readonly side: string;
|
|
@@ -218,6 +223,8 @@ export interface Report {
|
|
|
218
223
|
}[];
|
|
219
224
|
};
|
|
220
225
|
readonly findings: readonly WireFinding[];
|
|
226
|
+
/** Present where the run lists each component in full. */
|
|
227
|
+
readonly components?: readonly WireComponent[];
|
|
221
228
|
readonly edge_evaluations: readonly WireEdgeEvaluation[];
|
|
222
229
|
readonly stale_suppressions: readonly WireStaleSuppression[];
|
|
223
230
|
readonly declared_gaps: readonly WireDeclaredGap[];
|
|
@@ -231,13 +238,20 @@ export interface Report {
|
|
|
231
238
|
}
|
|
232
239
|
|
|
233
240
|
/** One hint about the run's setup, as the report schema's `notes` spells it. */
|
|
234
|
-
export
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
+
export type ReportNote =
|
|
242
|
+
| {
|
|
243
|
+
readonly kind: "unmapped-entry";
|
|
244
|
+
/** The directory of the manifest the note is about, below the target root. */
|
|
245
|
+
readonly path: string;
|
|
246
|
+
readonly key: "ts.entry_files";
|
|
247
|
+
readonly message: string;
|
|
248
|
+
}
|
|
249
|
+
| {
|
|
250
|
+
readonly kind: "computed-load";
|
|
251
|
+
/** The directory of the file that writes the load, below the target root. */
|
|
252
|
+
readonly path: string;
|
|
253
|
+
readonly message: string;
|
|
254
|
+
};
|
|
241
255
|
|
|
242
256
|
/** What one run answered, from which its report is built. */
|
|
243
257
|
export interface ReportInput {
|
|
@@ -258,6 +272,26 @@ export interface ReportInput {
|
|
|
258
272
|
readonly conventionsApplied: readonly AppliedConvention[];
|
|
259
273
|
/** The notes the run states, in any order. */
|
|
260
274
|
readonly notes?: readonly ReportNote[];
|
|
275
|
+
/** Whether the run lists each component in full, which `reporters.cascade: full` asks for. */
|
|
276
|
+
readonly listsComponents?: boolean;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Each component the findings name, once, with the members the first finding naming it
|
|
281
|
+
* carries, in the order the findings are: `findings` paired with their wire forms and
|
|
282
|
+
* sorted, then the edge evaluations' findings.
|
|
283
|
+
*/
|
|
284
|
+
function componentsOf(
|
|
285
|
+
findings: readonly (readonly [WireFinding, CompletedFinding])[],
|
|
286
|
+
): readonly WireComponent[] {
|
|
287
|
+
const listed = new Map<string, WireComponent>();
|
|
288
|
+
for (const [wire, finding] of findings) {
|
|
289
|
+
const { members } = finding.component;
|
|
290
|
+
if (members !== undefined && !listed.has(wire.component.id)) {
|
|
291
|
+
listed.set(wire.component.id, { id: wire.component.id, members: members.map(wireSymbol) });
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return [...listed.values()];
|
|
261
295
|
}
|
|
262
296
|
|
|
263
297
|
/** Two strings ordered bytewise. */
|
|
@@ -342,9 +376,6 @@ export function wireFinding(finding: CompletedFinding): WireFinding {
|
|
|
342
376
|
root: finding.component.root,
|
|
343
377
|
symbol_count: finding.component.symbolCount,
|
|
344
378
|
deletable_lines: finding.component.deletableLines,
|
|
345
|
-
...(finding.component.members === undefined
|
|
346
|
-
? {}
|
|
347
|
-
: { members: finding.component.members.map(wireSymbol) }),
|
|
348
379
|
},
|
|
349
380
|
retained_by: finding.retainedBy,
|
|
350
381
|
configurations: finding.configurations,
|
|
@@ -470,7 +501,13 @@ function totalsOf(findings: readonly WireFinding[], result: PassResult): WireTot
|
|
|
470
501
|
|
|
471
502
|
/** Builds the report of one run, every array in the order the Contract fixes for it. */
|
|
472
503
|
export function buildReport(input: ReportInput): Report {
|
|
473
|
-
const
|
|
504
|
+
const paired = input.result.findings
|
|
505
|
+
.map((finding) => [wireFinding(finding), finding] as const)
|
|
506
|
+
.sort(([a], [b]) => canonical(a, b));
|
|
507
|
+
const findings = paired.map(([wire]) => wire);
|
|
508
|
+
const evaluated = input.result.edgeEvaluations.flatMap((one) =>
|
|
509
|
+
one.finding === undefined ? [] : [[wireFinding(one.finding), one.finding] as const],
|
|
510
|
+
);
|
|
474
511
|
const unavailable = [...new Map(input.unavailable.map((one) => [one.id, one])).values()]
|
|
475
512
|
.sort((a, b) => compare(a.id, b.id))
|
|
476
513
|
.map((one) => ({ id: one.id, role: "consumer" as const, reason: one.reason }));
|
|
@@ -500,6 +537,9 @@ export function buildReport(input: ReportInput): Report {
|
|
|
500
537
|
.map((one) => ({ id: one.id, project: one.project, error: one.error })),
|
|
501
538
|
consumers: { declared: loaded.length + unavailable.length, loaded, unavailable },
|
|
502
539
|
findings,
|
|
540
|
+
...(input.listsComponents === true
|
|
541
|
+
? { components: componentsOf([...paired, ...evaluated]) }
|
|
542
|
+
: {}),
|
|
503
543
|
edge_evaluations: input.result.edgeEvaluations.map(wireEvaluation).sort(byEdge),
|
|
504
544
|
stale_suppressions: input.result.staleSuppressions.map(wireStale).sort(canonicalStale),
|
|
505
545
|
// Every gap shares the one component of the canonical key a gap supplies, the analyzer's
|
package/src/reporters/sarif.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { Confidence, Severity } from "../config.ts";
|
|
|
2
2
|
import { jsonChunks } from "../json-chunks.ts";
|
|
3
3
|
import { KINDS, type KindRow } from "../kinds.ts";
|
|
4
4
|
import { declarationKey } from "../ref.ts";
|
|
5
|
-
import type { Report, WireFinding, WireStaleSuppression } from "../report.ts";
|
|
5
|
+
import type { Report, WireComponent, WireFinding, WireStaleSuppression } from "../report.ts";
|
|
6
6
|
import { lineHashes, symbolFingerprint } from "./fingerprint.ts";
|
|
7
7
|
import { RenderError, type RenderOptions } from "./reporter.ts";
|
|
8
8
|
import { utf8Bytes } from "./sha256.ts";
|
|
@@ -151,8 +151,11 @@ interface Related {
|
|
|
151
151
|
readonly endLine: number;
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
+
/** The members the report lists for one component, none where it lists none. */
|
|
155
|
+
type MembersOf = (id: string) => WireComponent["members"];
|
|
156
|
+
|
|
154
157
|
/** Every position a finding names beyond its own, in the mapping's order, the first hundred. */
|
|
155
|
-
function relatedOf(found: WireFinding): readonly Related[] {
|
|
158
|
+
function relatedOf(found: WireFinding, members: MembersOf): readonly Related[] {
|
|
156
159
|
const related: Related[] = [];
|
|
157
160
|
for (const one of found.details.implementations ?? []) {
|
|
158
161
|
related.push({ label: "implementation", at: one.position, endLine: one.position.end_line });
|
|
@@ -164,7 +167,7 @@ function relatedOf(found: WireFinding): readonly Related[] {
|
|
|
164
167
|
const at = found.details.name_literal;
|
|
165
168
|
related.push({ label: "name", at, endLine: at.end_line });
|
|
166
169
|
}
|
|
167
|
-
for (const one of found.component.
|
|
170
|
+
for (const one of members(found.component.id)) {
|
|
168
171
|
if (
|
|
169
172
|
declarationKey(one.ref, one.position.path) !==
|
|
170
173
|
declarationKey(found.symbol.ref, found.position.path)
|
|
@@ -188,7 +191,9 @@ function messageWithLinks(message: string, related: readonly Related[]): string
|
|
|
188
191
|
}
|
|
189
192
|
|
|
190
193
|
/** Every member of the finding the mapping places nowhere else, under its schema name. */
|
|
191
|
-
|
|
194
|
+
/** A result's member bag, its component holding the members the report lists for it once. */
|
|
195
|
+
function propertiesOf(found: WireFinding, members: MembersOf): Record<string, unknown> {
|
|
196
|
+
const listed = members(found.component.id);
|
|
192
197
|
return {
|
|
193
198
|
language: found.language,
|
|
194
199
|
symbol: found.symbol,
|
|
@@ -199,7 +204,7 @@ function propertiesOf(found: WireFinding): Record<string, unknown> {
|
|
|
199
204
|
: { liveness_relation: found.liveness_relation }),
|
|
200
205
|
test_only: found.test_only,
|
|
201
206
|
generated: found.generated,
|
|
202
|
-
component: found.component,
|
|
207
|
+
component: listed.length === 0 ? found.component : { ...found.component, members: listed },
|
|
203
208
|
retained_by: found.retained_by,
|
|
204
209
|
configurations: found.configurations,
|
|
205
210
|
consumers_loaded: found.consumers_loaded,
|
|
@@ -220,8 +225,9 @@ function findingResult(
|
|
|
220
225
|
found: WireFinding,
|
|
221
226
|
index: ReadonlyMap<string, number>,
|
|
222
227
|
hashOf: (at: Located) => string,
|
|
228
|
+
members: MembersOf,
|
|
223
229
|
): Record<string, unknown> {
|
|
224
|
-
const related = relatedOf(found);
|
|
230
|
+
const related = relatedOf(found, members);
|
|
225
231
|
return {
|
|
226
232
|
ruleId: found.code,
|
|
227
233
|
ruleIndex: ruleIndex(index, found.code),
|
|
@@ -241,7 +247,7 @@ function findingResult(
|
|
|
241
247
|
primaryLocationLineHash: hashOf(found.position),
|
|
242
248
|
"deadsetSymbolRef/v1": symbolFingerprint(found.code, found.symbol.ref),
|
|
243
249
|
},
|
|
244
|
-
properties: propertiesOf(found),
|
|
250
|
+
properties: propertiesOf(found, members),
|
|
245
251
|
};
|
|
246
252
|
}
|
|
247
253
|
|
|
@@ -275,8 +281,10 @@ export function sarif(report: Report, options: RenderOptions): Iterable<string>
|
|
|
275
281
|
const ruleList = rules(kinds);
|
|
276
282
|
const index = new Map(kinds.map((kind, at) => [kind.code, at]));
|
|
277
283
|
const hashOf = lineHashCache(options.readSource);
|
|
284
|
+
const listed = new Map((report.components ?? []).map((one) => [one.id, one.members]));
|
|
285
|
+
const members: MembersOf = (id) => listed.get(id) ?? [];
|
|
278
286
|
const results = [
|
|
279
|
-
...report.findings.map((found) => findingResult(found, index, hashOf)),
|
|
287
|
+
...report.findings.map((found) => findingResult(found, index, hashOf, members)),
|
|
280
288
|
...report.stale_suppressions.map((stale) => staleResult(stale, index, hashOf)),
|
|
281
289
|
];
|
|
282
290
|
const languages = [...report.analyzer.languages].sort().join("+");
|