prettier-plugin-go-template icon indicating copy to clipboard operation
prettier-plugin-go-template copied to clipboard

Duplicating code if wrapping start and end HTML tag in separate `if` statements

Open schnerring opened this issue 4 years ago • 5 comments

The following minimal example code ...

{{ if .url }}<a href="{{ .url }}">{{ end }}

<span>Content</span>

{{ if .url }}</a>{{ end }}

... duplicates to the following upon format:

{{ if .url }}<a href="{{ .url }}"></a>{{ end }}


<span>Content</span>

{{ if .url }}<a href="{{ .url }}">{{ end }}

<span>Content</span>

{{ if .url }}</a>{{ end }}


My current workaround is this:

<!-- prettier-ignore -->
{{ if .url }}<a href="{{ .url }}">{{ end }}

<span>Content</span>

<!-- prettier-ignore -->
{{ if .url }}</a>{{ end }}

schnerring avatar Oct 12 '21 19:10 schnerring

Hey there, sadly this is notoriously hard to fix as currently the template is parsed as a tree. The go formatter invokes the prettier html formatter for the contained text, but that one struggles when there are missing tags etc.

I'll try to find a solution to work around this. Thanks for reporting the issue.

NiklasPor avatar Mar 28 '22 06:03 NiklasPor

@schnerring sorry, I have this nearly fixed – but until the HTML formatter has an option to disable auto-closing tags, I'll have to keep this open.

Related prettier issue: https://github.com/prettier/prettier/issues/5665

NiklasPor avatar Mar 30 '22 18:03 NiklasPor

Would it be possible to error & link to this issue instead of duplicating code?

I'm pretty sure this pattern isn't allowed in JSX.. it seems reasonable to not allow it until there's a workaround.

jasikpark avatar Mar 30 '22 21:03 jasikpark

It moght be worth looking at how handlebars was implemented 🤔

jasikpark avatar Apr 01 '22 21:04 jasikpark

@schnerring sorry, I have this nearly fixed – but until the HTML formatter has an option to disable auto-closing tags, I'll have to keep this open.

As I've mentioned, I have a workaround, so no pressure. 😄 Thanks for looking into this!

schnerring avatar Apr 01 '22 22:04 schnerring