eslint-plugin-playwright
Version:
ESLint plugin for Playwright testing.
58 lines (57 loc) • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ast_1 = require("../utils/ast");
function getPropertyRange(node) {
return node.type === 'Identifier'
? node.range
: [node.range[0] + 1, node.range[1] - 1];
}
exports.default = {
create(context) {
return {
CallExpression(node) {
if ((0, ast_1.isPageMethod)(node, '$') || (0, ast_1.isPageMethod)(node, '$$')) {
context.report({
messageId: 'noElementHandle',
suggest: [
{
messageId: (0, ast_1.isPageMethod)(node, '$')
? 'replaceElementHandleWithLocator'
: 'replaceElementHandlesWithLocator',
fix: (fixer) => {
const { property } = node.callee;
// Replace $/$$ with locator
const fixes = [
fixer.replaceTextRange(getPropertyRange(property), 'locator'),
];
// Remove the await expression if it exists as locators do
// not need to be awaited.
if (node.parent.type === 'AwaitExpression') {
fixes.push(fixer.removeRange([node.parent.range[0], node.range[0]]));
}
return fixes;
},
},
],
node: node.callee,
});
}
},
};
},
meta: {
docs: {
category: 'Possible Errors',
description: 'The use of ElementHandle is discouraged, use Locator instead',
recommended: true,
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-element-handle.md',
},
hasSuggestions: true,
messages: {
noElementHandle: 'Unexpected use of element handles.',
replaceElementHandleWithLocator: 'Replace `page.$` with `page.locator`',
replaceElementHandlesWithLocator: 'Replace `page.$$` with `page.locator`',
},
type: 'suggestion',
},
};