-
Notifications
You must be signed in to change notification settings - Fork 62
/
no-return-and-callback.js
57 lines (47 loc) · 1.54 KB
/
no-return-and-callback.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
51
52
53
54
55
56
57
'use strict';
const astUtils = require('../util/ast');
function reportIfShortArrowFunction(context, node) {
if (node.body.type !== 'BlockStatement') {
context.report({
node: node.body,
message: 'Confusing implicit return in a test with callback'
});
return true;
}
return false;
}
function isFunctionCallWithName(node, name) {
return node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
node.callee.name === name;
}
function isAllowedReturnStatement(node, doneName) {
const argument = node.argument;
if (astUtils.isReturnOfUndefined(node) || argument.type === 'Literal') {
return true;
}
return isFunctionCallWithName(argument, doneName);
}
function reportIfFunctionWithBlock(context, node, doneName) {
const returnStatement = astUtils.findReturnStatement(node.body.body);
if (returnStatement && !isAllowedReturnStatement(returnStatement, doneName)) {
context.report({
node: returnStatement,
message: 'Unexpected use of `return` in a test with callback'
});
}
}
module.exports = function (context) {
function check(node) {
if (node.params.length === 0 || !astUtils.hasParentMochaFunctionCall(node)) {
return;
}
if (!reportIfShortArrowFunction(context, node)) {
reportIfFunctionWithBlock(context, node, node.params[0].name);
}
}
return {
FunctionExpression: check,
ArrowFunctionExpression: check
};
};