await-async-utils.js
4.88 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RULE_NAME = void 0;
var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
var utils_1 = require("../utils");
var node_utils_1 = require("../node-utils");
exports.RULE_NAME = 'await-async-utils';
var ASYNC_UTILS_REGEXP = new RegExp("^(" + utils_1.ASYNC_UTILS.join('|') + ")$");
function isPromiseAll(node) {
return node_utils_1.isMemberExpression(node.callee) && node_utils_1.isIdentifier(node.callee.object) && node.callee.object.name === 'Promise' && node_utils_1.isIdentifier(node.callee.property) && node.callee.property.name === 'all';
}
function isInPromiseAll(node) {
var parent = node.parent;
return node_utils_1.isCallExpression(parent) && node_utils_1.isArrayExpression(parent.parent) && node_utils_1.isCallExpression(parent.parent.parent) && isPromiseAll(parent.parent.parent);
}
exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
name: exports.RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Enforce async utils to be awaited properly',
category: 'Best Practices',
recommended: 'warn',
},
messages: {
awaitAsyncUtil: 'Promise returned from `{{ name }}` must be handled',
},
fixable: null,
schema: [],
},
defaultOptions: [],
create: function (context) {
var _a;
var asyncUtilsUsage = [];
var importedAsyncUtils = [];
return _a = {
'ImportDeclaration > ImportSpecifier,ImportNamespaceSpecifier': function (node) {
var parent = node.parent;
if (!utils_1.LIBRARY_MODULES.includes(parent.source.value.toString()))
return;
if (node_utils_1.isImportSpecifier(node)) {
importedAsyncUtils.push(node.imported.name);
}
if (node_utils_1.isImportNamespaceSpecifier(node)) {
importedAsyncUtils.push(node.local.name);
}
}
},
_a["CallExpression > Identifier[name=" + ASYNC_UTILS_REGEXP + "]"] = function (node) {
asyncUtilsUsage.push({ node: node, name: node.name });
},
_a["CallExpression > MemberExpression > Identifier[name=" + ASYNC_UTILS_REGEXP + "]"] = function (node) {
var memberExpression = node.parent;
var identifier = memberExpression.object;
var memberExpressionName = identifier.name;
asyncUtilsUsage.push({
node: memberExpression,
name: memberExpressionName,
});
},
_a['Program:exit'] = function () {
var testingLibraryUtilUsage = asyncUtilsUsage.filter(function (usage) {
if (node_utils_1.isMemberExpression(usage.node)) {
var object = usage.node.object;
return importedAsyncUtils.includes(object.name);
}
return importedAsyncUtils.includes(usage.name);
});
testingLibraryUtilUsage.forEach(function (_a) {
var node = _a.node, name = _a.name;
var references = node_utils_1.getVariableReferences(context, node.parent.parent);
if (references &&
references.length === 0 &&
!node_utils_1.isAwaited(node.parent.parent) &&
!node_utils_1.isPromiseResolved(node) &&
!isInPromiseAll(node)) {
context.report({
node: node,
messageId: 'awaitAsyncUtil',
data: {
name: name,
},
});
}
else {
for (var _i = 0, references_1 = references; _i < references_1.length; _i++) {
var reference = references_1[_i];
var referenceNode = reference.identifier;
if (!node_utils_1.isAwaited(referenceNode.parent) &&
!node_utils_1.isPromiseResolved(referenceNode)) {
context.report({
node: node,
messageId: 'awaitAsyncUtil',
data: {
name: name,
},
});
break;
}
}
}
});
},
_a;
},
});