]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-fallthrough.js
import 8.3.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 module.exports = {
62 meta: {
63 type: "problem",
64
65 docs: {
66 description: "disallow fallthrough of `case` statements",
67 recommended: true,
68 url: "https://eslint.org/docs/rules/no-fallthrough"
69 },
70
71 schema: [
72 {
73 type: "object",
74 properties: {
75 commentPattern: {
76 type: "string",
77 default: ""
78 }
79 },
80 additionalProperties: false
81 }
82 ],
83 messages: {
84 case: "Expected a 'break' statement before 'case'.",
85 default: "Expected a 'break' statement before 'default'."
86 }
87 },
88
89 create(context) {
90 const options = context.options[0] || {};
91 let currentCodePath = null;
92 const sourceCode = context.getSourceCode();
93
94 /*
95 * We need to use leading comments of the next SwitchCase node because
96 * trailing comments is wrong if semicolons are omitted.
97 */
98 let fallthroughCase = null;
99 let fallthroughCommentPattern = null;
100
101 if (options.commentPattern) {
102 fallthroughCommentPattern = new RegExp(options.commentPattern, "u");
103 } else {
104 fallthroughCommentPattern = DEFAULT_FALLTHROUGH_COMMENT;
105 }
106
107 return {
108 onCodePathStart(codePath) {
109 currentCodePath = codePath;
110 },
111 onCodePathEnd() {
112 currentCodePath = currentCodePath.upper;
113 },
114
115 SwitchCase(node) {
116
117 /*
118 * Checks whether or not there is a fallthrough comment.
119 * And reports the previous fallthrough node if that does not exist.
120 */
121 if (fallthroughCase && !hasFallthroughComment(fallthroughCase, node, context, fallthroughCommentPattern)) {
122 context.report({
123 messageId: node.test ? "case" : "default",
124 node
125 });
126 }
127 fallthroughCase = null;
128 },
129
130 "SwitchCase:exit"(node) {
131 const nextToken = sourceCode.getTokenAfter(node);
132
133 /*
134 * `reachable` meant fall through because statements preceded by
135 * `break`, `return`, or `throw` are unreachable.
136 * And allows empty cases and the last case.
137 */
138 if (currentCodePath.currentSegments.some(isReachable) &&
139 (node.consequent.length > 0 || hasBlankLinesBetween(node, nextToken)) &&
140 node.parent.cases[node.parent.cases.length - 1] !== node) {
141 fallthroughCase = node;
142 }
143 }
144 };
145 }
146 };