eslint-plugin-sonarjs
Version:
SonarJS rules for ESLint
130 lines (129 loc) • 5.88 kB
JavaScript
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* 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/S4043/javascript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = void 0;
const typescript_1 = __importDefault(require("typescript"));
const index_js_1 = require("../helpers/index.js");
const meta_js_1 = require("./meta.js");
const arrayMutatingMethods = ['reverse', "'reverse'", '"reverse"', ...index_js_1.sortLike];
exports.rule = {
meta: (0, index_js_1.generateMeta)(meta_js_1.meta, {
hasSuggestions: true,
messages: {
moveMethod: 'Move this array "{{method}}" operation to a separate statement or replace it with "{{suggestedMethod}}".',
suggestMethod: 'Replace with "{{suggestedMethod}}" method',
},
}),
create(context) {
const services = context.sourceCode.parserServices;
if (!(0, index_js_1.isRequiredParserServices)(services)) {
return {};
}
return {
CallExpression(node) {
const { callee } = node;
if (callee.type === 'MemberExpression') {
const propertyText = context.sourceCode.getText(callee.property);
if (isArrayMutatingCall(callee, services, propertyText)) {
const mutatedArray = callee.object;
if (isIdentifierOrPropertyAccessExpression(mutatedArray, services) &&
!isInSelfAssignment(mutatedArray, node) &&
isForbiddenOperation(node)) {
const method = formatMethod(propertyText);
const suggestedMethod = method === 'sort' ? 'toSorted' : 'toReversed';
context.report({
messageId: 'moveMethod',
data: {
method,
suggestedMethod,
},
node,
suggest: [
{
messageId: 'suggestMethod',
data: {
suggestedMethod,
},
fix: fixer => {
const fixedPropertyText = propertyText.replace(method, suggestedMethod);
return fixer.replaceText(callee.property, fixedPropertyText);
},
},
],
});
}
}
}
},
};
},
};
function formatMethod(mutatingMethod) {
if (mutatingMethod.startsWith('"') || mutatingMethod.startsWith("'")) {
return mutatingMethod.substring(1, mutatingMethod.length - 1);
}
else {
return mutatingMethod;
}
}
function isArrayMutatingCall(memberExpression, services, propertyText) {
return arrayMutatingMethods.includes(propertyText) && (0, index_js_1.isArray)(memberExpression.object, services);
}
function isIdentifierOrPropertyAccessExpression(node, services) {
return (node.type === 'Identifier' ||
(node.type === 'MemberExpression' && !isGetAccessor(node.property, services)));
}
function isGetAccessor(node, services) {
const symbol = (0, index_js_1.getSymbolAtLocation)(node, services);
const declarations = symbol?.declarations;
return declarations?.length === 1 && declarations[0].kind === typescript_1.default.SyntaxKind.GetAccessor;
}
function isInSelfAssignment(mutatedArray, node) {
const parent = node.parent;
return (
// check assignment
parent !== undefined &&
parent.type === 'AssignmentExpression' &&
parent.operator === '=' &&
parent.left.type === 'Identifier' &&
mutatedArray.type === 'Identifier' &&
parent.left.name === mutatedArray.name);
}
function isForbiddenOperation(node) {
return !isStandaloneExpression(node) && !isReturnedExpression(node);
}
function isStandaloneExpression(node) {
const ancestors = (0, index_js_1.localAncestorsChain)(node);
const returnIdx = ancestors.findIndex(ancestor => ancestor.type === 'ExpressionStatement');
return (returnIdx > -1 &&
ancestors
.slice(0, returnIdx)
.every(ancestor => ['ChainExpression', 'LogicalExpression'].includes(ancestor.type)));
}
function isReturnedExpression(node) {
const ancestors = (0, index_js_1.localAncestorsChain)(node);
const returnIdx = ancestors.findIndex(ancestor => ancestor.type === 'ReturnStatement');
return (returnIdx > -1 &&
ancestors
.slice(0, returnIdx)
.every(ancestor => ['ArrayExpression', 'ObjectExpression', 'ConditionalExpression', 'SpreadElement'].includes(ancestor.type)));
}
;