From 08773d93f7ccee5bed425b52c1c2c13e535f1842 Mon Sep 17 00:00:00 2001 From: zipg Date: Sat, 26 Sep 2026 01:33:32 +0800 Subject: [PATCH] fix(formatter): stop line comments swallowing following tokens --- .../lib/__tests__/sql/sqlFormatter.spec.ts | 10 +++ apps/desktop/src/lib/sql/layout/ddl.ts | 5 +- apps/desktop/src/lib/sql/layout/primitives.ts | 66 ++++++++++++++++++- apps/desktop/src/lib/sql/layout/printer.ts | 10 +-- packages/app-tests/sqlFormatter.test.ts | 35 ++++++++++ 5 files changed, 119 insertions(+), 7 deletions(-) diff --git a/apps/desktop/src/lib/__tests__/sql/sqlFormatter.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlFormatter.spec.ts index 6f1f24e3c1..ca0ecabb95 100644 --- a/apps/desktop/src/lib/__tests__/sql/sqlFormatter.spec.ts +++ b/apps/desktop/src/lib/__tests__/sql/sqlFormatter.spec.ts @@ -75,6 +75,16 @@ describe("sqlFormatter", () => { expect(formatted).toContain('"ACTIVE_USERS"'); }); + it("keeps a view's trailing line comment from swallowing the next column (issue #10278)", async () => { + // A `--` comment consumes the rest of its line, so the column that follows + // it in the view text has to start a new line; appending it to the comment + // would silently drop it from the DDL shown to the user. + const viewDdl = `CREATE VIEW "TEMP_TEST_VIEW" AS SELECT trunc(sysdate) AS dates, -- 测试\n(SELECT sysdate FROM dual t) AS nows\nFROM dual;`; + const formatted = await formatSqlForDisplay(viewDdl, sqlFormatDialectForDbType("oracle")); + + expect(formatted).toBe('CREATE VIEW "TEMP_TEST_VIEW" AS\nSELECT trunc(sysdate) AS dates,\n -- 测试\n (SELECT sysdate FROM dual t) AS nows\nFROM dual;'); + }); + it("collapses a short OceanBase Oracle view DDL onto one line (issue #7540)", async () => { // The default style joins a statement that fits the line width onto one // line, so a short view body comes back with its keywords cased rather diff --git a/apps/desktop/src/lib/sql/layout/ddl.ts b/apps/desktop/src/lib/sql/layout/ddl.ts index 3d6848f93d..511827c752 100644 --- a/apps/desktop/src/lib/sql/layout/ddl.ts +++ b/apps/desktop/src/lib/sql/layout/ddl.ts @@ -1,5 +1,5 @@ import type { AstNode, ClauseNode, ParenthesisNode, StatementNode } from "sql-formatter/dist/esm/parser/ast.js"; -import { CREATE_TABLE_KEYWORD, keywordText, renderInline, splitByComma, Writer, type SqlLayoutContext } from "./primitives"; +import { CREATE_TABLE_KEYWORD, endsWithLineComment, keywordText, renderInline, splitByComma, Writer, type SqlLayoutContext } from "./primitives"; /** * Column alignment for `CREATE TABLE`. @@ -131,6 +131,7 @@ export function createTableLayout(statement: StatementNode, ctx: SqlLayoutContex } else { writer.newline(indent); writer.write(ctx.renderers.block(rest).trim()); + if (endsWithLineComment(rest)) writer.markLineComment(); } } if (statement.hasSemicolon) writer.write(";"); @@ -153,8 +154,10 @@ function emitDefinition(writer: Writer, nodes: AstNode[], indent: number, ctx: S writer.write(`${keywordText(first.text, ctx)} ${renderInline(ctx, [second], ctx.options.lineWidth) ?? second.text}`); writer.newline(indent + ctx.options.indentWidth); writer.write(ctx.renderers.block(nodes.slice(2)).trim()); + if (endsWithLineComment(nodes.slice(2))) writer.markLineComment(); return; } writer.write(ctx.renderers.block(nodes).trim()); + if (endsWithLineComment(nodes)) writer.markLineComment(); } diff --git a/apps/desktop/src/lib/sql/layout/primitives.ts b/apps/desktop/src/lib/sql/layout/primitives.ts index eda899feca..d50668dbd1 100644 --- a/apps/desktop/src/lib/sql/layout/primitives.ts +++ b/apps/desktop/src/lib/sql/layout/primitives.ts @@ -74,6 +74,15 @@ export interface SqlLayoutContext { export class Writer { private lines: string[] = [""]; private columns: number[] = [0]; + /** + * Whether the text written last ends inside a `--` comment. + * + * A line comment consumes the rest of its line, so whatever the printer emits + * next — the separating comma, the closing parenthesis, the statement's `;` — + * would land inside the comment and disappear from the formatted SQL. The next + * `write`/`space` therefore continues on a new line first. + */ + private lineCommentOpen = false; constructor(private readonly options: Pick) {} @@ -84,12 +93,18 @@ export class Writer { /** Appends text verbatim. */ write(text: string): void { + if (text.length === 0) return; + this.closeLineComment(); this.lines[this.lines.length - 1] += text; this.columns[this.columns.length - 1] += text.length; } /** Appends a single separating space, unless one is already there. */ space(): void { + if (this.lineCommentOpen) { + this.closeLineComment(); + return; + } const line = this.lines[this.lines.length - 1]; if (line.length > 0 && !line.endsWith(" ")) { this.lines[this.lines.length - 1] = `${line} `; @@ -97,6 +112,16 @@ export class Writer { } } + /** + * Records that the text written last ends inside a `--` comment, so the next + * `write`/`space` has to start a new line. Callers know this from the AST — + * see {@link endsWithLineComment} — because only they can tell a comment token + * apart from the same two dashes inside a string literal. + */ + markLineComment(): void { + this.lineCommentOpen = true; + } + /** Starts a new line at `indent` columns, dropping trailing blanks first. */ newline(indent: number): void { const last = this.lines.length - 1; @@ -104,6 +129,7 @@ export class Writer { const column = Math.max(0, indent); this.lines.push(this.indentation(column)); this.columns.push(column); + this.lineCommentOpen = false; } toString(): string { @@ -113,6 +139,28 @@ export class Writer { .trim(); } + /** + * Continues on a new line after a comment, keeping the indentation of the + * line the comment is on: the next column of a `SELECT` list lines up under + * the one the comment was written on. + */ + private closeLineComment(): void { + if (!this.lineCommentOpen) return; + this.newline(this.lineIndent()); + } + + /** Display columns taken by the indentation of the current line. */ + private lineIndent(): number { + const indentWidth = Math.max(1, this.options.indentWidth); + let columns = 0; + for (const character of this.lines[this.lines.length - 1]) { + if (character === "\t") columns += indentWidth; + else if (character === " ") columns += 1; + else break; + } + return columns; + } + /** The whitespace achieving `column` columns of indentation. */ private indentation(column: number): string { if (!this.options.useTabs) return " ".repeat(column); @@ -125,6 +173,21 @@ export function isParenthesis(node: AstNode): node is ParenthesisNode { return node.type === "parenthesis"; } +/** Whether `node` is a `--` comment, which comments out the rest of its line. */ +export function isLineComment(node: AstNode | undefined): boolean { + return node?.type === "line_comment"; +} + +/** + * Whether `nodes` end with a `--` comment. + * + * The caller that renders such a run has to tell the {@link Writer} about it, so + * that the next token is not written into the comment. + */ +export function endsWithLineComment(nodes: AstNode[]): boolean { + return isLineComment(nodes[nodes.length - 1]); +} + /** * The keyword a node opens its line with, or `null` for a node that is part of * an expression. Clauses, set operations and limit clauses each start a line. @@ -425,7 +488,7 @@ function renderLogicalJoin(ctx: SqlLayoutContext, nodes: AstNode[], width: numbe * column the block starts at. `text` is rendered relative to column 0, so its * own leading whitespace carries the nesting levels and only needs shifting. */ -export function emitText(writer: Writer, text: string, baseColumn: number): void { +export function emitText(writer: Writer, text: string, baseColumn: number, endsWithComment = false): void { const lines = text.split("\n"); writer.write(lines[0].trimEnd()); for (let index = 1; index < lines.length; index++) { @@ -437,4 +500,5 @@ export function emitText(writer: Writer, text: string, baseColumn: number): void writer.newline(baseColumn + line.length - line.trimStart().length); writer.write(line.trimStart()); } + if (endsWithComment) writer.markLineComment(); } diff --git a/apps/desktop/src/lib/sql/layout/printer.ts b/apps/desktop/src/lib/sql/layout/printer.ts index 4bcbea6970..2166ca62be 100644 --- a/apps/desktop/src/lib/sql/layout/printer.ts +++ b/apps/desktop/src/lib/sql/layout/printer.ts @@ -1,6 +1,6 @@ import type { AstNode, ClauseNode, LimitClauseNode, ParenthesisNode, StatementNode } from "sql-formatter/dist/esm/parser/ast.js"; import { createTableLayout, isCreateTable } from "./ddl"; -import { clauseChildContext, collapsedContext, emitText, isBodyNode, isCallParen, isJoinKeyword, isLogicalOperator, isParenthesis, keywordText, limitChildren, renderInline, splitByComma, splitLogicalOperands, Writer, type SqlLayoutContext } from "./primitives"; +import { clauseChildContext, collapsedContext, emitText, endsWithLineComment, isBodyNode, isCallParen, isJoinKeyword, isLineComment, isLogicalOperator, isParenthesis, keywordText, limitChildren, renderInline, splitByComma, splitLogicalOperands, Writer, type SqlLayoutContext } from "./primitives"; export type { SqlLayoutContext, SqlLayoutOptions } from "./primitives"; @@ -74,7 +74,7 @@ function printLimitClause(writer: Writer, clause: LimitClauseNode, ctx: SqlLayou } writer.write(keyword); writer.newline(writer.column + ctx.options.indentWidth); - emitText(writer, ctx.renderers.block(children).trim(), writer.column); + emitText(writer, ctx.renderers.block(children).trim(), writer.column, endsWithLineComment(children)); } /** Index just past the nodes that share a line with the group at `from`. */ @@ -200,7 +200,7 @@ function printElement(writer: Writer, nodes: AstNode[], baseColumn: number, ctx: } else { // A construct too wide to inline (a long CASE, a nested function call): // fall back to sql-formatter's own multi-line rendering of it. - emitText(writer, ctx.renderers.block(run).trim(), writer.column); + emitText(writer, ctx.renderers.block(run).trim(), writer.column, endsWithLineComment(run)); } index = end; } @@ -217,7 +217,7 @@ function printBlock(writer: Writer, nodes: AstNode[], baseColumn: number, ctx: S if (!nodes.some(isBodyNode)) { // A plain expression group (not a subquery) that did not fit: let // sql-formatter break it, then re-base the block on this column. - emitText(writer, ctx.renderers.block(nodes).trim(), baseColumn); + emitText(writer, ctx.renderers.block(nodes).trim(), baseColumn, endsWithLineComment(nodes)); return; } @@ -234,7 +234,7 @@ function writeBodyNode(writer: Writer, node: AstNode, baseColumn: number, ctx: S if (node.type === "clause") printClause(writer, node, baseColumn, ctx); else if (node.type === "set_operation") writer.write(keywordText(node.nameKw.text, ctx)); else if (node.type === "limit_clause") printLimitClause(writer, node, ctx); - else emitText(writer, ctx.renderers.block([node]).trim(), writer.column); + else emitText(writer, ctx.renderers.block([node]).trim(), writer.column, isLineComment(node)); } /** Emits a whole statement, one clause per line unless the statement collapses. */ diff --git a/packages/app-tests/sqlFormatter.test.ts b/packages/app-tests/sqlFormatter.test.ts index f93beab288..a4ce016ff0 100644 --- a/packages/app-tests/sqlFormatter.test.ts +++ b/packages/app-tests/sqlFormatter.test.ts @@ -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");