google_forms/node_modules/eslint/lib/rules/no-fallthrough.js

197 lines
6.9 KiB
JavaScript
Raw Permalink Normal View History

2024-08-09 12:04:48 +00:00
/**
* @fileoverview Rule to flag fall-through cases in switch statements.
* @author Matt DuVall <http://mattduvall.com/>
*/
"use strict";
2024-08-21 06:34:30 +00:00
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const { directivesPattern } = require("../shared/directives");
2024-08-09 12:04:48 +00:00
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const DEFAULT_FALLTHROUGH_COMMENT = /falls?\s?through/iu;
2024-08-21 06:34:30 +00:00
/**
* Checks all segments in a set and returns true if any are reachable.
* @param {Set<CodePathSegment>} segments The segments to check.
* @returns {boolean} True if any segment is reachable; false otherwise.
*/
function isAnySegmentReachable(segments) {
for (const segment of segments) {
if (segment.reachable) {
return true;
}
}
return false;
}
/**
* Checks whether or not a given comment string is really a fallthrough comment and not an ESLint directive.
* @param {string} comment The comment string to check.
* @param {RegExp} fallthroughCommentPattern The regular expression used for checking for fallthrough comments.
* @returns {boolean} `true` if the comment string is truly a fallthrough comment.
*/
function isFallThroughComment(comment, fallthroughCommentPattern) {
return fallthroughCommentPattern.test(comment) && !directivesPattern.test(comment.trim());
}
2024-08-09 12:04:48 +00:00
/**
* Checks whether or not a given case has a fallthrough comment.
* @param {ASTNode} caseWhichFallsThrough SwitchCase node which falls through.
* @param {ASTNode} subsequentCase The case after caseWhichFallsThrough.
* @param {RuleContext} context A rule context which stores comments.
* @param {RegExp} fallthroughCommentPattern A pattern to match comment to.
* @returns {boolean} `true` if the case has a valid fallthrough comment.
*/
function hasFallthroughComment(caseWhichFallsThrough, subsequentCase, context, fallthroughCommentPattern) {
2024-08-21 06:34:30 +00:00
const sourceCode = context.sourceCode;
2024-08-09 12:04:48 +00:00
if (caseWhichFallsThrough.consequent.length === 1 && caseWhichFallsThrough.consequent[0].type === "BlockStatement") {
const trailingCloseBrace = sourceCode.getLastToken(caseWhichFallsThrough.consequent[0]);
const commentInBlock = sourceCode.getCommentsBefore(trailingCloseBrace).pop();
2024-08-21 06:34:30 +00:00
if (commentInBlock && isFallThroughComment(commentInBlock.value, fallthroughCommentPattern)) {
2024-08-09 12:04:48 +00:00
return true;
}
}
const comment = sourceCode.getCommentsBefore(subsequentCase).pop();
2024-08-21 06:34:30 +00:00
return Boolean(comment && isFallThroughComment(comment.value, fallthroughCommentPattern));
2024-08-09 12:04:48 +00:00
}
/**
* Checks whether a node and a token are separated by blank lines
* @param {ASTNode} node The node to check
* @param {Token} token The token to compare against
* @returns {boolean} `true` if there are blank lines between node and token
*/
function hasBlankLinesBetween(node, token) {
return token.loc.start.line > node.loc.end.line + 1;
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
2024-08-21 06:34:30 +00:00
/** @type {import('../shared/types').Rule} */
2024-08-09 12:04:48 +00:00
module.exports = {
meta: {
type: "problem",
docs: {
2024-08-21 06:34:30 +00:00
description: "Disallow fallthrough of `case` statements",
2024-08-09 12:04:48 +00:00
recommended: true,
2024-08-21 06:34:30 +00:00
url: "https://eslint.org/docs/latest/rules/no-fallthrough"
2024-08-09 12:04:48 +00:00
},
schema: [
{
type: "object",
properties: {
commentPattern: {
type: "string",
default: ""
2024-08-21 06:34:30 +00:00
},
allowEmptyCase: {
type: "boolean",
default: false
2024-08-09 12:04:48 +00:00
}
},
additionalProperties: false
}
],
messages: {
case: "Expected a 'break' statement before 'case'.",
default: "Expected a 'break' statement before 'default'."
}
},
create(context) {
const options = context.options[0] || {};
2024-08-21 06:34:30 +00:00
const codePathSegments = [];
let currentCodePathSegments = new Set();
const sourceCode = context.sourceCode;
const allowEmptyCase = options.allowEmptyCase || false;
2024-08-09 12:04:48 +00:00
/*
* We need to use leading comments of the next SwitchCase node because
* trailing comments is wrong if semicolons are omitted.
*/
let fallthroughCase = null;
let fallthroughCommentPattern = null;
if (options.commentPattern) {
fallthroughCommentPattern = new RegExp(options.commentPattern, "u");
} else {
fallthroughCommentPattern = DEFAULT_FALLTHROUGH_COMMENT;
}
return {
2024-08-21 06:34:30 +00:00
onCodePathStart() {
codePathSegments.push(currentCodePathSegments);
currentCodePathSegments = new Set();
2024-08-09 12:04:48 +00:00
},
2024-08-21 06:34:30 +00:00
2024-08-09 12:04:48 +00:00
onCodePathEnd() {
2024-08-21 06:34:30 +00:00
currentCodePathSegments = codePathSegments.pop();
},
onUnreachableCodePathSegmentStart(segment) {
currentCodePathSegments.add(segment);
2024-08-09 12:04:48 +00:00
},
2024-08-21 06:34:30 +00:00
onUnreachableCodePathSegmentEnd(segment) {
currentCodePathSegments.delete(segment);
},
onCodePathSegmentStart(segment) {
currentCodePathSegments.add(segment);
},
onCodePathSegmentEnd(segment) {
currentCodePathSegments.delete(segment);
},
2024-08-09 12:04:48 +00:00
SwitchCase(node) {
/*
* Checks whether or not there is a fallthrough comment.
* And reports the previous fallthrough node if that does not exist.
*/
2024-08-21 06:34:30 +00:00
if (fallthroughCase && (!hasFallthroughComment(fallthroughCase, node, context, fallthroughCommentPattern))) {
2024-08-09 12:04:48 +00:00
context.report({
messageId: node.test ? "case" : "default",
node
});
}
fallthroughCase = null;
},
"SwitchCase:exit"(node) {
const nextToken = sourceCode.getTokenAfter(node);
/*
* `reachable` meant fall through because statements preceded by
* `break`, `return`, or `throw` are unreachable.
* And allows empty cases and the last case.
*/
2024-08-21 06:34:30 +00:00
if (isAnySegmentReachable(currentCodePathSegments) &&
(node.consequent.length > 0 || (!allowEmptyCase && hasBlankLinesBetween(node, nextToken))) &&
2024-08-09 12:04:48 +00:00
node.parent.cases[node.parent.cases.length - 1] !== node) {
fallthroughCase = node;
}
}
};
}
};