feat(sql-formatter): add setting to preserve blank lines
This commit is contained in:
16 files changed
+158
-7
No files matched your search
@@ -16,6 +16,30 @@ describe("sqlFormatter", () => {
|
||||
expect(canFormatSqlForDatabaseType("mysql")).toBe(true);
|
||||
});
|
||||
|
||||
it("preserves source empty lines only when configured", async () => {
|
||||
const sql = "-- tstt\n\nSELECT * FROM AUX_TABLE AS au LIMIT 100;";
|
||||
const consecutiveEmptyLines = "-- section\n\n\nSELECT 1;";
|
||||
const queriesWithEmptyLine = "SELECT 1;\n\nSELECT 2;";
|
||||
const queriesWithTwoEmptyLines = "SELECT 1;\n\n\nSELECT 2;";
|
||||
|
||||
const defaultFormatted = await formatSqlForEditing(sql, "generic");
|
||||
const preserved = await formatSqlForEditing(sql, "generic", { preserveEmptyLines: true });
|
||||
const preservedConsecutive = await formatSqlForEditing(consecutiveEmptyLines, "generic", { preserveEmptyLines: true });
|
||||
const preservedQueriesNoSpacing = await formatSqlForEditing(queriesWithEmptyLine, "generic", { preserveEmptyLines: true, linesBetweenQueries: 0 });
|
||||
const preservedQueries = await formatSqlForEditing(queriesWithEmptyLine, "generic", { preserveEmptyLines: true, linesBetweenQueries: 1 });
|
||||
const preservedQueriesWideSpacing = await formatSqlForEditing(queriesWithEmptyLine, "generic", { preserveEmptyLines: true, linesBetweenQueries: 2 });
|
||||
const preservedQueriesWithTwoEmptyLines = await formatSqlForEditing(queriesWithTwoEmptyLines, "generic", { preserveEmptyLines: true, linesBetweenQueries: 1 });
|
||||
|
||||
expect(defaultFormatted).toContain("-- tstt\nSELECT");
|
||||
expect(preserved).toContain("-- tstt\n\nSELECT");
|
||||
expect(preservedConsecutive).toContain("-- section\n\n\nSELECT");
|
||||
expect(preservedQueriesNoSpacing).toBe("SELECT\n 1;\n\nSELECT\n 2;");
|
||||
expect(preservedQueries).toBe("SELECT\n 1;\n\nSELECT\n 2;");
|
||||
expect(preservedQueriesWideSpacing).toBe("SELECT\n 1;\n\n\nSELECT\n 2;");
|
||||
expect(preservedQueriesWithTwoEmptyLines).toBe("SELECT\n 1;\n\n\nSELECT\n 2;");
|
||||
expect(preserved).not.toContain("__DBX_PRESERVE_EMPTY_LINE_");
|
||||
});
|
||||
|
||||
it("maps PostgreSQL-compatible database types to the postgres formatter dialect", () => {
|
||||
for (const dbType of ["postgres", "kwdb", "gaussdb", "opengauss", "questdb", "kingbase", "highgo", "vastbase", "redshift"]) {
|
||||
expect(sqlFormatDialectForDbType(dbType)).toBe("postgres");
|
||||
|
||||
@@ -63,4 +63,13 @@ describe("sqlFormatterConfig shortcut storage", () => {
|
||||
if (result.ok) expect(result.settings.fromClauseLayout).toBe("sameLine");
|
||||
expect(JSON.parse(serializeSqlFormatterConfig({ fromClauseLayout: "sameLine" })).options.fromClauseLayout).toBe("sameLine");
|
||||
});
|
||||
|
||||
it("defaults empty-line preservation off and serializes an explicit opt-in", () => {
|
||||
const defaultConfig = JSON.parse(serializeSqlFormatterConfig({}));
|
||||
const optIn = parseSqlFormatterConfig(JSON.stringify({ version: 1, formatter: "sql-formatter", options: { preserveEmptyLines: true } }));
|
||||
|
||||
expect(defaultConfig.options.preserveEmptyLines).toBe(false);
|
||||
expect(optIn).toEqual(expect.objectContaining({ ok: true }));
|
||||
if (optIn.ok) expect(optIn.settings.preserveEmptyLines).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user