/
no-unnecessary-waiting.js
59 lines (51 loc) · 1.54 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
/**
* @fileoverview Prevent waiting for arbitrary time periods
* @author Elad Shahar
*/
'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 isCallingCyWait (node) {
return node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'cy' &&
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[0].type !== 'Identifier') {
return false
}
const resolvedIdentifier = scope.resolve(node.arguments[0]).resolved
const IdentifierValue = resolvedIdentifier.defs[0].node.init.value
return typeof IdentifierValue === 'number'
}