eslint-plugin-sonarjs
Version:
168 lines (167 loc) • 6.68 kB
JavaScript
;
/*
* SonarQube JavaScript Plugin
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
// https://sonarsource.github.io/rspec/#/rspec/S8783/javascript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = void 0;
const ast_js_1 = require("../helpers/ast.js");
const cypress_js_1 = require("../helpers/cypress.js");
const dependencies_js_1 = require("../helpers/dependency-manifests/dependencies.js");
const generate_meta_js_1 = require("../helpers/generate-meta.js");
const module_js_1 = require("../helpers/module.js");
const assertion_utils_js_1 = require("../S5906/assertion-utils.js");
const meta = __importStar(require("./generated-meta.js"));
const messages = {
removeForce: 'Remove this forced interaction and wait for the element to be actionable instead.',
};
const PLAYWRIGHT_MODULES = ['@playwright/test'];
// The options object is always the trailing argument of these commands, but it can
// follow a variable number of positional arguments (e.g. Cypress `.click(options)`,
// `.click(position, options)`, `.click(x, y, options)`). The mapped value is the
// minimum index at which the options object may appear, guarding required positional
// arguments (e.g. the event name of Cypress `.trigger()` or the value of `.type()`).
const CYPRESS_MIN_OPTIONS_INDEX = new Map([
['click', 0],
['check', 0],
['clear', 0],
['dblclick', 0],
['rightclick', 0],
['select', 1],
['trigger', 1],
['type', 1],
['uncheck', 0],
]);
const PLAYWRIGHT_MIN_OPTIONS_INDEX = new Map([
['check', 0],
['clear', 0],
['click', 0],
['dblclick', 0],
['dragTo', 1],
['fill', 1],
['hover', 0],
['selectOption', 1],
['selectText', 0],
['setChecked', 1],
['tap', 0],
['uncheck', 0],
]);
exports.rule = {
meta: (0, generate_meta_js_1.generateMeta)(meta, { messages }),
create(context) {
const hasCypress = (0, dependencies_js_1.getDependenciesSanitizePaths)(context).has('cypress');
const hasPlaywright = (0, module_js_1.importsOrDependsOnModule)(context, PLAYWRIGHT_MODULES, PLAYWRIGHT_MODULES);
const playwrightLocators = new Set();
if (!hasCypress && !hasPlaywright) {
return {};
}
function reportIfForced(call, minOptionsIndex) {
if (call.arguments.length <= minOptionsIndex) {
return;
}
const optionsArgument = call.arguments.at(-1);
const forceProperty = findForceTrueProperty(context, optionsArgument);
if (!forceProperty) {
return;
}
context.report({
node: forceProperty,
messageId: 'removeForce',
});
}
return {
...(hasPlaywright ? (0, assertion_utils_js_1.trackPlaywrightLocators)(context, playwrightLocators) : {}),
CallExpression(node) {
if (node.type !== 'CallExpression' || !(0, ast_js_1.isMethodCall)(node)) {
return;
}
if (hasCypress) {
checkCypressCall(node);
}
if (hasPlaywright) {
checkPlaywrightCall(node);
}
},
};
function checkCypressCall(call) {
if (!(0, ast_js_1.isMethodCall)(call) || !(0, cypress_js_1.chainStartsWithCy)(call.callee.object)) {
return;
}
const minOptionsIndex = CYPRESS_MIN_OPTIONS_INDEX.get(call.callee.property.name);
if (minOptionsIndex !== undefined) {
reportIfForced(call, minOptionsIndex);
}
}
function checkPlaywrightCall(call) {
if (!(0, ast_js_1.isMethodCall)(call)) {
return;
}
const minOptionsIndex = PLAYWRIGHT_MIN_OPTIONS_INDEX.get(call.callee.property.name);
if (minOptionsIndex === undefined) {
return;
}
if ((0, assertion_utils_js_1.isPlaywrightLocatorExpression)(context, call.callee.object, playwrightLocators)) {
reportIfForced(call, minOptionsIndex);
}
}
},
};
function findForceTrueProperty(context, candidate) {
if (!candidate || candidate.type === 'SpreadElement') {
return null;
}
const options = (0, ast_js_1.getValueOfExpression)(context, candidate, 'ObjectExpression', true);
const forceProperty = (0, ast_js_1.getProperty)(options, 'force', context);
if (!forceProperty) {
return null;
}
return forceProperty.value.type === 'Literal' && forceProperty.value.value === true
? forceProperty
: null;
}