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