Skip to content

Commit

Permalink
refactor(napi/parser): clean up code (#7596)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 3, 2024
1 parent 943462f commit 16adaf9
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions napi/parser/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#![allow(
clippy::needless_pass_by_value // Napi value need to be passed as value
)]

use std::sync::Arc;

use napi::{bindgen_prelude::AsyncTask, Task};
Expand Down Expand Up @@ -36,22 +40,16 @@ fn parse<'a>(
}

/// Parse without returning anything.
/// This is for benchmark purposes such as measuring napi communication overhead.
///
/// # Panics
///
/// * File extension is invalid
/// * Serde JSON serialization
#[allow(clippy::needless_pass_by_value)]
/// This is for benchmark purposes such as measuring napi communication overhead.
#[napi]
pub fn parse_without_return(source_text: String, options: Option<ParserOptions>) {
let options = options.unwrap_or_default();
let allocator = Allocator::default();
parse(&allocator, &source_text, &options);
}

#[allow(clippy::needless_lifetimes)]
fn parse_with_return<'a>(source_text: &'a str, options: &ParserOptions) -> ParseResult {
fn parse_with_return(source_text: &str, options: &ParserOptions) -> ParseResult {
let allocator = Allocator::default();
let ret = parse(&allocator, source_text, options);
let program = serde_json::to_string(&ret.program).unwrap();
Expand Down Expand Up @@ -86,11 +84,7 @@ fn parse_with_return<'a>(source_text: &'a str, options: &ParserOptions) -> Parse
ParseResult { program, comments, errors }
}

/// # Panics
///
/// * File extension is invalid
/// * Serde JSON serialization
#[allow(clippy::needless_pass_by_value)]
/// Parse synchronously.
#[napi]
pub fn parse_sync(source_text: String, options: Option<ParserOptions>) -> ParseResult {
let options = options.unwrap_or_default();
Expand All @@ -116,10 +110,9 @@ impl Task for ResolveTask {
}
}

/// # Panics
/// Parse asynchronously.
///
/// * Tokio crashes
#[allow(clippy::needless_pass_by_value)]
/// Note: This function can be slower than `parseSync` due to the overhead of spawning a thread.
#[napi]
pub fn parse_async(source_text: String, options: Option<ParserOptions>) -> AsyncTask<ResolveTask> {
let options = options.unwrap_or_default();
Expand Down

0 comments on commit 16adaf9

Please sign in to comment.