eslint-plugin-sonarjs
Version:
63 lines (62 loc) • 2.71 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/
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCypressSuggestion = getCypressSuggestion;
const ast_js_1 = require("../helpers/ast.js");
const assertion_utils_js_1 = require("./assertion-utils.js");
function getCypressSuggestion(node, sourceCode) {
if (!(0, ast_js_1.isMethodCall)(node) ||
!['should', 'and'].includes(node.callee.property.name) ||
node.arguments.length !== 2) {
return null;
}
if (!hasCyWrapCall(node.callee.object) || node.arguments[0].type !== 'Literal') {
return null;
}
const chainer = node.arguments[0].value;
const expected = node.arguments[1];
const property = node.callee.property.name;
const subject = sourceCode.getText(node.callee.object);
if ((0, assertion_utils_js_1.isNullLiteral)(expected)) {
return getCypressNullishSuggestion(node, sourceCode, chainer, subject, property, 'null');
}
if ((0, assertion_utils_js_1.isUndefinedExpression)(expected)) {
return getCypressNullishSuggestion(node, sourceCode, chainer, subject, property, 'undefined');
}
return null;
}
function getCypressNullishSuggestion(node, sourceCode, chainer, subject, property, nullish) {
if (chainer !== 'equal' &&
chainer !== 'not.equal' &&
chainer !== 'deep.equal' &&
chainer !== 'not.deep.equal') {
return null;
}
const negation = chainer === 'not.equal' || chainer === 'not.deep.equal' ? 'not.' : '';
return (0, assertion_utils_js_1.replacement)(`${subject}.${property}('${negation}be.${nullish}')`, node, sourceCode);
}
function hasCyWrapCall(node) {
if (node.type === 'CallExpression' && (0, ast_js_1.isMethodCall)(node)) {
return (((0, ast_js_1.isIdentifier)(node.callee.property, 'wrap') && (0, ast_js_1.isIdentifier)(node.callee.object, 'cy')) ||
hasCyWrapCall(node.callee.object));
}
if (node.type === 'MemberExpression' && !node.computed) {
return hasCyWrapCall(node.object);
}
return false;
}