no-fallthrough.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/iu;
  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. default: ""
  62. }
  63. },
  64. additionalProperties: false
  65. }
  66. ],
  67. messages: {
  68. case: "Expected a 'break' statement before 'case'.",
  69. default: "Expected a 'break' statement before 'default'."
  70. }
  71. },
  72. create(context) {
  73. const options = context.options[0] || {};
  74. let currentCodePath = null;
  75. const sourceCode = context.getSourceCode();
  76. /*
  77. * We need to use leading comments of the next SwitchCase node because
  78. * trailing comments is wrong if semicolons are omitted.
  79. */
  80. let fallthroughCase = null;
  81. let fallthroughCommentPattern = null;
  82. if (options.commentPattern) {
  83. fallthroughCommentPattern = new RegExp(options.commentPattern, "u");
  84. } else {
  85. fallthroughCommentPattern = DEFAULT_FALLTHROUGH_COMMENT;
  86. }
  87. return {
  88. onCodePathStart(codePath) {
  89. currentCodePath = codePath;
  90. },
  91. onCodePathEnd() {
  92. currentCodePath = currentCodePath.upper;
  93. },
  94. SwitchCase(node) {
  95. /*
  96. * Checks whether or not there is a fallthrough comment.
  97. * And reports the previous fallthrough node if that does not exist.
  98. */
  99. if (fallthroughCase && !hasFallthroughComment(node, context, fallthroughCommentPattern)) {
  100. context.report({
  101. messageId: node.test ? "case" : "default",
  102. node
  103. });
  104. }
  105. fallthroughCase = null;
  106. },
  107. "SwitchCase:exit"(node) {
  108. const nextToken = sourceCode.getTokenAfter(node);
  109. /*
  110. * `reachable` meant fall through because statements preceded by
  111. * `break`, `return`, or `throw` are unreachable.
  112. * And allows empty cases and the last case.
  113. */
  114. if (currentCodePath.currentSegments.some(isReachable) &&
  115. (node.consequent.length > 0 || hasBlankLinesBetween(node, nextToken)) &&
  116. lodash.last(node.parent.cases) !== node) {
  117. fallthroughCase = node;
  118. }
  119. }
  120. };
  121. }
  122. };