mirror of
https://github.com/t8y2/dbx.git
synced 2026-10-19 16:16:45 +00:00
fix(sql): honor newline before semicolon in dbx layout
This commit is contained in:
+70
-33
@@ -18,10 +18,7 @@ test("rejects very large SQL before importing formatter", async () => {
|
||||
});
|
||||
|
||||
test("formats SQL with uppercase keywords and readable line breaks by default", async () => {
|
||||
const formatted = await formatSqlText(
|
||||
"select id, name, email, created_at, updated_at, deleted_at, tenant_id from users where active = 1 and verified = 1 order by name, created_at desc",
|
||||
"postgres",
|
||||
);
|
||||
const formatted = await formatSqlText("select id, name, email, created_at, updated_at, deleted_at, tenant_id from users where active = 1 and verified = 1 order by name, created_at desc", "postgres");
|
||||
|
||||
assert.match(formatted, /^SELECT\b/);
|
||||
assert.match(formatted, /\nFROM\b/);
|
||||
@@ -39,10 +36,7 @@ test("aligns SELECT field continuations under the first field", async () => {
|
||||
test("aligns explicit SELECT aliases in the default layout", async () => {
|
||||
const formatted = await formatSqlText("select bh.id as unique_value, bh.title as title, bh.house_type as type from biz_house bh", "mysql");
|
||||
|
||||
assert.equal(
|
||||
formatted,
|
||||
"SELECT bh.id AS unique_value,\n bh.title AS title,\n bh.house_type AS type\nFROM biz_house bh",
|
||||
);
|
||||
assert.equal(formatted, "SELECT bh.id AS unique_value,\n bh.title AS title,\n bh.house_type AS type\nFROM biz_house bh");
|
||||
assert.equal(await formatSqlText(formatted, "mysql"), formatted);
|
||||
});
|
||||
|
||||
@@ -53,22 +47,18 @@ test("collapses a single-field statement that fits on one line", async () => {
|
||||
});
|
||||
|
||||
test("formats SQL with custom keyword case and indentation settings", async () => {
|
||||
const formatted = await formatSqlText(
|
||||
"select id, name, email, created_at, updated_at, deleted_at, tenant_id from users where active = 1 and verified = 1 order by name, created_at desc",
|
||||
"postgres",
|
||||
{
|
||||
keywordCase: "lower",
|
||||
dataTypeCase: "preserve",
|
||||
functionCase: "preserve",
|
||||
useTabs: true,
|
||||
tabWidth: 2,
|
||||
logicalOperatorNewline: "before",
|
||||
expressionWidth: 50,
|
||||
linesBetweenQueries: 1,
|
||||
denseOperators: false,
|
||||
newlineBeforeSemicolon: false,
|
||||
},
|
||||
);
|
||||
const formatted = await formatSqlText("select id, name, email, created_at, updated_at, deleted_at, tenant_id from users where active = 1 and verified = 1 order by name, created_at desc", "postgres", {
|
||||
keywordCase: "lower",
|
||||
dataTypeCase: "preserve",
|
||||
functionCase: "preserve",
|
||||
useTabs: true,
|
||||
tabWidth: 2,
|
||||
logicalOperatorNewline: "before",
|
||||
expressionWidth: 50,
|
||||
linesBetweenQueries: 1,
|
||||
denseOperators: false,
|
||||
newlineBeforeSemicolon: false,
|
||||
});
|
||||
|
||||
assert.match(formatted, /^select\b/);
|
||||
assert.match(formatted, /\nfrom\b/);
|
||||
@@ -91,16 +81,9 @@ test("keeps a trailing line comment from swallowing the column after it", async
|
||||
// line is followed by the next column on its own line. A `--` comment consumes
|
||||
// the rest of its line, so the next column has to start a new line instead of
|
||||
// being appended to that comment.
|
||||
const formatted = await formatSqlText(
|
||||
'CREATE VIEW "TEMP_TEST_VIEW" AS SELECT trunc(sysdate) AS dates, -- 测试\n(SELECT sysdate FROM dual t) AS nows\nFROM dual;',
|
||||
"oracle",
|
||||
{ keywordCase: "upper" },
|
||||
);
|
||||
const formatted = await formatSqlText('CREATE VIEW "TEMP_TEST_VIEW" AS SELECT trunc(sysdate) AS dates, -- 测试\n(SELECT sysdate FROM dual t) AS nows\nFROM dual;', "oracle", { keywordCase: "upper" });
|
||||
|
||||
assert.equal(
|
||||
formatted,
|
||||
'CREATE VIEW "TEMP_TEST_VIEW" AS\nSELECT trunc(sysdate) AS dates,\n -- 测试\n (SELECT sysdate FROM dual t) AS nows\nFROM dual;',
|
||||
);
|
||||
assert.equal(formatted, 'CREATE VIEW "TEMP_TEST_VIEW" AS\nSELECT trunc(sysdate) AS dates,\n -- 测试\n (SELECT sysdate FROM dual t) AS nows\nFROM dual;');
|
||||
});
|
||||
|
||||
test("keeps a line comment from swallowing the separator after it", async () => {
|
||||
@@ -336,3 +319,57 @@ test("formats SQL preserving original keyword case when keywordCase is preserve
|
||||
assert.doesNotMatch(formatted, /\bFROM\b/);
|
||||
assert.doesNotMatch(formatted, /\bWHERE\b/);
|
||||
});
|
||||
|
||||
test("formats SQL placing semicolons on their own line when newlineBeforeSemicolon is true (#12440)", async () => {
|
||||
const selectSql = "SELECT id, name FROM users WHERE id = 1;";
|
||||
const formattedSelect = await formatSqlText(selectSql, "mysql", {
|
||||
layoutStyle: "dbx",
|
||||
newlineBeforeSemicolon: true,
|
||||
});
|
||||
assert.match(formattedSelect, /\n;$/);
|
||||
|
||||
const updateSql = "UPDATE users SET name = 'Alice' WHERE id = 1;";
|
||||
const formattedUpdate = await formatSqlText(updateSql, "mysql", {
|
||||
layoutStyle: "dbx",
|
||||
newlineBeforeSemicolon: true,
|
||||
});
|
||||
assert.match(formattedUpdate, /\n;$/);
|
||||
|
||||
const collapsedSql = "SELECT 1;";
|
||||
const formattedCollapsed = await formatSqlText(collapsedSql, "mysql", {
|
||||
layoutStyle: "dbx",
|
||||
newlineBeforeSemicolon: true,
|
||||
});
|
||||
assert.equal(formattedCollapsed, "SELECT 1\n;");
|
||||
|
||||
const ddlSql = "CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50));";
|
||||
const formattedDdl = await formatSqlText(ddlSql, "mysql", {
|
||||
layoutStyle: "dbx",
|
||||
newlineBeforeSemicolon: true,
|
||||
});
|
||||
assert.match(formattedDdl, /\n;$/);
|
||||
|
||||
const multiSql = "SELECT 1; SELECT 2;";
|
||||
const formattedMulti = await formatSqlText(multiSql, "mysql", {
|
||||
layoutStyle: "dbx",
|
||||
newlineBeforeSemicolon: true,
|
||||
});
|
||||
assert.match(formattedMulti, /SELECT 1\n;\n\nSELECT 2\n;/);
|
||||
});
|
||||
|
||||
test("formats SQL with semicolons on same line by default when newlineBeforeSemicolon is false (#12440)", async () => {
|
||||
const selectSql = "SELECT id, name FROM users WHERE id = 1;";
|
||||
const formattedSelect = await formatSqlText(selectSql, "mysql", {
|
||||
layoutStyle: "dbx",
|
||||
newlineBeforeSemicolon: false,
|
||||
});
|
||||
assert.match(formattedSelect, /1;$/);
|
||||
assert.doesNotMatch(formattedSelect, /\n;$/);
|
||||
|
||||
const collapsedSql = "SELECT 1;";
|
||||
const formattedCollapsed = await formatSqlText(collapsedSql, "mysql", {
|
||||
layoutStyle: "dbx",
|
||||
newlineBeforeSemicolon: false,
|
||||
});
|
||||
assert.equal(formattedCollapsed, "SELECT 1;");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user