Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting of long if/else chains #2369

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/prettier-plugin/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { type SquiggleNode } from "./types.js";

export const squiggleParser: Parser<SquiggleNode> = {
parse: (text) => {
const parseResult = parse(text);
const parseResult = parse(text, { recognizeIfElseChains: true });
if (!parseResult.ok) {
throw new Error(`Parse failed. ${parseResult.value}`);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/prettier-plugin/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function getNodePrecedence(node: SquiggleNode): number {
};
switch (node.type) {
case "Ternary":
case "IfElseChain": // Added new case for IfElseChain
return 1;
case "InfixCall": {
const precedence = infixPrecedence[node.op];
Expand Down Expand Up @@ -309,13 +310,16 @@ export function createSquigglePrinter(
case "String":
return [JSON.stringify(node.value).replaceAll("\\n", "\n")];
case "Ternary":
case "IfElseChain": // Added new case for IfElseChain
return [
node.kind === "C" ? [] : "if ",
path.call(print, "condition"),
node.kind === "C" ? " ? " : " then ",
path.call(print, "trueExpression"),
node.kind === "C" ? " : " : " else ",
path.call(print, "falseExpression"),
// Add line breaks for IfElseChain
node.type === "IfElseChain" ? hardline : "",
];
case "Void":
return "()";
Expand Down Expand Up @@ -350,6 +354,7 @@ export function createSquigglePrinter(
}
switch (node.type) {
case "Program":
case "IfElseChain": // Added new case for IfElseChain
return node.statements;
case "Block":
return node.statements;
Expand Down
Loading