feat(formatter): Export doc() API to inspect IR in example#14068
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
CodSpeed Instrumentation Performance ReportMerging #14068 will not alter performanceComparing Summary
|
There was a problem hiding this comment.
Pull Request Overview
Adds a new doc() API to the Formatter that returns the intermediate representation (IR) before printing, allowing inspection of the formatting document structure. This enables debugging and understanding of how the formatter processes code internally.
- Introduces
doc()method that returns the formatting IR as a Document - Refactors
build()method to use shared internalformat()function - Adds
--irflag to formatter example to display the IR structure
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| crates/oxc_formatter/src/lib.rs | Adds doc() API and refactors build() to share formatting logic |
| crates/oxc_formatter/examples/formatter.rs | Adds --ir flag support to display IR structure |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Dunqing
left a comment
There was a problem hiding this comment.
It looks good for now. I will refactor it when the TypeScript support is complete.
Merge activity
|
```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),
]
```
fe97cc6 to
10a41ab
Compare

❯ 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), ]