Skip to content

Commit 91cda77

Browse files
committed
whitelist cy commands that synchronously return values
1 parent f8ca080 commit 91cda77

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

lib/rules/no-assigning-return-values.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55

66
'use strict'
77

8+
// safely get nested object property
9+
function get (obj, propertyString = '') {
10+
const properties = propertyString.split('.')
11+
for (let i = 0; i < properties.length; i++) {
12+
const value = (obj || {})[properties[i]]
13+
if (value == null) return value
14+
obj = value
15+
}
16+
return obj
17+
}
18+
819
module.exports = {
920
meta: {
1021
docs: {
@@ -29,21 +40,27 @@ module.exports = {
2940
},
3041
}
3142

32-
function isCypressCommandDeclaration (declarator) {
33-
if (!declarator) { return }
34-
if (!declarator.init) { return }
35-
if (!declarator.init.callee) { return }
43+
const whitelistedCommands = {
44+
now: true,
45+
spy: true,
46+
state: true,
47+
stub: true,
48+
}
3649

37-
let object = declarator.init.callee.object
50+
function isCypressCommandDeclaration (declarator) {
51+
let object = get(declarator, 'init.callee.object')
3852

39-
if (!object) { return }
53+
if (!object) return
4054

4155
while (object.callee) {
4256
object = object.callee.object
43-
if (!object) {
44-
return
45-
}
57+
58+
if (!object) return
4659
}
4760

61+
const commandName = get(object, 'parent.property.name')
62+
63+
if (commandName && whitelistedCommands[commandName]) return
64+
4865
return object.name === 'cy'
4966
}

tests/lib/rules/no-assigning-return-values.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ ruleTester.run('no-assigning-return-values', rule, {
1515
{ code: 'const foo = true;', parserOptions },
1616
{ code: 'const foo = bar();', parserOptions },
1717
{ code: 'const foo = bar().baz();', parserOptions },
18+
{ code: 'const spy = cy.spy();', parserOptions },
19+
{ code: 'const stub = cy.stub();', parserOptions },
20+
{ code: 'const result = cy.now();', parserOptions },
21+
{ code: 'const state = cy.state();', parserOptions },
1822
{ code: 'cy.get("foo");', parserOptions },
1923
{ code: 'cy.contains("foo").click();', parserOptions },
2024
],

0 commit comments

Comments
 (0)