1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const astUtils = require('../util/ast');
|
9 |
|
10 | const defaultTestNames = [ 'it', 'test', 'specify' ];
|
11 |
|
12 | function inlineOptions(context) {
|
13 | const pattern = context.options[0] ? new RegExp(context.options[0]) : /^should/;
|
14 | const testNames = context.options[1] ? context.options[1] : defaultTestNames;
|
15 | const message = context.options[2];
|
16 |
|
17 | return { pattern, testNames, message };
|
18 | }
|
19 |
|
20 | function objectOptions(options) {
|
21 | const pattern = options.pattern ? new RegExp(options.pattern) : /^should/;
|
22 | const testNames = options.testNames ? options.testNames : defaultTestNames;
|
23 | const message = options.message;
|
24 |
|
25 | return { pattern, testNames, message };
|
26 | }
|
27 |
|
28 | module.exports = function (context) {
|
29 | const options = context.options[0];
|
30 |
|
31 | const { pattern, testNames, message } = typeof options === 'object' && !(options instanceof RegExp) ?
|
32 | objectOptions(options) :
|
33 | inlineOptions(context);
|
34 |
|
35 | function isTest(node) {
|
36 | return node.callee && node.callee.name && testNames.indexOf(node.callee.name) > -1;
|
37 | }
|
38 |
|
39 | function hasValidTestDescription(mochaCallExpression) {
|
40 | const args = mochaCallExpression.arguments;
|
41 | const testDescriptionArgument = args[0];
|
42 |
|
43 | if (astUtils.isStringLiteral(testDescriptionArgument)) {
|
44 | return pattern.test(testDescriptionArgument.value);
|
45 | }
|
46 |
|
47 | return true;
|
48 | }
|
49 |
|
50 | function hasValidOrNoTestDescription(mochaCallExpression) {
|
51 | const args = mochaCallExpression.arguments;
|
52 | const hasNoTestDescription = args.length === 0;
|
53 |
|
54 | return hasNoTestDescription || hasValidTestDescription(mochaCallExpression);
|
55 | }
|
56 |
|
57 | return {
|
58 | CallExpression(node) {
|
59 | const callee = node.callee;
|
60 |
|
61 | if (isTest(node)) {
|
62 | if (!hasValidOrNoTestDescription(node)) {
|
63 | context.report(node, message || `Invalid "${ callee.name }()" description found.`);
|
64 | }
|
65 | }
|
66 | }
|
67 | };
|
68 | };
|