forked from typescript-eslint/typescript-eslint
/
unbound-method.ts
123 lines (103 loc) · 3.03 KB
/
unbound-method.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/typescript-estree';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
import * as util from '../util';
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
interface Config {
ignoreStatic: boolean;
}
type Options = [Config];
type MessageIds = 'unbound';
export default util.createRule<Options, MessageIds>({
name: 'unbound-method',
meta: {
docs: {
category: 'Best Practices',
description:
'Enforces unbound methods are called with their expected scope.',
tslintName: 'no-unbound-method',
recommended: 'error',
},
messages: {
unbound:
'Avoid referencing unbound methods which may cause unintentional scoping of `this`.',
},
schema: [
{
type: 'object',
properties: {
ignoreStatic: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
type: 'problem',
},
defaultOptions: [
{
ignoreStatic: false,
},
],
create(context, [{ ignoreStatic }]) {
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
return {
[AST_NODE_TYPES.MemberExpression](node: TSESTree.MemberExpression) {
if (isSafeUse(node)) {
return;
}
const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const symbol = checker.getSymbolAtLocation(originalNode);
if (symbol && isDangerousMethod(symbol, ignoreStatic)) {
context.report({
messageId: 'unbound',
node,
});
}
},
};
},
});
function isDangerousMethod(symbol: ts.Symbol, ignoreStatic: boolean) {
const { valueDeclaration } = symbol;
switch (valueDeclaration.kind) {
case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.MethodSignature:
return !(
ignoreStatic &&
tsutils.hasModifier(
valueDeclaration.modifiers,
ts.SyntaxKind.StaticKeyword,
)
);
}
return false;
}
function isSafeUse(node: TSESTree.Node): boolean {
const parent = node.parent!;
switch (parent.type) {
case AST_NODE_TYPES.IfStatement:
case AST_NODE_TYPES.ForStatement:
case AST_NODE_TYPES.MemberExpression:
case AST_NODE_TYPES.UpdateExpression:
case AST_NODE_TYPES.WhileStatement:
return true;
case AST_NODE_TYPES.CallExpression:
return parent.callee === node;
case AST_NODE_TYPES.ConditionalExpression:
return parent.test === node;
case AST_NODE_TYPES.LogicalExpression:
return parent.operator !== '||';
case AST_NODE_TYPES.TaggedTemplateExpression:
return parent.tag === node;
case AST_NODE_TYPES.TSNonNullExpression:
case AST_NODE_TYPES.TSAsExpression:
case AST_NODE_TYPES.TSTypeAssertion:
return isSafeUse(parent);
}
return false;
}