Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/rules/no-raw-locators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ runRuleTester('no-raw-locators', rule, {
code: test('await page.locator()'),
errors: [{ column: 34, endColumn: 48, line: 1, messageId }],
},
{
code: test('const locator = await page.locator()'),
errors: [{ column: 50, endColumn: 64, line: 1, messageId }],
},
{
code: test('let locator = await page.locator()'),
errors: [{ column: 48, endColumn: 62, line: 1, messageId }],
},
{
code: test('await this.page.locator()'),
errors: [{ column: 34, endColumn: 53, line: 1, messageId }],
Expand Down Expand Up @@ -37,7 +45,14 @@ runRuleTester('no-raw-locators', rule, {
),
errors: [{ column: 77, endColumn: 100, line: 1, messageId }],
},

{
code: test('const button = page.locator(); page.locator(button)'),
errors: [{ column: 43, endColumn: 57, line: 1, messageId }],
},
{
code: test('let button = page.locator(); page.locator(button)'),
errors: [{ column: 41, endColumn: 55, line: 1, messageId }],
},
// Allowed
{
code: test('await page.locator("[aria-busy=false]")'),
Expand Down Expand Up @@ -81,6 +96,14 @@ runRuleTester('no-raw-locators', rule, {
'const section = page.getByRole("section"); section.getByRole("button")',
),

// Variable references with proper locators
test(
'const button = page.getByRole("button", { name: "common button" }); page.locator(button)',
),
test(
'const firstButton = page.getByRole("region", { name: "first" }).locator(button); const secondButton = page.getByRole("region", { name: "second" }).locator(button)',
),

// bare calls
test('() => page.locator'),

Expand Down
6 changes: 5 additions & 1 deletion src/rules/no-raw-locators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export default createRule({

return {
CallExpression(node) {
if (node.callee.type !== 'MemberExpression') return
if (
node.callee.type !== 'MemberExpression' ||
node.arguments[0]?.type === 'Identifier'
)
return
const method = getStringValue(node.callee.property)
const arg = getStringValue(node.arguments[0])
const isLocator = isPageMethod(node, 'locator') || method === 'locator'
Expand Down
Loading