Files
test-repo/src/language-js/print/if-statement.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

59 lines
1.5 KiB
JavaScript
Raw Normal View History

2025-12-06 14:39:00 +08:00
import { group, hardline } from "../../document/index.js";
2025-12-02 06:49:28 +08:00
import { printDanglingComments } from "../../main/comments/print.js";
import {
CommentCheckFlags,
hasComment,
needsHardlineAfterDanglingComment,
} from "../utilities/index.js";
2025-12-06 14:39:00 +08:00
import { adjustClause, printIfStatementCondition } from "./miscellaneous.js";
2025-12-02 06:49:28 +08:00
/**
* @import AstPath from "../../common/ast-path.js"
* @import {Doc} from "../../document/index.js"
*/
function printIfStatement(path, options, print) {
const { node } = path;
const consequent = adjustClause(node.consequent, print("consequent"));
const opening = group([
"if (",
2025-12-06 14:39:00 +08:00
printIfStatementCondition(path, options, print),
2025-12-02 06:49:28 +08:00
")",
consequent,
]);
/** @type{Doc[]} */
const parts = [opening];
if (node.alternate) {
const commentOnOwnLine =
hasComment(
node.consequent,
CommentCheckFlags.Trailing | CommentCheckFlags.Line,
) || needsHardlineAfterDanglingComment(node);
const elseOnSameLine =
node.consequent.type === "BlockStatement" && !commentOnOwnLine;
parts.push(elseOnSameLine ? " " : hardline);
if (hasComment(node, CommentCheckFlags.Dangling)) {
parts.push(
printDanglingComments(path, options),
commentOnOwnLine ? hardline : " ",
);
}
parts.push(
"else",
group(
adjustClause(
node.alternate,
print("alternate"),
node.alternate.type === "IfStatement",
),
),
);
}
return parts;
}
export { printIfStatement };