]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-fallthrough.js
import 8.4.0 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 },
81 additionalProperties: false
82 }
83 ],
84 messages: {
85 case: "Expected a 'break' statement before 'case'.",
86 default: "Expected a 'break' statement before 'default'."
87 }
88 },
89
90 create(context) {
91 const options = context.options[0] || {};
92 let currentCodePath = null;
93 const sourceCode = context.getSourceCode();
94
95 /*
96 * We need to use leading comments of the next SwitchCase node because
97 * trailing comments is wrong if semicolons are omitted.
98 */
99 let fallthroughCase = null;
100 let fallthroughCommentPattern = null;
101
102 if (options.commentPattern) {
103 fallthroughCommentPattern = new RegExp(options.commentPattern, "u");
104 } else {
105 fallthroughCommentPattern = DEFAULT_FALLTHROUGH_COMMENT;
106 }
107
108 return {
109 onCodePathStart(codePath) {
110 currentCodePath = codePath;
111 },
112 onCodePathEnd() {
113 currentCodePath = currentCodePath.upper;
114 },
115
116 SwitchCase(node) {
117
118 /*
119 * Checks whether or not there is a fallthrough comment.
120 * And reports the previous fallthrough node if that does not exist.
121 */
122 if (fallthroughCase && !hasFallthroughComment(fallthroughCase, node, context, fallthroughCommentPattern)) {
123 context.report({
124 messageId: node.test ? "case" : "default",
125 node
126 });
127 }
128 fallthroughCase = null;
129 },
130
131 "SwitchCase:exit"(node) {
132 const nextToken = sourceCode.getTokenAfter(node);
133
134 /*
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.
138 */
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;
143 }
144 }
145 };
146 }
147 };