UNPKG

1.19 kBJavaScriptView Raw
1'use strict';
2
3const {visitIf} = require('enhance-visitors');
4const util = require('../util');
5const createAvaRule = require('../create-ava-rule');
6
7const create = context => {
8 const ava = createAvaRule();
9
10 return ava.merge({
11 CallExpression: visitIf([
12 ava.isInTestFile,
13 ava.isInTestNode,
14 ])(node => {
15 if (
16 node.parent.type === 'ExpressionStatement'
17 && node.callee.type === 'MemberExpression'
18 && (node.callee.property.name === 'throwsAsync' || node.callee.property.name === 'notThrowsAsync')
19 && node.callee.object.name === 't'
20 ) {
21 const message = `Use \`await\` with \`t.${node.callee.property.name}()\`.`;
22 if (ava.isInTestNode().arguments[0].async) {
23 context.report({
24 node,
25 message,
26 fix: fixer => fixer.replaceText(node.callee, `await ${context.getSourceCode().getText(node.callee)}`),
27 });
28 } else {
29 context.report({
30 node,
31 message,
32 });
33 }
34 }
35 }),
36 });
37};
38
39module.exports = {
40 create,
41 meta: {
42 type: 'problem',
43 docs: {
44 description: 'Ensure that `t.throwsAsync()` and `t.notThrowsAsync()` are awaited.',
45 url: util.getDocsUrl(__filename),
46 },
47 fixable: 'code',
48 schema: [],
49 },
50};