NiklasPor / prettier-plugin-go-template

Fixes prettier formatting for go templates 🐹
prettier-plugin-go-template-niklaspor.vercel.app
MIT License
343 stars 24 forks source link

fails on html tags using if/else #95

Open josharian opened 2 years ago

josharian commented 2 years ago

We have code structurally similar to this (only more complex, with different tags):

{{ if .B }}
  <span>
{{ else }}
  <div>
{{ end }}

something

{{ if .B }}
  </span>
{{ else }}
  </div>
{{ end }}

This yields:

Error: An error occured during printing. Found invalid node block.

This is similar to https://github.com/NiklasPor/prettier-plugin-go-template/issues/59, but it causes an error instead of duplication.

josharian commented 2 years ago

This obviously can't be solved in the general case. (The if statement could call a Go function, so solving this is equivalent to solving the halting problem.) But some common cases could be special-cased to handle most scenarios.

maxrzaw commented 1 year ago

While not exactly the same. This seems like a similar scenario that can be handled correctly every time because the same tag is used in both the if and the else block:

{{ define "test" }}
  {{ if .Active }}
  <li class="active">
  {{ else }}
  <li class="inactive">
  {{ end }}
  </li>
{{ end }}

[error] test.html: Error: An error occured during printing. Found invalid node root.