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

Don't format contents of javascript_tag method. #32

Open
olbrich opened this issue Nov 9, 2023 · 1 comment
Open

Don't format contents of javascript_tag method. #32

olbrich opened this issue Nov 9, 2023 · 1 comment
Labels
looking for contributions This issue was accepted and is looking for a pull-request

Comments

@olbrich
Copy link

olbrich commented Nov 9, 2023

In normal usage of javascript_tag, the contents of the block are javascript, and not HTML. erb-formatter attempts to format these contents, which can mangle whitespace and make inline scripts hard to read. Something like...

<%= javascript_tag(nonce: true) do %>
// We're setting this directly here to help prevent Flash of Unstyled Content (FOUC).
class Showcase {
  static start() {
    const preference = window.matchMedia("(prefers-color-scheme: dark)").matches
      ? "dark"
      : "light";
    this.colorScheme = localStorage.colorScheme || preference;
  }

  static set colorScheme(value) {
    localStorage.colorScheme = value;
    document.documentElement.classList.toggle("sc-dark", value === "dark");
  }
}

Showcase.start();
<% end %>

becomes

<%= javascript_tag(nonce: true) do %>
// We're setting this directly here to help prevent Flash of Unstyled Content
(FOUC). class Showcase { static start() { const preference =
window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
this.colorScheme = localStorage.colorScheme || preference; } static set
colorScheme(value) { localStorage.colorScheme = value;
document.documentElement.classList.toggle("sc-dark", value === "dark"); } }
Showcase.start();
<% end %>

Note that it mangles the comment and results in malformed code in addition to making it difficult to read.

To complicate things, it is valid to include erb tags within the block. Ideally those tags should still be formatted.

This may apply to other helpers as well.

@elia
Copy link
Member

elia commented Dec 1, 2023

Oh good call, I guess we should have some generic way to tell it to ignore the contents of a block or tag 🤔

@elia elia added the looking for contributions This issue was accepted and is looking for a pull-request label Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
looking for contributions This issue was accepted and is looking for a pull-request
Projects
None yet
Development

No branches or pull requests

2 participants