1 | 'use strict';
|
2 |
|
3 | const astUtils = require('../util/ast');
|
4 |
|
5 | function reportIfShortArrowFunction(context, node) {
|
6 | if (node.body.type !== 'BlockStatement') {
|
7 | context.report({
|
8 | node: node.body,
|
9 | message: 'Confusing implicit return in a test with an async function'
|
10 | });
|
11 | return true;
|
12 | }
|
13 | return false;
|
14 | }
|
15 |
|
16 | function isAllowedReturnStatement(node) {
|
17 | const argument = node.argument;
|
18 |
|
19 | if (astUtils.isReturnOfUndefined(node) || argument.type === 'Literal') {
|
20 | return true;
|
21 | }
|
22 |
|
23 | return false;
|
24 | }
|
25 |
|
26 | function reportIfFunctionWithBlock(context, node) {
|
27 | const returnStatement = astUtils.findReturnStatement(node.body.body);
|
28 | if (returnStatement && !isAllowedReturnStatement(returnStatement)) {
|
29 | context.report({
|
30 | node: returnStatement,
|
31 | message: 'Unexpected use of `return` in a test with an async function'
|
32 | });
|
33 | }
|
34 | }
|
35 |
|
36 | module.exports = function (context) {
|
37 | function check(node) {
|
38 | if (!node.async || !astUtils.hasParentMochaFunctionCall(node)) {
|
39 | return;
|
40 | }
|
41 |
|
42 | if (!reportIfShortArrowFunction(context, node)) {
|
43 | reportIfFunctionWithBlock(context, node);
|
44 | }
|
45 | }
|
46 |
|
47 | return {
|
48 | FunctionExpression: check,
|
49 | ArrowFunctionExpression: check
|
50 | };
|
51 | };
|