feat(sql): support comma position configuration in SQL formatter
This commit is contained in:
1 parent
9a6a2049bf
commit
760423873a
23 files changed
+222
-5
No files matched your search
@@ -632,4 +632,21 @@ AND owner_id = 42`,
|
||||
expect(formatted).toContain("SELECT");
|
||||
expect(formatted).toContain("::jsonb");
|
||||
});
|
||||
|
||||
it("formats multiline items with leading comma position", async () => {
|
||||
const sql = "SELECT col1, col2, col3 FROM tbl WHERE a = 1;";
|
||||
const formatted = await formatSqlText(sql, "generic", { commaPosition: "before", indentStyle: "tabularLeft" });
|
||||
|
||||
expect(formatted).toContain(", col2");
|
||||
expect(formatted).toContain(", col3");
|
||||
expect(formatted).not.toMatch(/col1,/);
|
||||
});
|
||||
|
||||
it("preserves line comments when formatting with leading comma position", async () => {
|
||||
const sql = "SELECT\n col1, -- first column\n col2 -- second column\nFROM tbl;";
|
||||
const formatted = await formatSqlText(sql, "generic", { commaPosition: "before", indentStyle: "tabularLeft" });
|
||||
|
||||
expect(formatted).toContain("col1 -- first column");
|
||||
expect(formatted).toMatch(/,\s*col2\s*-- second column/);
|
||||
});
|
||||
});
|
||||
@@ -72,4 +72,20 @@ describe("sqlFormatterConfig shortcut storage", () => {
|
||||
expect(optIn).toEqual(expect.objectContaining({ ok: true }));
|
||||
if (optIn.ok) expect(optIn.settings.preserveEmptyLines).toBe(true);
|
||||
});
|
||||
|
||||
it("defaults commaPosition to after and accepts before", () => {
|
||||
const defaultConfig = JSON.parse(serializeSqlFormatterConfig({}));
|
||||
expect(defaultConfig.options.commaPosition).toBe("after");
|
||||
|
||||
const beforeResult = parseSqlFormatterConfig(JSON.stringify({ version: 1, formatter: "sql-formatter", options: { commaPosition: "before" } }));
|
||||
expect(beforeResult).toEqual(expect.objectContaining({ ok: true }));
|
||||
if (beforeResult.ok) expect(beforeResult.settings.commaPosition).toBe("before");
|
||||
expect(JSON.parse(serializeSqlFormatterConfig({ commaPosition: "before" })).options.commaPosition).toBe("before");
|
||||
|
||||
const invalidResult = parseSqlFormatterConfig(JSON.stringify({ version: 1, formatter: "sql-formatter", options: { commaPosition: "invalid" } }));
|
||||
expect(invalidResult).toEqual(expect.objectContaining({ ok: false }));
|
||||
|
||||
// sqlFormatterOptions must not include commaPosition so third-party sql-formatter does not throw
|
||||
expect("commaPosition" in sqlFormatterOptions({ commaPosition: "before" })).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -321,4 +321,42 @@ describe("sql layout", () => {
|
||||
it("formats a dialect without its own grammar", async () => {
|
||||
expect(await format("select * from t where a = 1 and b = 2;", {}, "generic")).toBe("SELECT * FROM t WHERE a = 1 AND b = 2;");
|
||||
});
|
||||
|
||||
it("formats a query with leading comma position (#7723, #5110)", async () => {
|
||||
const sql = lines("SELECT loc_id, loc_name, loc_type, delivery_emp_num, update_time", "FROM tbl", "WHERE rn = 1;");
|
||||
|
||||
expect(await format(sql, { commaPosition: "before" })).toBe(lines("SELECT loc_id", " , loc_name", " , loc_type", " , delivery_emp_num", " , update_time", "FROM tbl", "WHERE rn = 1;"));
|
||||
});
|
||||
|
||||
it("formats aligned projection aliases with leading commas", async () => {
|
||||
const sql = lines("SELECT id AS emp_id, department_name AS dept, active_status AS status", "FROM employees;");
|
||||
|
||||
expect(await format(sql, { commaPosition: "before" })).toBe(lines("SELECT id AS emp_id", " , department_name AS dept", " , active_status AS status", "FROM employees;"));
|
||||
});
|
||||
|
||||
it("formats CREATE TABLE with leading commas in DDL", async () => {
|
||||
const sql = lines(
|
||||
"CREATE TABLE IF NOT EXISTS `delivery_emp_info`",
|
||||
"(",
|
||||
" `id` varchar(20) PRIMARY KEY NOT NULL COMMENT '主键',",
|
||||
" `emp_id` varchar(20) NOT NULL COMMENT '员工id',",
|
||||
" `delivery_type_ids` varchar(512) DEFAULT NULL COMMENT '负责的运送类型id集合',",
|
||||
" `update_time` datetime NOT NULL COMMENT '更新时间',",
|
||||
" CONSTRAINT idx_emp_phone UNIQUE (emp_id, emp_phone_num) COMMENT '运送员id电话唯一'",
|
||||
") ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_bin COMMENT '运达员信息表';",
|
||||
);
|
||||
|
||||
expect(await format(sql, { commaPosition: "before" })).toBe(
|
||||
lines(
|
||||
"CREATE TABLE IF NOT EXISTS `delivery_emp_info`",
|
||||
"(",
|
||||
" `id` varchar(20) PRIMARY KEY NOT NULL COMMENT '主键'",
|
||||
" , `emp_id` varchar(20) NOT NULL COMMENT '员工id'",
|
||||
" , `delivery_type_ids` varchar(512) DEFAULT NULL COMMENT '负责的运送类型id集合'",
|
||||
" , `update_time` datetime NOT NULL COMMENT '更新时间'",
|
||||
" , CONSTRAINT idx_emp_phone UNIQUE (emp_id, emp_phone_num) COMMENT '运送员id电话唯一'",
|
||||
") ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_bin COMMENT '运达员信息表';",
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user