Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-fallthrough.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @fileoverview Rule to flag fall-through cases in switch statements.
  3. * @author Matt DuVall <http://mattduvall.com/>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const lodash = require("lodash");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. const DEFAULT_FALLTHROUGH_COMMENT = /falls?\s?through/i;
  14. /**
  15. * Checks whether or not a given node has a fallthrough comment.
  16. * @param {ASTNode} node - A SwitchCase node to get comments.
  17. * @param {RuleContext} context - A rule context which stores comments.
  18. * @param {RegExp} fallthroughCommentPattern - A pattern to match comment to.
  19. * @returns {boolean} `true` if the node has a valid fallthrough comment.
  20. */
  21. function hasFallthroughComment(node, context, fallthroughCommentPattern) {
  22. const sourceCode = context.getSourceCode();
  23. const comment = lodash.last(sourceCode.getCommentsBefore(node));
  24. return Boolean(comment && fallthroughCommentPattern.test(comment.value));
  25. }
  26. /**
  27. * Checks whether or not a given code path segment is reachable.
  28. * @param {CodePathSegment} segment - A CodePathSegment to check.
  29. * @returns {boolean} `true` if the segment is reachable.
  30. */
  31. function isReachable(segment) {
  32. return segment.reachable;
  33. }
  34. /**
  35. * Checks whether a node and a token are separated by blank lines
  36. * @param {ASTNode} node - The node to check
  37. * @param {Token} token - The token to compare against
  38. * @returns {boolean} `true` if there are blank lines between node and token
  39. */
  40. function hasBlankLinesBetween(node, token) {
  41. return token.loc.start.line > node.loc.end.line + 1;
  42. }
  43. //------------------------------------------------------------------------------
  44. // Rule Definition
  45. //------------------------------------------------------------------------------
  46. module.exports = {
  47. meta: {
  48. type: "problem",
  49. docs: {
  50. description: "disallow fallthrough of `case` statements",
  51. category: "Best Practices",
  52. recommended: true,
  53. url: "https://eslint.org/docs/rules/no-fallthrough"
  54. },
  55. schema: [
  56. {
  57. type: "object",
  58. properties: {
  59. commentPattern: {
  60. type: "string"
  61. }
  62. },
  63. additionalProperties: false
  64. }
  65. ]
  66. },
  67. create(context) {
  68. const options = context.options[0] || {};
  69. let currentCodePath = null;
  70. const sourceCode = context.getSourceCode();
  71. /*
  72. * We need to use leading comments of the next SwitchCase node because
  73. * trailing comments is wrong if semicolons are omitted.
  74. */
  75. let fallthroughCase = null;
  76. let fallthroughCommentPattern = null;
  77. if (options.commentPattern) {
  78. fallthroughCommentPattern = new RegExp(options.commentPattern);
  79. } else {
  80. fallthroughCommentPattern = DEFAULT_FALLTHROUGH_COMMENT;
  81. }
  82. return {
  83. onCodePathStart(codePath) {
  84. currentCodePath = codePath;
  85. },
  86. onCodePathEnd() {
  87. currentCodePath = currentCodePath.upper;
  88. },
  89. SwitchCase(node) {
  90. /*
  91. * Checks whether or not there is a fallthrough comment.
  92. * And reports the previous fallthrough node if that does not exist.
  93. */
  94. if (fallthroughCase && !hasFallthroughComment(node, context, fallthroughCommentPattern)) {
  95. context.report({
  96. message: "Expected a 'break' statement before '{{type}}'.",
  97. data: { type: node.test ? "case" : "default" },
  98. node
  99. });
  100. }
  101. fallthroughCase = null;
  102. },
  103. "SwitchCase:exit"(node) {
  104. const nextToken = sourceCode.getTokenAfter(node);
  105. /*
  106. * `reachable` meant fall through because statements preceded by
  107. * `break`, `return`, or `throw` are unreachable.
  108. * And allows empty cases and the last case.
  109. */
  110. if (currentCodePath.currentSegments.some(isReachable) &&
  111. (node.consequent.length > 0 || hasBlankLinesBetween(node, nextToken)) &&
  112. lodash.last(node.parent.cases) !== node) {
  113. fallthroughCase = node;
  114. }
  115. }
  116. };
  117. }
  118. };