bram209 / leptosfmt

A formatter for the leptos view! macro
Apache License 2.0
258 stars 27 forks source link

Unexpected Duplication of Comments in Conditional Logic with nested view macros #134

Closed mmanrai closed 3 weeks ago

mmanrai commented 2 months ago

I've encountered an issue where leptosfmt incorrectly copies a comment from the if block into the else block when formatting code.

Code Example:

Before running leptosfmt:

use leptos::*;

fn main() {
    mount_to_body(|| {
        view! {
            {move || {
                if true {
                    view! {
                        // comment in if condition.
                        <div>dummy text</div>
                    }
                        .into_view()
                } else {
                    view! {
                        // comment in else condition.
                        <div>dummy text</div>
                    }
                        .into_view()
                }
            }}
        }
    })
}

After running leptosfmt:

use leptos::*;

fn main() {
    mount_to_body(|| {
        view! {
            {move || {
                if true {
                    view! {
                        // comment in if condition.
                        <div>dummy text</div>
                    }
                        .into_view()
                } else {
                    view! {
                        // comment in if condition.

                        // comment in else condition.
                        <div>dummy text</div>
                    }
                        .into_view()
                }
            }}
        }
    })
}

Notice how '// comment in if condition' is copied over to else block.

niclas-ahden commented 1 month ago

I can reproduce this on c71652c1209367846864b3c2d7dc52fdcf37bd0e and it also seems to affect match expressions.

use leptos::*;

fn main() {
    mount_to_body(|| {
        view! {
            {move || {
                match true {
                    true => {
                        view! {
                            // comment in if condition.
                            <div>dummy text</div>
                        }
                            .into_view()
                    }
                    false => {
                        view! {
                            // comment in else condition.
                            <div>dummy text</div>
                        }
                            .into_view()
                    }
                }
            }}
        }
    })
}

Formats to:

use leptos::*;

fn main() {
    mount_to_body(|| {
        view! {
            {move || {
                match true {
                    true => {
                        view! {
                            // comment in if condition.
                            <div>dummy text</div>
                        }
                            .into_view()
                    }
                    false => {
                        view! {
                            // comment in if condition.
                            // comment in else condition.
                            <div>dummy text</div>
                        }
                            .into_view()
                    }
                }
            }}
        }
    })
}

Consecutive runs inserts another copy of the comment. After three runs:

use leptos::*;

fn main() {
    mount_to_body(|| {
        view! {
            {move || {
                match true {
                    true => {
                        view! {
                            // comment in if condition.
                            <div>dummy text</div>
                        }
                            .into_view()
                    }
                    false => {
                        view! {
                            // comment in if condition.
                            // comment in if condition.
                            // comment in if condition.
                            // comment in else condition.
                            <div>dummy text</div>
                        }
                            .into_view()
                    }
                }
            }}
        }
    })
}

Thanks for creating leptosfmt! I hope these details are helpful.

niclas-ahden commented 3 weeks ago

Thank you @bram209, I've tested the fix and can confirm it works!

bram209 commented 3 weeks ago

Thanks for letting me know :)