Skip to content

Commit 943f162

Browse files
authoredJul 24, 2024··
feat(catch-or-return, prefer-await-to-then): do not report Cypress commands (#495)
Fixes #180 **What is the purpose of this pull request?** - [x] Changes an existing rule **What changes did you make? (Give an overview)** Do not report Cypress commands (e.g., `cy.get('sel').then()`) for `catch-or-return` and `prefer-await-to-then`.
1 parent e1ce2ad commit 943f162

5 files changed

+39
-1
lines changed
 

‎__tests__/catch-or-return.js

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ ruleTester.run('catch-or-return', rule, {
2222
'Promise.resolve(frank)["catch"](jail)',
2323
'frank.then(to).finally(fn).catch(jail)',
2424

25+
// Cypress
26+
'cy.get(".myClass").then(go)',
27+
'cy.get("button").click().then()',
28+
2529
// arrow function use case
2630
'postJSON("/smajobber/api/reportJob.json")\n\t.then(()=>this.setState())\n\t.catch(()=>this.setState())',
2731

‎__tests__/prefer-await-to-then.js

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ ruleTester.run('prefer-await-to-then', rule, {
1616
'async function hi() { await thing().then() }',
1717
'async function hi() { await thing().catch() }',
1818
'async function hi() { await thing().finally() }',
19+
20+
// Cypress
21+
'function hi() { cy.get(".myClass").then(go) }',
22+
'function hi() { cy.get("button").click().then() }',
23+
1924
'function * hi() { yield thing().then() }',
2025
'a = async () => (await something())',
2126
`a = async () => {

‎rules/catch-or-return.js

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
const getDocsUrl = require('./lib/get-docs-url')
1010
const isPromise = require('./lib/is-promise')
11+
const isMemberCallWithObjectName = require('./lib/is-member-call-with-object-name')
1112

1213
module.exports = {
1314
meta: {
@@ -98,6 +99,11 @@ module.exports = {
9899
return true
99100
}
100101

102+
// cy.get().then(a, b);
103+
if (isMemberCallWithObjectName('cy', expression)) {
104+
return true
105+
}
106+
101107
return false
102108
}
103109

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
/**
4+
* @param {string} objectName
5+
* @param {Node} node
6+
* @returns {node is CallExpression}
7+
*/
8+
function isMemberCallWithObjectName(objectName, node) {
9+
return (
10+
node.type === 'CallExpression' &&
11+
node.callee.type === 'MemberExpression' &&
12+
((node.callee.object.type === 'Identifier' &&
13+
node.callee.object.name === objectName) ||
14+
isMemberCallWithObjectName(objectName, node.callee.object))
15+
)
16+
}
17+
18+
module.exports = isMemberCallWithObjectName

‎rules/prefer-await-to-then.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const { getAncestors, getScope } = require('./lib/eslint-compat')
99
const getDocsUrl = require('./lib/get-docs-url')
10+
const isMemberCallWithObjectName = require('./lib/is-member-call-with-object-name')
1011

1112
module.exports = {
1213
meta: {
@@ -53,7 +54,11 @@ module.exports = {
5354

5455
return {
5556
'CallExpression > MemberExpression.callee'(node) {
56-
if (isTopLevelScoped(node) || (!strict && isInsideYieldOrAwait(node))) {
57+
if (
58+
isTopLevelScoped(node) ||
59+
(!strict && isInsideYieldOrAwait(node)) ||
60+
isMemberCallWithObjectName('cy', node.parent)
61+
) {
5762
return
5863
}
5964

0 commit comments

Comments
 (0)
Please sign in to comment.