@cplieger/deadset-ts 7.0.0-dev.1 → 7.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 +95 -60
- package/dist/src/augmentations.js +14 -12
- package/dist/src/component-layer.js +1 -1
- package/dist/src/components.js +62 -20
- package/dist/src/configuration-files.js +230 -30
- package/dist/src/conformance-record.js +1 -1
- package/dist/src/contextual-uses.js +104 -13
- package/dist/src/convention-rows.js +10 -0
- package/dist/src/conventions.js +35 -1
- package/dist/src/dependencies.js +90 -32
- package/dist/src/discover.js +1 -1
- package/dist/src/edges.js +1 -1
- package/dist/src/element-access.js +341 -0
- package/dist/src/erased-stores.js +317 -0
- package/dist/src/exempt-classes.js +9 -22
- package/dist/src/exempt.js +3 -15
- package/dist/src/findings/completion.js +78 -16
- package/dist/src/findings/dependencies-and-module-machinery.js +1 -1
- package/dist/src/findings/emitters.js +1 -5
- package/dist/src/findings/self-check.js +27 -10
- package/dist/src/findings-pass.js +6 -5
- package/dist/src/in-operands.js +30 -0
- package/dist/src/indexed-access.js +264 -119
- package/dist/src/intra-function-parts.js +25 -11
- package/dist/src/inventory.js +28 -12
- package/dist/src/keyof-comparisons.js +170 -0
- package/dist/src/kinds.js +17 -7
- package/dist/src/manifest.js +6 -16
- package/dist/src/matrix.js +20 -2
- package/dist/src/query.js +3 -0
- package/dist/src/references.js +14 -3
- package/dist/src/report.js +4 -1
- package/dist/src/reporters/sarif.js +4 -0
- package/dist/src/roots.js +0 -1
- package/dist/src/run.js +0 -2
- package/dist/src/schema.js +1 -1
- package/dist/src/scope.js +2 -2
- package/dist/src/serialization-contract.js +43 -14
- package/dist/src/session.js +12 -8
- package/dist/src/suppress-file.js +58 -6
- package/dist/src/suppress-inline.js +14 -5
- package/dist/src/suppress.js +64 -35
- package/dist/src/unimplemented-callees.js +312 -0
- package/dist/src/value-reach.js +7 -7
- package/dist/src/verbs/analyze.js +11 -9
- package/dist/src/workspace-programs.js +0 -3
- package/dist/src/workspace-resolution.js +0 -2
- package/dist/src/workspace.js +0 -3
- package/jsr.json +1 -1
- package/package.json +3 -2
- package/src/analysis.ts +145 -82
- package/src/augmentations.ts +14 -11
- package/src/calls.ts +3 -3
- package/src/component-files.ts +7 -7
- package/src/component-layer.ts +2 -2
- package/src/components.ts +104 -22
- package/src/config.ts +4 -4
- package/src/configuration-files.ts +250 -31
- package/src/conformance-record.ts +1 -1
- package/src/contextual-uses.ts +135 -17
- package/src/convention-rows.ts +23 -0
- package/src/conventions.ts +53 -1
- package/src/dependencies.ts +119 -38
- package/src/discover.ts +3 -3
- package/src/edges.ts +3 -3
- package/src/element-access.ts +446 -0
- package/src/entry-points.ts +1 -1
- package/src/erased-stores.ts +413 -0
- package/src/exempt-classes.ts +10 -24
- package/src/exempt.ts +5 -29
- package/src/file-facts.ts +3 -1
- package/src/finding.ts +3 -1
- package/src/findings/completion.ts +97 -21
- package/src/findings/dependencies-and-module-machinery.ts +1 -1
- package/src/findings/emitters.ts +4 -9
- package/src/findings/self-check.ts +31 -14
- package/src/findings-pass.ts +14 -7
- package/src/graph.ts +2 -2
- package/src/html-entries.ts +1 -1
- package/src/in-operands.ts +45 -0
- package/src/indexed-access.ts +303 -137
- package/src/intra-function-parts.ts +41 -16
- package/src/inventory.ts +37 -33
- package/src/json-document.ts +1 -1
- package/src/keyof-comparisons.ts +211 -0
- package/src/kinds.ts +20 -7
- package/src/manifest.ts +7 -22
- package/src/matrix.ts +34 -2
- package/src/namespace-objects.ts +1 -1
- package/src/pnpm-workspace.ts +1 -1
- package/src/query.ts +10 -0
- package/src/references.ts +30 -7
- package/src/report.ts +11 -5
- package/src/reporters/sarif.ts +4 -0
- package/src/roots.ts +0 -3
- package/src/run.ts +0 -2
- package/src/schema.ts +2 -2
- package/src/scope.ts +2 -2
- package/src/serialization-contract.ts +53 -14
- package/src/session.ts +34 -31
- package/src/stores.ts +1 -1
- package/src/suppress-file.ts +68 -5
- package/src/suppress-inline.ts +17 -7
- package/src/suppress.ts +79 -47
- package/src/template-field.ts +1 -1
- package/src/unimplemented-callees.ts +411 -0
- package/src/value-reach.ts +12 -8
- package/src/verbs/analyze.ts +15 -9
- package/src/verbs/verb.ts +1 -4
- package/src/workspace-programs.ts +2 -9
- package/src/workspace-resolution.ts +0 -3
- package/src/workspace-run.ts +1 -1
- package/src/workspace.ts +1 -9
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { REPOSITORY_DOCUMENT } from "../config.js";
|
|
2
|
-
import {
|
|
2
|
+
import { FIXED_SEVERITY_CODES } from "../kinds.js";
|
|
3
|
+
import { isDependencyCode, isUnsuppressibleCode, STALE_SUPPRESSION, SUPPRESSION_WITHOUT_REASON, } from "../suppress.js";
|
|
3
4
|
/** The code a configured root that matches nothing is reported under. */
|
|
4
5
|
const UNMATCHED_ROOT = "DS1704";
|
|
5
6
|
/** The code a configured declaration that names nothing is reported under. */
|
|
@@ -12,13 +13,6 @@ const SUPPRESSION_SUBJECT = "suppression";
|
|
|
12
13
|
const ROOT_SUBJECT = "root";
|
|
13
14
|
/** The fixed position of a finding about a document rather than about a line of one. */
|
|
14
15
|
const DOCUMENT_POSITION = 1;
|
|
15
|
-
/** A run with nothing for the self-check family to report. */
|
|
16
|
-
export const NO_SELF_CHECK = {
|
|
17
|
-
refusals: [],
|
|
18
|
-
unmatchedRoots: [],
|
|
19
|
-
rootsDocument: "",
|
|
20
|
-
unmatchedDeclarations: [],
|
|
21
|
-
};
|
|
22
16
|
/** The words a message names a mechanism by. */
|
|
23
17
|
const WRITTEN = {
|
|
24
18
|
inline: "inline directive",
|
|
@@ -68,7 +62,7 @@ function suppressionRef(symbol, path, files) {
|
|
|
68
62
|
return symbol === "" ? (files(path) ?? path) : symbol;
|
|
69
63
|
}
|
|
70
64
|
/** One finding per refused suppression: the grammar's reason refusal and its scope refusal. */
|
|
71
|
-
|
|
65
|
+
function refusedSuppressions(refusals, files) {
|
|
72
66
|
return refusals.map((refused) => aboutSuppression(refused.reported, suppressionRef(refused.symbol, refused.path, files), refused.site, `${WRITTEN[refused.mechanism]} for ${refused.code} ${refused.reported === SUPPRESSION_WITHOUT_REASON
|
|
73
67
|
? "carries no reason"
|
|
74
68
|
: "names a symbol and no path"}`, { mechanism: refused.mechanism, entry: writtenEntry(refused) }));
|
|
@@ -93,7 +87,30 @@ export function staleSuppressions(stale, files) {
|
|
|
93
87
|
const reports = [...new Set(records.flatMap((one) => one.reports))]
|
|
94
88
|
.filter((code) => !codes.includes(code))
|
|
95
89
|
.sort();
|
|
96
|
-
const
|
|
90
|
+
const rows = codes.filter(isUnsuppressibleCode);
|
|
91
|
+
const dependencies = first.mechanism === "inline" ? codes.filter(isDependencyCode) : [];
|
|
92
|
+
const settable = rows.filter((code) => !FIXED_SEVERITY_CODES.includes(code));
|
|
93
|
+
const remedy = settable.length === 0
|
|
94
|
+
? "fix what the finding names instead"
|
|
95
|
+
: `fix what the finding names or set the severity of ${spelled(settable)} instead`;
|
|
96
|
+
const declared = reports.length === 0 ? "" : `its declaration reports ${spelled(reports)}`;
|
|
97
|
+
const because = [
|
|
98
|
+
...(rows.length > 0
|
|
99
|
+
? [
|
|
100
|
+
`${spelled(rows)} ${rows.length === 1 ? "reports a row" : "report rows"} of a document, which no suppression can name; ${remedy}`,
|
|
101
|
+
]
|
|
102
|
+
: []),
|
|
103
|
+
...(dependencies.length > 0
|
|
104
|
+
? [
|
|
105
|
+
`${spelled(dependencies)} ${dependencies.length === 1 ? "reports a dependency row" : "report dependency rows"}, which only an ignore entry or a baseline row can name`,
|
|
106
|
+
]
|
|
107
|
+
: []),
|
|
108
|
+
];
|
|
109
|
+
const instead = because.length > 0
|
|
110
|
+
? `, because ${because.join("; ")}${declared === "" ? "" : `; ${declared}`}`
|
|
111
|
+
: declared === ""
|
|
112
|
+
? ""
|
|
113
|
+
: `, and ${declared}`;
|
|
97
114
|
return {
|
|
98
115
|
code: STALE_SUPPRESSION,
|
|
99
116
|
mechanism: first.mechanism,
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { fails } from "./config.js";
|
|
7
7
|
import { decidedFindings } from "./findings/emitters.js";
|
|
8
8
|
import { fileRefsOf, staleSuppressions, } from "./findings/self-check.js";
|
|
9
|
-
import { isRowSubject, pairs, suppressionCounts } from "./suppress.js";
|
|
9
|
+
import { isDependencySubject, isRowSubject, pairs, suppressionCounts, } from "./suppress.js";
|
|
10
10
|
import { EXIT_CLEAN, EXIT_FINDINGS } from "./verbs/verb.js";
|
|
11
11
|
/** The exit code of a report holding at least one pending finding. */
|
|
12
12
|
export const EXIT_PENDING = 4;
|
|
@@ -78,14 +78,15 @@ export function verdictOf(result, config) {
|
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
80
|
* The rows one round of a baseline write records from its findings, in their order. A
|
|
81
|
-
* finding about a row of a document is not recorded: no record
|
|
82
|
-
* recording it would be stale on the first read back. Two findings
|
|
83
|
-
* same row, two parts of one declaration under one code, record it
|
|
81
|
+
* finding about a row of a document other than a dependency row is not recorded: no record
|
|
82
|
+
* withholds one, so a row recording it would be stale on the first read back. Two findings
|
|
83
|
+
* that would record the same row, two parts of one declaration under one code, record it
|
|
84
|
+
* once.
|
|
84
85
|
*/
|
|
85
86
|
export function recordedFindings(findings) {
|
|
86
87
|
const rows = new Map();
|
|
87
88
|
for (const finding of findings) {
|
|
88
|
-
if (isRowSubject(finding.symbol.kind)) {
|
|
89
|
+
if (isRowSubject(finding.symbol.kind) && !isDependencySubject(finding.symbol.kind)) {
|
|
89
90
|
continue;
|
|
90
91
|
}
|
|
91
92
|
const row = { code: finding.code, symbol: finding.symbol.ref, path: finding.position.path };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The property names `in` tests in a project's production files. A value narrowed by
|
|
3
|
+
* `"name" in value` is read back by that name, often through `unknown`, where no type
|
|
4
|
+
* links the read to a declaration, so only a text match can tell which member it reads.
|
|
5
|
+
*/
|
|
6
|
+
import { isBinaryExpression, isNoSubstitutionTemplateLiteral, isStringLiteral, SyntaxKind, } from "@typescript/native/unstable/ast";
|
|
7
|
+
import { sourceFilesOf } from "./source-files.js";
|
|
8
|
+
/**
|
|
9
|
+
* The text of each string literal, or template literal with no substitution, written as
|
|
10
|
+
* the left operand of an `in` expression in a file of the project that is no test file.
|
|
11
|
+
* `testFiles` holds the test files by their path below the target root.
|
|
12
|
+
*/
|
|
13
|
+
export function inOperandNames(project, targetRoot, testFiles) {
|
|
14
|
+
const tests = new Set(testFiles);
|
|
15
|
+
const found = new Set();
|
|
16
|
+
const visit = (node) => {
|
|
17
|
+
if (isBinaryExpression(node) &&
|
|
18
|
+
node.operatorToken.kind === SyntaxKind.InKeyword &&
|
|
19
|
+
(isStringLiteral(node.left) || isNoSubstitutionTemplateLiteral(node.left))) {
|
|
20
|
+
found.add(node.left.text);
|
|
21
|
+
}
|
|
22
|
+
node.forEachChild(visit);
|
|
23
|
+
};
|
|
24
|
+
for (const [path, file] of sourceFilesOf(project, targetRoot).byPath) {
|
|
25
|
+
if (!tests.has(path)) {
|
|
26
|
+
file.forEachChild(visit);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return [...found].sort();
|
|
30
|
+
}
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* that parameter, and a call of a method of a generic receiver fixes the receiver's by its
|
|
5
5
|
* type arguments. The type query of a type-query alias is no reference.
|
|
6
6
|
*/
|
|
7
|
-
import { isArrowFunction, isCallExpression, isClassDeclaration, isClassExpression, isFunctionDeclaration, isFunctionExpression,
|
|
8
|
-
import { SymbolFlags } from "@typescript/native/unstable/sync";
|
|
7
|
+
import { isArrowFunction, isBindingElement, isCallExpression, isClassDeclaration, isClassExpression, isFunctionDeclaration, isFunctionExpression, isFunctionTypeNode, isIdentifier, isIndexedAccessTypeNode, isInterfaceDeclaration, isLiteralTypeNode, isMappedTypeNode, isMethodDeclaration, isMethodSignatureDeclaration, isNewExpression, isNoSubstitutionTemplateLiteral, isNumericLiteral, isObjectBindingPattern, isParenthesizedTypeNode, isPropertyAccessExpression, isPropertySignatureDeclaration, isStringLiteral, isTypeAliasDeclaration, isTypeOperatorNode, isTypeReferenceNode, isUnionTypeNode, isVariableDeclaration, SyntaxKind, } from "@typescript/native/unstable/ast";
|
|
8
|
+
import { SignatureKind, SymbolFlags, } from "@typescript/native/unstable/sync";
|
|
9
9
|
import { nodeKey } from "./inventory.js";
|
|
10
10
|
import { renderPosition } from "./position.js";
|
|
11
11
|
import { UNANSWERED } from "./query.js";
|
|
@@ -16,6 +16,7 @@ function isGeneric(node) {
|
|
|
16
16
|
isMethodSignatureDeclaration(node) ||
|
|
17
17
|
isArrowFunction(node) ||
|
|
18
18
|
isFunctionExpression(node) ||
|
|
19
|
+
isFunctionTypeNode(node) ||
|
|
19
20
|
isTypeAliasDeclaration(node) ||
|
|
20
21
|
isInterfaceDeclaration(node) ||
|
|
21
22
|
isClassDeclaration(node) ||
|
|
@@ -26,6 +27,29 @@ function isGeneric(node) {
|
|
|
26
27
|
function isReceiver(node) {
|
|
27
28
|
return isInterfaceDeclaration(node) || isClassDeclaration(node) || isClassExpression(node);
|
|
28
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* The class or the interface one member function belongs to: a method's parent, or for a
|
|
32
|
+
* function type a property declares, the declaration that holds the property.
|
|
33
|
+
*/
|
|
34
|
+
function receiverOf(member) {
|
|
35
|
+
const parent = member.parent;
|
|
36
|
+
if (isFunctionTypeNode(member) &&
|
|
37
|
+
parent !== undefined &&
|
|
38
|
+
isPropertySignatureDeclaration(parent)) {
|
|
39
|
+
return parent.parent;
|
|
40
|
+
}
|
|
41
|
+
return parent;
|
|
42
|
+
}
|
|
43
|
+
/** Whether a member function of a generic receiver declares no type parameter of its own. */
|
|
44
|
+
function isReceiverMember(node) {
|
|
45
|
+
if (!(isMethodDeclaration(node) ||
|
|
46
|
+
isMethodSignatureDeclaration(node) ||
|
|
47
|
+
isFunctionTypeNode(node))) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
const receiver = receiverOf(node);
|
|
51
|
+
return receiver !== undefined && isReceiver(receiver) && isGeneric(receiver);
|
|
52
|
+
}
|
|
29
53
|
function unwrappedType(node) {
|
|
30
54
|
return isParenthesizedTypeNode(node) ? unwrappedType(node.type) : node;
|
|
31
55
|
}
|
|
@@ -86,6 +110,28 @@ function namedParameter(node, enclosing) {
|
|
|
86
110
|
}
|
|
87
111
|
return undefined;
|
|
88
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* The constraint of the mapped type whose type parameter one type node names whole: a
|
|
115
|
+
* mapped type instantiates what it holds once per constituent of that constraint.
|
|
116
|
+
*/
|
|
117
|
+
function mappedConstraint(node) {
|
|
118
|
+
const type = unwrappedType(node);
|
|
119
|
+
if (!isTypeReferenceNode(type) ||
|
|
120
|
+
!isIdentifier(type.typeName) ||
|
|
121
|
+
type.typeArguments !== undefined) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
const spelled = type.typeName.text;
|
|
125
|
+
for (let at = type.parent; at !== undefined; at = at.parent) {
|
|
126
|
+
if (isMappedTypeNode(at)) {
|
|
127
|
+
const { name, constraint } = at.typeParameter;
|
|
128
|
+
if (isIdentifier(name) && name.text === spelled) {
|
|
129
|
+
return constraint;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
89
135
|
/** The name a site writes to reach one generic: its own, or its variable's. */
|
|
90
136
|
function calledName(generic) {
|
|
91
137
|
const own = generic.name;
|
|
@@ -93,15 +139,23 @@ function calledName(generic) {
|
|
|
93
139
|
return own.text;
|
|
94
140
|
}
|
|
95
141
|
const parent = generic.parent;
|
|
96
|
-
return parent !== undefined &&
|
|
142
|
+
return parent !== undefined &&
|
|
143
|
+
(isVariableDeclaration(parent) || isPropertySignatureDeclaration(parent)) &&
|
|
144
|
+
isIdentifier(parent.name)
|
|
97
145
|
? parent.name.text
|
|
98
146
|
: undefined;
|
|
99
147
|
}
|
|
100
148
|
/** The generic a declaration a symbol names is: the function a variable is initialized with included. */
|
|
101
149
|
function genericOf(node) {
|
|
102
|
-
if (isGeneric(node)) {
|
|
150
|
+
if (isGeneric(node) || isReceiverMember(node)) {
|
|
103
151
|
return node;
|
|
104
152
|
}
|
|
153
|
+
if (isPropertySignatureDeclaration(node)) {
|
|
154
|
+
// The typings mark the annotation present; a signature written without one has none.
|
|
155
|
+
const declared = node.type;
|
|
156
|
+
const type = declared === undefined ? undefined : unwrappedType(declared);
|
|
157
|
+
return type !== undefined && isFunctionTypeNode(type) ? genericOf(type) : undefined;
|
|
158
|
+
}
|
|
105
159
|
if (isVariableDeclaration(node) &&
|
|
106
160
|
node.initializer !== undefined &&
|
|
107
161
|
isGeneric(node.initializer)) {
|
|
@@ -109,29 +163,60 @@ function genericOf(node) {
|
|
|
109
163
|
}
|
|
110
164
|
return undefined;
|
|
111
165
|
}
|
|
166
|
+
/** The operand of each `keyof` one type node holds, through every type node it is built of. */
|
|
167
|
+
function keyofOperands(node) {
|
|
168
|
+
const found = [];
|
|
169
|
+
const visit = (at) => {
|
|
170
|
+
if (isTypeOperatorNode(at) && at.operator === SyntaxKind.KeyOfKeyword) {
|
|
171
|
+
found.push(at.type);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
at.forEachChild(visit);
|
|
175
|
+
};
|
|
176
|
+
visit(node);
|
|
177
|
+
return found;
|
|
178
|
+
}
|
|
112
179
|
/**
|
|
113
180
|
* Each indexed access inside one generic whose index names a type parameter of it, read
|
|
114
181
|
* from its own text: the object side is a type parameter of the same generic, one of the
|
|
115
|
-
* receiver it is a
|
|
182
|
+
* receiver it is a member of, or a type it writes. A type parameter whose constraint holds
|
|
183
|
+
* `keyof X` is an index into `X` as `X[K]` is, and so is a parameter whose declared type
|
|
184
|
+
* holds `keyof X`, indexed by the argument a call passes it.
|
|
116
185
|
*/
|
|
117
186
|
function carriedIn(generic) {
|
|
118
187
|
const found = [];
|
|
119
|
-
const receiver = generic
|
|
188
|
+
const receiver = receiverOf(generic);
|
|
120
189
|
const outer = receiver !== undefined && isReceiver(receiver) && isGeneric(receiver) ? receiver : undefined;
|
|
190
|
+
const sideOf = (object) => {
|
|
191
|
+
const own = namedParameter(object, [generic]);
|
|
192
|
+
const fromReceiver = own === undefined && outer !== undefined ? namedParameter(object, [outer]) : undefined;
|
|
193
|
+
return own !== undefined
|
|
194
|
+
? { kind: "parameter", index: own.index }
|
|
195
|
+
: fromReceiver !== undefined
|
|
196
|
+
? { kind: "receiver", index: fromReceiver.index }
|
|
197
|
+
: { kind: "written", node: object };
|
|
198
|
+
};
|
|
199
|
+
(generic.typeParameters ?? []).forEach((parameter, index) => {
|
|
200
|
+
const constraint = parameter.constraint;
|
|
201
|
+
for (const object of constraint === undefined ? [] : keyofOperands(constraint)) {
|
|
202
|
+
found.push({ object: sideOf(object), index: { kind: "parameter", index } });
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
(generic.parameters ?? []).forEach((parameter, index) => {
|
|
206
|
+
for (const object of parameter.type === undefined ? [] : keyofOperands(parameter.type)) {
|
|
207
|
+
if (namedParameter(object, [generic]) === undefined) {
|
|
208
|
+
found.push({ object: sideOf(object), index: { kind: "argument", index } });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
});
|
|
121
212
|
const visit = (node) => {
|
|
122
213
|
if (isIndexedAccessTypeNode(node)) {
|
|
123
214
|
const index = namedParameter(node.indexType, [generic]);
|
|
124
215
|
if (index !== undefined) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const object = own !== undefined
|
|
130
|
-
? { kind: "parameter", index: own.index }
|
|
131
|
-
: fromReceiver !== undefined
|
|
132
|
-
? { kind: "receiver", index: fromReceiver.index }
|
|
133
|
-
: { kind: "written", node: node.objectType };
|
|
134
|
-
found.push({ object, index: { kind: "parameter", index: index.index } });
|
|
216
|
+
found.push({
|
|
217
|
+
object: sideOf(node.objectType),
|
|
218
|
+
index: { kind: "parameter", index: index.index },
|
|
219
|
+
});
|
|
135
220
|
}
|
|
136
221
|
}
|
|
137
222
|
if (node !== generic && isGeneric(node)) {
|
|
@@ -153,8 +238,8 @@ export function indexedAccessReferences(project, held, chains, root, testFiles)
|
|
|
153
238
|
const carried = new Map();
|
|
154
239
|
const names = new Set();
|
|
155
240
|
const sites = [];
|
|
156
|
-
/** Calls passing
|
|
157
|
-
const
|
|
241
|
+
/** Calls passing an argument, which a generic declared outside the own files may fix. */
|
|
242
|
+
const argumentCalls = [];
|
|
158
243
|
const carry = (generic, one) => {
|
|
159
244
|
const known = carried.get(generic) ?? [];
|
|
160
245
|
const same = known.some((other) => JSON.stringify(sideKey(other)) === JSON.stringify(sideKey(one)));
|
|
@@ -174,7 +259,6 @@ export function indexedAccessReferences(project, held, chains, root, testFiles)
|
|
|
174
259
|
continue;
|
|
175
260
|
}
|
|
176
261
|
const aliasTypes = new Set(typeQueryAliases(file).map((alias) => alias.type));
|
|
177
|
-
const imported = packageBindings(file);
|
|
178
262
|
const visit = (node, from, enclosing) => {
|
|
179
263
|
const own = held.declarations.get(nodeKey(file, node));
|
|
180
264
|
const holder = own ?? from;
|
|
@@ -203,10 +287,8 @@ export function indexedAccessReferences(project, held, chains, root, testFiles)
|
|
|
203
287
|
if (name !== undefined) {
|
|
204
288
|
const site = { file, from: holder, node, name, enclosing: inside };
|
|
205
289
|
sites.push(site);
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
if (literal && isCallExpression(node) && head !== undefined && imported.has(head)) {
|
|
209
|
-
literalCalls.push(site);
|
|
290
|
+
if (isCallExpression(node) && node.arguments.length > 0) {
|
|
291
|
+
argumentCalls.push(site);
|
|
210
292
|
}
|
|
211
293
|
}
|
|
212
294
|
}
|
|
@@ -234,6 +316,47 @@ export function indexedAccessReferences(project, held, chains, root, testFiles)
|
|
|
234
316
|
return generic === undefined ? [] : [generic];
|
|
235
317
|
});
|
|
236
318
|
};
|
|
319
|
+
/**
|
|
320
|
+
* The type a call's callee is read off: the receiver of a property access, the value an
|
|
321
|
+
* object binding pattern destructures the callee from, or the value whose member a
|
|
322
|
+
* variable holding the callee is initialized with.
|
|
323
|
+
*/
|
|
324
|
+
const receiverTypeOf = (callee) => {
|
|
325
|
+
if (isPropertyAccessExpression(callee)) {
|
|
326
|
+
const type = queries.typeAt(callee.expression);
|
|
327
|
+
const args = type === UNANSWERED || type === undefined ? [] : queries.typeArguments(type);
|
|
328
|
+
if (type !== UNANSWERED && type !== undefined && args !== UNANSWERED && args.length > 0) {
|
|
329
|
+
return type;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
const name = isPropertyAccessExpression(callee) ? callee.name : callee;
|
|
333
|
+
const [named] = queries.symbolsAt([name]);
|
|
334
|
+
const symbol = named === UNANSWERED || named === undefined || (named.flags & SymbolFlags.Alias) === 0
|
|
335
|
+
? named
|
|
336
|
+
: queries.aliased(named);
|
|
337
|
+
if (symbol === UNANSWERED || symbol === undefined) {
|
|
338
|
+
return undefined;
|
|
339
|
+
}
|
|
340
|
+
for (const handle of symbol.declarations) {
|
|
341
|
+
const node = project.declarationAt(handle)?.node;
|
|
342
|
+
const holder = node !== undefined &&
|
|
343
|
+
isBindingElement(node) &&
|
|
344
|
+
isObjectBindingPattern(node.parent) &&
|
|
345
|
+
isVariableDeclaration(node.parent.parent)
|
|
346
|
+
? node.parent.parent.initializer
|
|
347
|
+
: node !== undefined &&
|
|
348
|
+
isVariableDeclaration(node) &&
|
|
349
|
+
node.initializer !== undefined &&
|
|
350
|
+
isPropertyAccessExpression(node.initializer)
|
|
351
|
+
? node.initializer.expression
|
|
352
|
+
: undefined;
|
|
353
|
+
const type = holder === undefined ? undefined : queries.typeAt(holder);
|
|
354
|
+
if (type !== UNANSWERED && type !== undefined) {
|
|
355
|
+
return type;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return undefined;
|
|
359
|
+
};
|
|
237
360
|
/** What one site fixes for one side of a carried access. */
|
|
238
361
|
const fixedAt = (site, side, generic) => {
|
|
239
362
|
if (side.kind === "written") {
|
|
@@ -242,23 +365,26 @@ export function indexedAccessReferences(project, held, chains, root, testFiles)
|
|
|
242
365
|
const { node } = site;
|
|
243
366
|
if (side.kind === "receiver") {
|
|
244
367
|
const callee = isCallExpression(node) ? node.expression : undefined;
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
}
|
|
248
|
-
const receiver = queries.typeAt(callee.expression);
|
|
249
|
-
if (receiver === UNANSWERED || receiver === undefined) {
|
|
368
|
+
const receiver = callee === undefined ? undefined : receiverTypeOf(callee);
|
|
369
|
+
if (receiver === undefined) {
|
|
250
370
|
return undefined;
|
|
251
371
|
}
|
|
252
372
|
const args = queries.typeArguments(receiver);
|
|
253
373
|
const type = args === UNANSWERED ? undefined : args[side.index];
|
|
254
374
|
return type === undefined ? undefined : { kind: "type", type };
|
|
255
375
|
}
|
|
376
|
+
if (side.kind === "argument") {
|
|
377
|
+
const argument = isTypeReferenceNode(node) ? undefined : node.arguments?.[side.index];
|
|
378
|
+
const type = argument === undefined ? undefined : queries.typeAt(argument);
|
|
379
|
+
return type === UNANSWERED || type === undefined ? undefined : { kind: "type", type };
|
|
380
|
+
}
|
|
256
381
|
const written = node.typeArguments?.[side.index];
|
|
257
382
|
if (written !== undefined) {
|
|
258
383
|
const passed = namedParameter(written, site.enclosing);
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
384
|
+
const mapped = passed === undefined ? mappedConstraint(written) : undefined;
|
|
385
|
+
return passed !== undefined
|
|
386
|
+
? { kind: "passed", generic: passed.generic, index: passed.index }
|
|
387
|
+
: { kind: "node", node: mapped ?? written };
|
|
262
388
|
}
|
|
263
389
|
if (isTypeReferenceNode(node)) {
|
|
264
390
|
return undefined;
|
|
@@ -352,40 +478,120 @@ export function indexedAccessReferences(project, held, chains, root, testFiles)
|
|
|
352
478
|
}
|
|
353
479
|
});
|
|
354
480
|
}
|
|
355
|
-
// A generic
|
|
356
|
-
//
|
|
357
|
-
|
|
358
|
-
const
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
481
|
+
// A generic declared outside the project's own files carries what its own text indexes
|
|
482
|
+
// by its type parameters and its receiver's. Only a call passing an argument the compiler
|
|
483
|
+
// types as a literal can fix an index, so only such a call's signature is resolved.
|
|
484
|
+
const outside = argumentCalls.filter((site) => !resolvedGenerics.has(site));
|
|
485
|
+
const argumentNodes = argumentCalls.flatMap((site) => [
|
|
486
|
+
...site.node.arguments,
|
|
487
|
+
]);
|
|
488
|
+
const argumentTypes = queries.typesAt(argumentNodes);
|
|
489
|
+
const literalArgument = new Set();
|
|
490
|
+
argumentTypes.forEach((type, at) => {
|
|
491
|
+
const node = argumentNodes[at];
|
|
492
|
+
if (node !== undefined &&
|
|
493
|
+
type !== UNANSWERED &&
|
|
494
|
+
type !== undefined &&
|
|
495
|
+
(literalTypeNames(type, partsOf)?.length ?? 0) > 0) {
|
|
496
|
+
literalArgument.add(node);
|
|
497
|
+
}
|
|
498
|
+
});
|
|
364
499
|
const own = project.ownPaths();
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
? named
|
|
369
|
-
: queries.aliased(named);
|
|
370
|
-
if (symbol === UNANSWERED ||
|
|
371
|
-
symbol === undefined ||
|
|
372
|
-
(symbol.flags & (SymbolFlags.Function | SymbolFlags.Method)) === 0) {
|
|
373
|
-
return;
|
|
500
|
+
for (const site of outside) {
|
|
501
|
+
if (!site.node.arguments.some((one) => literalArgument.has(one))) {
|
|
502
|
+
continue;
|
|
374
503
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
504
|
+
const resolved = queries.resolvedSignature(site.node);
|
|
505
|
+
const handle = resolved === UNANSWERED || resolved === undefined ? undefined : resolved.declaration;
|
|
506
|
+
if (handle === undefined || own.has(handle.path)) {
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
const node = project.declarationAt(handle)?.node;
|
|
510
|
+
const generic = node === undefined ? undefined : genericOf(node);
|
|
511
|
+
if (generic === undefined) {
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
for (const one of carriedIn(generic)) {
|
|
515
|
+
readAt(site, generic, one);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
for (const site of argumentCalls) {
|
|
519
|
+
if (site.node.arguments.some((one) => literalArgument.has(one))) {
|
|
520
|
+
accesses.push(...instantiatedAccesses(site));
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* The indexed accesses the generic signature a call instantiates holds, read through the
|
|
525
|
+
* checker: the signature its receiver's instantiation gives, so an object type its
|
|
526
|
+
* receiver fixes is concrete whatever declaration spells the receiver's type, a type
|
|
527
|
+
* literal a generic function returns among them. An index naming a type parameter is the
|
|
528
|
+
* literal type the call passes at each parameter of that type.
|
|
529
|
+
*/
|
|
530
|
+
function instantiatedAccesses(site) {
|
|
531
|
+
const resolved = queries.resolvedSignature(site.node);
|
|
532
|
+
const generic = resolved === UNANSWERED || resolved === undefined ? undefined : queries.genericOf(resolved);
|
|
533
|
+
if (resolved === UNANSWERED ||
|
|
534
|
+
resolved === undefined ||
|
|
535
|
+
generic === UNANSWERED ||
|
|
536
|
+
generic === undefined) {
|
|
537
|
+
return [];
|
|
538
|
+
}
|
|
539
|
+
const count = site.node.arguments.length;
|
|
540
|
+
const parameters = [];
|
|
541
|
+
for (let at = 0; at < count; at += 1) {
|
|
542
|
+
const type = queries.parameterType(generic, at);
|
|
543
|
+
if (type !== UNANSWERED && type !== undefined) {
|
|
544
|
+
parameters[at] = type;
|
|
378
545
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
546
|
+
}
|
|
547
|
+
const result = queries.returnTypeOf(generic);
|
|
548
|
+
const held = [
|
|
549
|
+
...parameters,
|
|
550
|
+
...(result === UNANSWERED || result === undefined ? [] : [result]),
|
|
551
|
+
];
|
|
552
|
+
// A parameter of a callback and a type argument, a rest tuple's among them, hold the
|
|
553
|
+
// accesses one level down: `fn: (payload: M[K]) => void` and `...[M[K]]`.
|
|
554
|
+
const nested = held.flatMap((type) => [
|
|
555
|
+
...partsOf(type),
|
|
556
|
+
...partsOf(type).flatMap((part) => {
|
|
557
|
+
const args = queries.typeArguments(part);
|
|
558
|
+
const calls = queries.signaturesOf(part, SignatureKind.Call);
|
|
559
|
+
return [
|
|
560
|
+
...(args === UNANSWERED ? [] : args),
|
|
561
|
+
...(calls === UNANSWERED ? [] : calls).flatMap((signature) => signature.parameters.flatMap((_parameter, index) => {
|
|
562
|
+
const one = queries.parameterType(signature, index);
|
|
563
|
+
return one === UNANSWERED || one === undefined ? [] : [one];
|
|
564
|
+
})),
|
|
565
|
+
];
|
|
566
|
+
}),
|
|
567
|
+
]);
|
|
568
|
+
const found = [];
|
|
569
|
+
for (const type of nested) {
|
|
570
|
+
const access = queries.indexedAccessOf(type);
|
|
571
|
+
if (access === UNANSWERED || access === undefined || access.object.isTypeParameter()) {
|
|
382
572
|
continue;
|
|
383
573
|
}
|
|
384
|
-
|
|
385
|
-
|
|
574
|
+
const names = access.index.isTypeParameter()
|
|
575
|
+
? parameters.flatMap((parameter, at) => {
|
|
576
|
+
if (parameter.id !== access.index.id) {
|
|
577
|
+
return [];
|
|
578
|
+
}
|
|
579
|
+
const passed = queries.typeAtPosition(resolved, at);
|
|
580
|
+
return passed === UNANSWERED ? [] : (literalTypeNames(passed, partsOf) ?? []);
|
|
581
|
+
})
|
|
582
|
+
: (literalTypeNames(access.index, partsOf) ?? []);
|
|
583
|
+
if (names.length > 0) {
|
|
584
|
+
found.push({
|
|
585
|
+
file: site.file,
|
|
586
|
+
from: site.from,
|
|
587
|
+
at: site.node,
|
|
588
|
+
object: access.object,
|
|
589
|
+
index: names,
|
|
590
|
+
});
|
|
386
591
|
}
|
|
387
592
|
}
|
|
388
|
-
|
|
593
|
+
return found;
|
|
594
|
+
}
|
|
389
595
|
const typeNodes = accesses.flatMap((one) => [
|
|
390
596
|
...("kind" in one.object ? [one.object] : []),
|
|
391
597
|
...(!Array.isArray(one.index) && "kind" in one.index ? [one.index] : []),
|
|
@@ -437,67 +643,6 @@ export function indexedAccessReferences(project, held, chains, root, testFiles)
|
|
|
437
643
|
}
|
|
438
644
|
return found;
|
|
439
645
|
}
|
|
440
|
-
/**
|
|
441
|
-
* The local names one file binds to a package: each name an import declaration with a
|
|
442
|
-
* bare specifier binds, and each variable initialized by a call or a construction whose
|
|
443
|
-
* callee such a name heads, read in source order.
|
|
444
|
-
*/
|
|
445
|
-
function packageBindings(file) {
|
|
446
|
-
const names = new Set();
|
|
447
|
-
for (const statement of file.statements) {
|
|
448
|
-
if (!isImportDeclaration(statement) ||
|
|
449
|
-
!isStringLiteral(statement.moduleSpecifier) ||
|
|
450
|
-
/^[./]/u.test(statement.moduleSpecifier.text)) {
|
|
451
|
-
continue;
|
|
452
|
-
}
|
|
453
|
-
const clause = statement.importClause;
|
|
454
|
-
if (clause?.name !== undefined) {
|
|
455
|
-
names.add(clause.name.text);
|
|
456
|
-
}
|
|
457
|
-
const bindings = clause?.namedBindings;
|
|
458
|
-
if (bindings !== undefined && isNamespaceImport(bindings)) {
|
|
459
|
-
names.add(bindings.name.text);
|
|
460
|
-
}
|
|
461
|
-
else if (bindings !== undefined && isNamedImports(bindings)) {
|
|
462
|
-
bindings.elements.forEach((element) => names.add(element.name.text));
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
if (names.size === 0) {
|
|
466
|
-
return names;
|
|
467
|
-
}
|
|
468
|
-
const visit = (node) => {
|
|
469
|
-
if (isVariableDeclaration(node) &&
|
|
470
|
-
isIdentifier(node.name) &&
|
|
471
|
-
node.initializer !== undefined &&
|
|
472
|
-
(isCallExpression(node.initializer) || isNewExpression(node.initializer))) {
|
|
473
|
-
const head = rootName(node.initializer.expression);
|
|
474
|
-
if (head !== undefined && names.has(head)) {
|
|
475
|
-
names.add(node.name.text);
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
node.forEachChild(visit);
|
|
479
|
-
};
|
|
480
|
-
visit(file);
|
|
481
|
-
return names;
|
|
482
|
-
}
|
|
483
|
-
/** The identifier an expression is headed by, through property accesses and calls. */
|
|
484
|
-
function rootName(expression) {
|
|
485
|
-
let at = expression;
|
|
486
|
-
for (;;) {
|
|
487
|
-
if (isIdentifier(at)) {
|
|
488
|
-
return at.text;
|
|
489
|
-
}
|
|
490
|
-
if (isPropertyAccessExpression(at) || isCallExpression(at) || isNewExpression(at)) {
|
|
491
|
-
at = at.expression;
|
|
492
|
-
continue;
|
|
493
|
-
}
|
|
494
|
-
if (isParenthesizedExpression(at) || isNonNullExpression(at)) {
|
|
495
|
-
at = at.expression;
|
|
496
|
-
continue;
|
|
497
|
-
}
|
|
498
|
-
return undefined;
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
646
|
/** What makes two carried accesses one: the kind and position of each side, or its written node. */
|
|
502
647
|
function sideKey(one) {
|
|
503
648
|
const key = (side) => side.kind === "written" ? ["written", side.node.pos, side.node.end] : [side.kind, side.index];
|