]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-fallthrough.js
import 8.23.1 source
[pve-eslint.git] / eslint / lib / rules / no-fallthrough.js
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 //------------------------------------------------------------------------------
8 // Helpers
9 //------------------------------------------------------------------------------
10
11 const DEFAULT_FALLTHROUGH_COMMENT = /falls?\s?through/iu;
12
13 /**
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.
20 */
21 function hasFallthroughComment(caseWhichFallsThrough, subsequentCase, context, fallthroughCommentPattern) {
22 const sourceCode = context.getSourceCode();
23
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();
27
28 if (commentInBlock && fallthroughCommentPattern.test(commentInBlock.value)) {
29 return true;
30 }
31 }
32
33 const comment = sourceCode.getCommentsBefore(subsequentCase).pop();
34
35 return Boolean(comment && fallthroughCommentPattern.test(comment.value));
36 }
37
38 /**
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.
42 */
43 function isReachable(segment) {
44 return segment.reachable;
45 }
46
47 /**
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
52 */
53 function hasBlankLinesBetween(node, token) {
54 return token.loc.start.line > node.loc.end.line + 1;
55 }
56
57 //------------------------------------------------------------------------------
58 // Rule Definition
59 //------------------------------------------------------------------------------
60
61 /** @type {import('../shared/types').Rule} */
62 module.exports = {
63 meta: {
64 type: "problem",
65
66 docs: {
67 description: "Disallow fallthrough of `case` statements",
68 recommended: true,
69 url: "https://eslint.org/docs/rules/no-fallthrough"
70 },
71
72 schema: [
73 {
74 type: "object",
75 properties: {
76 commentPattern: {
77 type: "string",
78 default: ""
79 },
80 allowEmptyCase: {
81 type: "boolean",
82 default: false
83 }
84 },
85 additionalProperties: false
86 }
87 ],
88 messages: {
89 case: "Expected a 'break' statement before 'case'.",
90 default: "Expected a 'break' statement before 'default'."
91 }
92 },
93
94 create(context) {
95 const options = context.options[0] || {};
96 let currentCodePath = null;
97 const sourceCode = context.getSourceCode();
98 const allowEmptyCase = options.allowEmptyCase || false;
99
100 /*
101 * We need to use leading comments of the next SwitchCase node because
102 * trailing comments is wrong if semicolons are omitted.
103 */
104 let fallthroughCase = null;
105 let fallthroughCommentPattern = null;
106
107 if (options.commentPattern) {
108 fallthroughCommentPattern = new RegExp(options.commentPattern, "u");
109 } else {
110 fallthroughCommentPattern = DEFAULT_FALLTHROUGH_COMMENT;
111 }
112 return {
113 onCodePathStart(codePath) {
114 currentCodePath = codePath;
115 },
116 onCodePathEnd() {
117 currentCodePath = currentCodePath.upper;
118 },
119
120 SwitchCase(node) {
121
122 /*
123 * Checks whether or not there is a fallthrough comment.
124 * And reports the previous fallthrough node if that does not exist.
125 */
126
127 if (fallthroughCase && (!hasFallthroughComment(fallthroughCase, node, context, fallthroughCommentPattern))) {
128 context.report({
129 messageId: node.test ? "case" : "default",
130 node
131 });
132 }
133 fallthroughCase = null;
134 },
135
136 "SwitchCase:exit"(node) {
137 const nextToken = sourceCode.getTokenAfter(node);
138
139 /*
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.
143 */
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;
148 }
149 }
150 };
151 }
152 };