eslint-plugin-sonarjs
Version:
SonarJS rules for ESLint
102 lines (101 loc) • 4.67 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/S3782/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 index_js_1 = require("../helpers/index.js");
const typescript_1 = __importDefault(require("typescript"));
const meta_js_1 = require("./meta.js");
exports.rule = {
meta: (0, index_js_1.generateMeta)(meta_js_1.meta),
create(context) {
const services = context.sourceCode.parserServices;
if (!(0, index_js_1.isRequiredParserServices)(services)) {
return {};
}
const tc = services.program.getTypeChecker();
function isBuiltInMethod(symbol) {
const parent = symbol.valueDeclaration?.parent;
if (!parent || parent.kind !== typescript_1.default.SyntaxKind.InterfaceDeclaration) {
return false;
}
const parentSymbol = tc.getSymbolAtLocation(parent.name);
if (!parentSymbol) {
return false;
}
const fqn = tc.getFullyQualifiedName(parentSymbol);
// some of the built-in objects are deliberately excluded, because they generate many FPs
// and no relevant TP, e.g. RegExp, Function
return ['String', 'Math', 'Array', 'Number', 'Date'].includes(fqn);
}
function isVarArg(param) {
return !!param.dotDotDotToken;
}
function isTypeParameter(type) {
return type.getFlags() & typescript_1.default.TypeFlags.TypeParameter;
}
function declarationMismatch(declaration, callExpression) {
const parameters = declaration.parameters;
for (let i = 0; i < Math.min(parameters.length, callExpression.arguments.length); i++) {
const parameterType = parameters[i].type;
if (!parameterType) {
return null;
}
const declaredType = tc.getTypeFromTypeNode(parameterType);
const actualType = (0, index_js_1.getTypeFromTreeNode)(callExpression.arguments[i], services);
if (
// @ts-ignore private API, see https://github.com/microsoft/TypeScript/issues/9879
!tc.isTypeAssignableTo(actualType, declaredType) &&
!isTypeParameter(declaredType) &&
!typescript_1.default.isFunctionTypeNode(parameterType) &&
!isVarArg(parameters[i])) {
return { actualType, declaredType, node: callExpression.arguments[i] };
}
}
return null;
}
function typeToString(type) {
return tc.typeToString(tc.getBaseTypeOfLiteralType(type));
}
return {
CallExpression: (node) => {
const callExpression = node;
const tsCallExpr = services.esTreeNodeToTSNodeMap.get(callExpression.callee);
const symbol = tc.getSymbolAtLocation(tsCallExpr);
if (symbol?.declarations && isBuiltInMethod(symbol)) {
let mismatch = null;
for (const declaration of symbol.declarations) {
mismatch = declarationMismatch(declaration, callExpression);
if (!mismatch) {
return;
}
}
if (mismatch) {
context.report({
node: mismatch.node,
message: `Verify that argument is of correct type: expected '${typeToString(mismatch.declaredType)}' instead of '${typeToString(mismatch.actualType)}'.`,
});
}
}
},
};
},
};
;