index.js
15.5 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findImportSpecifier = exports.isEmptyFunction = exports.getStatementCallExpression = exports.hasImportMatch = exports.getInnermostReturningFunction = exports.hasClosestExpectResolvesRejects = exports.getAssertNodeInfo = exports.getImportModuleName = exports.getFunctionName = exports.getReferenceNode = exports.getDeepestIdentifierNode = exports.getPropertyIdentifierNode = exports.getFunctionReturnStatementNode = exports.getInnermostFunctionScope = exports.getVariableReferences = exports.isPromiseHandled = exports.isPromisesArrayResolved = exports.isPromiseAllSettled = exports.isPromiseAll = exports.isPromiseIdentifier = exports.hasChainedThen = exports.hasThenProperty = exports.findClosestCallNode = exports.findClosestVariableDeclaratorNode = exports.findClosestCallExpressionNode = void 0;
const utils_1 = require("@typescript-eslint/utils");
const is_node_of_type_1 = require("./is-node-of-type");
__exportStar(require("./is-node-of-type"), exports);
const ValidLeftHandSideExpressions = [
utils_1.AST_NODE_TYPES.CallExpression,
utils_1.AST_NODE_TYPES.ClassExpression,
utils_1.AST_NODE_TYPES.ClassDeclaration,
utils_1.AST_NODE_TYPES.FunctionExpression,
utils_1.AST_NODE_TYPES.Literal,
utils_1.AST_NODE_TYPES.TemplateLiteral,
utils_1.AST_NODE_TYPES.MemberExpression,
utils_1.AST_NODE_TYPES.ArrayExpression,
utils_1.AST_NODE_TYPES.ArrayPattern,
utils_1.AST_NODE_TYPES.ClassExpression,
utils_1.AST_NODE_TYPES.FunctionExpression,
utils_1.AST_NODE_TYPES.Identifier,
utils_1.AST_NODE_TYPES.JSXElement,
utils_1.AST_NODE_TYPES.JSXFragment,
utils_1.AST_NODE_TYPES.JSXOpeningElement,
utils_1.AST_NODE_TYPES.MetaProperty,
utils_1.AST_NODE_TYPES.ObjectExpression,
utils_1.AST_NODE_TYPES.ObjectPattern,
utils_1.AST_NODE_TYPES.Super,
utils_1.AST_NODE_TYPES.ThisExpression,
utils_1.AST_NODE_TYPES.TSNullKeyword,
utils_1.AST_NODE_TYPES.TaggedTemplateExpression,
utils_1.AST_NODE_TYPES.TSNonNullExpression,
utils_1.AST_NODE_TYPES.TSAsExpression,
utils_1.AST_NODE_TYPES.ArrowFunctionExpression,
];
function findClosestCallExpressionNode(node, shouldRestrictInnerScope = false) {
if ((0, is_node_of_type_1.isCallExpression)(node)) {
return node;
}
if (!node || !node.parent) {
return null;
}
if (shouldRestrictInnerScope &&
!ValidLeftHandSideExpressions.includes(node.parent.type)) {
return null;
}
return findClosestCallExpressionNode(node.parent, shouldRestrictInnerScope);
}
exports.findClosestCallExpressionNode = findClosestCallExpressionNode;
function findClosestVariableDeclaratorNode(node) {
if (!node) {
return null;
}
if (utils_1.ASTUtils.isVariableDeclarator(node)) {
return node;
}
return findClosestVariableDeclaratorNode(node.parent);
}
exports.findClosestVariableDeclaratorNode = findClosestVariableDeclaratorNode;
function findClosestCallNode(node, name) {
if (!node.parent) {
return null;
}
if ((0, is_node_of_type_1.isCallExpression)(node) &&
utils_1.ASTUtils.isIdentifier(node.callee) &&
node.callee.name === name) {
return node;
}
else {
return findClosestCallNode(node.parent, name);
}
}
exports.findClosestCallNode = findClosestCallNode;
function hasThenProperty(node) {
return ((0, is_node_of_type_1.isMemberExpression)(node) &&
utils_1.ASTUtils.isIdentifier(node.property) &&
node.property.name === 'then');
}
exports.hasThenProperty = hasThenProperty;
function hasChainedThen(node) {
const parent = node.parent;
if ((0, is_node_of_type_1.isCallExpression)(parent) && parent.parent) {
return hasThenProperty(parent.parent);
}
return !!parent && hasThenProperty(parent);
}
exports.hasChainedThen = hasChainedThen;
function isPromiseIdentifier(node) {
return utils_1.ASTUtils.isIdentifier(node) && node.name === 'Promise';
}
exports.isPromiseIdentifier = isPromiseIdentifier;
function isPromiseAll(node) {
return ((0, is_node_of_type_1.isMemberExpression)(node.callee) &&
isPromiseIdentifier(node.callee.object) &&
utils_1.ASTUtils.isIdentifier(node.callee.property) &&
node.callee.property.name === 'all');
}
exports.isPromiseAll = isPromiseAll;
function isPromiseAllSettled(node) {
return ((0, is_node_of_type_1.isMemberExpression)(node.callee) &&
isPromiseIdentifier(node.callee.object) &&
utils_1.ASTUtils.isIdentifier(node.callee.property) &&
node.callee.property.name === 'allSettled');
}
exports.isPromiseAllSettled = isPromiseAllSettled;
function isPromisesArrayResolved(node) {
const closestCallExpression = findClosestCallExpressionNode(node, true);
if (!closestCallExpression) {
return false;
}
return (!!closestCallExpression.parent &&
(0, is_node_of_type_1.isArrayExpression)(closestCallExpression.parent) &&
(0, is_node_of_type_1.isCallExpression)(closestCallExpression.parent.parent) &&
(isPromiseAll(closestCallExpression.parent.parent) ||
isPromiseAllSettled(closestCallExpression.parent.parent)));
}
exports.isPromisesArrayResolved = isPromisesArrayResolved;
function isPromiseHandled(nodeIdentifier) {
const closestCallExpressionNode = findClosestCallExpressionNode(nodeIdentifier, true);
const suspiciousNodes = [nodeIdentifier, closestCallExpressionNode].filter(Boolean);
for (const node of suspiciousNodes) {
if (!node || !node.parent) {
continue;
}
if (utils_1.ASTUtils.isAwaitExpression(node.parent)) {
return true;
}
if ((0, is_node_of_type_1.isArrowFunctionExpression)(node.parent) ||
(0, is_node_of_type_1.isReturnStatement)(node.parent)) {
return true;
}
if (hasClosestExpectResolvesRejects(node.parent)) {
return true;
}
if (hasChainedThen(node)) {
return true;
}
if (isPromisesArrayResolved(node)) {
return true;
}
}
return false;
}
exports.isPromiseHandled = isPromiseHandled;
function getVariableReferences(context, node) {
var _a, _b, _c;
if (utils_1.ASTUtils.isVariableDeclarator(node)) {
return (_c = (_b = (_a = context.getDeclaredVariables(node)[0]) === null || _a === void 0 ? void 0 : _a.references) === null || _b === void 0 ? void 0 : _b.slice(1)) !== null && _c !== void 0 ? _c : [];
}
return [];
}
exports.getVariableReferences = getVariableReferences;
function getInnermostFunctionScope(context, asyncQueryNode) {
const innermostScope = utils_1.ASTUtils.getInnermostScope(context.getScope(), asyncQueryNode);
if (innermostScope.type === 'function' &&
utils_1.ASTUtils.isFunction(innermostScope.block)) {
return innermostScope;
}
return null;
}
exports.getInnermostFunctionScope = getInnermostFunctionScope;
function getFunctionReturnStatementNode(functionNode) {
if ((0, is_node_of_type_1.isBlockStatement)(functionNode.body)) {
const returnStatementNode = functionNode.body.body.find((statement) => (0, is_node_of_type_1.isReturnStatement)(statement));
if (!returnStatementNode) {
return null;
}
return returnStatementNode.argument;
}
else if (functionNode.expression) {
return functionNode.body;
}
return null;
}
exports.getFunctionReturnStatementNode = getFunctionReturnStatementNode;
function getPropertyIdentifierNode(node) {
if (utils_1.ASTUtils.isIdentifier(node)) {
return node;
}
if ((0, is_node_of_type_1.isMemberExpression)(node)) {
return getPropertyIdentifierNode(node.object);
}
if ((0, is_node_of_type_1.isCallExpression)(node)) {
return getPropertyIdentifierNode(node.callee);
}
if ((0, is_node_of_type_1.isExpressionStatement)(node)) {
return getPropertyIdentifierNode(node.expression);
}
return null;
}
exports.getPropertyIdentifierNode = getPropertyIdentifierNode;
function getDeepestIdentifierNode(node) {
if (utils_1.ASTUtils.isIdentifier(node)) {
return node;
}
if ((0, is_node_of_type_1.isMemberExpression)(node) && utils_1.ASTUtils.isIdentifier(node.property)) {
return node.property;
}
if ((0, is_node_of_type_1.isCallExpression)(node)) {
return getDeepestIdentifierNode(node.callee);
}
if (utils_1.ASTUtils.isAwaitExpression(node)) {
return getDeepestIdentifierNode(node.argument);
}
return null;
}
exports.getDeepestIdentifierNode = getDeepestIdentifierNode;
function getReferenceNode(node) {
if (node.parent &&
((0, is_node_of_type_1.isMemberExpression)(node.parent) || (0, is_node_of_type_1.isCallExpression)(node.parent))) {
return getReferenceNode(node.parent);
}
return node;
}
exports.getReferenceNode = getReferenceNode;
function getFunctionName(node) {
var _a, _b;
return ((_b = (_a = utils_1.ASTUtils.getFunctionNameWithKind(node)
.match(/('\w+')/g)) === null || _a === void 0 ? void 0 : _a[0].replace(/'/g, '')) !== null && _b !== void 0 ? _b : '');
}
exports.getFunctionName = getFunctionName;
function getImportModuleName(node) {
if ((0, is_node_of_type_1.isImportDeclaration)(node) && typeof node.source.value === 'string') {
return node.source.value;
}
if ((0, is_node_of_type_1.isCallExpression)(node) &&
(0, is_node_of_type_1.isLiteral)(node.arguments[0]) &&
typeof node.arguments[0].value === 'string') {
return node.arguments[0].value;
}
return undefined;
}
exports.getImportModuleName = getImportModuleName;
function getAssertNodeInfo(node) {
const emptyInfo = { matcher: null, isNegated: false };
if (!(0, is_node_of_type_1.isCallExpression)(node.object) ||
!utils_1.ASTUtils.isIdentifier(node.object.callee)) {
return emptyInfo;
}
if (node.object.callee.name !== 'expect') {
return emptyInfo;
}
let matcher = utils_1.ASTUtils.getPropertyName(node);
const isNegated = matcher === 'not';
if (isNegated) {
matcher =
node.parent && (0, is_node_of_type_1.isMemberExpression)(node.parent)
? utils_1.ASTUtils.getPropertyName(node.parent)
: null;
}
if (!matcher) {
return emptyInfo;
}
return { matcher, isNegated };
}
exports.getAssertNodeInfo = getAssertNodeInfo;
const matcherNamesHandlePromise = [
'resolves',
'rejects',
'toResolve',
'toReject',
];
function hasClosestExpectResolvesRejects(node) {
if ((0, is_node_of_type_1.isCallExpression)(node) &&
utils_1.ASTUtils.isIdentifier(node.callee) &&
node.parent &&
(0, is_node_of_type_1.isMemberExpression)(node.parent) &&
node.callee.name === 'expect') {
const expectMatcher = node.parent.property;
return (utils_1.ASTUtils.isIdentifier(expectMatcher) &&
matcherNamesHandlePromise.includes(expectMatcher.name));
}
if (!node.parent) {
return false;
}
return hasClosestExpectResolvesRejects(node.parent);
}
exports.hasClosestExpectResolvesRejects = hasClosestExpectResolvesRejects;
function getInnermostReturningFunction(context, node) {
const functionScope = getInnermostFunctionScope(context, node);
if (!functionScope) {
return undefined;
}
const returnStatementNode = getFunctionReturnStatementNode(functionScope.block);
if (!returnStatementNode) {
return undefined;
}
const returnStatementIdentifier = getDeepestIdentifierNode(returnStatementNode);
if ((returnStatementIdentifier === null || returnStatementIdentifier === void 0 ? void 0 : returnStatementIdentifier.name) !== node.name) {
return undefined;
}
return functionScope.block;
}
exports.getInnermostReturningFunction = getInnermostReturningFunction;
function hasImportMatch(importNode, identifierName) {
if (utils_1.ASTUtils.isIdentifier(importNode)) {
return importNode.name === identifierName;
}
return importNode.local.name === identifierName;
}
exports.hasImportMatch = hasImportMatch;
function getStatementCallExpression(statement) {
if ((0, is_node_of_type_1.isExpressionStatement)(statement)) {
const { expression } = statement;
if ((0, is_node_of_type_1.isCallExpression)(expression)) {
return expression;
}
if (utils_1.ASTUtils.isAwaitExpression(expression) &&
(0, is_node_of_type_1.isCallExpression)(expression.argument)) {
return expression.argument;
}
if ((0, is_node_of_type_1.isAssignmentExpression)(expression)) {
if ((0, is_node_of_type_1.isCallExpression)(expression.right)) {
return expression.right;
}
if (utils_1.ASTUtils.isAwaitExpression(expression.right) &&
(0, is_node_of_type_1.isCallExpression)(expression.right.argument)) {
return expression.right.argument;
}
}
}
if ((0, is_node_of_type_1.isReturnStatement)(statement) && (0, is_node_of_type_1.isCallExpression)(statement.argument)) {
return statement.argument;
}
if ((0, is_node_of_type_1.isVariableDeclaration)(statement)) {
for (const declaration of statement.declarations) {
if ((0, is_node_of_type_1.isCallExpression)(declaration.init)) {
return declaration.init;
}
}
}
return undefined;
}
exports.getStatementCallExpression = getStatementCallExpression;
function isEmptyFunction(node) {
if (utils_1.ASTUtils.isFunction(node) && (0, is_node_of_type_1.isBlockStatement)(node.body)) {
return node.body.body.length === 0;
}
return false;
}
exports.isEmptyFunction = isEmptyFunction;
function findImportSpecifier(specifierName, node) {
if ((0, is_node_of_type_1.isImportDeclaration)(node)) {
const namedExport = node.specifiers.find((n) => {
return ((0, is_node_of_type_1.isImportSpecifier)(n) &&
[n.imported.name, n.local.name].includes(specifierName));
});
if (namedExport) {
return namedExport;
}
return node.specifiers.find((n) => (0, is_node_of_type_1.isImportNamespaceSpecifier)(n));
}
else {
if (!utils_1.ASTUtils.isVariableDeclarator(node.parent)) {
return undefined;
}
const requireNode = node.parent;
if (utils_1.ASTUtils.isIdentifier(requireNode.id)) {
return requireNode.id;
}
if (!(0, is_node_of_type_1.isObjectPattern)(requireNode.id)) {
return undefined;
}
const property = requireNode.id.properties.find((n) => (0, is_node_of_type_1.isProperty)(n) &&
utils_1.ASTUtils.isIdentifier(n.key) &&
n.key.name === specifierName);
if (!property) {
return undefined;
}
return property.key;
}
}
exports.findImportSpecifier = findImportSpecifier;