UNPKG

1.1 kBJavaScriptView Raw
1'use strict';
2
3const {visitIf} = require('enhance-visitors');
4const util = require('../util');
5const createAvaRule = require('../create-ava-rule');
6
7function sortByName(a, b) {
8 if (a.name < b.name) {
9 return -1;
10 }
11
12 if (a.name > b.name) {
13 return 1;
14 }
15
16 return 0;
17}
18
19const create = context => {
20 const ava = createAvaRule();
21
22 return ava.merge({
23 CallExpression: visitIf([
24 ava.isInTestFile,
25 ava.isTestNode,
26 ])(node => {
27 const testModifiers = util.getTestModifiers(node).sort(sortByName);
28
29 if (testModifiers.length === 0) {
30 return;
31 }
32
33 // TODO: Remove `.reduce()` usage.
34 // eslint-disable-next-line unicorn/no-array-reduce
35 testModifiers.reduce((previous, current) => {
36 if (previous.name === current.name) {
37 context.report({
38 node: current,
39 message: `Duplicate test modifier \`.${current.name}\`.`,
40 });
41 }
42
43 return current;
44 });
45 }),
46 });
47};
48
49module.exports = {
50 create,
51 meta: {
52 type: 'problem',
53 docs: {
54 description: 'Ensure tests do not have duplicate modifiers.',
55 url: util.getDocsUrl(__filename),
56 },
57 schema: [],
58 },
59};