Skip to content

Commit 10a41ab

Browse files
committed
feat(formatter): Export doc() API to inspect IR in example (#14068)
```sh ❯ cargo run -p oxc_formatter --example formatter t.ts --ir Finished `dev` profile [unoptimized] target(s) in 0.05s Running `target/debug/examples/formatter t.ts --ir` [ StaticText("import"), Space, StaticText("{"), Space, DynamicText("useState"), Space, StaticText("}"), Space, StaticText("from"), Space, LocatedTokenText("\"react\""), StaticText(";"), Line(Empty), StaticText("export"), Space, StaticText("function"), Space, DynamicText("useData"), Tag(StartGroup(Group { id: None, mode: Cell { value: Flat } })), Tag(EndGroup), Tag(StartGroup(Group { id: None, mode: Cell { value: Flat } })), StaticText("("), Tag(StartGroup(Group { id: None, mode: Cell { value: Flat } })), Tag(EndGroup), StaticText(")"), Tag(EndGroup), Space, Space, StaticText("{"), Tag(StartIndent), Line(Hard), ... StaticText("}"), Line(Hard), ] ```
1 parent 1b3f437 commit 10a41ab

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

crates/oxc_formatter/examples/formatter.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use pico_args::Arguments;
2323
fn main() -> Result<(), String> {
2424
let mut args = Arguments::from_env();
2525
let no_semi = args.contains("--no-semi");
26+
let show_ir = args.contains("--ir");
2627
let name = args.free_from_str().unwrap_or_else(|_| "test.js".to_string());
2728

2829
// Read source file
@@ -57,9 +58,19 @@ fn main() -> Result<(), String> {
5758
semicolons,
5859
..Default::default()
5960
};
60-
let code = Formatter::new(&allocator, options).build(&ret.program);
6161

62-
println!("{code}");
62+
let formatter = Formatter::new(&allocator, options);
63+
if show_ir {
64+
let doc = formatter.doc(&ret.program);
65+
println!("[");
66+
for el in doc.iter() {
67+
println!(" {el:?},");
68+
}
69+
println!("]");
70+
} else {
71+
let code = formatter.build(&ret.program);
72+
println!("{code}");
73+
}
6374

6475
Ok(())
6576
}

crates/oxc_formatter/src/lib.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use write::FormatWrite;
3636
pub use crate::options::*;
3737
pub use crate::service::source_type::get_supported_source_type;
3838
use crate::{
39-
formatter::FormatContext,
39+
formatter::{FormatContext, Formatted, format_element::document::Document},
4040
generated::ast_nodes::{AstNode, AstNodes},
4141
};
4242

@@ -53,20 +53,31 @@ impl<'a> Formatter<'a> {
5353
Self { allocator, source_text: "", options }
5454
}
5555

56+
/// Formats the given AST `Program` and returns the IR before printing.
57+
pub fn doc(mut self, program: &'a Program<'a>) -> Document<'a> {
58+
let formatted = self.format(program);
59+
formatted.into_document()
60+
}
61+
62+
/// Formats the given AST `Program` and returns the formatted string.
5663
pub fn build(mut self, program: &Program<'a>) -> String {
64+
let formatted = self.format(program);
65+
formatted.print().unwrap().into_code()
66+
}
67+
68+
fn format(mut self, program: &'a Program<'a>) -> Formatted<'a> {
5769
let parent = self.allocator.alloc(AstNodes::Dummy());
5870
let program_node = AstNode::new(program, parent, self.allocator);
5971

6072
let source_text = program.source_text;
6173
self.source_text = source_text;
6274
let context = FormatContext::new(program, self.allocator, self.options);
63-
let formatted = formatter::format(
75+
formatter::format(
6476
program,
6577
context,
6678
formatter::Arguments::new(&[formatter::Argument::new(&program_node)]),
6779
)
68-
.unwrap();
69-
formatted.print().unwrap().into_code()
80+
.unwrap()
7081
}
7182
}
7283

0 commit comments

Comments
 (0)