123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- "use strict";
- /*
- * eslint-plugin-sonarjs
- * Copyright (C) 2018-2021 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 GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * 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 GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- // https://sonarsource.github.io/rspec/#/rspec/S3972
- const docs_url_1 = require("../utils/docs-url");
- const locations_1 = require("../utils/locations");
- const message = 'Move this "if" to a new line or add the missing "else".';
- const rule = {
- meta: {
- messages: {
- sameLineCondition: message,
- sonarRuntime: '{{sonarRuntimeData}}',
- suggestAddingElse: 'Add "else" keyword',
- suggestAddingNewline: 'Move this "if" to a new line',
- },
- type: 'problem',
- hasSuggestions: true,
- docs: {
- description: 'Conditionals should start on new lines',
- recommended: 'error',
- url: (0, docs_url_1.default)(__filename),
- },
- schema: [
- {
- // internal parameter
- enum: ['sonar-runtime'],
- },
- ],
- },
- create(context) {
- function checkStatements(statements) {
- const sourceCode = context.getSourceCode();
- const siblingIfStatements = getSiblingIfStatements(statements);
- siblingIfStatements.forEach(siblingIfStatement => {
- const precedingIf = siblingIfStatement.first;
- const followingIf = siblingIfStatement.following;
- if (!!precedingIf.loc &&
- !!followingIf.loc &&
- precedingIf.loc.end.line === followingIf.loc.start.line &&
- precedingIf.loc.start.line !== followingIf.loc.end.line) {
- const precedingIfLastToken = sourceCode.getLastToken(precedingIf);
- const followingIfToken = sourceCode.getFirstToken(followingIf);
- (0, locations_1.report)(context, {
- messageId: 'sameLineCondition',
- loc: followingIfToken.loc,
- suggest: [
- {
- messageId: 'suggestAddingElse',
- fix: fixer => fixer.insertTextBefore(followingIfToken, 'else '),
- },
- {
- messageId: 'suggestAddingNewline',
- fix: fixer => fixer.replaceTextRange([precedingIf.range[1], followingIf.range[0]], '\n' + ' '.repeat(precedingIf.loc.start.column)),
- },
- ],
- }, [(0, locations_1.issueLocation)(precedingIfLastToken.loc)], message);
- }
- });
- }
- return {
- Program: (node) => checkStatements(node.body),
- BlockStatement: (node) => checkStatements(node.body),
- SwitchCase: (node) => checkStatements(node.consequent),
- };
- },
- };
- function getSiblingIfStatements(statements) {
- return statements.reduce((siblingsArray, statement, currentIndex) => {
- const previousStatement = statements[currentIndex - 1];
- if (statement.type === 'IfStatement' &&
- !!previousStatement &&
- previousStatement.type === 'IfStatement') {
- return [{ first: previousStatement, following: statement }, ...siblingsArray];
- }
- return siblingsArray;
- }, []);
- }
- module.exports = rule;
- //# sourceMappingURL=no-same-line-conditional.js.map
|