项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

no-fallthrough.js 4.6 KiB

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. // 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. docs: {
  49. description: "disallow fallthrough of `case` statements",
  50. category: "Best Practices",
  51. recommended: true,
  52. url: "https://eslint.org/docs/rules/no-fallthrough"
  53. },
  54. schema: [
  55. {
  56. type: "object",
  57. properties: {
  58. commentPattern: {
  59. type: "string"
  60. }
  61. },
  62. additionalProperties: false
  63. }
  64. ]
  65. },
  66. create(context) {
  67. const options = context.options[0] || {};
  68. let currentCodePath = null;
  69. const sourceCode = context.getSourceCode();
  70. /*
  71. * We need to use leading comments of the next SwitchCase node because
  72. * trailing comments is wrong if semicolons are omitted.
  73. */
  74. let fallthroughCase = null;
  75. let fallthroughCommentPattern = null;
  76. if (options.commentPattern) {
  77. fallthroughCommentPattern = new RegExp(options.commentPattern);
  78. } else {
  79. fallthroughCommentPattern = DEFAULT_FALLTHROUGH_COMMENT;
  80. }
  81. return {
  82. onCodePathStart(codePath) {
  83. currentCodePath = codePath;
  84. },
  85. onCodePathEnd() {
  86. currentCodePath = currentCodePath.upper;
  87. },
  88. SwitchCase(node) {
  89. /*
  90. * Checks whether or not there is a fallthrough comment.
  91. * And reports the previous fallthrough node if that does not exist.
  92. */
  93. if (fallthroughCase && !hasFallthroughComment(node, context, fallthroughCommentPattern)) {
  94. context.report({
  95. message: "Expected a 'break' statement before '{{type}}'.",
  96. data: { type: node.test ? "case" : "default" },
  97. node
  98. });
  99. }
  100. fallthroughCase = null;
  101. },
  102. "SwitchCase:exit"(node) {
  103. const nextToken = sourceCode.getTokenAfter(node);
  104. /*
  105. * `reachable` meant fall through because statements preceded by
  106. * `break`, `return`, or `throw` are unreachable.
  107. * And allows empty cases and the last case.
  108. */
  109. if (currentCodePath.currentSegments.some(isReachable) &&
  110. (node.consequent.length > 0 || hasBlankLinesBetween(node, nextToken)) &&
  111. lodash.last(node.parent.cases) !== node) {
  112. fallthroughCase = node;
  113. }
  114. }
  115. };
  116. }
  117. };