Skip to content

Commit d1afcfb

Browse files
committed
Implement sqlLiteralCase option for TRUE/FALSE/NULL
1 parent b57fded commit d1afcfb

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

src/options.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export const options: SupportOptions = {
4141
type: "choice",
4242
category: "SQL",
4343
default: "upper",
44-
description: "Enforces upper/lower case for SQL literals",
44+
description:
45+
"Enforces upper/lower case for SQL literals (TRUE, FALSE, NULL)",
4546
choices: [
4647
{
4748
value: "preserve",

src/syntax/expr.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
isCompoundSelectStmt,
2424
} from "../node_utils";
2525
import { isString, last } from "../utils";
26+
import { AllPrettierOptions } from "src/options";
2627

2728
export const exprMap: CstToDocMap<AllExprNodes> = {
2829
list_expr: (print, node, path) => {
@@ -169,13 +170,15 @@ export const exprMap: CstToDocMap<AllExprNodes> = {
169170
/** cst-ignore: value */
170171
number_literal: (print) => print("text"),
171172
/** cst-ignore: value */
172-
boolean_literal: (print) => print("valueKw"),
173+
boolean_literal: (print, node, path, options) =>
174+
printLiteral(node.valueKw, options),
173175
/** cst-ignore: value */
174176
string_literal: (print) => print("text"),
175177
/** cst-ignore: value */
176178
blob_literal: (print) => print("text"),
177179
/** cst-ignore: value */
178-
null_literal: (print) => print("nullKw"),
180+
null_literal: (print, node, path, options) =>
181+
printLiteral(node.nullKw, options),
179182
numeric_literal: (print) => print.spaced(["numericKw", "string"]),
180183
bignumeric_literal: (print) => print.spaced(["bignumericKw", "string"]),
181184
date_literal: (print) => print.spaced(["dateKw", "string"]),
@@ -191,4 +194,15 @@ export const exprMap: CstToDocMap<AllExprNodes> = {
191194
parameter: (print) => print("text"),
192195
};
193196

197+
const printLiteral = <T>(node: Keyword, options: AllPrettierOptions<T>) => {
198+
switch (options.sqlLiteralCase) {
199+
case "preserve":
200+
return node.text;
201+
case "upper":
202+
return node.text.toUpperCase();
203+
case "lower":
204+
return node.text.toLowerCase();
205+
}
206+
};
207+
194208
const isBooleanOp = ({ name }: Keyword) => name === "AND" || name === "OR";

test/options/literalCase.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import dedent from "dedent-js";
2+
import { pretty } from "../test_utils";
3+
4+
describe("sqlLiteralCase option", () => {
5+
it(`defaults to uppercasing of all literals`, async () => {
6+
expect(await pretty(`SELECT true, false, null`)).toBe(dedent`
7+
SELECT TRUE, FALSE, NULL
8+
`);
9+
});
10+
11+
it(`sqlKeywordCase: "preserve" keeps keywords case as-is`, async () => {
12+
expect(
13+
await pretty(`SELECT true, False, NULL`, {
14+
sqlLiteralCase: "preserve",
15+
}),
16+
).toBe(dedent`
17+
SELECT true, False, NULL
18+
`);
19+
});
20+
21+
it(`sqlLiteralCase: "upper" converts keywords to uppercase`, async () => {
22+
expect(
23+
await pretty(`SELECT true, False, NULL`, {
24+
sqlLiteralCase: "upper",
25+
}),
26+
).toBe(dedent`
27+
SELECT TRUE, FALSE, NULL
28+
`);
29+
});
30+
31+
it(`sqlLiteralCase: "lower" converts keywords to lowercase`, async () => {
32+
expect(
33+
await pretty(`SELECT true, False, NULL`, {
34+
sqlLiteralCase: "lower",
35+
}),
36+
).toBe(dedent`
37+
SELECT true, false, null
38+
`);
39+
});
40+
});

0 commit comments

Comments
 (0)