1 | 'use strict';
|
2 |
|
3 | const { getAdditionalTestFunctions, getAdditionalXFunctions } = require('../util/settings');
|
4 |
|
5 | let mochaTestFunctions;
|
6 | let mochaXFunctions;
|
7 |
|
8 | function matchesMochaTestFunction(object) {
|
9 | return object && mochaTestFunctions.indexOf(object.name) !== -1;
|
10 | }
|
11 |
|
12 | function isPropertyNamedSkip(property) {
|
13 | return property && (property.name === 'skip' || property.value === 'skip');
|
14 | }
|
15 |
|
16 | function isCallToMochasSkipFunction(callee) {
|
17 | return callee.type === 'MemberExpression' &&
|
18 | matchesMochaTestFunction(callee.object) &&
|
19 | isPropertyNamedSkip(callee.property);
|
20 | }
|
21 |
|
22 | function createSkipAutofixFunction(callee) {
|
23 | const [ , endRangeOfMemberExpression ] = callee.range;
|
24 | const [ , endRangeOfMemberExpressionObject ] = callee.object.range;
|
25 |
|
26 | const rangeToRemove = [ endRangeOfMemberExpressionObject, endRangeOfMemberExpression ];
|
27 |
|
28 | return function removeSkipProperty(fixer) {
|
29 | return fixer.removeRange(rangeToRemove);
|
30 | };
|
31 | }
|
32 |
|
33 | function createXAutofixFunction(callee) {
|
34 | const rangeToRemove = [ callee.range[0], callee.range[0] + 1 ];
|
35 |
|
36 | return function removeXPrefix(fixer) {
|
37 | return fixer.removeRange(rangeToRemove);
|
38 | };
|
39 | }
|
40 |
|
41 | function isMochaXFunction(name) {
|
42 | return mochaXFunctions.indexOf(name) !== -1;
|
43 | }
|
44 |
|
45 | function isCallToMochaXFunction(callee) {
|
46 | return callee.type === 'Identifier' && isMochaXFunction(callee.name);
|
47 | }
|
48 |
|
49 | module.exports = function (context) {
|
50 | const settings = context.settings;
|
51 | const additionalTestFunctions = getAdditionalTestFunctions(settings);
|
52 | const additionalXFunctions = getAdditionalXFunctions(settings);
|
53 |
|
54 | mochaTestFunctions = [
|
55 | 'it',
|
56 | 'describe',
|
57 | 'suite',
|
58 | 'test',
|
59 | 'context',
|
60 | 'specify'
|
61 | ].concat(additionalTestFunctions);
|
62 | mochaXFunctions = [
|
63 | 'xit',
|
64 | 'xdescribe',
|
65 | 'xcontext',
|
66 | 'xspecify'
|
67 | ].concat(additionalXFunctions);
|
68 |
|
69 | return {
|
70 | CallExpression(node) {
|
71 | const callee = node.callee;
|
72 |
|
73 | if (isCallToMochasSkipFunction(callee)) {
|
74 | context.report({
|
75 | node: callee.property,
|
76 | message: 'Unexpected skipped mocha test.',
|
77 | fix: createSkipAutofixFunction(callee)
|
78 | });
|
79 | } else if (isCallToMochaXFunction(callee)) {
|
80 | context.report({
|
81 | node: callee,
|
82 | message: 'Unexpected skipped mocha test.',
|
83 | fix: createXAutofixFunction(callee)
|
84 | });
|
85 | }
|
86 | }
|
87 | };
|
88 | };
|
89 |
|
90 | module.exports.schema = [
|
91 | {
|
92 | type: 'object',
|
93 | properties: {
|
94 | additionalTestFunctions: {
|
95 | type: 'array',
|
96 | items: {
|
97 | type: 'string'
|
98 | },
|
99 | minItems: 1,
|
100 | uniqueItems: true
|
101 | },
|
102 | additionalXFunctions: {
|
103 | type: 'array',
|
104 | items: {
|
105 | type: 'string'
|
106 | },
|
107 | minItems: 1,
|
108 | uniqueItems: true
|
109 | }
|
110 | }
|
111 | }
|
112 | ];
|