Skip to content

Commit

Permalink
chore: highlight legacy page.click on hover (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Oct 4, 2023
1 parent 49373a7 commit 59f828d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/babelHighlightUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function locatorForSourcePosition(text: string, vars: { pages: string[],
expressionNode = path.node.object.arguments[0];


// *.locator(), *.getBy*(), call
// *.locator(), *.getBy*(), *.click(), *.fill(), *.type() call
if (t.isCallExpression(path.node) &&
t.isMemberExpression(path.node.callee) &&
t.isIdentifier(path.node.callee.property) &&
Expand Down
4 changes: 3 additions & 1 deletion src/debugHighlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { locatorForSourcePosition, pruneAstCaches } from './babelHighlightUtil';
import { debugSessionName } from './debugSessionName';
import { locatorMethodRegex } from './methodNames';
import { replaceActionWithLocator, locatorMethodRegex } from './methodNames';
import type { Location } from './reporter';
import { ReusedBrowser } from './reusedBrowser';
import * as vscodeTypes from './vscodeTypes';
Expand Down Expand Up @@ -148,6 +148,8 @@ async function locatorToHighlight(debugSessions: Map<string, vscodeTypes.DebugSe
locatorExpression = locatorExpression?.replace(/^component\s*\./, `page.locator('#root').locator('internal:control=component').`);
// Translate 'this.page', or 'this._page' to 'page' to have best-effort support for POMs.
locatorExpression = locatorExpression?.replace(/this\._?page\s*\./, 'page.');
// Translate page.click() to page.locator()
locatorExpression = locatorExpression ? replaceActionWithLocator(locatorExpression) : undefined;
// Only consider locator expressions starting with "page." because we know the base for them (root).
// Other locators can be relative.
const match = locatorExpression?.match(/^page\s*\.([\s\S]*)/m);
Expand Down
12 changes: 10 additions & 2 deletions src/methodNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ export const locatorMethods = [
'first',
'last',
'nth',
'filter'
'filter',
'check',
'click',
'fill',
'type',
];

export const locatorMethodRegex = /\.\s*(locator|getBy[\w]+|first|last|nth|filter)\(/;
export const locatorMethodRegex = /\.\s*(check|click|fill|type|locator|getBy[\w]+|first|last|nth|filter)\(/;

export function replaceActionWithLocator(expression: string) {
return expression.replace(/\.\s*(?:check|click|fill|type)\(([^,]+)(?:,\s*{.*})\)/, '.locator($1)');
}

0 comments on commit 59f828d

Please sign in to comment.