|
1 | 1 | 'use strict' |
2 | 2 |
|
| 3 | +const { basename } = require('path') |
| 4 | + |
| 5 | +const NAME = basename(__dirname) |
| 6 | +const DESCRIPTION = 'Actions should be in the end of chains, not in the middle' |
| 7 | + |
| 8 | +/** |
| 9 | + * Commands listed in the documentation with text: 'It is unsafe to chain further commands that rely on the subject after xxx.' |
| 10 | + * See {@link https://docs.cypress.io/guides/core-concepts/retry-ability#Actions-should-be-at-the-end-of-chains-not-the-middle Actions should be at the end of chains, not the middle} |
| 11 | + * for more information. |
| 12 | + * |
| 13 | + * @type {string[]} |
| 14 | + */ |
| 15 | +const unsafeToChainActions = [ |
| 16 | + 'blur', |
| 17 | + 'clear', |
| 18 | + 'click', |
| 19 | + 'check', |
| 20 | + 'dblclick', |
| 21 | + 'each', |
| 22 | + 'focus', |
| 23 | + 'rightclick', |
| 24 | + 'screenshot', |
| 25 | + 'scrollIntoView', |
| 26 | + 'scrollTo', |
| 27 | + 'select', |
| 28 | + 'selectFile', |
| 29 | + 'spread', |
| 30 | + 'submit', |
| 31 | + 'type', |
| 32 | + 'trigger', |
| 33 | + 'uncheck', |
| 34 | + 'within', |
| 35 | +] |
| 36 | + |
| 37 | +/** |
| 38 | + * @type {import('eslint').Rule.RuleMetaData['schema']} |
| 39 | + */ |
| 40 | +const schema = { |
| 41 | + title: NAME, |
| 42 | + description: DESCRIPTION, |
| 43 | + type: 'object', |
| 44 | + properties: { |
| 45 | + methods: { |
| 46 | + type: 'array', |
| 47 | + description: |
| 48 | + 'An additional list of methods to check for unsafe chaining.', |
| 49 | + default: [], |
| 50 | + }, |
| 51 | + }, |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * @param {import('eslint').Rule.RuleContext} context |
| 56 | + * @returns {Record<string, any>} |
| 57 | + */ |
| 58 | +const getDefaultOptions = (context) => { |
| 59 | + return Object.entries(schema.properties).reduce((acc, [key, value]) => { |
| 60 | + if (!(value.default in value)) return acc |
| 61 | + |
| 62 | + return { |
| 63 | + ...acc, |
| 64 | + [key]: value.default, |
| 65 | + } |
| 66 | + }, context.options[0] || {}) |
| 67 | +} |
| 68 | + |
| 69 | +/** @type {import('eslint').Rule.RuleModule} */ |
3 | 70 | module.exports = { |
4 | 71 | meta: { |
5 | 72 | docs: { |
6 | | - description: 'Actions should be in the end of chains, not in the middle', |
| 73 | + description: DESCRIPTION, |
7 | 74 | category: 'Possible Errors', |
8 | 75 | recommended: true, |
9 | 76 | url: 'https://docs.cypress.io/guides/core-concepts/retry-ability#Actions-should-be-at-the-end-of-chains-not-the-middle', |
10 | 77 | }, |
11 | | - schema: [], |
| 78 | + schema: [schema], |
12 | 79 | messages: { |
13 | | - 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.', |
| 80 | + unexpected: |
| 81 | + '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.', |
14 | 82 | }, |
15 | 83 | }, |
16 | 84 | create (context) { |
| 85 | + const { methods } = getDefaultOptions(context) |
| 86 | + |
17 | 87 | return { |
18 | 88 | CallExpression (node) { |
19 | | - if (isRootCypress(node) && isActionUnsafeToChain(node) && node.parent.type === 'MemberExpression') { |
20 | | - context.report({ node, messageId: 'unexpected' }) |
| 89 | + if ( |
| 90 | + isRootCypress(node) && |
| 91 | + isActionUnsafeToChain(node, methods) && |
| 92 | + node.parent.type === 'MemberExpression' |
| 93 | + ) { |
| 94 | + context.report({ |
| 95 | + node, |
| 96 | + messageId: 'unexpected', |
| 97 | + }) |
21 | 98 | } |
22 | 99 | }, |
23 | 100 | } |
24 | 101 | }, |
25 | 102 | } |
26 | 103 |
|
27 | | -function isRootCypress (node) { |
28 | | - while (node.type === 'CallExpression') { |
29 | | - if (node.callee.type !== 'MemberExpression') return false |
30 | | - |
31 | | - if (node.callee.object.type === 'Identifier' && |
32 | | - node.callee.object.name === 'cy') { |
33 | | - return true |
34 | | - } |
| 104 | +/** |
| 105 | + * @param {import('estree').Node} node |
| 106 | + * @returns {boolean} |
| 107 | + */ |
| 108 | +const isRootCypress = (node) => { |
| 109 | + if ( |
| 110 | + node.type !== 'CallExpression' || |
| 111 | + node.callee.type !== 'MemberExpression' |
| 112 | + ) { |
| 113 | + return false |
| 114 | + } |
35 | 115 |
|
36 | | - node = node.callee.object |
| 116 | + if ( |
| 117 | + node.callee.object.type === 'Identifier' && |
| 118 | + node.callee.object.name === 'cy' |
| 119 | + ) { |
| 120 | + return true |
37 | 121 | } |
38 | 122 |
|
39 | | - return false |
| 123 | + return isRootCypress(node.callee.object) |
40 | 124 | } |
41 | 125 |
|
42 | | -function isActionUnsafeToChain (node) { |
43 | | - // commands listed in the documentation with text: 'It is unsafe to chain further commands that rely on the subject after xxx' |
44 | | - const unsafeToChainActions = ['blur', 'clear', 'click', 'check', 'dblclick', 'each', 'focus', 'rightclick', 'screenshot', 'scrollIntoView', 'scrollTo', 'select', 'selectFile', 'spread', 'submit', 'type', 'trigger', 'uncheck', 'within'] |
| 126 | +/** |
| 127 | + * @param {import('estree').Node} node |
| 128 | + * @param {(string | RegExp)[]} additionalMethods |
| 129 | + */ |
| 130 | +const isActionUnsafeToChain = (node, additionalMethods = []) => { |
| 131 | + const unsafeActionsRegex = new RegExp([ |
| 132 | + ...unsafeToChainActions, |
| 133 | + ...additionalMethods.map((method) => method instanceof RegExp ? method.source : method), |
| 134 | + ].join('|')) |
45 | 135 |
|
46 | | - return node.callee && node.callee.property && node.callee.property.type === 'Identifier' && unsafeToChainActions.includes(node.callee.property.name) |
| 136 | + return ( |
| 137 | + node.callee && |
| 138 | + node.callee.property && |
| 139 | + node.callee.property.type === 'Identifier' && |
| 140 | + unsafeActionsRegex.test(node.callee.property.name) |
| 141 | + ) |
47 | 142 | } |
0 commit comments