eslint-plugin-sonarjs
Version:
44 lines (43 loc) • 2.14 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.isSortUsedInJsonStringifyComparison = isSortUsedInJsonStringifyComparison;
const ancestor_js_1 = require("../../helpers/ancestor.js");
const helpers_js_1 = require("./helpers.js");
// Matches JSON.stringify(arr.sort()) == JSON.stringify(arr.sort()) or
// JSON.stringify(arr.toSorted()) == JSON.stringify(arr.toSorted()) when both
// sides use the same sort family over primitive arrays with known-safe
// default sort semantics (number, string, boolean, bigint).
function isSortUsedInJsonStringifyComparison(call, ruleContext) {
const parent = (0, ancestor_js_1.getNodeParent)(call);
if (!(0, helpers_js_1.isJsonStringifyCall)(parent) || parent.arguments[0] !== call) {
return false;
}
const sibling = (0, helpers_js_1.getEqualityComparisonSibling)(parent);
if (!(0, helpers_js_1.isJsonStringifyCall)(sibling)) {
return false;
}
const callInfo = (0, helpers_js_1.getComparatorlessSortCallInfo)(call, ruleContext);
const siblingInfo = (0, helpers_js_1.getComparatorlessSortCallInfo)(sibling.arguments[0], ruleContext);
if (callInfo === null || siblingInfo === null) {
return false;
}
return (callInfo.methodName === siblingInfo.methodName &&
(0, helpers_js_1.isPrimitiveSortReceiver)(callInfo.receiver, ruleContext) &&
(0, helpers_js_1.isPrimitiveSortReceiver)(siblingInfo.receiver, ruleContext));
}