forked from tsdjs/tsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
62 lines (55 loc) · 1.89 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {CallExpression, TypeChecker} from '@tsd/typescript';
import {Diagnostic} from '../interfaces';
import {
Handler,
isIdentical,
isNotIdentical,
isNotAssignable,
expectDeprecated,
expectNotDeprecated,
isNever,
printTypeWarning,
expectDocCommentIncludes,
} 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',
EXPECT_NEVER = 'expectNever',
PRINT_TYPE = 'printType',
EXPECT_DOC_COMMENT_INCLUDES = 'expectDocCommentIncludes',
}
// 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],
[Assertion.EXPECT_NEVER, isNever],
[Assertion.PRINT_TYPE, printTypeWarning],
[Assertion.EXPECT_DOC_COMMENT_INCLUDES, expectDocCommentIncludes],
]);
/**
* 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;
};