bram209 / leptosfmt

A formatter for the leptos view! macro
Apache License 2.0
274 stars 30 forks source link

Leptosfmt is removing or replacing some characters #84

Closed svieujot closed 1 year ago

svieujot commented 1 year ago

Running leptosfmt on the following file:

use leptos::*;

#[component]
pub fn FormatingGoneWrong() -> impl IntoView {

    view! {
        <div>
        // <span>{{ (position.course.correction > 0 ? "+" : "") + formattedNumber(radiansToDegrees(position.course.correction), 0, "?", false) }}°</span>
        </div>
        <div>
            Switch
        </div>
        <span class="mx-1">@</span>
        <span class="ms-3">Manœuvering aid</span>
    }
}

Generates this:

use leptos::*;

#[component]
pub fn FormatingGoneWrong() -> impl IntoView {

    view! {
        <div>// <span>{{ (position.course.correction > 0 ? "+" : "") + formattedNumber(radiansToDegrees(position.course.correction), 0, "?", false) }}°</span>
        </div>
        <div> Switc</div>
        <span class="mx-1">></span>
        <span class="ms-3">>Manœuvering a</span>
    }
}
  1. The word Switch has become Switc (and looses one more character at every subsequent run).
  2. The @ has been replaced with a >
  3. The word aid has been replaced by a

It looks like this is due to the use of UTF-8 characters: The ° in the span, and the œ in Manœuvering. I have no explanation for the @ however.

bram209 commented 1 year ago

It looks like this is due to the use of UTF-8 characters: The ° in the span, and the œ in Manœuvering.

That is indeed the cause

I have no explanation for the @ however.

The issue is that it uses proc_macro2's get_source_text which impl comes down to source[lo..hi], where lo and hi are character indexes but are used as byte indexes here... So multibyte characters anywhere in the source file will cause issues with unquoted text at the moment.

Also from proc_macros docs (https://docs.rs/proc-macro2/latest/proc_macro2/struct.Span.html#method.source_text):

Note: The observable result of a macro should only rely on the tokens and not on this source text. The result of this function is a best effort to be used for diagnostics only.

bram209 commented 1 year ago

Ah I see now that the author of proc_macro2 pushed a fix 2 days ago :D

https://github.com/dtolnay/proc-macro2/commit/7f5533d6cc9ff783d174aec4e3be2caa202b62ca

bram209 commented 1 year ago

ok, so the proc_macro2 crate is still having a bug, issue here: https://github.com/dtolnay/proc-macro2/issues/410 will implement a workaround for now

svieujot commented 1 year ago

@bram209 Works perfectly. Thank you !