-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathprefer-to-contain.ts
109 lines (98 loc) · 3.28 KB
/
prefer-to-contain.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
import ESTree from 'estree'
import {
equalityMatchers,
getParent,
getStringValue,
isBooleanLiteral,
isPropertyAccessor,
} from '../utils/ast.js'
import { createRule } from '../utils/createRule.js'
import { parseFnCall } from '../utils/parseFnCall.js'
import { KnownCallExpression } from '../utils/types.js'
type FixableIncludesCallExpression = KnownCallExpression
const isFixableIncludesCallExpression = (
node: ESTree.Node,
): node is FixableIncludesCallExpression =>
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
isPropertyAccessor(node.callee, 'includes') &&
node.arguments.length === 1 &&
node.arguments[0].type !== 'SpreadElement'
export default createRule({
create(context) {
return {
CallExpression(node) {
const call = parseFnCall(context, node)
if (call?.type !== 'expect' || call.matcherArgs.length === 0) return
const expect = getParent(call.head.node)
if (expect?.type !== 'CallExpression') return
const [includesCall] = expect.arguments
const { matcher } = call
const [matcherArg] = call.matcherArgs
if (
!includesCall ||
matcherArg.type === 'SpreadElement' ||
!equalityMatchers.has(getStringValue(matcher)) ||
!isBooleanLiteral(matcherArg) ||
!isFixableIncludesCallExpression(includesCall)
) {
return
}
const notModifier = call.modifiers.find(
(node) => getStringValue(node) === 'not',
)
context.report({
fix(fixer) {
// We need to negate the expectation if the current expected
// value is itself negated by the "not" modifier
const addNotModifier =
matcherArg.type === 'Literal' &&
matcherArg.value === !!notModifier
const fixes = [
// remove the "includes" call entirely
fixer.removeRange([
includesCall.callee.property.range![0] - 1,
includesCall.range![1],
]),
// replace the current matcher with "toContain", adding "not" if needed
fixer.replaceText(
matcher,
addNotModifier ? 'not.toContain' : 'toContain',
),
// replace the matcher argument with the value from the "includes"
fixer.replaceText(
call.matcherArgs[0],
context.sourceCode.getText(includesCall.arguments[0]),
),
]
// Remove the "not" modifier if needed
if (notModifier) {
fixes.push(
fixer.removeRange([
notModifier.range![0],
notModifier.range![1] + 1,
]),
)
}
return fixes
},
messageId: 'useToContain',
node: matcher,
})
},
}
},
meta: {
docs: {
category: 'Best Practices',
description: 'Suggest using toContain()',
recommended: false,
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-to-contain.md',
},
fixable: 'code',
messages: {
useToContain: 'Use toContain() instead',
},
type: 'suggestion',
},
})