eslint-plugin-sonarjs
Version:
SonarJS rules for ESLint
88 lines (87 loc) • 3.61 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/S1472/javascript
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = void 0;
const index_js_1 = require("../helpers/index.js");
const meta_js_1 = require("./meta.js");
exports.rule = {
meta: (0, index_js_1.generateMeta)(meta_js_1.meta, {
messages: {
moveArguments: 'Make those call arguments start on line {{line}}.',
moveTemplateLiteral: 'Make this template literal start on line {{line}}.',
},
}),
create(context) {
const sourceCode = context.sourceCode;
return {
CallExpression: (node) => {
const call = node;
if (call.callee.type !== 'CallExpression' && call.arguments.length === 1) {
const callee = getCallee(call);
const parenthesis = sourceCode.getLastTokenBetween(callee, call.arguments[0], isClosingParen);
const calleeLastLine = (parenthesis ?? sourceCode.getLastToken(callee)).loc.end.line;
const { start } = sourceCode.getTokenAfter(callee, isNotClosingParen).loc;
if (calleeLastLine !== start.line) {
const { end } = sourceCode.getLastToken(call).loc;
if (end.line !== start.line) {
//If arguments span multiple lines, we only report the first one
reportIssue('moveArguments', start, calleeLastLine, context);
}
else {
reportIssue('moveArguments', { start, end }, calleeLastLine, context);
}
}
}
},
TaggedTemplateExpression(node) {
const { quasi } = node;
const tokenBefore = sourceCode.getTokenBefore(quasi);
if (tokenBefore && quasi.loc && tokenBefore.loc.end.line !== quasi.loc.start.line) {
const loc = {
start: quasi.loc.start,
end: {
line: quasi.loc.start.line,
column: quasi.loc.start.column + 1,
},
};
reportIssue('moveTemplateLiteral', loc, tokenBefore.loc.start.line, context);
}
},
};
},
};
function getCallee(call) {
const node = call;
return (node.typeArguments ?? node.callee);
}
function isClosingParen(token) {
return token.type === 'Punctuator' && token.value === ')';
}
function isNotClosingParen(token) {
return !isClosingParen(token);
}
function reportIssue(messageId, loc, line, context) {
context.report({
messageId,
data: {
line: line.toString(),
},
loc,
});
}
;