-
Notifications
You must be signed in to change notification settings - Fork 95
feat: add no-chained-get rule #249
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
Changes from 2 commits
18474cf
02ad33f
f36fd59
724209d
17435b0
0a1f26c
69a2473
da12e3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Disallow chain of `cy.get()` calls (`cypress/no-chained-get`) | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
| This rule disallow the usage of chained `.get()` calls as `cy.get()` always starts its search from the cy.root element. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| cy.get('parent').get('child') | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| cy.get('parent') | ||
| .find('child') | ||
| ``` | ||
|
|
||
| ```js | ||
| cy.get('parent').within(() => { | ||
| cy.get('child') | ||
| }) | ||
| ``` | ||
|
|
||
| ## Further Reading | ||
|
|
||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| 'use strict'; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Rule Definition | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| module.exports = { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'disallow chain of `cy.get()` calls', | ||
| recommended: false, | ||
| url: 'https://github.com/cypress-io/eslint-plugin-cypress/blob/master/docs/rules/no-chained-get.md', | ||
| }, | ||
| fixable: null, | ||
| schema: [], | ||
| messages: { | ||
| unexpected: 'Avoid chaining multiple cy.get() calls.', | ||
jennifer-shehane marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| const 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); | ||
| }; | ||
|
|
||
| const hasChainedGet = (node) => { | ||
| // Check if this node is a get() call | ||
| const isGetCall = | ||
| node.callee && | ||
| node.callee.type === 'MemberExpression' && | ||
| node.callee.property && | ||
| node.callee.property.type === 'Identifier' && | ||
| node.callee.property.name === 'get'; | ||
|
|
||
| if (!isGetCall) { | ||
| return false; | ||
| } | ||
|
|
||
| const obj = node.callee.object; | ||
|
|
||
| if (obj.type === 'CallExpression') { | ||
| const objCallee = obj.callee; | ||
|
|
||
| if ( | ||
| objCallee && | ||
| objCallee.type === 'MemberExpression' && | ||
| objCallee.property && | ||
| objCallee.property.type === 'Identifier' && | ||
| objCallee.property.name === 'get' | ||
| ) { | ||
| return true; | ||
| } | ||
|
|
||
| return hasChainedGet(obj); | ||
| } | ||
|
|
||
| return false; | ||
| }; | ||
|
|
||
| return { | ||
| CallExpression(node) { | ||
| if (isRootCypress(node) && hasChainedGet(node)) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'unexpected', | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * @fileoverview disallow chain of `cy.get()` calls | ||
| * @author benoit | ||
| */ | ||
| "use strict"; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Requirements | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| const rule = require("../../../lib/rules/no-chained-get"), | ||
| RuleTester = require("eslint").RuleTester; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Tests | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| const ruleTester = new RuleTester(); | ||
| ruleTester.run("no-chained-get", rule, { | ||
| valid: [ | ||
| { code: "cy.get('div')" }, | ||
| { code: "cy.get('.div').find().get()" }, | ||
| { code: "cy.get('input').should('be.disabled')" }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "cy.get('div').get('div')", | ||
| errors: [{ messageId: "unexpected" }], | ||
| }, | ||
| ], | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.