Skip to content

Commit 071a41b

Browse files
committed
refactor(pencil): add jsdoc types. use 'in' operator
1 parent 49c6eb3 commit 071a41b

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

lib/rules/unsafe-to-chain-command.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const DESCRIPTION = 'Actions should be in the end of chains, not in the middle'
99
* Commands listed in the documentation with text: 'It is unsafe to chain further commands that rely on the subject after xxx.'
1010
* 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}
1111
* for more information.
12+
*
13+
* @type {string[]}
1214
*/
1315
const unsafeToChainActions = [
1416
'blur',
@@ -32,20 +34,9 @@ const unsafeToChainActions = [
3234
'within',
3335
]
3436

35-
const getDefaultOptions = (schema, context) => {
36-
return {
37-
...Object.entries(schema.properties).reduce((acc, [key, value]) => {
38-
if (value.default === undefined) return acc
39-
40-
return {
41-
...acc,
42-
[key]: value.default,
43-
}
44-
}, {}),
45-
...context.options[0],
46-
}
47-
}
48-
37+
/**
38+
* @type {import('eslint').Rule.RuleMetaData['schema']}
39+
*/
4940
const schema = {
5041
title: NAME,
5142
description: DESCRIPTION,
@@ -60,6 +51,21 @@ const schema = {
6051
},
6152
}
6253

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+
6369
/** @type {import('eslint').Rule.RuleModule} */
6470
module.exports = {
6571
meta: {
@@ -76,7 +82,7 @@ module.exports = {
7682
},
7783
},
7884
create (context) {
79-
const { methods } = getDefaultOptions(schema, context)
85+
const { methods } = getDefaultOptions(context)
8086

8187
return {
8288
CallExpression (node) {
@@ -95,8 +101,17 @@ module.exports = {
95101
},
96102
}
97103

98-
function isRootCypress (node) {
99-
if (node.type !== 'CallExpression' || node.callee.type !== 'MemberExpression') return
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+
}
100115

101116
if (
102117
node.callee.object.type === 'Identifier' &&
@@ -108,7 +123,11 @@ function isRootCypress (node) {
108123
return isRootCypress(node.callee.object)
109124
}
110125

111-
function isActionUnsafeToChain (node, additionalMethods) {
126+
/**
127+
* @param {import('estree').Node} node
128+
* @param {string[] | RegExp[]} additionalMethods
129+
*/
130+
const isActionUnsafeToChain = (node, additionalMethods = []) => {
112131
const unsafeActionsRegex = new RegExp([
113132
...unsafeToChainActions,
114133
...additionalMethods.map((method) => method instanceof RegExp ? method.source : method),

0 commit comments

Comments
 (0)