Skip to content

Commit 917aced

Browse files
Allow valid locators declared as variables (#376)
* Allow valid locators declared as variables * Format --------- Co-authored-by: Mark Skelton <mdskelton99@gmail.com>
1 parent 059f41a commit 917aced

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/rules/no-raw-locators.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ runRuleTester('no-raw-locators', rule, {
99
code: test('await page.locator()'),
1010
errors: [{ column: 34, endColumn: 48, line: 1, messageId }],
1111
},
12+
{
13+
code: test('const locator = await page.locator()'),
14+
errors: [{ column: 50, endColumn: 64, line: 1, messageId }],
15+
},
16+
{
17+
code: test('let locator = await page.locator()'),
18+
errors: [{ column: 48, endColumn: 62, line: 1, messageId }],
19+
},
1220
{
1321
code: test('await this.page.locator()'),
1422
errors: [{ column: 34, endColumn: 53, line: 1, messageId }],
@@ -37,7 +45,14 @@ runRuleTester('no-raw-locators', rule, {
3745
),
3846
errors: [{ column: 77, endColumn: 100, line: 1, messageId }],
3947
},
40-
48+
{
49+
code: test('const button = page.locator(); page.locator(button)'),
50+
errors: [{ column: 43, endColumn: 57, line: 1, messageId }],
51+
},
52+
{
53+
code: test('let button = page.locator(); page.locator(button)'),
54+
errors: [{ column: 41, endColumn: 55, line: 1, messageId }],
55+
},
4156
// Allowed
4257
{
4358
code: test('await page.locator("[aria-busy=false]")'),
@@ -81,6 +96,14 @@ runRuleTester('no-raw-locators', rule, {
8196
'const section = page.getByRole("section"); section.getByRole("button")',
8297
),
8398

99+
// Variable references with proper locators
100+
test(
101+
'const button = page.getByRole("button", { name: "common button" }); page.locator(button)',
102+
),
103+
test(
104+
'const firstButton = page.getByRole("region", { name: "first" }).locator(button); const secondButton = page.getByRole("region", { name: "second" }).locator(button)',
105+
),
106+
84107
// bare calls
85108
test('() => page.locator'),
86109

src/rules/no-raw-locators.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ export default createRule({
2020

2121
return {
2222
CallExpression(node) {
23-
if (node.callee.type !== 'MemberExpression') return
23+
if (
24+
node.callee.type !== 'MemberExpression' ||
25+
node.arguments[0]?.type === 'Identifier'
26+
)
27+
return
2428
const method = getStringValue(node.callee.property)
2529
const arg = getStringValue(node.arguments[0])
2630
const isLocator = isPageMethod(node, 'locator') || method === 'locator'

0 commit comments

Comments
 (0)