diff --git a/crates/oxc_formatter/Cargo.toml b/crates/oxc_formatter/Cargo.toml index 441ba5895b3c1..157f0380fcac7 100644 --- a/crates/oxc_formatter/Cargo.toml +++ b/crates/oxc_formatter/Cargo.toml @@ -37,7 +37,7 @@ unicode-width = { workspace = true } [dev-dependencies] insta = { workspace = true } -oxc_tasks_common = { path = "../../tasks/common" } +oxc_tasks_common = { workspace = true } pico-args = { workspace = true } serde_json = { workspace = true } serde = { workspace = true, features = ["derive"] } diff --git a/crates/oxc_formatter/examples/formatter.rs b/crates/oxc_formatter/examples/formatter.rs index c7bce33670b37..ef699831cb1a9 100644 --- a/crates/oxc_formatter/examples/formatter.rs +++ b/crates/oxc_formatter/examples/formatter.rs @@ -28,6 +28,7 @@ fn main() -> Result<(), String> { let mut args = Arguments::from_env(); let no_semi = args.contains("--no-semi"); let show_ir = args.contains("--ir"); + // Show diff between original and formatted code let show_diff = args.contains("--diff"); let print_width = args.opt_value_from_str::<&'static str, u16>("--print-width").unwrap_or(None); let name = args.free_from_str().unwrap_or_else(|_| "test.js".to_string()); @@ -36,6 +37,20 @@ fn main() -> Result<(), String> { let path = Path::new(&name); let source_text = fs::read_to_string(path).map_err(|_| format!("Missing '{name}'"))?; let source_type = SourceType::from_path(path).unwrap(); + + // Format the parsed code + let semicolons = if no_semi { Semicolons::AsNeeded } else { Semicolons::Always }; + let line_width = match print_width { + Some(width) => LineWidth::try_from(width).unwrap(), + None => LineWidth::try_from(80).unwrap(), + }; + let options = FormatOptions { + bracket_same_line: BracketSameLine::from(true), + semicolons, + line_width, + ..Default::default() + }; + let allocator = Allocator::new(); // Parse the source code @@ -50,36 +65,26 @@ fn main() -> Result<(), String> { println!("Parsed with Errors."); } - // Format the parsed code - let semicolons = if no_semi { Semicolons::AsNeeded } else { Semicolons::Always }; - let line_width = match print_width { - Some(width) => LineWidth::try_from(width).unwrap(), - None => LineWidth::try_from(80).unwrap(), - }; - let options = FormatOptions { - bracket_same_line: BracketSameLine::from(true), - semicolons, - line_width, - ..Default::default() - }; + let formatted = Formatter::new(&allocator, options).format(&ret.program); - let formatter = Formatter::new(&allocator, options); - let formatted = formatter.format(&ret.program); if show_ir { println!("--- IR ---"); println!("{}", &formatted.document().to_string()); println!("--- End IR ---\n"); } - let code = formatted.print().map_err(|e| e.to_string())?.into_code(); + let formatted_code = formatted.print().unwrap().into_code(); if show_diff { - println!("--- Diff ---"); - print_diff_in_terminal(&source_text, &code); - println!("--- End Diff ---"); + // First diff: compare formatted output to original input + if source_text == formatted_code { + println!("{formatted_code}"); + } else { + print_diff_in_terminal(&source_text, &formatted_code); + } } else { println!("--- Formatted Code ---"); - println!("{code}"); + println!("{formatted_code}"); println!("--- End Formatted Code ---"); }