diff --git a/__tests__/catch-or-return.js b/__tests__/catch-or-return.js index c4426d5b..324f1482 100644 --- a/__tests__/catch-or-return.js +++ b/__tests__/catch-or-return.js @@ -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())', diff --git a/__tests__/prefer-await-to-then.js b/__tests__/prefer-await-to-then.js index 2770c7b2..897002a1 100644 --- a/__tests__/prefer-await-to-then.js +++ b/__tests__/prefer-await-to-then.js @@ -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 () => { diff --git a/rules/catch-or-return.js b/rules/catch-or-return.js index a22da314..c12b82fc 100644 --- a/rules/catch-or-return.js +++ b/rules/catch-or-return.js @@ -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: { @@ -98,6 +99,11 @@ module.exports = { return true } + // cy.get().then(a, b); + if (isMemberCallWithObjectName('cy', expression)) { + return true + } + return false } diff --git a/rules/lib/is-member-call-with-object-name b/rules/lib/is-member-call-with-object-name new file mode 100644 index 00000000..60107e02 --- /dev/null +++ b/rules/lib/is-member-call-with-object-name @@ -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 diff --git a/rules/prefer-await-to-then.js b/rules/prefer-await-to-then.js index 3c2fab76..6fafa8dc 100644 --- a/rules/prefer-await-to-then.js +++ b/rules/prefer-await-to-then.js @@ -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: { @@ -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 }