Skip to content
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

Swap to fork of eslint-plugin-cypress, add missing recommended rule #106

Merged
merged 5 commits into from
May 25, 2023
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
8 changes: 8 additions & 0 deletions .changeset/funny-garlics-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'eslint-config-seek': patch
---

Replace `eslint-plugin-cypress` with the [`@finsit/eslint-plugin-cypress`] fork that supports ESLint v8.
Consumers that were overriding `cypress/*` rules will need to override `@finsit/cypress/*` rules instead.

[`@finsit/eslint-plugin-cypress`]: https://github.com/foretagsplatsen/eslint-plugin-cypress
12 changes: 9 additions & 3 deletions base.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,17 @@ const baseConfig = {
{
// Cypress config
files: [`**/cypress/**/*.{${allExtensions}}`],
// eslint-plugin-cypress doesn't support ESLint v8.
// Use fork by `@finsit` until this is solved.
// https://github.com/cypress-io/eslint-plugin-cypress/issues/89
extends: ['plugin:@finsit/cypress/recommended'],
env: {
'cypress/globals': true,
'@finsit/cypress/globals': true,
askoufis marked this conversation as resolved.
Show resolved Hide resolved
},
plugins: ['@finsit/cypress', 'eslint-plugin-local-rules'],
rules: {
'local-rules/unsafe-to-chain-command': ERROR,
},
extends: ['plugin:cypress/recommended'],
plugins: ['cypress'],
},
],
};
Expand Down
3 changes: 3 additions & 0 deletions eslint-local-rules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'unsafe-to-chain-command': require('./unsafe-to-chain-command.js'),
};
88 changes: 88 additions & 0 deletions eslint-local-rules/unsafe-to-chain-command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/** This rule is copied from the original `eslint-plugin-cypress` so we can use the fork (which
* supports eslint 8) while having the same recommended rules as the upstream
* https://github.com/foretagsplatsen/eslint-plugin-cypress
* https://github.com/cypress-io/eslint-plugin-cypress/blob/c626ad543f65babf1def5caabd1bc9bb9900d2c7/lib/rules/unsafe-to-chain-command.js
*/
// eslint-disable-next-line strict
'use strict';

module.exports = {
meta: {
docs: {
description: 'Actions should be in the end of chains, not in the middle',
category: 'Possible Errors',
recommended: true,
url: 'https://docs.cypress.io/guides/core-concepts/retry-ability#Actions-should-be-at-the-end-of-chains-not-the-middle',
},
schema: [],
messages: {
unexpected:
'It is unsafe to chain further commands that rely on the subject after this command. It is best to split the chain, chaining again from `cy.` in a next command line.',
},
},
create(context) {
return {
CallExpression(node) {
if (
isRootCypress(node) &&
isActionUnsafeToChain(node) &&
node.parent.type === 'MemberExpression'
) {
context.report({ node, messageId: 'unexpected' });
}
},
};
},
};

function isRootCypress(node) {
while (node.type === 'CallExpression') {
if (node.callee.type !== 'MemberExpression') {
return false;
}

if (
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'cy'
) {
return true;
}

// eslint-disable-next-line no-param-reassign
node = node.callee.object;
}

return false;
}

function isActionUnsafeToChain(node) {
// commands listed in the documentation with text: 'It is unsafe to chain further commands that rely on the subject after xxx'
const unsafeToChainActions = [
'blur',
'clear',
'click',
'check',
'dblclick',
'each',
'focus',
'rightclick',
'screenshot',
'scrollIntoView',
'scrollTo',
'select',
'selectFile',
'spread',
'submit',
'type',
'trigger',
'uncheck',
'within',
];

return (
node.callee &&
node.callee.property &&
node.callee.property.type === 'Identifier' &&
unsafeToChainActions.includes(node.callee.property.name)
);
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"files": [
"index.js",
"base.js",
"extensions.js"
"extensions.js",
"eslint-local-rules/*"
],
"repository": {
"type": "git",
Expand All @@ -27,13 +28,14 @@
"@babel/core": "^7.21.0",
"@babel/eslint-parser": "^7.19.1",
"@babel/preset-react": "^7.18.6",
"@finsit/eslint-plugin-cypress": "^3.1.1",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@typescript-eslint/parser": "^5.53.0",
"eslint-config-prettier": "^8.6.0",
"eslint-import-resolver-typescript": "3.5.3",
"eslint-plugin-cypress": "^2.12.1",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jest": "^27.2.1",
"eslint-plugin-local-rules": "^1.3.2",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"find-root": "^1.1.0"
Expand Down
32 changes: 20 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.