Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-doc comments are sometimes removed #60

Closed
kik4444 opened this issue Aug 3, 2023 · 1 comment · Fixed by #70
Closed

Non-doc comments are sometimes removed #60

kik4444 opened this issue Aug 3, 2023 · 1 comment · Fixed by #70

Comments

@kik4444
Copy link

kik4444 commented Aug 3, 2023

Apologies in advance for the long post, I just don't know if I can demonstrate the issue without these examples.

Suppose we have the following snippet

#[component]
fn Example() -> impl IntoView {
    view! {
        <div>
            // Kept
            <p>
                {if true {
                    // Removed
                    // Removed
                    // Removed
                    // Removed
                    "Hello"
                } else {
                    "There"
                }}

            </p>
        // Kept
        </div>
    }
}

When I run leptosfmt myfile, it removes my // Removed comment, no matter how long it is or how many lines it spans

#[component]
fn Example() -> impl IntoView {
    view! {
        <div>
            // Kept
            <p>{if true { "Hello" } else { "There" }}
            </p>
        // Kept
        </div>
    }
}

However if I turn // Removed into a doc comment, it remains, but I get a clippy warning for an unused doc comment

#[component]
fn Example() -> impl IntoView {
    view! {
        <div>
            // Kept
            <p>
                {if true {
                    /// Removed
                    /// Removed
                    /// Removed
                    /// Removed
                    "Hello"
                } else {
                    "There"
                }}

            </p>
        // Kept
        </div>
    }
}

There are some slightly more complicated examples too. Suppose we have this snippet

let fallback = |errors: RwSignal<Errors>| {
    view! {
        // Removed
        <div class="error">
        // Kept
            <p>"Not a number! Errors: "</p>
            // Kept
            <ul>
            // Kept
                {move || {
                    // Kept, but moved 1
                    errors
                        .get()
                        .into_iter()
                        // Kept, but moved 2
                        .map(|(_, e)| view! { <li>{e.to_string()}</li> })
                        .collect_view()
                }}

            </ul>
        </div>
    }
};

When I format it, it becomes

let fallback = |errors: RwSignal<Errors>| {
    view! {
        <div class="error">
            // Kept
            <p>"Not a number! Errors: "</p>
            // Kept
            <ul>
                // Kept
                {move || {
                    errors
                        .get()
                        .into_iter()
                        .map(|(_, e)| {
                            view! {
                                // Kept, but moved 1
                                // Kept, but moved 2
                                <li>{e.to_string()}</li>
                            }
                        })
                        .collect_view()
                }}

            </ul>
        </div>
    }
};

One comment is removed and two are moved. However this behavior is slightly different when this code is inlined instead of being assigned to a variable.


If I try inlining the above example, the behavior is slightly different

view! {
    <h1>"Error Handling"</h1>
    <label>
        "Type a number (or something that's not a number!) " <input on:input=on_input/>
        // the fallback receives a signal containing current errors
        <ErrorBoundary fallback=|errors|
            view! {
                // Removed
                <div class="error">
                // Kept
                    <p>"Not a number! Errors: "</p>
                    // Kept
                    <ul>
                    // Kept
                        {move || {
                            // Kept, but moved 1
                            errors
                                .get()
                                .into_iter()
                                // Kept, but moved 2
                                .map(|(_, e)| view! { <li>{e.to_string()}</li> })
                                .collect_view()
                        }}

                    </ul>
                </div>
            }
        >
            <p>"You entered " <strong>{value}</strong></p>
        </ErrorBoundary>
    </label>
}

The above is formatted into this

view! {
    <h1>"Error Handling"</h1>
    <label>
        "Type a number (or something that's not a number!) " <input on:input=on_input/>
        // the fallback receives a signal containing current errors
        <ErrorBoundary fallback=|errors| {
            view! {
                // Removed
                <div class="error">
                    // Kept
                    <p>"Not a number! Errors: "</p>
                    // Kept
                    <ul>
                        // Kept
                        {move || {
                            errors
                                .get()
                                .into_iter()
                                .map(|(_, e)| {
                                    view! {
                                        // Kept, but moved 1
                                        // Kept, but moved 2
                                        <li>{e.to_string()}</li>
                                    }
                                })
                                .collect_view()
                        }}

                    </ul>
                </div>
            }
        }>

            <p>"You entered " <strong>{value}</strong></p>
        </ErrorBoundary>
    </label>
}

Noticeably the Removed comment is not removed, but the other two are still moved.


Finally here's one example where the formatter actually breaks the code if I have an invalid doc comment

view! {
    <h1>"Error Handling"</h1>
    <label>
        "Type a number (or something that's not a number!) " <input on:input=on_input/>
        // the fallback receives a signal containing current errors
        <ErrorBoundary fallback=|errors|
            view! {
                <div class="error">
                    <p>"Not a number! Errors: "</p>
                    <ul>
                        {move || {
                            errors
                                .get()
                                .into_iter()
                                /// I am invalid
                                .map(|(_, e)| view! { <li>{e.to_string()}</li> })
                                .collect_view()
                        }}

                    </ul>
                </div>
            }
        >
            <p>"You entered " <strong>{value}</strong></p>
        </ErrorBoundary>
    </label>
}

This is formatted into

view! {
    <h1>"Error Handling"</h1>
    <label>
        "Type a number (or something that's not a number!) " <input on:input=on_input/>
        // the fallback receives a signal containing current errors
        <ErrorBoundary fallback=|errors| {
            view! {
                < div class = "error" > < p > "Not a number! Errors: " </ p > < ul > { move || {
                errors.get().into_iter() #[doc = " I am invalid"] .map(| (_, e) | view! { < li >
                { e.to_string() } </ li > }).collect_view() } } </ ul > </ div >
            }
        }>

            <p>"You entered " <strong>{value}</strong></p>
        </ErrorBoundary>
    </label>
}

Here are my versions

key value
rustc 1.73.0-nightly (d12c6e947 2023-08-01)
leptos 0.5.0-alpha
leptosfmt 0.1.12
@bram209
Copy link
Owner

bram209 commented Aug 3, 2023

Hi, we currently don't support non-doc comments in code blocks. We use prettyplease for formatting rust code and it does not support this. I have a fork that I would like to not diverge too much (so I can easily keep in sync with upstream), therefore I didn't add non-doc comment support in my prettyplease fork for now.

I should probably clarify this in the documentation though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants