no-all-duplicated-branches.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/S3923
  22. const nodes_1 = require("../utils/nodes");
  23. const equivalence_1 = require("../utils/equivalence");
  24. const conditions_1 = require("../utils/conditions");
  25. const docs_url_1 = require("../utils/docs-url");
  26. const rule = {
  27. meta: {
  28. messages: {
  29. removeOrEditConditionalStructure: "Remove this conditional structure or edit its code blocks so that they're not all the same.",
  30. returnsTheSameValue: 'This conditional operation returns the same value whether the condition is "true" or "false".',
  31. },
  32. schema: [],
  33. type: 'problem',
  34. docs: {
  35. description: 'All branches in a conditional structure should not have exactly the same implementation',
  36. recommended: 'error',
  37. url: (0, docs_url_1.default)(__filename),
  38. },
  39. },
  40. create(context) {
  41. return {
  42. IfStatement(node) {
  43. const ifStmt = node;
  44. // don't visit `else if` statements
  45. if (!(0, nodes_1.isIfStatement)(node.parent)) {
  46. const { branches, endsWithElse } = (0, conditions_1.collectIfBranches)(ifStmt);
  47. if (endsWithElse && allDuplicated(branches)) {
  48. context.report({ messageId: 'removeOrEditConditionalStructure', node: ifStmt });
  49. }
  50. }
  51. },
  52. SwitchStatement(node) {
  53. const switchStmt = node;
  54. const { branches, endsWithDefault } = (0, conditions_1.collectSwitchBranches)(switchStmt);
  55. if (endsWithDefault && allDuplicated(branches)) {
  56. context.report({ messageId: 'removeOrEditConditionalStructure', node: switchStmt });
  57. }
  58. },
  59. ConditionalExpression(node) {
  60. const conditional = node;
  61. const branches = [conditional.consequent, conditional.alternate];
  62. if (allDuplicated(branches)) {
  63. context.report({ messageId: 'returnsTheSameValue', node: conditional });
  64. }
  65. },
  66. };
  67. function allDuplicated(branches) {
  68. return (branches.length > 1 &&
  69. branches.slice(1).every((branch, index) => {
  70. return (0, equivalence_1.areEquivalent)(branch, branches[index], context.getSourceCode());
  71. }));
  72. }
  73. },
  74. };
  75. module.exports = rule;
  76. //# sourceMappingURL=no-all-duplicated-branches.js.map