1 | 'use strict';
|
2 |
|
3 | const {visitIf} = require('enhance-visitors');
|
4 | const createAvaRule = require('../create-ava-rule');
|
5 | const util = require('../util');
|
6 |
|
7 | function containsThen(node) {
|
8 | if (!node
|
9 | || node.type !== 'CallExpression'
|
10 | || node.callee.type !== 'MemberExpression'
|
11 | ) {
|
12 | return false;
|
13 | }
|
14 |
|
15 | const {callee} = node;
|
16 | if (callee.property.type === 'Identifier'
|
17 | && callee.property.name === 'then'
|
18 | ) {
|
19 | return true;
|
20 | }
|
21 |
|
22 | return containsThen(callee.object);
|
23 | }
|
24 |
|
25 | const create = context => {
|
26 | const ava = createAvaRule();
|
27 |
|
28 | const check = visitIf([
|
29 | ava.isInTestFile,
|
30 | ava.isInTestNode,
|
31 | ])(node => {
|
32 | if (node.body.type !== 'BlockStatement') {
|
33 | return;
|
34 | }
|
35 |
|
36 | const statements = node.body.body;
|
37 | const returnStatement = statements.find(statement => statement.type === 'ReturnStatement');
|
38 | if (returnStatement && containsThen(returnStatement.argument)) {
|
39 | context.report({
|
40 | node,
|
41 | message: 'Prefer using async/await instead of returning a Promise.',
|
42 | });
|
43 | }
|
44 | });
|
45 |
|
46 | return ava.merge({
|
47 | ArrowFunctionExpression: check,
|
48 | FunctionExpression: check,
|
49 | });
|
50 | };
|
51 |
|
52 | module.exports = {
|
53 | create,
|
54 | meta: {
|
55 | type: 'suggestion',
|
56 | docs: {
|
57 | description: 'Prefer using async/await instead of returning a Promise.',
|
58 | url: util.getDocsUrl(__filename),
|
59 | },
|
60 | schema: [],
|
61 | },
|
62 | };
|