From 0964b4edab5cc11c64a5a7948eddc643db0644bc Mon Sep 17 00:00:00 2001 From: Joris MASSON Date: Tue, 19 May 2020 19:11:40 +0200 Subject: [PATCH] fix: Allow aliases in require-data-selectors rule (#59) --- lib/rules/require-data-selectors.js | 10 ++++++++-- tests/lib/rules/require-data-selectors.js | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/rules/require-data-selectors.js b/lib/rules/require-data-selectors.js index f3f76afb..7325cff8 100644 --- a/lib/rules/require-data-selectors.js +++ b/lib/rules/require-data-selectors.js @@ -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) + }) +} diff --git a/tests/lib/rules/require-data-selectors.js b/tests/lib/rules/require-data-selectors.js index 92036bb0..93968bf5 100644 --- a/tests/lib/rules/require-data-selectors.js +++ b/tests/lib/rules/require-data-selectors.js @@ -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: [