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 //------------------------------------------------------------------------------
61 /** @type {import('../shared/types').Rule} */
67 description
: "Disallow fallthrough of `case` statements",
69 url
: "https://eslint.org/docs/rules/no-fallthrough"
85 additionalProperties
: false
89 case: "Expected a 'break' statement before 'case'.",
90 default: "Expected a 'break' statement before 'default'."
95 const options
= context
.options
[0] || {};
96 let currentCodePath
= null;
97 const sourceCode
= context
.getSourceCode();
98 const allowEmptyCase
= options
.allowEmptyCase
|| false;
101 * We need to use leading comments of the next SwitchCase node because
102 * trailing comments is wrong if semicolons are omitted.
104 let fallthroughCase
= null;
105 let fallthroughCommentPattern
= null;
107 if (options
.commentPattern
) {
108 fallthroughCommentPattern
= new RegExp(options
.commentPattern
, "u");
110 fallthroughCommentPattern
= DEFAULT_FALLTHROUGH_COMMENT
;
113 onCodePathStart(codePath
) {
114 currentCodePath
= codePath
;
117 currentCodePath
= currentCodePath
.upper
;
123 * Checks whether or not there is a fallthrough comment.
124 * And reports the previous fallthrough node if that does not exist.
127 if (fallthroughCase
&& (!hasFallthroughComment(fallthroughCase
, node
, context
, fallthroughCommentPattern
))) {
129 messageId
: node
.test
? "case" : "default",
133 fallthroughCase
= null;
136 "SwitchCase:exit"(node
) {
137 const nextToken
= sourceCode
.getTokenAfter(node
);
140 * `reachable` meant fall through because statements preceded by
141 * `break`, `return`, or `throw` are unreachable.
142 * And allows empty cases and the last case.
144 if (currentCodePath
.currentSegments
.some(isReachable
) &&
145 (node
.consequent
.length
> 0 || (!allowEmptyCase
&& hasBlankLinesBetween(node
, nextToken
))) &&
146 node
.parent
.cases
[node
.parent
.cases
.length
- 1] !== node
) {
147 fallthroughCase
= node
;