-
Notifications
You must be signed in to change notification settings - Fork 97
feat: add no-add rule to disallow .and() in favor of .should() #310
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fa8fb33
feat: add no-add rule to disallow .and() in favor of .should()
todd-m-kemp b722323
Fix naming typo (and vs. add).
todd-m-kemp 43e1684
feat: update no-and rule to allow .and() after .should(), .and(), and…
todd-m-kemp 683a1fc
test: add comprehensive test coverage for no-and rule allowlist behavior
todd-m-kemp b698ddf
docs: clarify no-and rule allows .and() after .should(), .and(), and …
todd-m-kemp afef6f0
refactor: rename no-and rule to no-starting-and for clarity
todd-m-kemp a01b305
cleanup and rename
mschile eca7c7e
fix rename
mschile ee566f7
refactor is-root-cypress
mschile File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # cypress/no-and | ||
|
|
||
| 📝 Enforce `.should()` over `.and()` for starting assertion chains. | ||
|
|
||
| 🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| Cypress's [.and()](https://on.cypress.io/and) is an alias for [.should()](https://on.cypress.io/should). Using `.and()` reads naturally when it follows `.should()` or `.contains()` — it continues an assertion chain like "should be empty **and** be hidden." In other positions, `.should()` is clearer. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule allows `.and()` only when it immediately follows `.should()`, `.and()`, or `.contains()`. In all other positions, it flags `.and()` and auto-fixes it to `.should()`. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| cy.get('elem').and('have.text', 'blah') | ||
| cy.get('foo').find('.bar').and('have.class', 'active') | ||
| cy.get('foo').click().and('be.disabled') | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| cy.get('elem').should('have.text', 'blah') | ||
| cy.get('.err').should('be.empty').and('be.hidden') | ||
| cy.contains('Login').and('be.visible') | ||
| cy.get('foo').should('be.visible').and('have.text', 'bar').and('have.class', 'active') | ||
| ``` | ||
|
|
||
| ## When Not To Use It | ||
|
|
||
| If you prefer using `.and()` interchangeably with `.should()` in all positions, turn this rule off. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| 'use strict' | ||
|
|
||
| const { isRootCypress } = require('../utils/is-root-cypress') | ||
|
|
||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| module.exports = { | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: 'enforce .should() over .and() for starting assertion chains', | ||
| recommended: false, | ||
| url: 'https://github.com/cypress-io/eslint-plugin-cypress/blob/master/docs/rules/no-and.md', | ||
| }, | ||
| fixable: 'code', | ||
| schema: [], | ||
| messages: { | ||
| unexpected: | ||
| 'Use .should() here; .and() is only allowed after .should(), .and(), or .contains().', | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| const allowAndAfter = new Set(['should', 'and', 'contains']) | ||
|
|
||
| function isAllowedAfter(node) { | ||
| const object = node.callee.object | ||
| if ( | ||
| object | ||
| && object.type === 'CallExpression' | ||
| && object.callee.type === 'MemberExpression' | ||
| ) { | ||
| return allowAndAfter.has(object.callee.property.name) | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| return { | ||
| CallExpression(node) { | ||
| if ( | ||
| node.callee.type === 'MemberExpression' | ||
| && node.callee.property.name === 'and' | ||
| && isRootCypress(node) | ||
| && !isAllowedAfter(node) | ||
| ) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'unexpected', | ||
| fix(fixer) { | ||
| return fixer.replaceText(node.callee.property, 'should') | ||
| }, | ||
| }) | ||
| } | ||
| }, | ||
| } | ||
| }, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| 'use strict' | ||
|
|
||
| /** | ||
| * Whether `node` is a CallExpression that belongs to a Cypress command chain | ||
| * rooted at `cy` (walks `callee.object` through nested calls). | ||
| * | ||
| * @param {import('estree').Node} node | ||
| * @returns {boolean} | ||
| */ | ||
| function isRootCypress(node) { | ||
| if ( | ||
| node.type !== 'CallExpression' | ||
| || node.callee.type !== 'MemberExpression' | ||
| ) { | ||
| return false | ||
| } | ||
|
|
||
| if ( | ||
| node.callee.object.type === 'Identifier' | ||
| && node.callee.object.name === 'cy' | ||
| ) { | ||
| return true | ||
| } | ||
|
|
||
| return isRootCypress(node.callee.object) | ||
| } | ||
|
|
||
| module.exports = { isRootCypress } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.