Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/oxc_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
43 changes: 24 additions & 19 deletions crates/oxc_formatter/examples/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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
Expand All @@ -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 ---");
}

Expand Down
Loading