Skip to content

Commit 0964b4e

Browse files
authored
fix: Allow aliases in require-data-selectors rule (#59)
1 parent e4f1c4e commit 0964b4e

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/rules/require-data-selectors.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ function isCallingCyGet (node) {
4040
function isDataArgument (node) {
4141
return node.arguments.length > 0 &&
4242
(
43-
(node.arguments[0].type === 'Literal' && String(node.arguments[0].value).startsWith('[data-')) ||
44-
(node.arguments[0].type === 'TemplateLiteral' && String(node.arguments[0].quasis[0].value.cooked).startsWith('[data-'))
43+
(node.arguments[0].type === 'Literal' && isAliasOrDataSelector(String(node.arguments[0].value))) ||
44+
(node.arguments[0].type === 'TemplateLiteral' && isAliasOrDataSelector(String(node.arguments[0].quasis[0].value.cooked)))
4545
)
4646
}
47+
48+
function isAliasOrDataSelector (selector) {
49+
return ['[data-', '@'].some(function (validValue) {
50+
return selector.startsWith(validValue)
51+
})
52+
}

tests/lib/rules/require-data-selectors.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ ruleTester.run('require-data-selectors', rule, {
1616
{ code: 'cy.scrollTo(0, 10)', parserOptions },
1717
{ code: 'cy.tick(500)', parserOptions },
1818
{ code: 'cy.get(\`[data-cy=${1}]\`)', parserOptions },
19+
{ code: 'cy.get("@my-alias")', parserOptions, errors },
20+
{ code: 'cy.get(`@my-alias`)', parserOptions, errors },
1921
],
2022

2123
invalid: [

0 commit comments

Comments
 (0)