Skip to content

Commit

Permalink
feat(catch-or-return, prefer-await-to-then): do not report Cypres…
Browse files Browse the repository at this point in the history
…s 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`.
  • Loading branch information
brettz9 authored Jul 24, 2024
1 parent e1ce2ad commit 943f162
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions __tests__/catch-or-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ ruleTester.run('catch-or-return', rule, {
'Promise.resolve(frank)["catch"](jail)',
'frank.then(to).finally(fn).catch(jail)',

// Cypress
'cy.get(".myClass").then(go)',
'cy.get("button").click().then()',

// arrow function use case
'postJSON("/smajobber/api/reportJob.json")\n\t.then(()=>this.setState())\n\t.catch(()=>this.setState())',

Expand Down
5 changes: 5 additions & 0 deletions __tests__/prefer-await-to-then.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ ruleTester.run('prefer-await-to-then', rule, {
'async function hi() { await thing().then() }',
'async function hi() { await thing().catch() }',
'async function hi() { await thing().finally() }',

// Cypress
'function hi() { cy.get(".myClass").then(go) }',
'function hi() { cy.get("button").click().then() }',

'function * hi() { yield thing().then() }',
'a = async () => (await something())',
`a = async () => {
Expand Down
6 changes: 6 additions & 0 deletions rules/catch-or-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

module.exports = {
meta: {
Expand Down Expand Up @@ -98,6 +99,11 @@ module.exports = {
return true
}

// cy.get().then(a, b);
if (isMemberCallWithObjectName('cy', expression)) {
return true
}

return false
}

Expand Down
18 changes: 18 additions & 0 deletions rules/lib/is-member-call-with-object-name
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

/**
* @param {string} objectName
* @param {Node} node
* @returns {node is CallExpression}
*/
function isMemberCallWithObjectName(objectName, node) {
return (
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
((node.callee.object.type === 'Identifier' &&
node.callee.object.name === objectName) ||
isMemberCallWithObjectName(objectName, node.callee.object))
)
}

module.exports = isMemberCallWithObjectName
7 changes: 6 additions & 1 deletion rules/prefer-await-to-then.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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

module.exports = {
meta: {
Expand Down Expand Up @@ -53,7 +54,11 @@ module.exports = {

return {
'CallExpression > MemberExpression.callee'(node) {
if (isTopLevelScoped(node) || (!strict && isInsideYieldOrAwait(node))) {
if (
isTopLevelScoped(node) ||
(!strict && isInsideYieldOrAwait(node)) ||
isMemberCallWithObjectName('cy', node.parent)
) {
return
}

Expand Down

0 comments on commit 943f162

Please sign in to comment.