Skip to content
Merged
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
11 changes: 8 additions & 3 deletions src/syntax/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,18 @@ export const exprMap: CstToDocMap<AllExprNodes> = {
print("endKw"),
],
case_when: (print, node) => {
const whenThen = group([
group([print("whenKw"), indent([line, print("condition")])]),
line,
print("thenKw"),
]);
if (isProgram(node.result)) {
return [
print.spaced(["whenKw", "condition", "thenKw"]),
whenThen,
indent([hardline, stripTrailingHardline(print("result"))]),
];
}
return print.spaced(["whenKw", "condition", "thenKw", "result"]);
return group([whenThen, indent([line, print("result")])]);
},
case_else: (print, node) => {
if (isProgram(node.result)) {
Expand All @@ -137,7 +142,7 @@ export const exprMap: CstToDocMap<AllExprNodes> = {
indent([hardline, stripTrailingHardline(print("result"))]),
];
}
return print.spaced(["elseKw", "result"]);
return group([print("elseKw"), indent([line, print("result")])]);
},
member_expr: (print, node) =>
isArraySubscript(node.property)
Expand Down
73 changes: 73 additions & 0 deletions test/expr/expr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,79 @@ describe("expr", () => {
END
`);
});

it(`breaks long WHEN/THEN into separate lines`, async () => {
await test(
dedent`
SELECT
CASE
WHEN column_name = 1 THEN
result_name
END
`,
{ printWidth: 40 },
);
});

it(`breaks multiple long WHEN/THEN clauses without blank lines between them`, async () => {
await test(
dedent`
SELECT
CASE
WHEN column_name = 1 THEN
result_name
WHEN column_name = 2 THEN
other_result
ELSE foo
END
`,
{ printWidth: 40 },
);
});

it(`breaks long ELSE into separate lines`, async () => {
await test(
dedent`
SELECT
CASE
WHEN x = 1 THEN 'A'
ELSE
long_fallback_expression
END
`,
{ printWidth: 25 },
);
});

it(`indents long multi-condition WHEN clauses`, async () => {
await test(
dedent`
SELECT
CASE
WHEN
column_name = 1
AND (other_name = 2 OR other_name = 3)
THEN
result_name
END
`,
{ printWidth: 50 },
);
});

it(`indents long multi-expression THEN clauses`, async () => {
await test(
dedent`
SELECT
CASE
WHEN column_name = 1 THEN
result_name = 1
AND other_name = 2
END
`,
{ printWidth: 45 },
);
});
});

it(`formats quantifier expressions`, async () => {
Expand Down
17 changes: 17 additions & 0 deletions test/proc/case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,21 @@ describe("case", () => {
END CASE
`);
});

it(`breaks long WHEN/THEN clauses into separate lines`, async () => {
await testBigquery(
dedent`
CASE foo
WHEN
column_name = 1
AND (other_name = 2 OR other_name = 3)
THEN
SELECT CONCAT('Product one');
ELSE
SELECT CONCAT('Invalid product');
END CASE
`,
{ printWidth: 50 },
);
});
});
Loading