fix(formatter): stop line comments swallowing following tokens

This commit is contained in:
zipg authored and GitHub committed 2026-09-26 01:33:32 +08:00
1 parent c224b3c972
commit 08773d93f7
5 files changed
+119 -7

No files matched your search

+35
View File
@@ -69,6 +69,41 @@ test("rejects very large SQL before loading formatter work", async () => {
await assert.rejects(() => formatSqlText("x".repeat(MAX_SQL_FORMAT_CHARS + 1), "generic"), /too large/i);
});
test("keeps a trailing line comment from swallowing the column after it", async () => {
// Oracle stores a view's text as written, so a comment at the end of a column
// 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" },
);
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 () => {
const formatted = await formatSqlText("select a -- c\n, b from t", "postgres", { keywordCase: "upper" });
assert.equal(formatted, "SELECT a -- c\n,\n b\nFROM t");
});
test("keeps a line comment from swallowing the statement terminator", async () => {
const formatted = await formatSqlText("select a -- c\n;", "postgres", { keywordCase: "upper" });
assert.equal(formatted, "SELECT a -- c\n;");
});
test("formats a comment on its own line without extra breaks", async () => {
const formatted = await formatSqlText("select a, -- c\nb from t", "postgres", { keywordCase: "upper" });
assert.equal(formatted, "SELECT a,\n -- c\n b\nFROM t");
});
test("compressSqlText collapses whitespace into single spaces", () => {
const sql = "SELECT id,\n\t\tname\n FROM\n users\nWHERE active = 1";
assert.equal(compressSqlText(sql), "SELECT id, name FROM users WHERE active = 1");