fix(editor): ignore formatter parse errors for incomplete SQL
Closes #5462
This commit is contained in:
3 files changed
+31
-3
No files matched your search
@@ -106,6 +106,21 @@ export async function formatSqlText(sql: string, dialect: SqlFormatDialect = "ge
|
||||
}
|
||||
}
|
||||
|
||||
function isSqlFormatterParseError(error: unknown): boolean {
|
||||
if (!(error instanceof Error) || !error.message.startsWith("Parse error at token:")) return false;
|
||||
const candidate = error as Error & { offset?: unknown; token?: unknown };
|
||||
return typeof candidate.offset === "number" && candidate.token !== undefined;
|
||||
}
|
||||
|
||||
export async function formatSqlForEditing(sql: string, dialect: SqlFormatDialect = "generic", settings: Partial<SqlFormatterSettings> = DEFAULT_SQL_FORMATTER_SETTINGS): Promise<string> {
|
||||
try {
|
||||
return await formatSqlText(sql, dialect, settings);
|
||||
} catch (error) {
|
||||
if (isSqlFormatterParseError(error)) return sql;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 压缩 SQL 时使用的方言。不同方言对引号、注释、转义的处理不同:
|
||||
* - `mysql`:保留 MySQL 可执行注释与 optimizer hint;单引号字符串支持反斜杠转义
|
||||
|
||||
Reference in new issue
Block a user