prettier-plugin-astro icon indicating copy to clipboard operation
prettier-plugin-astro copied to clipboard

🐛 BUG: breaks the <tag> to separate lines

Open teinett opened this issue 1 year ago • 8 comments

Describe the Bug

The bug: code <svg viewBox="0 0 800 250" class="_hero-heading_1csbc_7"><defs>...</defs></svg> became ugly:

<svg viewBox="0 0 800 250" class="_hero-heading_1csbc_7"
    ><defs
        >
        ...
        ></defs
    ></svg
>

Screenshot:

Screenshot 2023-11-26 at 14 40 46

VS Code version: Version: 1.84.2 (Universal) Commit: 1a5daa3a0231a0fbba4f14db7ec463cf99d7768e Date: 2023-11-09T10:52:33.687Z Electron: 25.9.2 ElectronBuildId: 24603566 Chromium: 114.0.5735.289 Node.js: 18.15.0 V8: 11.4.183.29-electron.0 OS: Darwin arm64 23.1.0

Astro VS Code Extension version: 2.5.2 Prettier VS Code Extension version: 10.1.0

npm package.json: "prettier-plugin-astro": "^0.12.2"

File .prettierrc.mjs:

/** @type {import("prettier").Config} */
export default {
    plugins: ['prettier-plugin-astro'],
    overrides: [
        {
            files: '*.astro',
            options: {
                parser: 'astro',
            },
        },
    ],
};

Steps to Reproduce

I use this code in AstroJS project: src/pages/index.astro

Code:

<svg viewBox="0 0 800 250" class="_hero-heading_1csbc_7"><defs><g id="text-group" class="_text-group_1csbc_11"><text x="50%" y="80">learn. build. grow.</text><text x="50%" y="190" class="_bigger_1csbc_21">together.</text></g></defs><rect class="_o-filler_1csbc_48" x="450" y="58" width="8" height="8"></rect><rect class="_o-filler2_1csbc_52" x="615" y="54" width="7" height="18"></rect><rect class="_o-filler3_1csbc_56" x="535" y="54" width="7" height="18"></rect><use id="gradient-stroke" class="_gradient-stroke_1csbc_42" href="#text-group"></use><use id="text" class="_text_1csbc_11" href="#text-group"></use></svg>

Resulted code in VS Code after Prettier:

<svg viewBox="0 0 800 250" class="_hero-heading_1csbc_7"
    ><defs
        ><g id="text-group" class="_text-group_1csbc_11"
            ><text x="50%" y="80">learn. build. grow.</text><text
                x="50%"
                y="190"
                class="_bigger_1csbc_21">together.</text
            ></g
        ></defs
    ><rect class="_o-filler_1csbc_48" x="450" y="58" width="8" height="8"
    ></rect><rect
        class="_o-filler2_1csbc_52"
        x="615"
        y="54"
        width="7"
        height="18"></rect><rect
        class="_o-filler3_1csbc_56"
        x="535"
        y="54"
        width="7"
        height="18"></rect><use
        id="gradient-stroke"
        class="_gradient-stroke_1csbc_42"
        href="#text-group"></use><use
        id="text"
        class="_text_1csbc_11"
        href="#text-group"></use></svg
>

Expected result:

<svg viewBox="0 0 800 250" class="_hero-heading_1csbc_7">
  <defs>
    <g id="text-group" class="_text-group_1csbc_11">
      <text x="50%" y="80">
        learn. build. grow.
      </text>
      <text x="50%" y="190" class="_bigger_1csbc_21">
        together.
      </text>
    </g>
  </defs>
  <rect class="_o-filler_1csbc_48" x="450" y="58" width="8" height="8"></rect>
  <rect class="_o-filler2_1csbc_52" x="615" y="54" width="7" height="18"></rect>
  <rect class="_o-filler3_1csbc_56" x="535" y="54" width="7" height="18"></rect>
  <use
    id="gradient-stroke"
    class="_gradient-stroke_1csbc_42"
    href="#text-group"
  ></use>
  <use id="text" class="_text_1csbc_11" href="#text-group"></use>
</svg>;

In the same project I create page.html, and the code is formatted correctly.

Tried the code in https://prettier.io/playground/: yes, the formatting is perfect.

Prettier extension log:

["INFO" - 14:28:22] Formatting completed in 20ms.
["INFO" - 14:31:00] Using config file at '/Users/teine/Development/landing-brasil-docs/.prettierrc.mjs'

teinett avatar Nov 26 '23 17:11 teinett

Hmm, we might be handling SVGs elements wrongly. Not sure if they should all be treated as block elements, will have to investigate! Thank you for creating an issue for this.

Princesseuh avatar Nov 28 '23 16:11 Princesseuh

It's not just svg elements actually. image

khalibloo avatar Jan 10 '24 00:01 khalibloo

span is an inline element, as such this is expected behavior, putting the content in a different manner would result in spaces in your page. You can reproduce similar behavior in HTML files

Princesseuh avatar Jan 10 '24 01:01 Princesseuh

Interesting. So that's the rationale behind that. I've only ever observed it in astro since most of my projects use jsx. It still looks very odd to me, and triggers my OCD, but glad to know it's not a bug. Thanks

khalibloo avatar Jan 10 '24 05:01 khalibloo

I'm having a similar issue (version 0.12.3).

Before formatting:

<span class:list={["uppercase", dark ? "text-ds-white" : "text-ds-night-sky"]}>by <a href={personUrl(author)} class="hover:text-ds-sky-blue transition-colors duration-hover">{author}</a></span>

After formatting:

<span class:list={["uppercase", dark ? "text-ds-white" : "text-ds-night-sky"]}
  >by <a
    href={personUrl(author)}
    class="hover:text-ds-sky-blue transition-colors duration-hover">{author}</a
  ></span
>

@Princesseuh As you can see from the above, it seems to extend beyond svg elements.

lucperkins avatar Feb 01 '24 16:02 lucperkins

I'm having a similar issue (version 0.12.3).

Before formatting:

<span class:list={["uppercase", dark ? "text-ds-white" : "text-ds-night-sky"]}>by <a href={personUrl(author)} class="hover:text-ds-sky-blue transition-colors duration-hover">{author}</a></span>

After formatting:

<span class:list={["uppercase", dark ? "text-ds-white" : "text-ds-night-sky"]}
  >by <a
    href={personUrl(author)}
    class="hover:text-ds-sky-blue transition-colors duration-hover">{author}</a
  ></span
>

@Princesseuh As you can see from the above, it seems to extend beyond svg elements.

As replied right before your comment, this seems like expected behavior to me, with span being an inline element.

Princesseuh avatar Feb 01 '24 18:02 Princesseuh

This is really kind of surprising. I got this after formatting inside my new project and felt there might be something wrongly configured first:

  <p>
    Umsatzsteuer-Identifikationsnummer gemäß § 27 a Umsatzsteuergesetz: <em
      >ausstehend</em
    >
  </p>

swernerx avatar Feb 06 '24 13:02 swernerx

This is also the same thing as the previous comments, em is an inline element and so it needs to hugs its children. Though, Prettier format it a bit differently in HTML files in this specific case because the whitespace before can be converted to a new line, which we don't currently do. Nonetheless, at this time, this result is expected.

This issue only affects SVGs elements, everything else are another, unrelated issues. HTML, Astro and Svelte are whitespace sensitive languages, unlike JSX. So the formatting has to and will be different than JSX.

Princesseuh avatar Feb 06 '24 13:02 Princesseuh