fix(editor): normalize LIKE case and completed SQL indentation

This commit is contained in:
zipg authored and GitHub committed 2026-08-17 02:09:50 +08:00
1 parent dbd93dae5a
commit 92ab9bb0dc
9 files changed
+237 -2

No files matched your search

+47
View File
@@ -52,6 +52,53 @@ describe("sqlFormatter", () => {
}
});
it.each(["mysql", "sqlite"] as const)("applies keyword case to LIKE operators in the %s dialect", async (dialect) => {
const sql = "select * from users where name like '%like%' and note not like 'LIKE'";
const upper = await formatSqlText(sql, dialect, { keywordCase: "upper", functionCase: "preserve" });
const lower = await formatSqlText(sql.toUpperCase(), dialect, { keywordCase: "lower", functionCase: "preserve", identifierCase: "lower" });
expect(upper).toContain("name LIKE '%like%'");
expect(upper).toContain("note NOT LIKE 'LIKE'");
expect(lower).toContain("name like '%LIKE%'");
expect(lower).toContain("note not like 'LIKE'");
});
it("keeps SQLite LIKE functions and qualified identifiers under their own case settings", async () => {
const formatted = await formatSqlText("select like('%a%', name), filters.like from users", "sqlite", {
keywordCase: "upper",
functionCase: "preserve",
identifierCase: "preserve",
});
expect(formatted).toContain("like(");
expect(formatted).toContain("filters.like");
});
it.each(["mysql", "sqlite"] as const)("does not rewrite LIKE inside DBX placeholders in the %s dialect", async (dialect) => {
for (const [lowerPlaceholder, upperPlaceholder] of [
["${like}", "${LIKE}"],
["#{like}", "#{LIKE}"],
[":like", ":LIKE"],
["@like", "@LIKE"],
]) {
const upper = await formatSqlText(`select * from users where name like 'a';\nselect ${lowerPlaceholder} as marker`, dialect, {
keywordCase: "upper",
functionCase: "preserve",
});
const lower = await formatSqlText(`SELECT * FROM users WHERE name LIKE 'a';\nSELECT ${upperPlaceholder} AS marker`, dialect, {
keywordCase: "lower",
functionCase: "preserve",
identifierCase: "lower",
});
expect(upper).toContain(lowerPlaceholder);
expect(upper).toContain("name LIKE 'a'");
expect(lower).toContain(upperPlaceholder);
expect(lower).toContain("name like 'a'");
}
});
it("falls back to the postgres formatter when the generic dialect cannot parse SQL", async () => {
const formatted = await formatSqlText("SELECT 1::int AS id;", "generic");
@@ -40,6 +40,14 @@ describe("sqlFormatterConfig shortcut storage", () => {
});
});
it("recognizes DBX positional and named parameter syntaxes by default", () => {
expect(sqlFormatterOptions({}).paramTypes).toEqual({
positional: true,
named: [":", "@"],
custom: [{ regex: String.raw`\$\{[^}]+\}` }, { regex: String.raw`#\{[^}]+\}` }],
});
});
it("accepts the same-line logical operator mode", () => {
const result = parseSqlFormatterConfig(JSON.stringify({ version: 1, formatter: "sql-formatter", options: { logicalOperatorNewline: "none" } }));