-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenforce-error-with-use-async.js
50 lines (50 loc) · 1.54 KB
/
enforce-error-with-use-async.js
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
module.exports = {
meta: {
type: 'problem',
docs: {
description: `Enforce that 'error' key is handled from 'useAsync'.`,
},
schema: []
},
create(context) {
const REPORT_MESSAGE = `'error' key is not used from 'useAsync', resulting in swallowed exceptions in 'useAsync' callback.`;
const useAsyncNodes = [];
return {
VariableDeclarator(node) {
if (node.init && node.init.type === 'CallExpression' && node.init.callee && node.init.callee.name === 'useAsync') {
if (node.id.type === 'ObjectPattern') {
const hasErrorKey = node.id.properties.some(property => {
return property.key && property.key.name === 'error';
});
if (!hasErrorKey) {
context.report({
node,
message: REPORT_MESSAGE,
});
}
} else if (node.id.type === 'Identifier') {
useAsyncNodes.push(node);
}
}
},
MemberExpression(node) {
if (node.property && node.property.name === 'error') {
const index = useAsyncNodes.findIndex(useAsyncNode => {
return node.object && useAsyncNode.id.name === node.object.name;
});
if (index > -1) {
useAsyncNodes.splice(index, 1);
}
}
},
'Program:exit': function(node) {
if (useAsyncNodes.length) {
context.report({
node: useAsyncNodes[0],
message: REPORT_MESSAGE,
});
}
}
};
}
};