Skip to content

Commit 36a3f43

Browse files
leaysgurCopilot
authored andcommitted
refactor(oxfmt): Rename FormatFileSource > FormatFileStrategy (#16635)
Addressed review in #16593 - Add `OxfmtOptions` struct - Rename `FormatFileSource` to `FormatFileStrategy` - And include `package.json` as `ExternalFormatterPackageJson` variant
1 parent 427d1d3 commit 36a3f43

File tree

5 files changed

+108
-74
lines changed

5 files changed

+108
-74
lines changed

apps/oxfmt/src/cli/service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rayon::prelude::*;
66
use oxc_diagnostics::{DiagnosticSender, DiagnosticService};
77

88
use super::command::OutputOptions;
9-
use crate::core::{FormatFileSource, FormatResult, SourceFormatter, utils};
9+
use crate::core::{FormatFileStrategy, FormatResult, SourceFormatter, utils};
1010

1111
pub enum SuccessResult {
1212
Changed(String),
@@ -30,7 +30,7 @@ impl FormatService {
3030
/// Process entries as they are received from the channel
3131
pub fn run_streaming(
3232
&self,
33-
rx_entry: mpsc::Receiver<FormatFileSource>,
33+
rx_entry: mpsc::Receiver<FormatFileStrategy>,
3434
tx_error: &DiagnosticSender,
3535
tx_success: &mpsc::Sender<SuccessResult>,
3636
) {

apps/oxfmt/src/cli/walk.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55

66
use ignore::gitignore::GitignoreBuilder;
77

8-
use crate::core::FormatFileSource;
8+
use crate::core::FormatFileStrategy;
99

1010
pub struct Walk {
1111
inner: ignore::WalkParallel,
@@ -166,8 +166,8 @@ impl Walk {
166166
}
167167

168168
/// Stream entries through a channel as they are discovered
169-
pub fn stream_entries(self) -> mpsc::Receiver<FormatFileSource> {
170-
let (sender, receiver) = mpsc::channel::<FormatFileSource>();
169+
pub fn stream_entries(self) -> mpsc::Receiver<FormatFileStrategy> {
170+
let (sender, receiver) = mpsc::channel::<FormatFileStrategy>();
171171

172172
// Spawn the walk operation in a separate thread
173173
rayon::spawn(move || {
@@ -202,7 +202,7 @@ fn load_ignore_paths(cwd: &Path, ignore_paths: &[PathBuf]) -> Vec<PathBuf> {
202202
// ---
203203

204204
struct WalkBuilder {
205-
sender: mpsc::Sender<FormatFileSource>,
205+
sender: mpsc::Sender<FormatFileStrategy>,
206206
}
207207

208208
impl<'s> ignore::ParallelVisitorBuilder<'s> for WalkBuilder {
@@ -212,7 +212,7 @@ impl<'s> ignore::ParallelVisitorBuilder<'s> for WalkBuilder {
212212
}
213213

214214
struct WalkVisitor {
215-
sender: mpsc::Sender<FormatFileSource>,
215+
sender: mpsc::Sender<FormatFileStrategy>,
216216
}
217217

218218
impl ignore::ParallelVisitor for WalkVisitor {
@@ -231,13 +231,13 @@ impl ignore::ParallelVisitor for WalkVisitor {
231231
// Tier 2 = `.html`, `.json`, etc: Other files supported by Prettier
232232
// (Tier 3 = `.astro`, `.svelte`, etc: Other files supported by Prettier plugins)
233233
// Tier 4 = everything else: Not handled
234-
let Ok(format_file_source) = FormatFileSource::try_from(entry.into_path())
234+
let Ok(format_file_source) = FormatFileStrategy::try_from(entry.into_path())
235235
else {
236236
return ignore::WalkState::Continue;
237237
};
238238

239239
#[cfg(not(feature = "napi"))]
240-
if matches!(format_file_source, FormatFileSource::ExternalFormatter { .. }) {
240+
if !matches!(format_file_source, FormatFileStrategy::OxcFormatter { .. }) {
241241
return ignore::WalkState::Continue;
242242
}
243243

apps/oxfmt/src/core/format.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use oxc_formatter::{FormatOptions, Formatter, enable_jsx_source_type, get_parse_
88
use oxc_parser::Parser;
99
use oxc_span::SourceType;
1010

11-
use super::FormatFileSource;
11+
use super::FormatFileStrategy;
1212

1313
pub enum FormatResult {
1414
Success { is_changed: bool, code: String },
@@ -49,32 +49,22 @@ impl SourceFormatter {
4949
}
5050

5151
/// Format a file based on its source type.
52-
pub fn format(&self, entry: &FormatFileSource, source_text: &str) -> FormatResult {
52+
pub fn format(&self, entry: &FormatFileStrategy, source_text: &str) -> FormatResult {
5353
let result = match entry {
54-
FormatFileSource::OxcFormatter { path, source_type } => {
54+
FormatFileStrategy::OxcFormatter { path, source_type } => {
5555
self.format_by_oxc_formatter(source_text, path, *source_type)
5656
}
5757
#[cfg(feature = "napi")]
58-
FormatFileSource::ExternalFormatter { path, parser_name } => {
59-
let text_to_format: Cow<'_, str> =
60-
if self.is_sort_package_json && entry.is_package_json() {
61-
match sort_package_json::sort_package_json(source_text) {
62-
Ok(sorted) => Cow::Owned(sorted),
63-
Err(err) => {
64-
return FormatResult::Error(vec![OxcDiagnostic::error(format!(
65-
"Failed to sort package.json: {}\n{err}",
66-
path.display()
67-
))]);
68-
}
69-
}
70-
} else {
71-
Cow::Borrowed(source_text)
72-
};
73-
74-
self.format_by_external_formatter(&text_to_format, path, parser_name)
58+
FormatFileStrategy::ExternalFormatter { path, parser_name } => {
59+
self.format_by_external_formatter(source_text, path, parser_name)
60+
}
61+
#[cfg(feature = "napi")]
62+
FormatFileStrategy::ExternalFormatterPackageJson { path, parser_name } => {
63+
self.format_by_external_formatter_package_json(source_text, path, parser_name)
7564
}
7665
#[cfg(not(feature = "napi"))]
77-
FormatFileSource::ExternalFormatter { .. } => {
66+
FormatFileStrategy::ExternalFormatter { .. }
67+
| FormatFileStrategy::ExternalFormatterPackageJson { .. } => {
7868
unreachable!("If `napi` feature is disabled, this should not be passed here")
7969
}
8070
};
@@ -170,4 +160,26 @@ impl SourceFormatter {
170160
))
171161
})
172162
}
163+
164+
/// Format `package.json`: optionally sort then format by external formatter.
165+
#[cfg(feature = "napi")]
166+
fn format_by_external_formatter_package_json(
167+
&self,
168+
source_text: &str,
169+
path: &Path,
170+
parser_name: &str,
171+
) -> Result<String, OxcDiagnostic> {
172+
let source_text: Cow<'_, str> = if self.is_sort_package_json {
173+
Cow::Owned(sort_package_json::sort_package_json(source_text).map_err(|err| {
174+
OxcDiagnostic::error(format!(
175+
"Failed to sort package.json: {}\n{err}",
176+
path.display()
177+
))
178+
})?)
179+
} else {
180+
Cow::Borrowed(source_text)
181+
};
182+
183+
self.format_by_external_formatter(&source_text, path, parser_name)
184+
}
173185
}

apps/oxfmt/src/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub mod utils;
66
mod external_formatter;
77

88
pub use format::{FormatResult, SourceFormatter};
9-
pub use support::FormatFileSource;
9+
pub use support::FormatFileStrategy;
1010

1111
#[cfg(feature = "napi")]
1212
pub use external_formatter::{

0 commit comments

Comments
 (0)