forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-unsafe-call.ts
91 lines (85 loc) · 2.78 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* eslint-disable eslint-comments/no-use */
/* eslint eslint-plugin/no-unused-message-ids:"off" -- disabled because the rule doesn't understand our helper function checkCall() */
/* eslint-enable eslint-comments/no-use */
import { TSESTree } from '@typescript-eslint/utils';
import * as tsutils from 'tsutils';
import * as util from '../util';
import { getThisExpression } from '../util';
type MessageIds =
| 'unsafeCall'
| 'unsafeCallThis'
| 'unsafeNew'
| 'unsafeTemplateTag';
export default util.createRule<[], MessageIds>({
name: 'no-unsafe-call',
meta: {
type: 'problem',
docs: {
description: 'Disallow calling a value with type `any`',
recommended: 'error',
requiresTypeChecking: true,
},
messages: {
unsafeCall: 'Unsafe call of an `any` typed value.',
unsafeCallThis: [
'Unsafe call of an `any` typed value. `this` is typed as `any`.',
'You can try to fix this by turning on the `noImplicitThis` compiler option, or adding a `this` parameter to the function.',
].join('\n'),
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();
const compilerOptions = program.getCompilerOptions();
const isNoImplicitThis = tsutils.isStrictCompilerOptionEnabled(
compilerOptions,
'noImplicitThis',
);
function checkCall(
node: TSESTree.Node,
reportingNode: TSESTree.Node,
messageId: MessageIds,
): void {
const tsNode = esTreeNodeToTSNodeMap.get(node);
const type = util.getConstrainedTypeAtLocation(checker, tsNode);
if (util.isTypeAnyType(type)) {
if (!isNoImplicitThis) {
// `this()` or `this.foo()` or `this.foo[bar]()`
const thisExpression = getThisExpression(node);
if (
thisExpression &&
util.isTypeAnyType(
util.getConstrainedTypeAtLocation(
checker,
esTreeNodeToTSNodeMap.get(thisExpression),
),
)
) {
messageId = 'unsafeCallThis';
}
}
context.report({
node: reportingNode,
messageId: messageId,
});
}
}
return {
'CallExpression > *.callee'(
node: TSESTree.CallExpression['callee'],
): void {
checkCall(node, node, 'unsafeCall');
},
NewExpression(node): void {
checkCall(node.callee, node, 'unsafeNew');
},
'TaggedTemplateExpression > *.tag'(node: TSESTree.Node): void {
checkCall(node, node, 'unsafeTemplateTag');
},
};
},
});