no-same-line-conditional.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. /*
  3. * eslint-plugin-sonarjs
  4. * Copyright (C) 2018-2021 SonarSource SA
  5. * mailto:info AT sonarsource DOT com
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this program; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. // https://sonarsource.github.io/rspec/#/rspec/S3972
  22. const docs_url_1 = require("../utils/docs-url");
  23. const locations_1 = require("../utils/locations");
  24. const message = 'Move this "if" to a new line or add the missing "else".';
  25. const rule = {
  26. meta: {
  27. messages: {
  28. sameLineCondition: message,
  29. sonarRuntime: '{{sonarRuntimeData}}',
  30. suggestAddingElse: 'Add "else" keyword',
  31. suggestAddingNewline: 'Move this "if" to a new line',
  32. },
  33. type: 'problem',
  34. hasSuggestions: true,
  35. docs: {
  36. description: 'Conditionals should start on new lines',
  37. recommended: 'error',
  38. url: (0, docs_url_1.default)(__filename),
  39. },
  40. schema: [
  41. {
  42. // internal parameter
  43. enum: ['sonar-runtime'],
  44. },
  45. ],
  46. },
  47. create(context) {
  48. function checkStatements(statements) {
  49. const sourceCode = context.getSourceCode();
  50. const siblingIfStatements = getSiblingIfStatements(statements);
  51. siblingIfStatements.forEach(siblingIfStatement => {
  52. const precedingIf = siblingIfStatement.first;
  53. const followingIf = siblingIfStatement.following;
  54. if (!!precedingIf.loc &&
  55. !!followingIf.loc &&
  56. precedingIf.loc.end.line === followingIf.loc.start.line &&
  57. precedingIf.loc.start.line !== followingIf.loc.end.line) {
  58. const precedingIfLastToken = sourceCode.getLastToken(precedingIf);
  59. const followingIfToken = sourceCode.getFirstToken(followingIf);
  60. (0, locations_1.report)(context, {
  61. messageId: 'sameLineCondition',
  62. loc: followingIfToken.loc,
  63. suggest: [
  64. {
  65. messageId: 'suggestAddingElse',
  66. fix: fixer => fixer.insertTextBefore(followingIfToken, 'else '),
  67. },
  68. {
  69. messageId: 'suggestAddingNewline',
  70. fix: fixer => fixer.replaceTextRange([precedingIf.range[1], followingIf.range[0]], '\n' + ' '.repeat(precedingIf.loc.start.column)),
  71. },
  72. ],
  73. }, [(0, locations_1.issueLocation)(precedingIfLastToken.loc)], message);
  74. }
  75. });
  76. }
  77. return {
  78. Program: (node) => checkStatements(node.body),
  79. BlockStatement: (node) => checkStatements(node.body),
  80. SwitchCase: (node) => checkStatements(node.consequent),
  81. };
  82. },
  83. };
  84. function getSiblingIfStatements(statements) {
  85. return statements.reduce((siblingsArray, statement, currentIndex) => {
  86. const previousStatement = statements[currentIndex - 1];
  87. if (statement.type === 'IfStatement' &&
  88. !!previousStatement &&
  89. previousStatement.type === 'IfStatement') {
  90. return [{ first: previousStatement, following: statement }, ...siblingsArray];
  91. }
  92. return siblingsArray;
  93. }, []);
  94. }
  95. module.exports = rule;
  96. //# sourceMappingURL=no-same-line-conditional.js.map