Skip to content

feat(catch-or-return, prefer-await-to-then): do not report Cypress commands #495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading