Skip to content
Merged
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
45 changes: 43 additions & 2 deletions wasm/parser/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![expect(clippy::needless_pass_by_value)]

use oxc::{allocator::Allocator, parser::Parser, span::SourceType};
use oxc::{allocator::Allocator, ast::CommentKind, parser::Parser, span::SourceType};
use serde::{Deserialize, Serialize};
use tsify::Tsify;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -30,6 +30,10 @@ pub struct ParseResult {
#[tsify(type = "Program")]
pub program: JsValue,

#[wasm_bindgen(readonly, skip_typescript)]
#[tsify(type = "Comment[]")]
pub comments: Vec<JsValue>,

#[wasm_bindgen(readonly, skip_typescript)]
#[tsify(type = "Diagnostic[]")]
pub errors: Vec<JsValue>,
Expand All @@ -43,6 +47,21 @@ pub struct Diagnostic {
pub message: String,
}

#[derive(Clone, Tsify, Serialize)]
pub struct Comment {
pub r#type: CommentType,
pub value: String,
pub start: u32,
pub end: u32,
}

#[derive(Clone, Copy, Tsify, Serialize)]
#[tsify(into_wasm_abi)]
pub enum CommentType {
Line,
Block,
}

/// # Errors
///
/// * wasm bindgen serialization failed
Expand Down Expand Up @@ -78,6 +97,28 @@ pub fn parse_sync(

let program = ret.program.serialize(&serializer)?;

let comments: Vec<JsValue> = if ret.program.comments.is_empty() {
vec![]
} else {
ret.program
.comments
.iter()
.map(|comment| {
Comment {
r#type: match comment.kind {
CommentKind::Line => CommentType::Line,
CommentKind::Block => CommentType::Block,
},
value: comment.content_span().source_text(&source_text).to_string(),
start: comment.span.start,
end: comment.span.end,
}
.serialize(&serializer)
.unwrap()
})
.collect::<Vec<JsValue>>()
};

let errors = if ret.errors.is_empty() {
vec![]
} else {
Expand All @@ -102,5 +143,5 @@ pub fn parse_sync(
.collect::<Vec<JsValue>>()
};

Ok(ParseResult { program, errors })
Ok(ParseResult { program, comments, errors })
}
Loading