tools icon indicating copy to clipboard operation
tools copied to clipboard

Write a tool for helping debug rome_ir

Open IWANABETHATGUY opened this issue 2 years ago • 3 comments

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

IWANABETHATGUY avatar May 16 '22 16:05 IWANABETHATGUY

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.

NicholasLYang avatar May 26 '22 00:05 NicholasLYang

This issue is stale because it has been open 14 days with no activity.

github-actions[bot] avatar Sep 19 '22 12:09 github-actions[bot]

One more option that could help us to achieve this is by using serde. Each data structure could implement a custom de-serialization function

ematipico avatar Sep 20 '22 10:09 ematipico

This issue is stale because it has been open 14 days with no activity.

github-actions[bot] avatar Nov 04 '22 12:11 github-actions[bot]

I think the hard part isn't about writing the IR elements but getting the conditional logic right.

MichaReiser avatar Nov 10 '22 17:11 MichaReiser