forked from typescript-eslint/typescript-eslint
/
unbound-method.ts
132 lines (111 loc) · 3.02 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
/**
* @fileoverview Enforces unbound methods are called with their expected scope.
* @author Josh Goldberg <https://github.com/joshuakgoldberg>
*/
'use strict';
import { TSESTree } 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';
/**
* @type {import("eslint").Rule.RuleModule}
*/
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 {
MemberExpression(node) {
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.BaseNode): boolean {
const parent = node.parent!;
switch (parent.type) {
case 'IfStatement':
case 'ForStatement':
case 'MemberExpression':
case 'UpdateExpression':
case 'WhileStatement':
return true;
case 'CallExpression':
return parent.callee === node;
case 'ConditionalExpression':
return parent.test === node;
case 'LogicalExpression':
return parent.operator !== '||';
case 'TaggedTemplateExpression':
return parent.tag === node;
case 'TSNonNullExpression':
case 'TSAsExpression':
case 'TSTypeAssertion':
return isSafeUse(parent);
}
return false;
}