g-plane / markup_fmt

Configurable HTML, Vue, Svelte, Astro, Angular, Jinja, Twig, Nunjucks and Vento formatter with dprint integration.
https://dprint.dev/plugins/markup_fmt/
MIT License
91 stars 4 forks source link

Astro comments are unnecessarily multiline #51

Closed michaelhthomas closed 2 days ago

michaelhthomas commented 2 weeks ago

Currently, the following line:

{/* Comment */}

is formatted as

{
  /* Comment */
}

in an Astro file. This is likely related more to the typescript plugin, just curious if there's any way to keep these on one line.

g-plane commented 2 days ago

I can't reproduce.

michaelhthomas commented 2 days ago

I'll try to publish a minimal reproduction.

michaelhthomas commented 2 days ago

Turns out this is a very specific case, and I'm a bit dumbfounded. The issue occurs when you have a comment expression containing two forward slashes (maybe these are being considered a comment as well?). In my case, this occurred because I included a URL in a comment. For example,

{/* Hello world */}

will not have any weird wrapping, but

{/* https://example.com */}

will be formatted as

- {/* https://example.com */}
+ {
+   /* https://example.com */
+ }
g-plane commented 2 days ago

markup_fmt doesn't know those two slashes are in comments, and they're treated as line comments, such as:

{// xxx}

which causes wrapping.

michaelhthomas commented 2 days ago

Ah that makes sense. I feel like this is a bug, especially since afaik

{// comment}

isn't even valid syntax to begin with. Is it possible to have the parser keep track of whether it's already in a comment, and ignore the //?

g-plane commented 2 days ago

However this can be valid:

{
  // comment
}
g-plane commented 2 days ago

It's a good idea for detecting this in parser.

g-plane commented 2 days ago

It seems that there is a GitHub bug when I was commenting. It's re-opened now.