/
unbound-method.ts
140 lines (119 loc) · 3.56 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as tsutils from 'tsutils';
import 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',
recommended: 'error',
requiresTypeChecking: true,
},
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 {
'MemberExpression, OptionalMemberExpression'(
node: TSESTree.MemberExpression | TSESTree.OptionalMemberExpression,
): void {
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): boolean {
const { valueDeclaration } = symbol;
if (!valueDeclaration) {
// working around https://github.com/microsoft/TypeScript/issues/31294
return false;
}
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.OptionalMemberExpression:
case AST_NODE_TYPES.SwitchStatement:
case AST_NODE_TYPES.UpdateExpression:
case AST_NODE_TYPES.WhileStatement:
return true;
case AST_NODE_TYPES.CallExpression:
case AST_NODE_TYPES.OptionalCallExpression:
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.UnaryExpression:
return parent.operator === 'typeof';
case AST_NODE_TYPES.BinaryExpression:
return ['instanceof', '==', '!=', '===', '!=='].includes(parent.operator);
case AST_NODE_TYPES.TSNonNullExpression:
case AST_NODE_TYPES.TSAsExpression:
case AST_NODE_TYPES.TSTypeAssertion:
return isSafeUse(parent);
}
return false;
}