This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tsdLite.ts
77 lines (62 loc) · 2.23 KB
/
tsdLite.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import * as ts from "@tsd/typescript";
import { handleAssertions, toAssertionResult } from "./handleAssertions";
import {
type Location,
extractAssertions,
parseErrorAssertionToLocation,
} from "./parser";
import { silenceError } from "./silenceError";
import type { AssertionResult, TsdResult } from "./types";
import {
TsdError,
isDiagnosticWithLocation,
resolveCompilerOptions,
} from "./utils";
function toTsdResult(rawResult: AssertionResult | ts.Diagnostic): TsdResult {
return {
messageText: rawResult.messageText,
file: rawResult.file,
start: rawResult.start,
};
}
export function tsdLite(testFilePath: string): {
assertionsCount: number;
tsdResults: Array<TsdResult>;
} {
const compilerOptions = resolveCompilerOptions(testFilePath);
const program = ts.createProgram([testFilePath], compilerOptions || {});
const syntacticDiagnostics = program.getSyntacticDiagnostics();
if (syntacticDiagnostics.length !== 0) {
throw new TsdError("SyntaxError", syntacticDiagnostics[0]);
}
const semanticDiagnostics = program.getSemanticDiagnostics();
const { assertions, assertionsCount } = extractAssertions(program);
const typeChecker = program.getTypeChecker();
const assertionResults = handleAssertions(typeChecker, assertions);
const expectedErrors = parseErrorAssertionToLocation(assertions);
const expectedErrorsLocationsWithFoundDiagnostics: Array<Location> = [];
for (const diagnostic of semanticDiagnostics) {
if (isDiagnosticWithLocation(diagnostic)) {
const silenceErrorResult = silenceError(diagnostic, expectedErrors);
if (silenceErrorResult !== "preserve") {
if (silenceErrorResult !== "ignore") {
expectedErrorsLocationsWithFoundDiagnostics.push(silenceErrorResult);
}
continue;
}
}
assertionResults.push(diagnostic);
}
for (const errorLocation of expectedErrorsLocationsWithFoundDiagnostics) {
expectedErrors.delete(errorLocation);
}
for (const [, node] of expectedErrors) {
assertionResults.push(
toAssertionResult(node, "Expected an error, but found none."),
);
}
const tsdResults = assertionResults.map((result) => {
return toTsdResult(result);
});
return { assertionsCount, tsdResults };
}