UNPKG

2.57 kBJavaScriptView Raw
1'use strict';
2
3const { getDocsUrl, isDescribe, isFunction } = require('./util');
4
5const isAsync = node => node.async;
6
7const isString = node =>
8 (node.type === 'Literal' && typeof node.value === 'string') ||
9 node.type === 'TemplateLiteral';
10
11const hasParams = node => node.params.length > 0;
12
13const paramsLocation = params => {
14 const [first] = params;
15 const last = params[params.length - 1];
16 return {
17 start: {
18 line: first.loc.start.line,
19 column: first.loc.start.column,
20 },
21 end: {
22 line: last.loc.end.line,
23 column: last.loc.end.column,
24 },
25 };
26};
27
28module.exports = {
29 meta: {
30 docs: {
31 url: getDocsUrl(__filename),
32 },
33 },
34 create(context) {
35 return {
36 CallExpression(node) {
37 if (isDescribe(node)) {
38 if (node.arguments.length === 0) {
39 return context.report({
40 message: 'Describe requires name and callback arguments',
41 loc: node.loc,
42 });
43 }
44
45 const [name] = node.arguments;
46 const [, callbackFunction] = node.arguments;
47 if (!isString(name)) {
48 context.report({
49 message: 'First argument must be name',
50 loc: paramsLocation(node.arguments),
51 });
52 }
53 if (callbackFunction === undefined) {
54 return context.report({
55 message: 'Describe requires name and callback arguments',
56 loc: paramsLocation(node.arguments),
57 });
58 }
59 if (!isFunction(callbackFunction)) {
60 return context.report({
61 message: 'Second argument must be function',
62 loc: paramsLocation(node.arguments),
63 });
64 }
65 if (isAsync(callbackFunction)) {
66 context.report({
67 message: 'No async describe callback',
68 node: callbackFunction,
69 });
70 }
71 if (hasParams(callbackFunction)) {
72 context.report({
73 message: 'Unexpected argument(s) in describe callback',
74 loc: paramsLocation(callbackFunction.params),
75 });
76 }
77 if (callbackFunction.body.type === 'BlockStatement') {
78 callbackFunction.body.body.forEach(node => {
79 if (node.type === 'ReturnStatement') {
80 context.report({
81 message: 'Unexpected return statement in describe callback',
82 node,
83 });
84 }
85 });
86 }
87 }
88 },
89 };
90 },
91};