Skip to content

Commit

Permalink
fix: Allow aliases in require-data-selectors rule (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyzual committed May 19, 2020
1 parent e4f1c4e commit 0964b4e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/rules/require-data-selectors.js
Expand Up @@ -40,7 +40,13 @@ function isCallingCyGet (node) {
function isDataArgument (node) {
return node.arguments.length > 0 &&
(
(node.arguments[0].type === 'Literal' && String(node.arguments[0].value).startsWith('[data-')) ||
(node.arguments[0].type === 'TemplateLiteral' && String(node.arguments[0].quasis[0].value.cooked).startsWith('[data-'))
(node.arguments[0].type === 'Literal' && isAliasOrDataSelector(String(node.arguments[0].value))) ||
(node.arguments[0].type === 'TemplateLiteral' && isAliasOrDataSelector(String(node.arguments[0].quasis[0].value.cooked)))
)
}

function isAliasOrDataSelector (selector) {
return ['[data-', '@'].some(function (validValue) {
return selector.startsWith(validValue)
})
}
2 changes: 2 additions & 0 deletions tests/lib/rules/require-data-selectors.js
Expand Up @@ -16,6 +16,8 @@ ruleTester.run('require-data-selectors', rule, {
{ code: 'cy.scrollTo(0, 10)', parserOptions },
{ code: 'cy.tick(500)', parserOptions },
{ code: 'cy.get(\`[data-cy=${1}]\`)', parserOptions },
{ code: 'cy.get("@my-alias")', parserOptions, errors },
{ code: 'cy.get(`@my-alias`)', parserOptions, errors },
],

invalid: [
Expand Down

0 comments on commit 0964b4e

Please sign in to comment.