-
Notifications
You must be signed in to change notification settings - Fork 91
/
no-unnecessary-waiting.js
81 lines (66 loc) · 2.31 KB
/
no-unnecessary-waiting.js
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
'use strict'
module.exports = {
meta: {
docs: {
description: 'Prevent waiting for arbitrary time periods',
category: 'Possible Errors',
recommended: true,
url: 'https://on.cypress.io/best-practices#Unnecessary-Waiting',
},
schema: [],
messages: {
unexpected: 'Do not wait for arbitrary time periods',
},
},
create (context) {
return {
CallExpression (node) {
if (isCallingCyWait(node)) {
const scope = context.getScope()
if (isIdentifierNumberConstArgument(node, scope) || isNumberArgument(node)) {
context.report({ node, messageId: 'unexpected' })
}
}
},
}
},
}
function nodeIsCalledByCy (node) {
if (node.type === 'Identifier' && node.name === 'cy') return true
if (typeof node.callee === 'undefined' || typeof node.callee.object === 'undefined') {
return false
}
return nodeIsCalledByCy(node.callee.object)
}
function isCallingCyWait (node) {
return node.callee.type === 'MemberExpression' &&
nodeIsCalledByCy(node) &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'wait'
}
function isNumberArgument (node) {
return node.arguments.length > 0 &&
node.arguments[0].type === 'Literal' &&
typeof (node.arguments[0].value) === 'number'
}
function isIdentifierNumberConstArgument (node, scope) {
if (node.arguments.length === 0) return false
if (node.arguments[0].type !== 'Identifier') return false
const identifier = node.arguments[0]
const resolvedIdentifier = scope.references.find((ref) => ref.identifier === identifier).resolved
const definition = resolvedIdentifier.defs[0]
const isVariable = definition.type === 'Variable'
// const amount = 1000 or const amount = '@alias'
// cy.wait(amount)
if (isVariable) {
if (!definition.node.init) return false
return typeof definition.node.init.value === 'number'
}
const param = definition.node.params[definition.index]
// function wait (amount) { cy.wait(amount) }
// we can't know the type of value, so don't fail
if (!param || param.type !== 'AssignmentPattern') return false
// function wait (amount = 1) { cy.wait(amount) } or
// function wait (amount = '@alias') { cy.wait(amount) }
return typeof param.right.value === 'number'
}