diff --git a/napi/parser/index.d.ts b/napi/parser/index.d.ts index 73ef36e5c29c6..8a9cad6e3210d 100644 --- a/napi/parser/index.d.ts +++ b/napi/parser/index.d.ts @@ -95,15 +95,9 @@ export interface ParserOptions { } export interface ParseResult { program: string - comments: Array + comments: string errors: Array } -export interface Comment { - type: string - value: 'Line' | 'Block' - start: number - end: number -} /** * Parse without returning anything. * This is for benchmark purposes such as measuring napi communication overhead. diff --git a/napi/parser/parse.bench.mjs b/napi/parser/parse.bench.mjs index f7a546285a572..1e200b9e5cb61 100644 --- a/napi/parser/parse.bench.mjs +++ b/napi/parser/parse.bench.mjs @@ -44,5 +44,6 @@ for (const {filename, code} of files) { const res = parseSync(code, {sourceFilename: filename}); assert(res.errors.length === 0); JSON.parse(res.program); + JSON.parse(res.comments); }); } diff --git a/napi/parser/src/lib.rs b/napi/parser/src/lib.rs index 3086b0cc8bda7..9bafd7990798f 100644 --- a/napi/parser/src/lib.rs +++ b/napi/parser/src/lib.rs @@ -40,15 +40,14 @@ pub struct ParserOptions { #[napi(object)] pub struct ParseResult { pub program: String, - pub comments: Vec, + pub comments: String, pub errors: Vec, } -#[napi(object)] -pub struct Comment { +#[derive(Serialize)] +pub struct Comment<'a> { pub r#type: &'static str, - #[napi(ts_type = "'Line' | 'Block'")] - pub value: String, + pub value: &'a str, pub start: u32, pub end: u32, } @@ -121,11 +120,12 @@ pub fn parse_sync(source_text: String, options: Option) -> ParseR CommentKind::SingleLine => "Line", CommentKind::MultiLine => "Block", }, - value: span.source_text(&source_text).to_string(), + value: span.source_text(&source_text), start: span.start, end: span.end, }) .collect::>(); + let comments = serde_json::to_string(&comments).unwrap(); ParseResult { program, comments, errors } }