-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
no-unsafe-call.ts
57 lines (53 loc) · 1.64 KB
/
no-unsafe-call.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
import { TSESTree } from '@typescript-eslint/experimental-utils';
import * as util from '../util';
type MessageIds = 'unsafeCall' | 'unsafeNew' | 'unsafeTemplateTag';
export default util.createRule<[], MessageIds>({
name: 'no-unsafe-call',
meta: {
type: 'problem',
docs: {
description: 'Disallows calling an any type value',
category: 'Possible Errors',
recommended: false,
requiresTypeChecking: true,
},
messages: {
unsafeCall: 'Unsafe call of an any typed value',
unsafeNew: 'Unsafe construction of an any type value',
unsafeTemplateTag: 'Unsafe any typed template tag',
},
schema: [],
},
defaultOptions: [],
create(context) {
const { program, esTreeNodeToTSNodeMap } = util.getParserServices(context);
const checker = program.getTypeChecker();
function checkCall(
node: TSESTree.Node,
reportingNode: TSESTree.Node,
messageId: MessageIds,
): void {
const tsNode = esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(tsNode);
if (util.isTypeAnyType(type)) {
context.report({
node: reportingNode,
messageId: messageId,
});
}
}
return {
'CallExpression, OptionalCallExpression'(
node: TSESTree.CallExpression | TSESTree.OptionalCallExpression,
): void {
checkCall(node.callee, node.callee, 'unsafeCall');
},
NewExpression(node): void {
checkCall(node.callee, node, 'unsafeNew');
},
'TaggedTemplateExpression > *.tag'(node: TSESTree.Node): void {
checkCall(node, node, 'unsafeTemplateTag');
},
};
},
});