/
index.ts
46 lines (39 loc) · 1.56 KB
/
index.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
import {CallExpression, TypeChecker} from '@tsd/typescript';
import {Diagnostic} from '../interfaces';
import {Handler, isIdentical, isNotIdentical, isNotAssignable, expectDeprecated, expectNotDeprecated} from './handlers';
export enum Assertion {
EXPECT_TYPE = 'expectType',
EXPECT_NOT_TYPE = 'expectNotType',
EXPECT_ERROR = 'expectError',
EXPECT_ASSIGNABLE = 'expectAssignable',
EXPECT_NOT_ASSIGNABLE = 'expectNotAssignable',
EXPECT_DEPRECATED = 'expectDeprecated',
EXPECT_NOT_DEPRECATED = 'expectNotDeprecated'
}
// List of diagnostic handlers attached to the assertion
const assertionHandlers = new Map<Assertion, Handler>([
[Assertion.EXPECT_TYPE, isIdentical],
[Assertion.EXPECT_NOT_TYPE, isNotIdentical],
[Assertion.EXPECT_NOT_ASSIGNABLE, isNotAssignable],
[Assertion.EXPECT_DEPRECATED, expectDeprecated],
[Assertion.EXPECT_NOT_DEPRECATED, expectNotDeprecated]
]);
/**
* Returns a list of diagnostics based on the assertions provided.
*
* @param typeChecker - The TypeScript type checker.
* @param assertions - Assertion map with the key being the assertion, and the value the list of all those assertion nodes.
* @returns List of diagnostics.
*/
export const handle = (typeChecker: TypeChecker, assertions: Map<Assertion, Set<CallExpression>>): Diagnostic[] => {
const diagnostics: Diagnostic[] = [];
for (const [assertion, nodes] of assertions) {
const handler = assertionHandlers.get(assertion);
if (!handler) {
// Ignore these assertions as no handler is found
continue;
}
diagnostics.push(...handler(typeChecker, nodes));
}
return diagnostics;
};