-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathno-slowed-test.ts
77 lines (71 loc) · 2.27 KB
/
no-slowed-test.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
import { getStringValue } from '../utils/ast.js'
import { createRule } from '../utils/createRule.js'
import { parseFnCall } from '../utils/parseFnCall.js'
export default createRule({
create(context) {
return {
CallExpression(node) {
const options = context.options[0] || {}
const allowConditional = !!options.allowConditional
const call = parseFnCall(context, node)
if (call?.group !== 'test') {
return
}
const slowNode = call.members.find((s) => getStringValue(s) === 'slow')
if (!slowNode) return
// If the call is a standalone `test.slow()` call, and not a test
// annotation, we have to treat it a bit differently.
const isStandalone = call.type === 'config'
// If allowConditional is enabled and it's not a test function,
// we ignore any `test.slow` calls that have no arguments.
if (isStandalone && allowConditional) {
return
}
context.report({
messageId: 'noSlowedTest',
node: isStandalone ? node : slowNode,
suggest: [
{
fix: (fixer) => {
return isStandalone
? fixer.remove(node.parent)
: fixer.removeRange([
slowNode.range![0] - 1,
slowNode.range![1] +
Number(slowNode.type !== 'Identifier'),
])
},
messageId: 'removeSlowedTestAnnotation',
},
],
})
},
}
},
meta: {
docs: {
category: 'Best Practices',
description: 'Prevent usage of the `.slow()` slow test annotation.',
recommended: true,
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-slowed-test.md',
},
hasSuggestions: true,
messages: {
noSlowedTest: 'Unexpected use of the `.slow()` annotation.',
removeSlowedTestAnnotation: 'Remove the `.slow()` annotation.',
},
schema: [
{
additionalProperties: false,
properties: {
allowConditional: {
default: false,
type: 'boolean',
},
},
type: 'object',
},
],
type: 'suggestion',
},
})