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

+10
View File
@@ -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
+4 -1
View File
@@ -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();
}
+65 -1
View File
@@ -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<SqlLayoutOptions, "useTabs" | "indentWidth">) {}
@@ -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();
}
+5 -5
View File
@@ -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. */