eslint-plugin-playwright
Version:
ESLint plugin for Playwright testing.
60 lines (59 loc) • 2.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ast_1 = require("../utils/ast");
const fixer_1 = require("../utils/fixer");
const parseExpectCall_1 = require("../utils/parseExpectCall");
const matcherMap = {
toBeVisible: 'toBeHidden',
toBeHidden: 'toBeVisible',
toBeEnabled: 'toBeDisabled',
toBeDisabled: 'toBeEnabled',
};
exports.default = {
create(context) {
return {
CallExpression(node) {
const expectCall = (0, parseExpectCall_1.parseExpectCall)(node);
if (!expectCall)
return;
// As the name implies, this rule only implies if the not modifier is
// part of the matcher chain
const notModifier = expectCall.modifiers.find((mod) => (0, ast_1.getStringValue)(mod) === 'not');
if (!notModifier)
return;
// This rule only applies to specific matchers that have opposites
if (expectCall.matcherName in matcherMap) {
const newMatcher = matcherMap[expectCall.matcherName];
context.report({
fix: (fixer) => [
fixer.removeRange([
notModifier.range[0] - (0, fixer_1.getRangeOffset)(notModifier),
notModifier.range[1] + 1,
]),
(0, fixer_1.replaceAccessorFixer)(fixer, expectCall.matcher, newMatcher),
],
messageId: 'noUselessNot',
data: { old: expectCall.matcherName, new: newMatcher },
loc: {
start: notModifier.loc.start,
end: expectCall.matcher.loc.end,
},
});
}
},
};
},
meta: {
docs: {
category: 'Best Practices',
description: `Disallow usage of 'not' matchers when a more specific matcher exists`,
recommended: true,
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-useless-not.md',
},
fixable: 'code',
messages: {
noUselessNot: 'Unexpected usage of not.{{old}}(). Use {{new}}() instead.',
},
type: 'problem',
},
};