-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathno-conditional-in-test.ts
37 lines (34 loc) · 1.1 KB
/
no-conditional-in-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
import { Rule } from 'eslint'
import { findParent } from '../utils/ast.js'
import { createRule } from '../utils/createRule.js'
import { isTypeOfFnCall } from '../utils/parseFnCall.js'
export default createRule({
create(context) {
function checkConditional(node: Rule.Node & Rule.NodeParentExtension) {
const call = findParent(node, 'CallExpression')
if (!call) return
if (isTypeOfFnCall(context, call, ['test', 'step'])) {
context.report({ messageId: 'conditionalInTest', node })
}
}
return {
ConditionalExpression: checkConditional,
IfStatement: checkConditional,
LogicalExpression: checkConditional,
SwitchStatement: checkConditional,
}
},
meta: {
docs: {
category: 'Best Practices',
description: 'Disallow conditional logic in tests',
recommended: true,
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-conditional-in-test.md',
},
messages: {
conditionalInTest: 'Avoid having conditionals in tests',
},
schema: [],
type: 'problem',
},
})