Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting fails for HTML custom elements #84

Closed
mtlynch opened this issue May 13, 2022 · 5 comments
Closed

Formatting fails for HTML custom elements #84

mtlynch opened this issue May 13, 2022 · 5 comments
Labels
bug Something isn't working p:2 Priority 2

Comments

@mtlynch
Copy link

mtlynch commented May 13, 2022

I'm still loving this plugin. Thanks so much for your work on this!

I'm noticing an issue when I try to use it on HTML custom elements that include Go templating.

Works fine

In the following example without any Go template syntax, the prettier-plugin-go template works fine:

<template id="template">
  <script>
    document.getElementById("demo").innerText = "clicked!";
  </script>
  <style>
    #demo {
      font-size: 1.5rem;
    }
  </style>
  <button id="demo">Click me</button>
</template>

Failure to process templated attributes

If I add templated attributes (the nonce={{ .Nonce }} attributes), the plugin seems to get confused

Original

<template id="template">
  <script nonce="{{ .Nonce }}">
    document.getElementById("demo").innerText = "clicked!";
  </script>
  <style nonce="{{ .Nonce }}">
    #demo {
      font-size: 1.5rem;
    }
  </style>
  <button id="demo">Click me</button>
</template>

Formatted

<template id="template"
  >  <script nonce="{{ .Nonce }}">
    document.getElementById("demo").innerText = "clicked!";
  </script>  <style nonce="{{ .Nonce }}">
    #demo {
      font-size: 1.5rem;
    }
  </style>
  <button id="demo">Click me</button>
</template>

And then it mostly stops formatting. So if I add lots of whitespace in the <script> section, the formatter doesn't touch it:

<template id="template"
  >  <script nonce="{{ .Nonce }}">
    document.getElementById("demo").innerText   =        "clicked!";
  </script>  <style nonce="{{ .Nonce }}">
    #demo {
      font-size: 1.5rem;
    }
  </style>
  <button id="demo">Click me</button>
</template>

Workaround

For this use case, the vanilla Prettier formatter formats the code correctly, so I can work around it by using more targeted matchers in my prettierrc to prevent prettier-plugin-go-template from handling these files.

mtlynch added a commit to mtlynch/picoshare that referenced this issue May 13, 2022
There seems to be a bug in prettier-plugin-go-template that prevents it from formatting custom elements correctly when they contain Go template syntax, so we're tightening the matcher for Go templates to not match HTML custom elements.

See: NiklasPor/prettier-plugin-go-template#84
mtlynch added a commit to mtlynch/picoshare that referenced this issue May 13, 2022
There seems to be a bug in prettier-plugin-go-template that prevents it from formatting custom elements correctly when they contain Go template syntax, so we're tightening the matcher for Go templates to not match HTML custom elements.

See: NiklasPor/prettier-plugin-go-template#84
@NiklasPor NiklasPor added bug Something isn't working p:2 Priority 2 labels May 14, 2022
@NiklasPor
Copy link
Owner

Hey, thanks for reporting the issue 👏

Could you check whether [email protected] resolved this for you?

@mtlynch
Copy link
Author

mtlynch commented May 14, 2022

@NiklasPor - wow, fast fix! Yep, I tried installing the beta and confirmed 71dc2e7 works on all of my custom element files.

@mtlynch mtlynch closed this as completed May 14, 2022
@NiklasPor
Copy link
Owner

Great, I'll publish [email protected] as a stable release with the fix then 👍

@NiklasPor
Copy link
Owner

@all-contributors please add @mtlynch for bug

@allcontributors
Copy link
Contributor

@NiklasPor

I've put up a pull request to add @mtlynch! 🎉

DmitroProciv added a commit to DmitroProciv/picoshare that referenced this issue Jan 4, 2025
There seems to be a bug in prettier-plugin-go-template that prevents it from formatting custom elements correctly when they contain Go template syntax, so we're tightening the matcher for Go templates to not match HTML custom elements.

See: NiklasPor/prettier-plugin-go-template#84
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p:2 Priority 2
Projects
None yet
Development

No branches or pull requests

2 participants