Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde-wasm-bindgen = "0.6"
console_error_panic_hook = "0.1"
js-sys = "0.3"

[profile.release]
opt-level = "s"
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

// Parser
export { Parser, init, parse, validate, format } from './parser.js';
export { Parser, init, parse, parseWithComments, validate, format } from './parser.js';
export type { ParserOptions, DialectInput } from './parser.js';

// Dialects
Expand Down
68 changes: 66 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sqlparser::dialect::{
GenericDialect, HiveDialect, MsSqlDialect, MySqlDialect, OracleDialect, PostgreSqlDialect,
RedshiftSqlDialect, SQLiteDialect, SnowflakeDialect,
};
use sqlparser::ast::comments::{Comment as SqlComment, CommentWithSpan};
use sqlparser::parser::Parser;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -82,7 +83,7 @@ pub fn parse_sql_with_options(
// Note: trailing_commas option support depends on sqlparser version

let tokens = sqlparser::tokenizer::Tokenizer::new(dialect_impl.as_ref(), sql)
.tokenize()
.tokenize_with_location()
.map_err(|e| {
let error = ParseError {
message: e.to_string(),
Expand All @@ -92,7 +93,7 @@ pub fn parse_sql_with_options(
serde_wasm_bindgen::to_value(&error).unwrap_or(JsValue::from_str(&e.to_string()))
})?;

parser = parser.with_tokens(tokens);
parser = parser.with_tokens_with_locations(tokens);

let statements = parser.parse_statements().map_err(|e| {
let error = ParseError {
Expand Down Expand Up @@ -163,6 +164,69 @@ pub fn get_supported_dialects() -> JsValue {
serde_wasm_bindgen::to_value(&dialects).unwrap()
}

/// A serializable source comment
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SerializedComment {
pub comment_type: String,
pub content: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub prefix: Option<String>,
pub start_line: u64,
pub start_column: u64,
pub end_line: u64,
pub end_column: u64,
}

impl From<&CommentWithSpan> for SerializedComment {
fn from(c: &CommentWithSpan) -> Self {
let (comment_type, content, prefix) = match &c.comment {
SqlComment::SingleLine { content, prefix } => {
("singleLine".to_string(), content.clone(), Some(prefix.clone()))
}
SqlComment::MultiLine(content) => {
("multiLine".to_string(), content.clone(), None)
}
};
SerializedComment {
comment_type, content, prefix,
start_line: c.span.start.line,
start_column: c.span.start.column,
end_line: c.span.end.line,
end_column: c.span.end.column,
}
}
}

/// Parse SQL and return both AST and source comments
#[wasm_bindgen]
pub fn parse_sql_with_comments(dialect: &str, sql: &str) -> Result<JsValue, JsValue> {
let dialect_impl = get_dialect(dialect);
let (statements, comments) =
Parser::parse_sql_with_comments(dialect_impl.as_ref(), sql).map_err(|e| {
let error = ParseError {
message: e.to_string(),
line: None,
column: None,
};
serde_wasm_bindgen::to_value(&error).unwrap_or(JsValue::from_str(&e.to_string()))
})?;

let comments_vec: Vec<CommentWithSpan> = comments.into();
let serialized_comments: Vec<SerializedComment> =
comments_vec.iter().map(SerializedComment::from).collect();

// Build JS object manually to avoid double-serialization
let obj = js_sys::Object::new();
let stmts_val = serde_wasm_bindgen::to_value(&statements)
.map_err(|e| JsValue::from_str(&format!("Serialization error: {}", e)))?;
let comments_val = serde_wasm_bindgen::to_value(&serialized_comments)
.map_err(|e| JsValue::from_str(&format!("Serialization error: {}", e)))?;
js_sys::Reflect::set(&obj, &"statements".into(), &stmts_val).unwrap();
js_sys::Reflect::set(&obj, &"comments".into(), &comments_val).unwrap();
Comment on lines +225 to +226
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

js_sys::Reflect::set(...).unwrap() can panic in WASM, aborting the module on what should be a recoverable error path. Prefer propagating the JsValue error (e.g., map Reflect::set errors into the Result) instead of unwrapping.

Suggested change
js_sys::Reflect::set(&obj, &"statements".into(), &stmts_val).unwrap();
js_sys::Reflect::set(&obj, &"comments".into(), &comments_val).unwrap();
js_sys::Reflect::set(&obj, &"statements".into(), &stmts_val).map_err(|e| e)?;
js_sys::Reflect::set(&obj, &"comments".into(), &comments_val).map_err(|e| e)?;

Copilot uses AI. Check for mistakes.
Ok(obj.into())
}

/// Validate SQL syntax without returning the full AST
#[wasm_bindgen]
pub fn validate_sql(dialect: &str, sql: &str) -> Result<bool, JsValue> {
Expand Down
23 changes: 23 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Dialect, DialectName } from './dialects.js';
import { dialectFromString, GenericDialect } from './dialects.js';
import { ParserError } from './types/errors.js';
import type { Statement } from './types/ast.js';
import type { SourceComment, ParseWithCommentsResult } from './types/comments.js';
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SourceComment is imported but never used. With the repo’s unused-imports/no-unused-imports rule enabled, this will fail linting. Remove SourceComment from the import or use it in a type position in this file.

Suggested change
import type { SourceComment, ParseWithCommentsResult } from './types/comments.js';
import type { ParseWithCommentsResult } from './types/comments.js';

Copilot uses AI. Check for mistakes.
import { getWasmModule } from './wasm.js';

export { init } from './wasm.js';
Expand Down Expand Up @@ -80,13 +81,28 @@ export class Parser {
}
}

/** Parse SQL and return both AST and source comments */
parseWithComments(sql: string): ParseWithCommentsResult<Statement> {
const wasm = getWasmModule();
try {
return wasm.parse_sql_with_comments(this.dialect.name, sql) as ParseWithCommentsResult<Statement>;
} catch (error) {
throw ParserError.fromWasmError(error);
}
}
Comment on lines +84 to +92
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseWithComments() ignores this.options (e.g., recursionLimit/trailingCommas) even when the caller configured them via withOptions() / withRecursionLimit(), unlike parse(). This is a behavioral inconsistency that can surprise users. Either plumb options through to the WASM API (e.g., add a parse_sql_with_comments_with_options entrypoint) or make this a static-only helper / explicitly reject or document non-empty options.

Copilot uses AI. Check for mistakes.

// Static methods

/** Parse SQL into AST */
static parse(sql: string, dialect: DialectInput = 'generic'): Statement[] {
return new Parser(resolveDialect(dialect)).parse(sql);
}

/** Parse SQL and return both AST and source comments */
static parseWithComments(sql: string, dialect: DialectInput = 'generic'): ParseWithCommentsResult<Statement> {
return new Parser(resolveDialect(dialect)).parseWithComments(sql);
}

/** Parse SQL and return AST as JSON string */
static parseToJson(sql: string, dialect: DialectInput = 'generic'): string {
const wasm = getWasmModule();
Expand Down Expand Up @@ -145,6 +161,13 @@ export function parse(sql: string, dialect: DialectInput = 'generic'): Statement
return Parser.parse(sql, dialect);
}

/**
* Parse SQL and return both AST and source comments
*/
export function parseWithComments(sql: string, dialect: DialectInput = 'generic'): ParseWithCommentsResult<Statement> {
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New public API (parseWithComments) is introduced without corresponding tests. There is existing Vitest coverage for parse/format/validate; add tests that exercise comment extraction and span fields to prevent regressions (including build-export checks if applicable).

Suggested change
export function parseWithComments(sql: string, dialect: DialectInput = 'generic'): ParseWithCommentsResult<Statement> {
function parseWithComments(sql: string, dialect: DialectInput = 'generic'): ParseWithCommentsResult<Statement> {

Copilot uses AI. Check for mistakes.
return Parser.parseWithComments(sql, dialect);
}

/**
* Validate SQL syntax
* @throws ParserError if SQL is invalid
Expand Down
23 changes: 23 additions & 0 deletions src/types/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** A source code comment extracted from parsed SQL */
export interface SourceComment {
/** "singleLine" for -- comments, "multiLine" for block comments */
commentType: 'singleLine' | 'multiLine'
/** The comment text content (excluding markers) */
content: string
/** For single-line comments, the prefix (e.g. "--", "#") */
prefix?: string
/** Start line (1-based) */
startLine: number
/** Start column (1-based) */
startColumn: number
/** End line (1-based) */
endLine: number
/** End column (1-based) */
endColumn: number
}

/** Result of parsing SQL with comments */
export interface ParseWithCommentsResult<T> {
statements: T[]
comments: SourceComment[]
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './ast.js';
export * from './errors.js';
export * from './comments.js';
1 change: 1 addition & 0 deletions src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { WasmInitError } from './types/errors.js';
export interface WasmModule {
parse_sql: (dialect: string, sql: string) => unknown;
parse_sql_with_options: (dialect: string, sql: string, options: unknown) => unknown;
parse_sql_with_comments: (dialect: string, sql: string) => unknown;
parse_sql_to_json_string: (dialect: string, sql: string) => string;
parse_sql_to_string: (dialect: string, sql: string) => string;
format_sql: (dialect: string, sql: string) => string;
Expand Down
Loading