1
1
import { Rule } from 'eslint'
2
2
import * as ESTree from 'estree'
3
- import { getStringValue , isFunction } from '../utils/ast'
3
+ import { getStringValue , isFunction , isStringLiteral } from '../utils/ast'
4
4
import { parseFnCall } from '../utils/parseFnCall'
5
5
6
6
const paramsLocation = (
@@ -15,14 +15,6 @@ const paramsLocation = (
15
15
}
16
16
}
17
17
18
- function parseArgs ( node : ESTree . CallExpression ) {
19
- const [ name , b , c ] = node . arguments
20
- const options = node . arguments . length === 2 ? b : undefined
21
- const callback = node . arguments . length === 3 ? c : b
22
-
23
- return [ name , options , callback ] as const
24
- }
25
-
26
18
export default {
27
19
create ( context ) {
28
20
return {
@@ -35,47 +27,49 @@ export default {
35
27
return
36
28
}
37
29
38
- const [ name , _ , callback ] = parseArgs ( node )
30
+ const callback = node . arguments . at ( - 1 )
39
31
40
- if ( node . arguments . length < 1 ) {
32
+ // e.g., test.describe()
33
+ if ( ! callback ) {
41
34
return context . report ( {
42
35
loc : node . loc ! ,
43
- messageId : 'nameAndCallback ' ,
36
+ messageId : 'missingCallback ' ,
44
37
} )
45
38
}
46
39
47
- if ( ! name || ! callback ) {
48
- context . report ( {
40
+ // e.g., test.describe("foo")
41
+ if ( node . arguments . length === 1 && isStringLiteral ( callback ) ) {
42
+ return context . report ( {
49
43
loc : paramsLocation ( node . arguments ) ,
50
- messageId : 'nameAndCallback ' ,
44
+ messageId : 'missingCallback ' ,
51
45
} )
52
-
53
- return
54
46
}
55
47
48
+ // e.g., test.describe("foo", "foo2");
56
49
if ( ! isFunction ( callback ) ) {
57
- context . report ( {
50
+ return context . report ( {
58
51
loc : paramsLocation ( node . arguments ) ,
59
52
messageId : 'invalidCallback' ,
60
53
} )
61
-
62
- return
63
54
}
64
55
56
+ // e.g., test.describe("foo", async () => {});
65
57
if ( callback . async ) {
66
58
context . report ( {
67
59
messageId : 'noAsyncDescribeCallback' ,
68
60
node : callback ,
69
61
} )
70
62
}
71
63
64
+ // e.g., test.describe("foo", (done) => {});
72
65
if ( callback . params . length ) {
73
66
context . report ( {
74
67
loc : paramsLocation ( callback . params ) ,
75
68
messageId : 'unexpectedDescribeArgument' ,
76
69
} )
77
70
}
78
71
72
+ // e.g., test.describe("foo", () => { return; });
79
73
if ( callback . body . type === 'CallExpression' ) {
80
74
context . report ( {
81
75
messageId : 'unexpectedReturnInDescribe' ,
@@ -105,7 +99,7 @@ export default {
105
99
} ,
106
100
messages : {
107
101
invalidCallback : 'Callback argument must be a function' ,
108
- nameAndCallback : 'Describe requires name and callback arguments ' ,
102
+ missingCallback : 'Describe requires a callback' ,
109
103
noAsyncDescribeCallback : 'No async describe callback' ,
110
104
unexpectedDescribeArgument : 'Unexpected argument(s) in describe callback' ,
111
105
unexpectedReturnInDescribe :
0 commit comments