Files
test-repo/src/language-js/print/switch-statement.js
T
fisker Cheungandautofix-ci[bot] 16eb5096b6 Extract code from js utilities (#18770)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-01-28 10:30:30 +08:00

73 lines
1.7 KiB
JavaScript

import {
group,
hardline,
indent,
join,
softline,
} from "../../document/index.js";
import { printDanglingComments } from "../../main/comments/print.js";
import { CommentCheckFlags, hasComment } from "../utilities/comments.js";
import { isNextLineEmpty } from "../utilities/utilities.js";
import { printStatementSequence } from "./statement-sequence.js";
function printSwitchStatement(path, options, print) {
return [
group([
"switch (",
indent([softline, print("discriminant")]),
softline,
")",
]),
" {",
path.node.cases.length > 0
? indent([
hardline,
join(
hardline,
path.map(
({ node, isLast }) => [
print(),
!isLast && isNextLineEmpty(node, options) ? hardline : "",
],
"cases",
),
),
])
: "",
hardline,
"}",
];
}
function printSwitchCase(path, options, print) {
const { node } = path;
const parts = [];
if (node.test) {
parts.push("case ", print("test"), ":");
} else {
parts.push("default:");
}
if (hasComment(node, CommentCheckFlags.Dangling)) {
parts.push(" ", printDanglingComments(path, options));
}
const consequent = node.consequent.filter(
(node) => node.type !== "EmptyStatement",
);
if (consequent.length > 0) {
const cons = printStatementSequence(path, options, print, "consequent");
parts.push(
consequent.length === 1 && consequent[0].type === "BlockStatement"
? [" ", cons]
: indent([hardline, cons]),
);
}
return parts;
}
export { printSwitchCase, printSwitchStatement };