2 * @fileoverview Rule to flag fall-through cases in switch statements.
3 * @author Matt DuVall <http://mattduvall.com/>
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const DEFAULT_FALLTHROUGH_COMMENT = /falls?\s?through/iu;
14 * Checks whether or not a given case has a fallthrough comment.
15 * @param {ASTNode} caseWhichFallsThrough SwitchCase node which falls through.
16 * @param {ASTNode} subsequentCase The case after caseWhichFallsThrough.
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 case has a valid fallthrough comment.
21 function hasFallthroughComment(caseWhichFallsThrough, subsequentCase, context, fallthroughCommentPattern) {
22 const sourceCode = context.getSourceCode();
24 if (caseWhichFallsThrough.consequent.length === 1 && caseWhichFallsThrough.consequent[0].type === "BlockStatement") {
25 const trailingCloseBrace = sourceCode.getLastToken(caseWhichFallsThrough.consequent[0]);
26 const commentInBlock = sourceCode.getCommentsBefore(trailingCloseBrace).pop();
28 if (commentInBlock && fallthroughCommentPattern.test(commentInBlock.value)) {
33 const comment = sourceCode.getCommentsBefore(subsequentCase).pop();
35 return Boolean(comment && fallthroughCommentPattern.test(comment.value));
39 * Checks whether or not a given code path segment is reachable.
40 * @param {CodePathSegment} segment A CodePathSegment to check.
41 * @returns {boolean} `true` if the segment is reachable.
43 function isReachable(segment) {
44 return segment.reachable;
48 * Checks whether a node and a token are separated by blank lines
49 * @param {ASTNode} node The node to check
50 * @param {Token} token The token to compare against
51 * @returns {boolean} `true` if there are blank lines between node and token
53 function hasBlankLinesBetween(node, token) {
54 return token.loc.start.line > node.loc.end.line + 1;
57 //------------------------------------------------------------------------------
59 //------------------------------------------------------------------------------
66 description: "disallow fallthrough of `case` statements",
67 category: "Best Practices",
69 url: "https://eslint.org/docs/rules/no-fallthrough"
81 additionalProperties: false
85 case: "Expected a 'break' statement before 'case'.",
86 default: "Expected a 'break' statement before 'default'."
91 const options = context.options[0] || {};
92 let currentCodePath = null;
93 const sourceCode = context.getSourceCode();
96 * We need to use leading comments of the next SwitchCase node because
97 * trailing comments is wrong if semicolons are omitted.
99 let fallthroughCase = null;
100 let fallthroughCommentPattern = null;
102 if (options.commentPattern) {
103 fallthroughCommentPattern = new RegExp(options.commentPattern, "u");
105 fallthroughCommentPattern = DEFAULT_FALLTHROUGH_COMMENT;
109 onCodePathStart(codePath) {
110 currentCodePath = codePath;
113 currentCodePath = currentCodePath.upper;
119 * Checks whether or not there is a fallthrough comment.
120 * And reports the previous fallthrough node if that does not exist.
122 if (fallthroughCase && !hasFallthroughComment(fallthroughCase, node, context, fallthroughCommentPattern)) {
124 messageId: node.test ? "case" : "default",
128 fallthroughCase = null;
131 "SwitchCase:exit"(node) {
132 const nextToken = sourceCode.getTokenAfter(node);
135 * `reachable` meant fall through because statements preceded by
136 * `break`, `return`, or `throw` are unreachable.
137 * And allows empty cases and the last case.
139 if (currentCodePath.currentSegments.some(isReachable) &&
140 (node.consequent.length > 0 || hasBlankLinesBetween(node, nextToken)) &&
141 node.parent.cases[node.parent.cases.length - 1] !== node) {
142 fallthroughCase = node;