/
no-implied-eval.ts
141 lines (122 loc) · 3.64 KB
/
no-implied-eval.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
141
import * as ts from 'typescript';
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import * as tsutils from 'tsutils';
import * as util from '../util';
const FUNCTION_CONSTRUCTOR = 'Function';
const GLOBAL_CANDIDATES = new Set(['global', 'window', 'globalThis']);
const EVAL_LIKE_METHODS = new Set([
'setImmediate',
'setInterval',
'setTimeout',
'execScript',
]);
export default util.createRule({
name: 'no-implied-eval',
meta: {
docs: {
description: 'Disallow the use of `eval()`-like methods',
category: 'Best Practices',
recommended: 'error',
requiresTypeChecking: true,
},
messages: {
noImpliedEvalError: 'Implied eval. Consider passing a function.',
noFunctionConstructor:
'Implied eval. Do not use the Function constructor to create functions.',
},
schema: [],
type: 'suggestion',
},
defaultOptions: [],
create(context) {
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
function getCalleeName(
node: TSESTree.LeftHandSideExpression,
): string | null {
if (node.type === AST_NODE_TYPES.Identifier) {
return node.name;
}
if (
node.type === AST_NODE_TYPES.MemberExpression &&
node.object.type === AST_NODE_TYPES.Identifier &&
GLOBAL_CANDIDATES.has(node.object.name)
) {
if (node.property.type === AST_NODE_TYPES.Identifier) {
return node.property.name;
}
if (
node.property.type === AST_NODE_TYPES.Literal &&
typeof node.property.value === 'string'
) {
return node.property.value;
}
}
return null;
}
function isFunctionType(node: TSESTree.Node): boolean {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(tsNode);
const symbol = type.getSymbol();
if (
symbol &&
tsutils.isSymbolFlagSet(
symbol,
ts.SymbolFlags.Function | ts.SymbolFlags.Method,
)
) {
return true;
}
const signatures = checker.getSignaturesOfType(
type,
ts.SignatureKind.Call,
);
return signatures.length > 0;
}
function isFunction(node: TSESTree.Node): boolean {
switch (node.type) {
case AST_NODE_TYPES.ArrowFunctionExpression:
case AST_NODE_TYPES.FunctionDeclaration:
case AST_NODE_TYPES.FunctionExpression:
return true;
case AST_NODE_TYPES.MemberExpression:
case AST_NODE_TYPES.Identifier:
return isFunctionType(node);
case AST_NODE_TYPES.CallExpression:
return (
(node.callee.type === AST_NODE_TYPES.Identifier &&
node.callee.name === 'bind') ||
isFunctionType(node)
);
default:
return false;
}
}
function checkImpliedEval(
node: TSESTree.NewExpression | TSESTree.CallExpression,
): void {
const calleeName = getCalleeName(node.callee);
if (calleeName === null) {
return;
}
if (calleeName === FUNCTION_CONSTRUCTOR) {
context.report({ node, messageId: 'noFunctionConstructor' });
return;
}
if (node.arguments.length === 0) {
return;
}
const [handler] = node.arguments;
if (EVAL_LIKE_METHODS.has(calleeName) && !isFunction(handler)) {
context.report({ node: handler, messageId: 'noImpliedEvalError' });
}
}
return {
NewExpression: checkImpliedEval,
CallExpression: checkImpliedEval,
};
},
});