no-duplicated-branches.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/S1871
  22. const nodes_1 = require("../utils/nodes");
  23. const equivalence_1 = require("../utils/equivalence");
  24. const conditions_1 = require("../utils/conditions");
  25. const locations_1 = require("../utils/locations");
  26. const docs_url_1 = require("../utils/docs-url");
  27. const message = "This {{type}}'s code block is the same as the block for the {{type}} on line {{line}}.";
  28. const rule = {
  29. meta: {
  30. messages: {
  31. sameConditionalBlock: message,
  32. sonarRuntime: '{{sonarRuntimeData}}',
  33. },
  34. type: 'problem',
  35. docs: {
  36. description: 'Two branches in a conditional structure should not have exactly the same implementation',
  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. return {
  49. IfStatement(node) {
  50. visitIfStatement(node);
  51. },
  52. SwitchStatement(node) {
  53. visitSwitchStatement(node);
  54. },
  55. };
  56. function visitIfStatement(ifStmt) {
  57. if ((0, nodes_1.isIfStatement)(ifStmt.parent)) {
  58. return;
  59. }
  60. const { branches, endsWithElse } = (0, conditions_1.collectIfBranches)(ifStmt);
  61. if (allEquivalentWithoutDefault(branches, endsWithElse)) {
  62. branches.slice(1).forEach((branch, i) => reportIssue(branch, branches[i], 'branch'));
  63. return;
  64. }
  65. for (let i = 1; i < branches.length; i++) {
  66. if (hasRequiredSize([branches[i]])) {
  67. for (let j = 0; j < i; j++) {
  68. if (compareIfBranches(branches[i], branches[j])) {
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. function visitSwitchStatement(switchStmt) {
  76. const { cases } = switchStmt;
  77. const { endsWithDefault } = (0, conditions_1.collectSwitchBranches)(switchStmt);
  78. const nonEmptyCases = cases.filter(c => (0, conditions_1.takeWithoutBreak)(expandSingleBlockStatement(c.consequent)).length > 0);
  79. const casesWithoutBreak = nonEmptyCases.map(c => (0, conditions_1.takeWithoutBreak)(expandSingleBlockStatement(c.consequent)));
  80. if (allEquivalentWithoutDefault(casesWithoutBreak, endsWithDefault)) {
  81. nonEmptyCases
  82. .slice(1)
  83. .forEach((caseStmt, i) => reportIssue(caseStmt, nonEmptyCases[i], 'case'));
  84. return;
  85. }
  86. for (let i = 1; i < cases.length; i++) {
  87. const firstClauseWithoutBreak = (0, conditions_1.takeWithoutBreak)(expandSingleBlockStatement(cases[i].consequent));
  88. if (hasRequiredSize(firstClauseWithoutBreak)) {
  89. for (let j = 0; j < i; j++) {
  90. const secondClauseWithoutBreak = (0, conditions_1.takeWithoutBreak)(expandSingleBlockStatement(cases[j].consequent));
  91. if ((0, equivalence_1.areEquivalent)(firstClauseWithoutBreak, secondClauseWithoutBreak, context.getSourceCode())) {
  92. reportIssue(cases[i], cases[j], 'case');
  93. break;
  94. }
  95. }
  96. }
  97. }
  98. }
  99. function hasRequiredSize(nodes) {
  100. if (nodes.length > 0) {
  101. const tokens = [
  102. ...context.getSourceCode().getTokens(nodes[0]),
  103. ...context.getSourceCode().getTokens(nodes[nodes.length - 1]),
  104. ].filter(token => token.value !== '{' && token.value !== '}');
  105. return (tokens.length > 0 && tokens[tokens.length - 1].loc.end.line > tokens[0].loc.start.line);
  106. }
  107. return false;
  108. }
  109. function compareIfBranches(a, b) {
  110. const equivalent = (0, equivalence_1.areEquivalent)(a, b, context.getSourceCode());
  111. if (equivalent && b.loc) {
  112. reportIssue(a, b, 'branch');
  113. }
  114. return equivalent;
  115. }
  116. function expandSingleBlockStatement(nodes) {
  117. if (nodes.length === 1) {
  118. const node = nodes[0];
  119. if ((0, nodes_1.isBlockStatement)(node)) {
  120. return node.body;
  121. }
  122. }
  123. return nodes;
  124. }
  125. function allEquivalentWithoutDefault(branches, endsWithDefault) {
  126. return (!endsWithDefault &&
  127. branches.length > 1 &&
  128. branches
  129. .slice(1)
  130. .every((branch, index) => (0, equivalence_1.areEquivalent)(branch, branches[index], context.getSourceCode())));
  131. }
  132. function reportIssue(node, equivalentNode, type) {
  133. const equivalentNodeLoc = equivalentNode.loc;
  134. (0, locations_1.report)(context, {
  135. messageId: 'sameConditionalBlock',
  136. data: { type, line: String(equivalentNode.loc.start.line) },
  137. node,
  138. }, [(0, locations_1.issueLocation)(equivalentNodeLoc, equivalentNodeLoc, 'Original')], message);
  139. }
  140. },
  141. };
  142. module.exports = rule;
  143. //# sourceMappingURL=no-duplicated-branches.js.map