From 83488d919e2a2312dee1d27a19805e6c4a4fd192 Mon Sep 17 00:00:00 2001 From: Shahmir Varqha Date: Sun, 10 Aug 2025 22:39:38 +0800 Subject: [PATCH 1/3] add types for dialect --- demo/index.ts | 12 +++++++----- src/index.ts | 2 +- src/sql/parser.ts | 28 ++++++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/demo/index.ts b/demo/index.ts index 6f3221a..96be798 100644 --- a/demo/index.ts +++ b/demo/index.ts @@ -8,6 +8,7 @@ import { DefaultSqlTooltipRenders, defaultSqlHoverTheme, NodeSqlParser, + type SupportedDialects, sqlExtension, } from "../src/index.js"; import { type Schema, tableTooltipRenderer } from "./custom-renderers.js"; @@ -146,9 +147,9 @@ const getKeywordDocs = async () => { }; }; -const setDatabase = StateEffect.define(); +const setDatabase = StateEffect.define(); -const databaseField = StateField.define({ +const databaseField = StateField.define({ create: () => "PostgreSQL", update: (prevValue, transaction) => { for (const effect of transaction.effects) { @@ -162,7 +163,7 @@ const databaseField = StateField.define({ // Initialize the SQL editor function initializeEditor() { - // Use the same parser for linter and gutter + // Use the same parser const parser = new NodeSqlParser({ getParserOptions: (state: EditorState) => { return { @@ -233,6 +234,7 @@ function initializeEditor() { table: tableTooltipRenderer, }, theme: defaultSqlHoverTheme("light"), + parser, }, }), dialect.language.data.of({ @@ -337,7 +339,7 @@ function setupExampleButtons() { }); } -function getDialect(state: EditorState): string { +function getDialect(state: EditorState): SupportedDialects { return state.field(databaseField); } @@ -345,7 +347,7 @@ function setupDialectSelect() { const select = document.querySelector("#dialect-select"); if (select) { select.addEventListener("change", (e) => { - const value = (e.target as HTMLSelectElement).value; + const value = (e.target as HTMLSelectElement).value as SupportedDialects; editor.dispatch({ effects: [setDatabase.of(value)], }); diff --git a/src/index.ts b/src/index.ts index d5f81d5..573253e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ export type { SqlKeywordInfo, } from "./sql/hover.js"; export { DefaultSqlTooltipRenders, defaultSqlHoverTheme, sqlHover } from "./sql/hover.js"; -export { NodeSqlParser } from "./sql/parser.js"; +export { NodeSqlParser, type SupportedDialects } from "./sql/parser.js"; export type { SqlStatement } from "./sql/structure-analyzer.js"; export { SqlStructureAnalyzer } from "./sql/structure-analyzer.js"; export type { SqlGutterConfig } from "./sql/structure-extension.js"; diff --git a/src/sql/parser.ts b/src/sql/parser.ts index 9a67fcd..0803a82 100644 --- a/src/sql/parser.ts +++ b/src/sql/parser.ts @@ -4,8 +4,12 @@ import { debug } from "../debug.js"; import { lazy } from "../utils.js"; import type { SqlParseError, SqlParseResult, SqlParser } from "./types.js"; +interface ParserOption extends Option { + database: SupportedDialects; +} + interface NodeSqlParserOptions { - getParserOptions?: (state: EditorState) => Option; + getParserOptions?: (state: EditorState) => ParserOption; } interface NodeSqlParserResult extends SqlParseResult { @@ -97,7 +101,7 @@ export class NodeSqlParser implements SqlParser { // Otherwise, try standard parsing with PostgreSQL dialect try { - const postgresOptions = { ...parserOptions, database: "PostgresQL" }; + const postgresOptions = { ...parserOptions, database: "PostgreSQL" }; const ast = parser.astify(sql, postgresOptions); return { success: true, @@ -202,3 +206,23 @@ export class NodeSqlParser implements SqlParser { } } } + +/** + * https://github.com/taozhi8833998/node-sql-parser?tab=readme-ov-file#supported-database-sql-syntax + * While DuckDB is not supported in the library, we perform some special handling for it and treat it as PostgreSQL. + */ +export type SupportedDialects = + | "Athena" + | "BigQuery" + | "DB2" + | "Hive" + | "MariaDB" + | "MySQL" + | "PostgreSQL" + | "DuckDB" + | "Redshift" + | "Sqlite" + | "TransactSQL" + | "FlinkSQL" + | "Snowflake" + | "Noql"; From 3e69ca43600421682ea858e66004c8e13ad5ce11 Mon Sep 17 00:00:00 2001 From: Shahmir Varqha Date: Mon, 11 Aug 2025 01:46:53 +0800 Subject: [PATCH 2/3] export dialect in index.ts --- package.json | 6 ++++++ src/dialects/index.ts | 1 + 2 files changed, 7 insertions(+) create mode 100644 src/dialects/index.ts diff --git a/package.json b/package.json index 5e88020..1158426 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,12 @@ "default": "./dist/index.js" } }, + "./dialects": { + "import": { + "types": "./dist/dialects/index.d.ts", + "default": "./dist/dialects/index.js" + } + }, "./data/common-keywords.json": "./src/data/common-keywords.json", "./data/duckdb-keywords.json": "./src/data/duckdb-keywords.json" }, diff --git a/src/dialects/index.ts b/src/dialects/index.ts new file mode 100644 index 0000000..c45977f --- /dev/null +++ b/src/dialects/index.ts @@ -0,0 +1 @@ +export { DuckDBDialect } from "./duckdb/duckdb.js"; From c4b1ca32c7187e7a3d299cee50994a84afd21807 Mon Sep 17 00:00:00 2001 From: Shahmir Varqha Date: Mon, 11 Aug 2025 01:48:34 +0800 Subject: [PATCH 3/3] add test --- src/__tests__/index.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 793aced..8f5a058 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -1,6 +1,7 @@ import { readdir } from "node:fs/promises"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; +import * as dialects from "../dialects"; import * as exports from "../index"; describe("index.ts exports", () => { @@ -34,3 +35,14 @@ describe("keywords", async () => { } }); }); + +describe("dialects.ts exports", () => { + it("should not change unexpectedly", () => { + const sortedExports = Object.keys(dialects).sort(); + expect(sortedExports).toMatchInlineSnapshot(` + [ + "DuckDBDialect", + ] + `); + }); +});