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

.erb format support #371

Closed
AlecRust opened this issue Aug 8, 2019 · 15 comments
Closed

.erb format support #371

AlecRust opened this issue Aug 8, 2019 · 15 comments
Labels
enhancement New feature or request

Comments

@AlecRust
Copy link

AlecRust commented Aug 8, 2019

Wondering if there's any plans for this plugin to support .erb format?

Prettier already supports .html format but if passed .html.erb it fails.

@kddnewton
Copy link
Member

Yeah absolutely! Still working on how to make that work exactly. Currently there isn't a lot of support for templating languages in general. There's some discussion going on w/r/t prettier as a whole, so I'm following that.

@nPaul
Copy link

nPaul commented Sep 25, 2019

You may bind types "*.html.erb" as "html"
its works for vim

but erb tags like
<% if condition %>
<%= foo %>
<%= foo %>
<%= foo %>
<% end %>

will formatting to
<% if condition %> <%= foo %> <%= foo %> <%= foo %> <% end %>

@kddnewton kddnewton pinned this issue Nov 6, 2019
@kddnewton
Copy link
Member

Okay I dug into this a ton today and here's what I've determined: ERB support is going to be hard.

There are a couple of options, I'm going to lay them out here:

  1. Use regex and pray. I'm not entirely kidding. I've messed around with this approach (you can see the work on the erb branch). Basically the idea is to replace ERB tags with valid HTML that HTML expects in each node location. You can see that on the erb branch right now where it supports generic tags. It transforms them from <%= foo %> into <prettier> ... ast ... </prettier>. It then replaces that pattern on the printing side. This approach is probably fraught with peril.
  2. Extend https://github.com/ikatyang/angular-html-parser to support ERB tags. That's... possible, but I'm not entirely sure it's advisable. I don't have a hankering to tack on something to that parser that really doesn't belong there. From a cursory glance it doesn't look like it supports plugins or anything like that (like babel). Not sure how to proceed with that approach.
  3. Write our own ERB parser. Believe it or not, this doesn't actually seem impossible. It already kind of exists with https://github.com/Shopify/better-html. I'm not sure of how feasible it would be to use it, but it definitely seems like an option.

Regardless, I'm going to table this issue for now. I just don't have the bandwidth to work on it right now. I'd be super happy if anyone wanted to really dig into this - I'm sure it would be a rewarding endeavor. Hopefully I'll be able to come back to it at some point.

@kddnewton kddnewton unpinned this issue Nov 6, 2019
@kddnewton kddnewton added the enhancement New feature or request label Nov 6, 2019
@kddnewton
Copy link
Member

Just for bookkeeping purposes I'm going to close this issue. I talk a little about it in the wiki https://github.com/prettier/plugin-ruby/wiki#do-you-support-erb-files-htmlerb-jserb-etc, and I'm talking to prettier core members about it. Hopefully we'll have something to look at in the future.

@bbugh
Copy link

bbugh commented Dec 20, 2019

@kddeisz that link is dead (the wiki seems to be inaccessible or gone). Did you post it anywhere else?

@vvo
Copy link

vvo commented Dec 22, 2019

@bbugh it's here: https://github.com/prettier/plugin-ruby/blob/master/CONTRIBUTING.md#do-you-support-erb-files-htmlerb-jserb-etc I think it was a mistake.

@ghost
Copy link

ghost commented Oct 6, 2020

Would still love to see this in Prettier. It would allow us to reduce the number of formatters we have in our project by 1 which is always great.

@kddnewton
Copy link
Member

kddnewton commented Oct 6, 2020 via email

@ghost
Copy link

ghost commented Oct 6, 2020 via email

@kddnewton
Copy link
Member

kddnewton commented Oct 6, 2020 via email

@chrisw-
Copy link

chrisw- commented Oct 12, 2020

Just FYI, I use https://github.com/threedaymonk/htmlbeautifier which works ok.

@ianks
Copy link
Contributor

ianks commented Nov 19, 2020

I think it's really close actually! It does not seem to be breaking our HTML at all, just a few small styling nits and i think this could be shipped.

Formatting Inline Ruby

A: If we format a file with this content:

<%= render partial: 'shared/search_header', locals: { search_url: routes.retailers_path } %>

<%= form %>

B: Prettier will output:

<%= render partial: 'shared/search_header', locals: { search_url: routes.retailers_path } %> <%= form %>

This isn't wrong; it's fully syntactically valid. However, I think the A output looks better. Would it make sense to treat the inline ruby <% tags the same way as a br /> is treated, so it will keep the line breaks?

@chrisw-
Copy link

chrisw- commented Feb 9, 2021

https://github.com/adamzapasnik/prettier-plugin-erb

@kevinhq
Copy link

kevinhq commented Feb 22, 2021

https://github.com/adamzapasnik/prettier-plugin-erb

not working on vim. not sure what's wrong.

@Juice10
Copy link

Juice10 commented Oct 4, 2022

https://github.com/adamzapasnik/prettier-plugin-erb currently doesn't support prettier 2.3+ which is kind of a deal breaker for me unfortunately.
More info: adamzapasnik/prettier-plugin-erb#49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

9 participants