-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathvalid-expect.ts
162 lines (141 loc) · 4.18 KB
/
valid-expect.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import * as ESTree from 'estree'
import { getParent, getStringValue } from '../utils/ast.js'
import { createRule } from '../utils/createRule.js'
import { getAmountData } from '../utils/misc.js'
import {
isSupportedAccessor,
modifiers,
parseFnCallWithReason,
} from '../utils/parseFnCall.js'
const findTopMostMemberExpression = (
node: ESTree.MemberExpression,
): ESTree.MemberExpression => {
let topMostMemberExpression = node
let parent = getParent(node)
while (parent) {
if (parent.type !== 'MemberExpression') {
break
}
topMostMemberExpression = parent
parent = parent.parent
}
return topMostMemberExpression
}
export default createRule({
create(context) {
const options = {
maxArgs: 2,
minArgs: 1,
...((context.options?.[0] as Record<string, unknown>) ?? {}),
}
const minArgs = Math.min(options.minArgs, options.maxArgs)
const maxArgs = Math.max(options.minArgs, options.maxArgs)
return {
CallExpression(node) {
const call = parseFnCallWithReason(context, node)
if (typeof call === 'string') {
const reportingNode =
node.parent?.type === 'MemberExpression'
? findTopMostMemberExpression(node.parent)!.property
: node
if (call === 'matcher-not-found') {
context.report({
messageId: 'matcherNotFound',
node: reportingNode,
})
return
}
if (call === 'matcher-not-called') {
context.report({
messageId:
isSupportedAccessor(reportingNode) &&
modifiers.has(getStringValue(reportingNode))
? 'matcherNotFound'
: 'matcherNotCalled',
node: reportingNode,
})
}
if (call === 'modifier-unknown') {
context.report({
messageId: 'modifierUnknown',
node: reportingNode,
})
return
}
return
} else if (call?.type !== 'expect') {
return
}
const expect = getParent(call.head.node)
if (expect?.type !== 'CallExpression') return
if (expect.arguments.length < minArgs) {
const expectLength = getStringValue(call.head.node).length
const loc: ESTree.SourceLocation = {
end: {
column: expect.loc!.start.column + expectLength + 1,
line: expect.loc!.start.line,
},
start: {
column: expect.loc!.start.column + expectLength,
line: expect.loc!.start.line,
},
}
context.report({
data: getAmountData(minArgs),
loc,
messageId: 'notEnoughArgs',
node: expect,
})
}
if (expect.arguments.length > maxArgs) {
const { start } = expect.arguments[maxArgs].loc!
const { end } = expect.arguments.at(-1)!.loc!
const loc = {
end: {
column: end.column,
line: end.line,
},
start,
}
context.report({
data: getAmountData(maxArgs),
loc,
messageId: 'tooManyArgs',
node: expect,
})
}
},
}
},
meta: {
docs: {
category: 'Possible Errors',
description: 'Enforce valid `expect()` usage',
recommended: true,
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/valid-expect.md',
},
messages: {
matcherNotCalled: 'Matchers must be called to assert.',
matcherNotFound: 'Expect must have a corresponding matcher call.',
notEnoughArgs: 'Expect requires at least {{amount}} argument{{s}}.',
tooManyArgs: 'Expect takes at most {{amount}} argument{{s}}.',
},
schema: [
{
additionalProperties: false,
properties: {
maxArgs: {
minimum: 1,
type: 'number',
},
minArgs: {
minimum: 1,
type: 'number',
},
},
type: 'object',
},
],
type: 'problem',
},
})