UNPKG

1.11 kBJavaScriptView Raw
1'use strict';
2
3const {visitIf} = require('enhance-visitors');
4const createAvaRule = require('../create-ava-rule');
5const util = require('../util');
6
7const create = context => {
8 const ava = createAvaRule();
9
10 return ava.merge({
11 CallExpression: visitIf([
12 ava.isInTestFile,
13 ava.isTestNode,
14 ])(node => {
15 const functionArgumentIndex = node.arguments.length - 1;
16 if (functionArgumentIndex > 1) {
17 return;
18 }
19
20 const functionArgument = node.arguments[functionArgumentIndex];
21
22 if (!util.isFunctionExpression(functionArgument)) {
23 return;
24 }
25
26 const {body} = functionArgument;
27 if (body.type === 'CallExpression') {
28 context.report({
29 node,
30 message: 'The test implementation should not be an inline arrow function.',
31 fix: fixer => [fixer.insertTextBefore(body, '{'), fixer.insertTextAfter(body, '}')],
32 });
33 }
34 }),
35 });
36};
37
38module.exports = {
39 create,
40 meta: {
41 type: 'suggestion',
42 docs: {
43 description: 'Ensure assertions are not called from inline arrow functions.',
44 url: util.getDocsUrl(__filename),
45 },
46 fixable: 'code',
47 schema: [],
48 },
49};