eslint-plugin-sonarjs
Version:
SonarJS rules for ESLint
92 lines (91 loc) • 4.01 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/S3402/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 message = `Review this expression to be sure that the concatenation was intended.`;
const objectLikeTypes = new Set(['object', 'Object']);
exports.rule = {
meta: (0, index_js_1.generateMeta)(meta_js_1.meta, undefined, true),
create(context) {
const services = context.sourceCode.parserServices;
if (!(0, index_js_1.isRequiredParserServices)(services)) {
return {};
}
const checker = services.program.getTypeChecker();
function isStringPlusNonString(type1, type2) {
if (isLiteralType(type1) || isLiteralType(type2)) {
return false;
}
const isObjectLike = objectLikeTypes.has(checker.typeToString(type2));
// @ts-ignore private API, see https://github.com/microsoft/TypeScript/issues/9879
return isStringType(type1) && !isObjectLike && !checker.isTypeAssignableTo(type1, type2);
}
function getOperatorLocation(left, right) {
return context.sourceCode
.getTokensBetween(left, right)
.find(token => token.value === '+' || token.value === '+=').loc;
}
function checkConcatenation(left, right) {
if ((0, index_js_1.isStringLiteral)(left) ||
(0, index_js_1.isStringLiteral)(right) ||
isConcatenation(left) ||
isConcatenation(right)) {
return;
}
const leftType = (0, index_js_1.getTypeFromTreeNode)(left, services);
const rightType = (0, index_js_1.getTypeFromTreeNode)(right, services);
if (isStringPlusNonString(leftType, rightType) ||
isStringPlusNonString(rightType, leftType)) {
(0, index_js_1.report)(context, {
message,
loc: getOperatorLocation(left, right),
}, [
(0, index_js_1.toSecondaryLocation)(left, `left operand has type ${checker.typeToString(leftType)}.`),
(0, index_js_1.toSecondaryLocation)(right, `right operand has type ${checker.typeToString(rightType)}.`),
]);
}
}
return {
'AssignmentExpression[operator="+="]'(node) {
checkConcatenation(node.left, node.right);
},
'BinaryExpression[operator="+"]'(node) {
checkConcatenation(node.left, node.right);
},
};
},
};
function isStringType(typ) {
return (typ.getFlags() & typescript_1.default.TypeFlags.StringLike) !== 0;
}
function isLiteralType(type) {
if (type.isUnion()) {
return type.types.some(t => isLiteralType(t));
}
return type.isStringLiteral();
}
function isConcatenation(node) {
return node.type === 'BinaryExpression' && node.operator === '+';
}
;