-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathparseFnCall.ts
514 lines (437 loc) · 13.3 KB
/
parseFnCall.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import { Rule } from 'eslint'
import * as ESTree from 'estree'
import {
findParent,
getParent,
getStringValue,
isFunction,
isIdentifier,
isStringNode,
StringNode,
} from './ast.js'
const testHooks = new Set(['afterAll', 'afterEach', 'beforeAll', 'beforeEach'])
const VALID_CHAINS = new Set([
// Hooks
'afterAll',
'afterEach',
'beforeAll',
'beforeEach',
'test.afterAll',
'test.afterEach',
'test.beforeAll',
'test.beforeEach',
// Describe
'describe',
'describe.only',
'describe.skip',
'describe.fixme',
'describe.fixme.only',
'describe.configure',
'describe.serial',
'describe.serial.only',
'describe.serial.skip',
'describe.serial.fixme',
'describe.serial.fixme.only',
'describe.parallel',
'describe.parallel.only',
'describe.parallel.skip',
'describe.parallel.fixme',
'describe.parallel.fixme.only',
'test.describe',
'test.describe.only',
'test.describe.skip',
'test.describe.fixme',
'test.describe.fixme.only',
'test.describe.configure',
'test.describe.serial',
'test.describe.serial.only',
'test.describe.serial.skip',
'test.describe.serial.fixme',
'test.describe.serial.fixme.only',
'test.describe.parallel',
'test.describe.parallel.only',
'test.describe.parallel.skip',
'test.describe.parallel.fixme',
'test.describe.parallel.fixme.only',
// Test
'test',
'test.fail',
'test.fail.only',
'test.fixme',
'test.only',
'test.skip',
'test.step',
'test.step.skip',
'test.slow',
'test.use',
])
export type AccessorNode = StringNode | ESTree.Identifier
const joinChains = (
a: AccessorNode[] | null,
b: AccessorNode[] | null,
): AccessorNode[] | null => (a && b ? [...a, ...b] : null)
/**
* Checks if the given `node` is a "supported accessor".
*
* This means that it's a node can be used to access properties, and who's
* "value" can be statically determined.
*
* `MemberExpression` nodes most commonly contain accessors, but it's possible
* for other nodes to contain them.
*
* If a `value` is provided & the `node` is an `AccessorNode`, the `value` will
* be compared to that of the `AccessorNode`.
*
* Note that `value` here refers to the normalised value. The property that
* holds the value is not always called `name`.
*/
export const isSupportedAccessor = (
node: ESTree.Node,
value?: string,
): node is AccessorNode =>
isIdentifier(node, value) || isStringNode(node, value)
class Chain {
#nodes: AccessorNode[] | null = null
#leaves: WeakSet<AccessorNode> = new WeakSet()
constructor(node: ESTree.Node) {
this.#nodes = this.#buildChain(node)
}
isLeaf(node: AccessorNode): boolean {
return this.#leaves.has(node)
}
get nodes() {
return this.#nodes
}
#buildChain(node: ESTree.Node, insideCall = false): AccessorNode[] | null {
if (isSupportedAccessor(node)) {
// If we are inside a call expression, then the current node is a leaf,
// that is, the end of the sub-chain. For example, in
// `expect.soft(x).not.toBe()`, `soft` and `toBe` are leaves.
if (insideCall) {
this.#leaves.add(node)
}
return [node]
}
switch (node.type) {
case 'TaggedTemplateExpression':
return this.#buildChain(node.tag)
case 'MemberExpression':
return joinChains(
this.#buildChain(node.object),
this.#buildChain(node.property, insideCall),
)
case 'CallExpression':
return this.#buildChain(node.callee, true)
default:
return null
}
}
}
const resolvePossibleAliasedGlobal = (
context: Rule.RuleContext,
global: string,
) => {
const globalAliases: Record<string, string[]> =
context.settings.playwright?.globalAliases ?? {}
const alias = Object.entries(globalAliases).find(([, aliases]) =>
aliases.includes(global),
)
return alias?.[0] ?? null
}
interface ResolvedFn {
local: string
original: string | null
}
const resolveToPlaywrightFn = (
context: Rule.RuleContext,
accessor: AccessorNode,
): ResolvedFn | null => {
const ident = getStringValue(accessor)
const resolved = /(^expect|Expect)$/.test(ident) ? 'expect' : ident
return {
// eslint-disable-next-line sort/object-properties
original: resolvePossibleAliasedGlobal(context, resolved),
local: resolved,
}
}
export type FnGroup =
| 'describe'
| 'expect'
| 'hook'
| 'step'
| 'test'
| 'unknown'
export type FnType = FnGroup | 'config'
function determinePlaywrightFnGroup(name: string): FnGroup {
if (name === 'step') return 'step'
if (name === 'expect') return 'expect'
if (name === 'describe') return 'describe'
if (name === 'test') return 'test'
if (testHooks.has(name)) return 'hook'
return 'unknown'
}
export const modifiers = new Set(['not', 'resolves', 'rejects'])
const findModifiersAndMatcher = (
chain: Chain,
members: KnownMemberExpressionProperty[],
stage: ExpectParseStage,
): ModifiersAndMatcher | string | null => {
const modifiers: KnownMemberExpressionProperty[] = []
for (const member of members) {
const name = getStringValue(member)
if (name === 'soft' || name === 'poll') {
// soft and poll must be the first modifier
if (modifiers.length > 0) {
return 'modifier-unknown'
}
} else if (name === 'resolves' || name === 'rejects') {
const lastModifier = getStringValue(modifiers.at(-1))
// resolves and rejects must be the first modifier following soft or poll
if (lastModifier && lastModifier !== 'soft' && lastModifier !== 'poll') {
return 'modifier-unknown'
}
} else if (name !== 'not') {
// If we're in the "modifiers" stage and we find an unknown modifier,
// then it's actually an asymmetric matcher which we don't care about.
if (stage === 'modifiers') {
return null
}
// Check if the member is being called, which means it is the matcher
// (and also the end of the entire "expect" call chain).
if (
member.parent?.type === 'MemberExpression' &&
member.parent.parent?.type === 'CallExpression'
) {
return {
matcher: member,
matcherArgs: member.parent.parent.arguments,
matcherName: name,
modifiers,
}
}
// not is the only other allowed modifier
return 'modifier-unknown'
}
// When we find a leaf node, we're done with the modifiers and are moving
// on to the matchers.
if (chain.isLeaf(member)) {
stage = 'matchers'
}
// Add the modifier to the list of modifiers
modifiers.push(member)
}
// This will only really happen if there are no members
return 'matcher-not-found'
}
function getExpectArguments(
call: Omit<ParsedFnCall, 'group' | 'type'>,
): ESTree.CallExpression['arguments'] {
return findParent(call.head.node, 'CallExpression')?.arguments ?? []
}
type KnownMemberExpression = ESTree.MemberExpression & {
parent: ESTree.CallExpression
}
type KnownMemberExpressionProperty = AccessorNode & {
parent: KnownMemberExpression
}
export interface ResolvedFnWithNode extends ResolvedFn {
node: AccessorNode
}
interface BaseParsedFnCall {
head: ResolvedFnWithNode
members: KnownMemberExpressionProperty[]
/**
* The name of the underlying Playwright function that is being called. This
* is the result of `(head.original ?? head.local)`.
*/
name: string
type: FnType
}
interface ParsedGeneralFnCall extends BaseParsedFnCall {
group: Exclude<FnGroup, 'expect'>
type: Exclude<FnType, 'expect'>
}
interface ModifiersAndMatcher {
matcher: KnownMemberExpressionProperty
matcherArgs: ESTree.CallExpression['arguments']
matcherName: string
modifiers: KnownMemberExpressionProperty[]
}
export interface ParsedExpectFnCall
extends BaseParsedFnCall,
ModifiersAndMatcher {
args: ESTree.CallExpression['arguments']
group: 'expect'
type: 'expect'
}
export type ParsedFnCall = ParsedGeneralFnCall | ParsedExpectFnCall
type ExpectParseStage = 'matchers' | 'modifiers'
const parseExpectCall = (
chain: Chain,
call: Omit<ParsedFnCall, 'group' | 'type'>,
stage: ExpectParseStage,
): ParsedExpectFnCall | string | null => {
const modifiersAndMatcher = findModifiersAndMatcher(
chain,
call.members,
stage,
)
if (!modifiersAndMatcher) {
return null
}
if (typeof modifiersAndMatcher === 'string') {
return modifiersAndMatcher
}
return {
...call,
args: getExpectArguments(call),
group: 'expect',
type: 'expect',
...modifiersAndMatcher,
}
}
export const findTopMostCallExpression = (
node: ESTree.CallExpression,
): ESTree.CallExpression & Rule.NodeParentExtension => {
let top = node
let parent = getParent(node)
let child: ESTree.Node = node
while (parent) {
// If the parent is a call expression, then we set that as the new top-most
// call expression and continue up the chain. We have to verify though that
// the child is the callee of the parent call expression and not an argument
// as this is valid: `expect(x).not.resolves.toBe(x)`, but this is not:
// `something(expect(x).not.resolves.toBe)`.
if (parent.type === 'CallExpression' && parent.callee === child) {
top = parent
node = parent
parent = getParent(parent)
continue
}
if (parent.type !== 'MemberExpression') {
break
}
child = parent
parent = getParent(parent)
}
return top as ESTree.CallExpression & Rule.NodeParentExtension
}
function parse(
context: Rule.RuleContext,
node: ESTree.CallExpression,
): ParsedFnCall | string | null {
const chain = new Chain(node)
if (!chain.nodes?.length) return null
const [first, ...rest] = chain.nodes
const resolved = resolveToPlaywrightFn(context, first)
if (!resolved) return null
let name = resolved.original ?? resolved.local
const links = [name, ...rest.map((link) => getStringValue(link))]
// To support Playwright's convention of `test.describe`, `test.beforeEach`,
// etc. we need to test the second link in the chain to find the true type.
if (name === 'test' && links.length > 1) {
const nextLinkName = links[1]
const nextLinkGroup = determinePlaywrightFnGroup(nextLinkName)
if (nextLinkGroup !== 'unknown') {
name = nextLinkName
}
}
if (name !== 'expect' && !VALID_CHAINS.has(links.join('.'))) {
return null
}
const parsedFnCall: Omit<ParsedFnCall, 'group' | 'type'> = {
head: { ...resolved, node: first },
// every member node must have a member expression as their parent
// in order to be part of the call chain we're parsing
members: rest as KnownMemberExpressionProperty[],
name,
}
const group = determinePlaywrightFnGroup(name)
if (group === 'expect') {
let stage: ExpectParseStage = chain.isLeaf(parsedFnCall.head.node)
? 'matchers'
: 'modifiers'
// If using `test.expect` style, the `rest` array will start with `expect`
// and we need to remove it to ensure the chain accurately represents the
// `expect` call chain.
if (isIdentifier(rest[0], 'expect')) {
stage = chain.isLeaf(rest[0]) ? 'matchers' : 'modifiers'
parsedFnCall.members.shift()
}
const result = parseExpectCall(chain, parsedFnCall, stage)
if (!result) return null
// If the `expect` call chain is not valid, only report on the topmost node
// since all members in the chain are likely to get flagged for some reason
if (
typeof result === 'string' &&
findTopMostCallExpression(node) !== node
) {
return null
}
if (result === 'matcher-not-found') {
if (getParent(node)?.type === 'MemberExpression') {
return 'matcher-not-called'
}
}
return result
}
// Check that every link in the chain except the last is a member expression
if (
chain.nodes
.slice(0, chain.nodes.length - 1)
.some((n) => getParent(n)?.type !== 'MemberExpression')
) {
return null
}
// Ensure that we're at the "top" of the function call chain otherwise when
// parsing e.g. x().y.z(), we'll incorrectly find & parse "x()" even though
// the full chain is not a valid Playwright function call chain
const parent = getParent(node)
if (
parent?.type === 'CallExpression' ||
parent?.type === 'MemberExpression'
) {
return null
}
// If the call is a configuration hook. E.g., `test.skip(true)`, `test.use()`,
// `test.describe.configure()`, etc.
let type: FnType = group
if (
(name === 'test' || name === 'describe') &&
(node.arguments.length < 2 || !isFunction(node.arguments.at(-1)))
) {
type = 'config'
}
return {
...parsedFnCall,
group,
type,
}
}
const cache = new WeakMap<ESTree.CallExpression, ParsedFnCall | string | null>()
export function parseFnCallWithReason(
context: Rule.RuleContext,
node: ESTree.CallExpression,
): ParsedFnCall | string | null {
if (cache.has(node)) {
return cache.get(node)!
}
const call = parse(context, node)
cache.set(node, call)
return call
}
export function parseFnCall(
context: Rule.RuleContext,
node: ESTree.CallExpression,
): ParsedFnCall | null {
const call = parseFnCallWithReason(context, node)
return typeof call === 'string' ? null : call
}
export const isTypeOfFnCall = (
context: Rule.RuleContext,
node: ESTree.CallExpression,
types: FnType[],
): boolean => {
const call = parseFnCall(context, node)
return call !== null && types.includes(call.type)
}