DioxusLabs / dioxus

Fullstack GUI library for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
18.5k stars 704 forks source link

dx fmt deletes comments #2341

Open magikmw opened 2 weeks ago

magikmw commented 2 weeks ago

Problem

Using dx fmt on source files is deleting comments, both in rsx macros and standard rust when using --all-code.
This is especially egregious because standard rustfmt formatter is really messing up any files with rsx in them, so dx fmt is the only viable alternative.

Steps To Reproduce

Steps to reproduce the behavior:

Expected behavior

Comments should be left alone.
I'm not entirely sure how rustfmt does it, but with my limited experience with it, it seems to keep comments intact and relatively in the relevelant place after formatting.

Screenshots

n/a

Environment:

Questionnaire

DogeDark commented 1 week ago

I am unable to reproduce this using the git version of the CLI. Can you test the git version of the CLI on your end?

You can install the git version with:

cargo install --git https://github.com/DioxusLabs/dioxus dioxus-cli
magikmw commented 1 week ago

I did run the command to install git version and checked again:

-                // TODO: Implement event handling here -> listen to eve
nts in child
                 let accept_keys = [Key::Enter, Key::Character(" ".to_string())];
-                if accept_keys.contains(&evt.key()){
+                if accept_keys.contains(&evt.key()) {
                     log::info!("{} pressed in {}", evt.key(), "{props.labelText}");
                     log::info!("{} pressed in {}", evt.key(), "{props.labelText}");
                 }
             },
             class: "flex flex-1 h-full w-3/4 items-center justify-center btn btn-outline my-1 py-1",
-            div { class: "flex-none py-1", // TODO: Refactor into separate component
+            div { class: "flex-none py-1",
                 Icon { width: 24, height: 24, icon: props.icon, title: "{props.iconTitle}" }
-            },
+            }
             "{props.labelText}"
-            input { // TODO: Refactor into separate component
+            input {

dx -V says:

dioxus 0.5.2

DogeDark commented 1 week ago

I can reproduce it now. The formatter doesn't like inline comments and removes them completely:

rsx! {
    div {   // this comment will be removed
        "hi",
    }
}
spookyvision commented 1 day ago

this also seems to affect the VSCode Dioxus extension. Block-level comments (/* .. */) are also affected; doc comments (///) however are not.

DogeDark commented 1 day ago

Adding to the list.

All comments are removed in event handlers (except doc comments):

rsx! {
    div {
        // also occurs in move || async move
        onclick: move |_| {
            // is removed
            /* is removed */
        }
    }
}
OlivierLemoine commented 10 hours ago

I think it comes from here: https://github.com/DioxusLabs/dioxus/blob/472031d1f51557601797d1cb469f09e4f3e0ca71/packages/cli/src/cli/autoformat.rs#L249-L253

syn does not keep comments.

https://crates.io/crates/rustfmt-wrapper (or spawning rustfmt directly) could be used instead. This won't help in non-rust code, but I think it's a start.

I don't know if Dioxus developers would agree with this solution, but if they do, I would be happy to do it.

jkelleyrtp commented 7 hours ago

We try our best to preserve comments by writing out whitepace on rsx indents, but there's no way to preserve whitespace with prettyplease. I'd be interested in supporting the rustfmt wrapper approach, but it is convenient for autoformatting to not rely on rustfmt so we can use it in things like wasm.