no-fallthrough.js 4.6 KB

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