Shopify / prettier-plugin-liquid

Prettier Liquid/HTML plugin
https://npm.im/@shopify/prettier-plugin-liquid
MIT License
94 stars 15 forks source link

Improve formatting for <script> and <label> #217

Closed domaine-preston closed 4 months ago

domaine-preston commented 4 months ago

Is your feature request related to a problem? Please describe.

  1. <script> is being formatted regardless of printWidth value
  2. <label> could be improved when formatted based on printWidth

image

In the screenshot:

Describe the solution you'd like I think the formatting can be improved. Here are a few ideas:

image

<script> tag should respect the printWidth value and <label> could do better at wrapping the last bracket, like the <div> is already doing.

Checklist

Additional context I have disabled all other formatting extensions: ESLint, Prettier, and Stylelint

charlespwd commented 4 months ago

What you're seeing is explained in these docs on whitespace handling.

The official HTML plugin offered by prettier does the same thing. The TL;DR is that the lack of whitespace in HTML can be meaningful and that the prettier plugin should not alter the semantic meaning of source files by default. So we're forced to maintain the lack of whitespace after formatting. <label>...</label> didn't have whitespace after l> nor before </, so the formatted code shouldn't have some.

You can turn that off by setting --htmlWhitespaceSensitivity to ignore, but it would be dangerous to run it blindly on multiple files because you may end up with spacing in places you don't expect after running it. Your site might be broken in subtle ways.

There's also this article which does a good job of explaining the same issue.


Re the script tag, if there wasn't a new line and the print width is enough for the tag and closing tag to fit on a line, it doesn't break automatically for me. Are you sure that's true?

domaine-preston commented 4 months ago

Ok, understood about the label tag. I don't like it, but I understand.

I checked the script tag in a different repo that has similar formatting and linting settings and the issue isn't happening there. So I guess it's related to that one project.

charlespwd commented 4 months ago

Ok, understood about the label tag. I don't like it, but I understand.

Like I said, you can set the following in your .prettierrc and have it format like you want. Just beware that if you run prettier wholesale, that might create bugs!

{
  "htmlWhitespaceSensitivity": "ignore"
}

You can also enable that from the command line with the --html-whitespace-sensitivity=ignore flag.