no-redundant-jump.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/S3626
  22. const docs_url_1 = require("../utils/docs-url");
  23. const loops = 'WhileStatement, ForStatement, DoWhileStatement, ForInStatement, ForOfStatement';
  24. const rule = {
  25. meta: {
  26. messages: {
  27. removeRedundantJump: 'Remove this redundant jump.',
  28. suggestJumpRemoval: 'Remove this redundant jump',
  29. },
  30. schema: [],
  31. type: 'suggestion',
  32. hasSuggestions: true,
  33. docs: {
  34. description: 'Jump statements should not be redundant',
  35. recommended: 'error',
  36. url: (0, docs_url_1.default)(__filename),
  37. },
  38. },
  39. create(context) {
  40. function reportIfLastStatement(node) {
  41. const withArgument = node.type === 'ContinueStatement' ? !!node.label : !!node.argument;
  42. if (!withArgument) {
  43. const block = node.parent;
  44. if (block.body[block.body.length - 1] === node && block.body.length > 1) {
  45. const previousComments = context.getSourceCode().getCommentsBefore(node);
  46. const previousToken = previousComments.length === 0
  47. ? context.getSourceCode().getTokenBefore(node)
  48. : previousComments[previousComments.length - 1];
  49. context.report({
  50. messageId: 'removeRedundantJump',
  51. node,
  52. suggest: [
  53. {
  54. messageId: 'suggestJumpRemoval',
  55. fix: fixer => fixer.removeRange([previousToken.range[1], node.range[1]]),
  56. },
  57. ],
  58. });
  59. }
  60. }
  61. }
  62. function reportIfLastStatementInsideIf(node) {
  63. const ancestors = context.getAncestors();
  64. const ifStatement = ancestors[ancestors.length - 2];
  65. const upperBlock = ancestors[ancestors.length - 3];
  66. if (upperBlock.body[upperBlock.body.length - 1] === ifStatement) {
  67. reportIfLastStatement(node);
  68. }
  69. }
  70. return {
  71. [`:matches(${loops}) > BlockStatement > ContinueStatement`]: (node) => {
  72. reportIfLastStatement(node);
  73. },
  74. [`:matches(${loops}) > BlockStatement > IfStatement > BlockStatement > ContinueStatement`]: (node) => {
  75. reportIfLastStatementInsideIf(node);
  76. },
  77. ':function > BlockStatement > ReturnStatement': (node) => {
  78. reportIfLastStatement(node);
  79. },
  80. ':function > BlockStatement > IfStatement > BlockStatement > ReturnStatement': (node) => {
  81. reportIfLastStatementInsideIf(node);
  82. },
  83. };
  84. },
  85. };
  86. module.exports = rule;
  87. //# sourceMappingURL=no-redundant-jump.js.map