fallow-type-aware 3.42.0 → 3.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/package.json +1 -1
- package/src/accessor-direction.mjs +113 -0
- package/src/call-evidence.mjs +1493 -0
- package/src/cli.mjs +16 -2
- package/src/declaration-facts.mjs +27 -0
- package/src/file-identity.mjs +18 -2
- package/src/generated-protocol.mjs +2 -0
- package/src/mutant-typecheck.mjs +2 -2
- package/src/optional-chain-proof.mjs +107 -82
- package/src/project-state.mjs +28 -1
- package/src/protocol.mjs +106 -0
- package/src/reference-names.mjs +97 -22
- package/src/semantic-identity.mjs +32 -3
- package/src/semantic.mjs +7 -20
package/src/cli.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import path from "node:path";
|
|
|
2
2
|
import { createInterface } from "node:readline";
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
+
CALL_EVIDENCE_OPERATION,
|
|
5
6
|
MUTANT_TYPECHECK_OPERATION,
|
|
6
7
|
SESSION_ENVELOPE_TYPES,
|
|
7
8
|
STATUS_OPERATION,
|
|
@@ -9,7 +10,9 @@ import {
|
|
|
9
10
|
} from "./generated-protocol.mjs";
|
|
10
11
|
import { analyzeSemanticQueries, createSemanticSession } from "./semantic.mjs";
|
|
11
12
|
import { checkMutants } from "./mutant-typecheck.mjs";
|
|
13
|
+
import { collectCallEvidence } from "./call-evidence.mjs";
|
|
12
14
|
import {
|
|
15
|
+
createCallEvidenceResponse,
|
|
13
16
|
createMutantTypecheckResponse,
|
|
14
17
|
createSemanticResponse,
|
|
15
18
|
createStatusResponse,
|
|
@@ -98,8 +101,11 @@ const runSession = async (input, output) => {
|
|
|
98
101
|
throw new Error("invalid semantic session envelope");
|
|
99
102
|
}
|
|
100
103
|
const request = parseRequest(envelope.request);
|
|
101
|
-
if (
|
|
102
|
-
|
|
104
|
+
if (
|
|
105
|
+
request.operation === MUTANT_TYPECHECK_OPERATION ||
|
|
106
|
+
request.operation === CALL_EVIDENCE_OPERATION
|
|
107
|
+
) {
|
|
108
|
+
throw new Error(`a semantic session does not accept a ${request.operation} request`);
|
|
103
109
|
}
|
|
104
110
|
if (!session) {
|
|
105
111
|
root = request.root;
|
|
@@ -163,6 +169,14 @@ export const run = async ({ input, output, args = [] }) => {
|
|
|
163
169
|
);
|
|
164
170
|
return;
|
|
165
171
|
}
|
|
172
|
+
if (request.operation === CALL_EVIDENCE_OPERATION) {
|
|
173
|
+
const evidence = collectCallEvidence(request);
|
|
174
|
+
writeJson(
|
|
175
|
+
output,
|
|
176
|
+
createCallEvidenceResponse({ ...evidence, elapsedMs: performance.now() - startedAt }),
|
|
177
|
+
);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
166
180
|
const result = analyzeSemanticQueries(request);
|
|
167
181
|
writeJson(output, responseFor(result, performance.now() - startedAt));
|
|
168
182
|
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Syntactic facts about a declaration that the type proofs share.
|
|
2
|
+
|
|
3
|
+
import { SyntaxKind } from "./backend.mjs";
|
|
4
|
+
|
|
5
|
+
const hasModifier = (node, kind) => node.modifiers?.some((modifier) => modifier.kind === kind);
|
|
6
|
+
|
|
7
|
+
/** True when a direct child of `node` is a token of `kind`, such as `!`. */
|
|
8
|
+
export const hasToken = (node, kind) => {
|
|
9
|
+
let found = false;
|
|
10
|
+
node.forEachChild((child) => {
|
|
11
|
+
found = found || child.kind === kind;
|
|
12
|
+
return found ? true : undefined;
|
|
13
|
+
});
|
|
14
|
+
return found;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* True for a `declare` declaration, a member of one, or anything in a
|
|
19
|
+
* declaration file: their types are claims that no code in the project checks.
|
|
20
|
+
*/
|
|
21
|
+
export const isAmbient = (node) => {
|
|
22
|
+
for (let current = node; current; current = current.parent) {
|
|
23
|
+
if (current.isDeclarationFile === true) return true;
|
|
24
|
+
if (hasModifier(current, SyntaxKind.DeclareKeyword)) return true;
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
};
|
package/src/file-identity.mjs
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
1
|
import { realpathSync } from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
// One request resolves the same file many times, and each resolution is a
|
|
5
|
+
// system call. The cache lives for one request, so a symlink that changes
|
|
6
|
+
// between session requests resolves again.
|
|
7
|
+
const identities = new Map();
|
|
8
|
+
|
|
9
|
+
/** Forgets the resolved file identities. Each semantic analysis calls it at its start. */
|
|
10
|
+
export const resetFileIdentityCache = () => identities.clear();
|
|
11
|
+
|
|
12
|
+
const resolveIdentity = (absolutePath) => {
|
|
6
13
|
try {
|
|
7
14
|
return path.normalize(realpathSync.native(absolutePath));
|
|
8
15
|
} catch {
|
|
9
16
|
return absolutePath;
|
|
10
17
|
}
|
|
11
18
|
};
|
|
19
|
+
|
|
20
|
+
export const canonicalFileIdentity = (fileName) => {
|
|
21
|
+
const absolutePath = path.normalize(path.resolve(fileName));
|
|
22
|
+
const cached = identities.get(absolutePath);
|
|
23
|
+
if (cached !== undefined) return cached;
|
|
24
|
+
const identity = resolveIdentity(absolutePath);
|
|
25
|
+
identities.set(absolutePath, identity);
|
|
26
|
+
return identity;
|
|
27
|
+
};
|
|
@@ -6,6 +6,7 @@ export const TYPE_AWARE_PROTOCOL = Object.freeze({
|
|
|
6
6
|
analysis_operation: "semantic-queries",
|
|
7
7
|
status_operation: "status",
|
|
8
8
|
mutant_typecheck_operation: "mutant-typecheck",
|
|
9
|
+
call_evidence_operation: "call-evidence",
|
|
9
10
|
query_operations: ["symbol-use", "symbol-trace", "api-surface", "symbol-impact", "type-coupling"],
|
|
10
11
|
session_envelope_types: ["analyze", "shutdown"],
|
|
11
12
|
backend: {
|
|
@@ -34,6 +35,7 @@ export const SEMANTIC_SCHEMA_VERSION = TYPE_AWARE_PROTOCOL.semantic_schema_versi
|
|
|
34
35
|
export const ANALYSIS_OPERATION = TYPE_AWARE_PROTOCOL.analysis_operation;
|
|
35
36
|
export const STATUS_OPERATION = TYPE_AWARE_PROTOCOL.status_operation;
|
|
36
37
|
export const MUTANT_TYPECHECK_OPERATION = TYPE_AWARE_PROTOCOL.mutant_typecheck_operation;
|
|
38
|
+
export const CALL_EVIDENCE_OPERATION = TYPE_AWARE_PROTOCOL.call_evidence_operation;
|
|
37
39
|
export const QUERY_OPERATIONS = Object.freeze(TYPE_AWARE_PROTOCOL.query_operations);
|
|
38
40
|
export const SESSION_ENVELOPE_TYPES = Object.freeze(TYPE_AWARE_PROTOCOL.session_envelope_types);
|
|
39
41
|
export const BACKEND_FAMILY = TYPE_AWARE_PROTOCOL.backend.family;
|
package/src/mutant-typecheck.mjs
CHANGED
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
} from "./backend.mjs";
|
|
20
20
|
|
|
21
21
|
import { canonicalFileIdentity } from "./file-identity.mjs";
|
|
22
|
-
import { NON_NULLISH_HINT,
|
|
22
|
+
import { NON_NULLISH_HINT, nonNullishMutants } from "./optional-chain-proof.mjs";
|
|
23
23
|
|
|
24
24
|
const { DiagnosticCategory } = sync;
|
|
25
25
|
|
|
@@ -154,7 +154,7 @@ const checkInProject = (context, file, original, config) => {
|
|
|
154
154
|
const reason = projectSkipReason(project, file, original);
|
|
155
155
|
if (reason !== null) return unchecked(file, reason);
|
|
156
156
|
check.baseline = countKeys(fileErrors(project, file.absolutePath));
|
|
157
|
-
check.hinted =
|
|
157
|
+
check.hinted = nonNullishMutants(project, file, original);
|
|
158
158
|
return file.mutants.map((mutant) => checkMutant(context, check, mutant));
|
|
159
159
|
} finally {
|
|
160
160
|
context.overlay.delete(check.identity);
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
// Finds optional-chaining mutants that the types suggest
|
|
2
|
-
// original code. The result is an advisory hint, never a
|
|
3
|
-
// keeps the status `survived`, because a type does not
|
|
1
|
+
// Finds optional-chaining and nullish-fallback mutants that the types suggest
|
|
2
|
+
// are equivalent to the original code. The result is an advisory hint, never a
|
|
3
|
+
// verdict: the mutant keeps the status `survived`, because a type does not
|
|
4
|
+
// prove a run-time value.
|
|
4
5
|
//
|
|
5
|
-
// The mutants `a?.b` -> `a.b`, `a?.()` -> `a()
|
|
6
|
-
// nothing when the receiver `a` is never `null` or
|
|
6
|
+
// The mutants `a?.b` -> `a.b`, `a?.()` -> `a()`, `a?.[k]` -> `a[k]` and
|
|
7
|
+
// `a ?? b` -> `a` change nothing when the receiver `a` is never `null` or
|
|
8
|
+
// `undefined` at run time.
|
|
7
9
|
// The proof trusts declarations and never trusts flow analysis or assertions:
|
|
8
10
|
//
|
|
9
11
|
// - It reads the declared type of the root binding or property, not the type
|
|
@@ -27,6 +29,8 @@
|
|
|
27
29
|
import path from "node:path";
|
|
28
30
|
|
|
29
31
|
import { ast, NodeFlags, projectCompilerOptions, sync, SyntaxKind } from "./backend.mjs";
|
|
32
|
+
import { targetDirection } from "./accessor-direction.mjs";
|
|
33
|
+
import { hasToken, isAmbient } from "./declaration-facts.mjs";
|
|
30
34
|
import { declarationsForSymbol } from "./semantic-identity.mjs";
|
|
31
35
|
|
|
32
36
|
const { TypeFlags, SymbolFlags } = sync;
|
|
@@ -61,6 +65,7 @@ const {
|
|
|
61
65
|
export const NON_NULLISH_HINT = "type-non-nullish";
|
|
62
66
|
|
|
63
67
|
const OPTIONAL_TOKEN = "?.";
|
|
68
|
+
const COALESCE_TOKEN = "??";
|
|
64
69
|
const TYPESCRIPT_EXTENSIONS = new Set([".ts", ".tsx", ".mts", ".cts"]);
|
|
65
70
|
const MAX_UNION_DEPTH = 8;
|
|
66
71
|
const MAX_BINDING_DEPTH = 8;
|
|
@@ -92,6 +97,39 @@ const neverNullish = (type, depth) => {
|
|
|
92
97
|
const isParameter = (node) => node.kind === SyntaxKind.Parameter;
|
|
93
98
|
const isPropertySignature = (node) => node.kind === SyntaxKind.PropertySignature;
|
|
94
99
|
|
|
100
|
+
// True for an optional chain: `a?.b`, `a?.()`, `a?.[k]`, or a longer chain
|
|
101
|
+
// after such a link, such as `a?.b.c()`. A chain gives `undefined` when a link
|
|
102
|
+
// short-circuits, whatever the declared type of its last link. A later link
|
|
103
|
+
// of the same chain is skipped too, so the receiver of an optional link can
|
|
104
|
+
// be a chain. The left operand of `??` is a value outside the chain.
|
|
105
|
+
const isOptionalChain = (node) => {
|
|
106
|
+
for (let current = node; current !== undefined; current = current.expression) {
|
|
107
|
+
const isLink =
|
|
108
|
+
isPropertyAccessExpression(current) ||
|
|
109
|
+
isElementAccessExpression(current) ||
|
|
110
|
+
isCallExpression(current) ||
|
|
111
|
+
isNonNullExpression(current);
|
|
112
|
+
if (!isLink) return false;
|
|
113
|
+
if (current.questionDotToken !== undefined) return true;
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const withoutParentheses = (node) => {
|
|
119
|
+
let current = node;
|
|
120
|
+
while (isParenthesizedExpression(current) || isSatisfiesExpression(current)) {
|
|
121
|
+
current = current.expression;
|
|
122
|
+
}
|
|
123
|
+
return current;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// The declared type of a value outside its chain. An optional chain can give
|
|
127
|
+
// `undefined` there, so it has no proven type.
|
|
128
|
+
const settledType = (project, expression, context) =>
|
|
129
|
+
isOptionalChain(withoutParentheses(expression))
|
|
130
|
+
? null
|
|
131
|
+
: declaredType(project, expression, context);
|
|
132
|
+
|
|
95
133
|
const isAssertion = (node) =>
|
|
96
134
|
isAsExpression(node) || isTypeAssertion(node) || isNonNullExpression(node);
|
|
97
135
|
|
|
@@ -107,25 +145,6 @@ const containsAssertion = (node) => {
|
|
|
107
145
|
|
|
108
146
|
const hasModifier = (node, kind) => node.modifiers?.some((modifier) => modifier.kind === kind);
|
|
109
147
|
|
|
110
|
-
const hasToken = (node, kind) => {
|
|
111
|
-
let found = false;
|
|
112
|
-
node.forEachChild((child) => {
|
|
113
|
-
found = found || child.kind === kind;
|
|
114
|
-
return found ? true : undefined;
|
|
115
|
-
});
|
|
116
|
-
return found;
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
// True for a `declare` declaration, a member of one, or anything in a
|
|
120
|
-
// declaration file: their types are claims that no code in the project checks.
|
|
121
|
-
const isAmbient = (node) => {
|
|
122
|
-
for (let current = node; current; current = current.parent) {
|
|
123
|
-
if (current.isDeclarationFile === true) return true;
|
|
124
|
-
if (hasModifier(current, SyntaxKind.DeclareKeyword)) return true;
|
|
125
|
-
}
|
|
126
|
-
return false;
|
|
127
|
-
};
|
|
128
|
-
|
|
129
148
|
const typeAt = (project, declaration, symbol) =>
|
|
130
149
|
project.checker.getTypeOfSymbolAtLocation(symbol, declaration);
|
|
131
150
|
|
|
@@ -191,53 +210,7 @@ const uniqueDeclaration = (project, symbol) => {
|
|
|
191
210
|
return declarations.length === 1 ? declarations[0] : null;
|
|
192
211
|
};
|
|
193
212
|
|
|
194
|
-
|
|
195
|
-
// `[a] = xs`, `({ a } = o)`, `({ k: a } = o)`, `(a as T) = v`.
|
|
196
|
-
const TARGET_WRAPPERS = new Set([
|
|
197
|
-
SyntaxKind.ParenthesizedExpression,
|
|
198
|
-
SyntaxKind.ArrayLiteralExpression,
|
|
199
|
-
SyntaxKind.ObjectLiteralExpression,
|
|
200
|
-
SyntaxKind.ShorthandPropertyAssignment,
|
|
201
|
-
SyntaxKind.SpreadElement,
|
|
202
|
-
SyntaxKind.SpreadAssignment,
|
|
203
|
-
SyntaxKind.AsExpression,
|
|
204
|
-
SyntaxKind.TypeAssertionExpression,
|
|
205
|
-
SyntaxKind.NonNullExpression,
|
|
206
|
-
SyntaxKind.SatisfiesExpression,
|
|
207
|
-
]);
|
|
208
|
-
|
|
209
|
-
const isAssignmentOperator = (kind) =>
|
|
210
|
-
kind >= SyntaxKind.FirstAssignment && kind <= SyntaxKind.LastAssignment;
|
|
211
|
-
|
|
212
|
-
const isIncrementOperator = (kind) =>
|
|
213
|
-
kind === SyntaxKind.PlusPlusToken || kind === SyntaxKind.MinusMinusToken;
|
|
214
|
-
|
|
215
|
-
const isWriteTarget = (identifier) => {
|
|
216
|
-
let node = identifier;
|
|
217
|
-
for (let parent = node.parent; parent; parent = node.parent) {
|
|
218
|
-
if (TARGET_WRAPPERS.has(parent.kind)) {
|
|
219
|
-
node = parent;
|
|
220
|
-
continue;
|
|
221
|
-
}
|
|
222
|
-
switch (parent.kind) {
|
|
223
|
-
case SyntaxKind.PropertyAssignment:
|
|
224
|
-
if (parent.initializer !== node) return false;
|
|
225
|
-
node = parent;
|
|
226
|
-
continue;
|
|
227
|
-
case SyntaxKind.BinaryExpression:
|
|
228
|
-
return parent.left === node && isAssignmentOperator(parent.operatorToken.kind);
|
|
229
|
-
case SyntaxKind.PrefixUnaryExpression:
|
|
230
|
-
case SyntaxKind.PostfixUnaryExpression:
|
|
231
|
-
return isIncrementOperator(parent.operator);
|
|
232
|
-
case SyntaxKind.ForInStatement:
|
|
233
|
-
case SyntaxKind.ForOfStatement:
|
|
234
|
-
return parent.initializer === node;
|
|
235
|
-
default:
|
|
236
|
-
return false;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
return false;
|
|
240
|
-
};
|
|
213
|
+
const isWriteTarget = (identifier) => targetDirection(identifier) !== "read";
|
|
241
214
|
|
|
242
215
|
const writtenSymbolId = (project, identifier) => {
|
|
243
216
|
const symbol = isShorthandName(identifier)
|
|
@@ -393,37 +366,89 @@ const receiversByLinkEnd = (sourceFile) => {
|
|
|
393
366
|
return receivers;
|
|
394
367
|
};
|
|
395
368
|
|
|
369
|
+
// Maps the UTF-16 start offset of each `??` token to its left operand and
|
|
370
|
+
// the end of its right operand.
|
|
371
|
+
const fallbacksByTokenStart = (sourceFile) => {
|
|
372
|
+
const fallbacks = new Map();
|
|
373
|
+
const visit = (node) => {
|
|
374
|
+
if (isBinaryExpression(node) && node.operatorToken.kind === SyntaxKind.QuestionQuestionToken) {
|
|
375
|
+
fallbacks.set(node.operatorToken.end - COALESCE_TOKEN.length, {
|
|
376
|
+
left: node.left,
|
|
377
|
+
rightEnd: node.right.end,
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
node.forEachChild((child) => {
|
|
381
|
+
visit(child);
|
|
382
|
+
return undefined;
|
|
383
|
+
});
|
|
384
|
+
};
|
|
385
|
+
visit(sourceFile);
|
|
386
|
+
return fallbacks;
|
|
387
|
+
};
|
|
388
|
+
|
|
396
389
|
const isOptionalLink = (original, mutant) =>
|
|
397
390
|
original.subarray(mutant.start, mutant.end).toString("utf8") === OPTIONAL_TOKEN;
|
|
398
391
|
|
|
392
|
+
// The `nullish-fallback` mutant removes the `??` token and the right operand.
|
|
393
|
+
const isFallbackRemoval = (original, mutant) =>
|
|
394
|
+
mutant.replacement === "" &&
|
|
395
|
+
original.subarray(mutant.start, mutant.start + COALESCE_TOKEN.length).toString("utf8") ===
|
|
396
|
+
COALESCE_TOKEN;
|
|
397
|
+
|
|
399
398
|
const utf16Offset = (original, byteOffset) =>
|
|
400
399
|
original.subarray(0, byteOffset).toString("utf8").length;
|
|
401
400
|
|
|
401
|
+
// `receiver.node` is the receiver expression. `receiver.settled` is true
|
|
402
|
+
// when the value has left its chain, as the left operand of `??` has.
|
|
402
403
|
const receiverIsNeverNullish = (project, receiver, writes) => {
|
|
403
|
-
if (containsAssertion(receiver)) return false;
|
|
404
|
+
if (containsAssertion(receiver.node)) return false;
|
|
404
405
|
try {
|
|
405
|
-
const type =
|
|
406
|
+
const type = receiver.settled
|
|
407
|
+
? settledType(project, receiver.node, { visited: new Set(), depth: 0, writes })
|
|
408
|
+
: declaredType(project, receiver.node, { visited: new Set(), depth: 0, writes });
|
|
406
409
|
return type !== null && neverNullish(type, 0);
|
|
407
410
|
} catch {
|
|
408
411
|
return false;
|
|
409
412
|
}
|
|
410
413
|
};
|
|
411
414
|
|
|
415
|
+
// The receiver of a mutant whose equivalence depends on a never-nullish
|
|
416
|
+
// value, or undefined when the mutant is not such a form.
|
|
417
|
+
const receiverOf = (original, mutant, sites) => {
|
|
418
|
+
if (isOptionalLink(original, mutant)) {
|
|
419
|
+
const node = sites.receivers.get(utf16Offset(original, mutant.end));
|
|
420
|
+
return node === undefined ? undefined : { node, settled: false };
|
|
421
|
+
}
|
|
422
|
+
if (!isFallbackRemoval(original, mutant)) return undefined;
|
|
423
|
+
const fallback = sites.fallbacks.get(utf16Offset(original, mutant.start));
|
|
424
|
+
return fallback?.rightEnd === utf16Offset(original, mutant.end)
|
|
425
|
+
? { node: fallback.left, settled: true }
|
|
426
|
+
: undefined;
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
const mayNeedProof = (original, mutant) =>
|
|
430
|
+
isOptionalLink(original, mutant) || isFallbackRemoval(original, mutant);
|
|
431
|
+
|
|
412
432
|
/**
|
|
413
|
-
* Returns the ids of the mutants of `file`
|
|
414
|
-
*
|
|
415
|
-
*
|
|
433
|
+
* Returns the ids of the mutants of `file` whose change has no effect when a
|
|
434
|
+
* receiver is never nullish, and whose receiver the declarations prove never
|
|
435
|
+
* nullish: the removal of an optional link (`a?.b` to `a.b`) and the removal
|
|
436
|
+
* of a nullish fallback (`a ?? b` to `a`). `project` is the project of the
|
|
437
|
+
* original file, before any mutant is applied.
|
|
416
438
|
*/
|
|
417
|
-
export const
|
|
418
|
-
const
|
|
419
|
-
if (
|
|
439
|
+
export const nonNullishMutants = (project, file, original) => {
|
|
440
|
+
const candidates = file.mutants.filter((mutant) => mayNeedProof(original, mutant));
|
|
441
|
+
if (candidates.length === 0 || !allowsTypeProof(project, file.absolutePath)) return new Set();
|
|
420
442
|
const sourceFile = project.program.getSourceFile(file.absolutePath);
|
|
421
443
|
if (!sourceFile) return new Set();
|
|
422
444
|
try {
|
|
423
|
-
const
|
|
445
|
+
const sites = {
|
|
446
|
+
receivers: receiversByLinkEnd(sourceFile),
|
|
447
|
+
fallbacks: fallbacksByTokenStart(sourceFile),
|
|
448
|
+
};
|
|
424
449
|
const writes = new Map();
|
|
425
|
-
const proven =
|
|
426
|
-
const receiver =
|
|
450
|
+
const proven = candidates.filter((mutant) => {
|
|
451
|
+
const receiver = receiverOf(original, mutant, sites);
|
|
427
452
|
return receiver !== undefined && receiverIsNeverNullish(project, receiver, writes);
|
|
428
453
|
});
|
|
429
454
|
return new Set(proven.map((mutant) => mutant.id));
|
package/src/project-state.mjs
CHANGED
|
@@ -20,11 +20,38 @@ const INFERRED_PROJECT = "<inferred>";
|
|
|
20
20
|
const compareText = (left, right) => Buffer.compare(Buffer.from(left), Buffer.from(right));
|
|
21
21
|
const slash = (value) => value.split(path.sep).join("/");
|
|
22
22
|
|
|
23
|
+
const DIRECTIVE_LINE = /^(?:\/\/\/?|\/\*+)\s*@ts-(?:ignore|expect-error)\b/u;
|
|
24
|
+
const BLANK_OR_LINE_COMMENT = /^(?:$|\/\/)/u;
|
|
25
|
+
|
|
26
|
+
// Strict-mode name errors, such as `arguments` used as a declaration name.
|
|
27
|
+
// They do not change which symbol a name resolves to. Other binding errors,
|
|
28
|
+
// such as a duplicate declaration, do, so they stay blocking under a directive.
|
|
29
|
+
const NAME_ONLY_BINDING_CODES = new Set([1100, 1101, 1102, 1212, 1213, 1214, 1215]);
|
|
30
|
+
|
|
31
|
+
// The compiler drops a binding or semantic diagnostic when an `@ts-ignore` or
|
|
32
|
+
// `@ts-expect-error` comment precedes its line, with only blank lines and `//`
|
|
33
|
+
// comment lines in between. Svelte, for example, declares
|
|
34
|
+
// `export const arguments` under `// @ts-ignore` in an ambient file.
|
|
35
|
+
const isSuppressedByDirective = (project, diagnostic) => {
|
|
36
|
+
if (!NAME_ONLY_BINDING_CODES.has(diagnostic.code)) return false;
|
|
37
|
+
const text = project.program.getSourceFile(diagnostic.fileName)?.text;
|
|
38
|
+
if (typeof text !== "string" || !Number.isInteger(diagnostic.pos)) return false;
|
|
39
|
+
const lines = text.slice(0, diagnostic.pos).split(/\r?\n/u);
|
|
40
|
+
for (let index = lines.length - 2; index >= 0; index -= 1) {
|
|
41
|
+
const line = lines[index].trim();
|
|
42
|
+
if (DIRECTIVE_LINE.test(line)) return true;
|
|
43
|
+
if (!BLANK_OR_LINE_COMMENT.test(line)) return false;
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
};
|
|
47
|
+
|
|
23
48
|
const structuralDiagnostics = (project) => [
|
|
24
49
|
...project.program.getConfigFileParsingDiagnostics(),
|
|
25
50
|
...project.program.getProgramDiagnostics(),
|
|
26
51
|
...project.program.getSyntacticDiagnostics(),
|
|
27
|
-
...project.program
|
|
52
|
+
...project.program
|
|
53
|
+
.getBindDiagnostics()
|
|
54
|
+
.filter((diagnostic) => !isSuppressedByDirective(project, diagnostic)),
|
|
28
55
|
];
|
|
29
56
|
|
|
30
57
|
const isProjectLocalDiagnostic = (project, diagnostic) => {
|
package/src/protocol.mjs
CHANGED
|
@@ -4,6 +4,7 @@ import path from "node:path";
|
|
|
4
4
|
import { BACKEND } from "./backend.mjs";
|
|
5
5
|
import {
|
|
6
6
|
ANALYSIS_OPERATION,
|
|
7
|
+
CALL_EVIDENCE_OPERATION,
|
|
7
8
|
MUTANT_TYPECHECK_OPERATION,
|
|
8
9
|
QUERY_OPERATIONS as MANIFEST_QUERY_OPERATIONS,
|
|
9
10
|
WIRE_PROTOCOL_VERSION,
|
|
@@ -457,6 +458,94 @@ const parseMutantTypecheckRequest = (value) => {
|
|
|
457
458
|
return { operation: MUTANT_TYPECHECK_OPERATION, root, files };
|
|
458
459
|
};
|
|
459
460
|
|
|
461
|
+
const CALL_EVIDENCE_REQUEST_KEYS = new Set([
|
|
462
|
+
"protocol_version",
|
|
463
|
+
"operation",
|
|
464
|
+
"root",
|
|
465
|
+
"defaults",
|
|
466
|
+
"classes",
|
|
467
|
+
"files",
|
|
468
|
+
]);
|
|
469
|
+
const DEFAULT_CANDIDATE_KEYS = new Set(["id", "arguments"]);
|
|
470
|
+
const ARGUMENT_KEYS = new Set(["path", "call_start", "index"]);
|
|
471
|
+
const CLASS_CANDIDATE_KEYS = new Set(["id", "path", "class", "methods"]);
|
|
472
|
+
const MAX_DEFAULT_CANDIDATES = 50_000;
|
|
473
|
+
const MAX_CANDIDATE_ARGUMENTS = 200_000;
|
|
474
|
+
const MAX_CLASS_CANDIDATES = 20_000;
|
|
475
|
+
const MAX_CLASS_METHODS = 2_000;
|
|
476
|
+
const MAX_PROJECT_FILES = 100_000;
|
|
477
|
+
|
|
478
|
+
const parseArgument = (value, field, root) => {
|
|
479
|
+
requireObject(value, field);
|
|
480
|
+
requireExactKeys(value, ARGUMENT_KEYS, field);
|
|
481
|
+
const { candidatePath } = parseCandidatePath(value.path, `${field}.path`, root);
|
|
482
|
+
return {
|
|
483
|
+
path: candidatePath,
|
|
484
|
+
call_start: requireInteger(value.call_start, `${field}.call_start`, 0),
|
|
485
|
+
index: requireInteger(value.index, `${field}.index`, 0),
|
|
486
|
+
};
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
const requireUniqueId = (ids, id, field) => {
|
|
490
|
+
if (ids.has(id)) throw new Error(`duplicate ${field} id ${id}`);
|
|
491
|
+
ids.add(id);
|
|
492
|
+
return id;
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
const parseCallEvidenceRequest = (value) => {
|
|
496
|
+
requireExactKeys(value, CALL_EVIDENCE_REQUEST_KEYS, "request");
|
|
497
|
+
const root = parseRoot(value.root);
|
|
498
|
+
const defaultIds = new Set();
|
|
499
|
+
let argumentCount = 0;
|
|
500
|
+
const defaults = requireBoundedArray(value.defaults, "defaults", MAX_DEFAULT_CANDIDATES).map(
|
|
501
|
+
(candidate, index) => {
|
|
502
|
+
const field = `defaults[${index}]`;
|
|
503
|
+
requireObject(candidate, field);
|
|
504
|
+
requireExactKeys(candidate, DEFAULT_CANDIDATE_KEYS, field);
|
|
505
|
+
const args = requireBoundedArray(
|
|
506
|
+
candidate.arguments,
|
|
507
|
+
`${field}.arguments`,
|
|
508
|
+
MAX_CANDIDATE_ARGUMENTS,
|
|
509
|
+
);
|
|
510
|
+
argumentCount += args.length;
|
|
511
|
+
if (argumentCount > MAX_CANDIDATE_ARGUMENTS) {
|
|
512
|
+
throw new Error(`arguments exceed the ${MAX_CANDIDATE_ARGUMENTS} item limit`);
|
|
513
|
+
}
|
|
514
|
+
return {
|
|
515
|
+
id: requireUniqueId(defaultIds, requireInteger(candidate.id, `${field}.id`, 0), "default"),
|
|
516
|
+
arguments: args.map((argument, at) =>
|
|
517
|
+
parseArgument(argument, `${field}.arguments[${at}]`, root),
|
|
518
|
+
),
|
|
519
|
+
};
|
|
520
|
+
},
|
|
521
|
+
);
|
|
522
|
+
const classIds = new Set();
|
|
523
|
+
const classes = requireBoundedArray(value.classes, "classes", MAX_CLASS_CANDIDATES).map(
|
|
524
|
+
(candidate, index) => {
|
|
525
|
+
const field = `classes[${index}]`;
|
|
526
|
+
requireObject(candidate, field);
|
|
527
|
+
requireExactKeys(candidate, CLASS_CANDIDATE_KEYS, field);
|
|
528
|
+
const { candidatePath } = parseCandidatePath(candidate.path, `${field}.path`, root);
|
|
529
|
+
return {
|
|
530
|
+
id: requireUniqueId(classIds, requireInteger(candidate.id, `${field}.id`, 0), "class"),
|
|
531
|
+
path: candidatePath,
|
|
532
|
+
class: requireString(candidate.class, `${field}.class`),
|
|
533
|
+
methods: [
|
|
534
|
+
...new Set(
|
|
535
|
+
requireBoundedArray(candidate.methods, `${field}.methods`, MAX_CLASS_METHODS).map(
|
|
536
|
+
(method, at) => requireString(method, `${field}.methods[${at}]`),
|
|
537
|
+
),
|
|
538
|
+
),
|
|
539
|
+
],
|
|
540
|
+
};
|
|
541
|
+
},
|
|
542
|
+
);
|
|
543
|
+
const files = requireBoundedArray(value.files, "files", MAX_PROJECT_FILES).map(
|
|
544
|
+
(file, index) => parseCandidatePath(file, `files[${index}]`, root).candidatePath,
|
|
545
|
+
);
|
|
546
|
+
return { operation: CALL_EVIDENCE_OPERATION, root, defaults, classes, files };
|
|
547
|
+
};
|
|
548
|
+
|
|
460
549
|
export const parseRequest = (value) => {
|
|
461
550
|
requireObject(value, "request");
|
|
462
551
|
if (
|
|
@@ -465,6 +554,12 @@ export const parseRequest = (value) => {
|
|
|
465
554
|
) {
|
|
466
555
|
return parseMutantTypecheckRequest(value);
|
|
467
556
|
}
|
|
557
|
+
if (
|
|
558
|
+
value.protocol_version === WIRE_PROTOCOL_VERSION &&
|
|
559
|
+
value.operation === CALL_EVIDENCE_OPERATION
|
|
560
|
+
) {
|
|
561
|
+
return parseCallEvidenceRequest(value);
|
|
562
|
+
}
|
|
468
563
|
if (value.protocol_version === WIRE_PROTOCOL_VERSION) {
|
|
469
564
|
return parseBatchRequest(value);
|
|
470
565
|
}
|
|
@@ -494,6 +589,17 @@ export const createSemanticResponse = ({
|
|
|
494
589
|
elapsed_ms: Math.max(0, Math.round(elapsedMs)),
|
|
495
590
|
});
|
|
496
591
|
|
|
592
|
+
export const createCallEvidenceResponse = ({ defaults, methods, elapsedMs }) => ({
|
|
593
|
+
protocol_version: WIRE_PROTOCOL_VERSION,
|
|
594
|
+
operation: CALL_EVIDENCE_OPERATION,
|
|
595
|
+
sidecar_version: SIDECAR_VERSION,
|
|
596
|
+
backend: BACKEND.family,
|
|
597
|
+
backend_version: BACKEND.version,
|
|
598
|
+
defaults,
|
|
599
|
+
methods,
|
|
600
|
+
elapsed_ms: Math.max(0, Math.round(elapsedMs)),
|
|
601
|
+
});
|
|
602
|
+
|
|
497
603
|
export const createMutantTypecheckResponse = ({ results, elapsedMs }) => ({
|
|
498
604
|
protocol_version: WIRE_PROTOCOL_VERSION,
|
|
499
605
|
operation: MUTANT_TYPECHECK_OPERATION,
|