UNPKG

eslint-plugin-sonarjs

Version:
96 lines (95 loc) 3.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractCypressChainAssertion = extractCypressChainAssertion; const ast_js_1 = require("./ast.js"); const assertions_chai_common_js_1 = require("./assertions-chai-common.js"); /** * Covers Cypress chain assertions like `cy.wrap(value).should('be.true')` or `cy.wrap(value).and('be.null')`. * The predicate is a Chai assertion string (dot-separated, optional `not.` prefix). */ function extractCypressChainAssertion(node) { if (node.type !== 'CallExpression' || !(0, ast_js_1.isMethodCall)(node)) { return null; } if (!(0, ast_js_1.isIdentifier)(node.callee.property, 'should', 'and')) { return null; } const predicateArg = node.arguments[0]; if (predicateArg?.type !== 'Literal' || typeof predicateArg.value !== 'string') { return null; } const subject = extractCyWrapSubject(node.callee.object); if (!subject) { return null; } const comparison = parseCypressComparisonString(predicateArg.value); if (comparison) { const expected = (0, assertions_chai_common_js_1.getArgumentAtIndex)(node, 1); if (!expected) { return null; } return { style: 'cypress', kind: 'comparison', comparison, actual: subject, expected, negated: predicateArg.value.split('.').includes('not'), node, reportNode: node.callee.property, }; } const parsed = parseCypressPredicateString(predicateArg.value); if (!parsed) { return null; } return { style: 'cypress', kind: 'predicate', predicate: parsed.predicate, actual: subject, negated: parsed.negated, node, reportNode: subject, }; } function parseCypressComparisonString(predicate) { const parts = predicate.split('.'); let idx = 0; if (parts[idx] === 'not') { idx++; } if (parts[idx] === 'deep' && parts[idx + 1] === 'equal') { return 'deep'; } if (parts[idx] === 'equal') { return 'strict'; } return null; } function extractCyWrapSubject(node) { if (node.type === 'CallExpression' && (0, ast_js_1.isMethodCall)(node)) { if ((0, ast_js_1.isIdentifier)(node.callee.object, 'cy') && (0, ast_js_1.isIdentifier)(node.callee.property, 'wrap')) { const arg = node.arguments[0]; return arg && arg.type !== 'SpreadElement' ? arg : null; } return extractCyWrapSubject(node.callee.object); } if (node.type === 'MemberExpression' && !node.computed) { return extractCyWrapSubject(node.object); } return null; } function parseCypressPredicateString(predicate) { const parts = predicate.split('.'); let idx = 0; const negated = parts[idx] === 'not'; if (negated) { idx++; } if (parts[idx] === 'be') { idx++; } const result = (0, assertions_chai_common_js_1.getChaiPropertyPredicate)(parts[idx]); return result ? { predicate: result.predicate, negated } : null; }