-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathvalid-describe-callback.ts
111 lines (99 loc) · 3.18 KB
/
valid-describe-callback.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import * as ESTree from 'estree'
import { getStringValue, isFunction, isStringLiteral } from '../utils/ast.js'
import { createRule } from '../utils/createRule.js'
import { parseFnCall } from '../utils/parseFnCall.js'
const paramsLocation = (
params: ESTree.CallExpression['arguments'] | ESTree.Pattern[],
) => {
const [first] = params
const last = params[params.length - 1]
return {
end: last.loc!.end,
start: first.loc!.start,
}
}
export default createRule({
create(context) {
return {
CallExpression(node) {
const call = parseFnCall(context, node)
if (call?.group !== 'describe') return
// Ignore `describe.configure()` calls
if (call.members.some((s) => getStringValue(s) === 'configure')) {
return
}
const callback = node.arguments.at(-1)
// e.g., test.describe()
if (!callback) {
return context.report({
loc: node.loc!,
messageId: 'missingCallback',
})
}
// e.g., test.describe("foo")
if (node.arguments.length === 1 && isStringLiteral(callback)) {
return context.report({
loc: paramsLocation(node.arguments),
messageId: 'missingCallback',
})
}
// e.g., test.describe("foo", "foo2");
if (!isFunction(callback)) {
return context.report({
loc: paramsLocation(node.arguments),
messageId: 'invalidCallback',
})
}
// e.g., test.describe("foo", async () => {});
if (callback.async) {
context.report({
messageId: 'noAsyncDescribeCallback',
node: callback,
})
}
// e.g., test.describe("foo", (done) => {});
if (callback.params.length) {
context.report({
loc: paramsLocation(callback.params),
messageId: 'unexpectedDescribeArgument',
})
}
// e.g., test.describe("foo", () => { return; });
if (callback.body.type === 'CallExpression') {
context.report({
messageId: 'unexpectedReturnInDescribe',
node: callback,
})
}
if (callback.body.type === 'BlockStatement') {
callback.body.body.forEach((node) => {
if (node.type === 'ReturnStatement') {
context.report({
messageId: 'unexpectedReturnInDescribe',
node,
})
}
})
}
},
}
},
meta: {
docs: {
category: 'Possible Errors',
description: 'Enforce valid `describe()` callback',
recommended: true,
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/valid-describe-callback.md',
},
messages: {
invalidCallback: 'Callback argument must be a function',
missingCallback: 'Describe requires a callback',
noAsyncDescribeCallback: 'No async describe callback',
unexpectedDescribeArgument: 'Unexpected argument(s) in describe callback',
unexpectedReturnInDescribe:
'Unexpected return statement in describe callback',
},
schema: [],
type: 'problem',
},
})