diff --git a/apps/oxfmt/src/core/config.rs b/apps/oxfmt/src/core/config.rs index a23cbbc19e282..9d2ab8be160f4 100644 --- a/apps/oxfmt/src/core/config.rs +++ b/apps/oxfmt/src/core/config.rs @@ -194,7 +194,10 @@ impl ConfigResolver { .expect("`build_and_validate()` must be called before `resolve()`") }; + #[cfg(feature = "napi")] let OxfmtOptions { sort_package_json, insert_final_newline, .. } = oxfmt_options; + #[cfg(not(feature = "napi"))] + let OxfmtOptions { insert_final_newline, .. } = oxfmt_options; match strategy { FormatFileStrategy::OxcFormatter { .. } => ResolvedOptions::OxcFormatter { diff --git a/apps/oxfmt/src/main_napi.rs b/apps/oxfmt/src/main_napi.rs index 834c4810eba18..b2a4203f372fd 100644 --- a/apps/oxfmt/src/main_napi.rs +++ b/apps/oxfmt/src/main_napi.rs @@ -94,9 +94,7 @@ pub async fn run_cli( Mode::Stdin(_) => { init_miette(); - // TODO: `.with_external_formatter()` is not needed, just pass with `new(command, external_formatter)` - let result = - StdinRunner::new(command).with_external_formatter(Some(external_formatter)).run(); + let result = StdinRunner::new(command, external_formatter).run(); ("stdin".to_string(), Some(result.exit_code())) } diff --git a/apps/oxfmt/src/stdin/mod.rs b/apps/oxfmt/src/stdin/mod.rs index 9d90338028957..83a2f0f476815 100644 --- a/apps/oxfmt/src/stdin/mod.rs +++ b/apps/oxfmt/src/stdin/mod.rs @@ -14,7 +14,7 @@ use crate::core::{ pub struct StdinRunner { options: FormatCommand, cwd: PathBuf, - external_formatter: Option, + external_formatter: ExternalFormatter, } impl StdinRunner { @@ -22,23 +22,14 @@ impl StdinRunner { /// /// # Panics /// Panics if the current working directory cannot be determined. - pub fn new(options: FormatCommand) -> Self { + pub fn new(options: FormatCommand, external_formatter: ExternalFormatter) -> Self { Self { options, cwd: env::current_dir().expect("Failed to get current working directory"), - external_formatter: None, + external_formatter, } } - #[must_use] - pub fn with_external_formatter( - mut self, - external_formatter: Option, - ) -> Self { - self.external_formatter = external_formatter; - self - } - pub fn run(self) -> CliRunResult { let stdout = &mut BufWriter::new(io::stdout()); let stderr = &mut BufWriter::new(io::stderr()); @@ -49,9 +40,6 @@ impl StdinRunner { let Mode::Stdin(filepath) = mode else { unreachable!("`StdinRunner::run()` called with non-Stdin mode"); }; - let Some(external_formatter) = self.external_formatter else { - unreachable!("`StdinRunner::run()` called without `external_formatter`"); - }; // Single threaded for stdin formatting let num_of_threads = 1; @@ -88,7 +76,7 @@ impl StdinRunner { } // Use `block_in_place()` to avoid nested async runtime access - match tokio::task::block_in_place(|| external_formatter.init(num_of_threads)) { + match tokio::task::block_in_place(|| self.external_formatter.init(num_of_threads)) { // TODO: Plugins support Ok(_) => {} Err(err) => { @@ -110,8 +98,8 @@ impl StdinRunner { let resolved_options = config_resolver.resolve(&strategy); // Create formatter and format - let source_formatter = - SourceFormatter::new(num_of_threads).with_external_formatter(Some(external_formatter)); + let source_formatter = SourceFormatter::new(num_of_threads) + .with_external_formatter(Some(self.external_formatter)); // Use `block_in_place()` to avoid nested async runtime access match tokio::task::block_in_place(|| {