tools
tools copied to clipboard
Write a tool for helping debug rome_ir
Description
Currently, suppose we want to debug why our formatter generate different result from prettier
. We need to use rome_playground, copy it, write some corresponding rust code, debug the rust rome_ir code, and finally, get expected rust rome_ir code that matches prettier, then adjust rome_js_formatter
until it generates expected result same as prettier.
I found that rome_ir
has some relation with our crate rome_formatter
, e.g.:
Assume we have rome_ir
below:
List [
SyntaxTokenSlice("test"),
HardGroup(
List [
SyntaxTokenSlice("("),
SyntaxTokenSlice(")"),
],
),
SyntaxTokenSlice(";"),
SyntaxTokenSlice(""),
Line(Hard),
]
We could convert rome_ir
above to rust code use rome_formatter
:
fn main() {
use rome_formatter::prelude::*;
use rome_formatter::Formatted;
let block = format_elements![
token("test"),
hard_group_elements(format_elements![token("("), token(")")]),
token(";"),
token(""),
hard_line_break()
];
println!(
"{}",
Formatted::new(block, PrinterOptions::default())
.print()
.as_code()
);
}
Although, SyntaxTokenSlice
should be more efficient than token
, in this scenario performance is not a big deal.
We want to use some tool to help us convert rome_ir
in the playground to rust code, I split this task to two steps:
- [x] Write a parser that could convert
rome_ir
to AST - [x] Visit the Ast to generate the corresponding rust code
This is an interesting idea! It's essentially a decompilation tool. One idea I had was to allow people to write format IR directly using s-expressions or a simple DSL. It could potentially tie into this.
This issue is stale because it has been open 14 days with no activity.
One more option that could help us to achieve this is by using serde
. Each data structure could implement a custom de-serialization function
This issue is stale because it has been open 14 days with no activity.
I think the hard part isn't about writing the IR elements but getting the conditional logic right.