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

Preprocessor to support .html.erb files (Ruby on Rails, ERB) #223

Open
Splines opened this issue Oct 30, 2024 · 2 comments
Open

Preprocessor to support .html.erb files (Ruby on Rails, ERB) #223

Splines opened this issue Oct 30, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@Splines
Copy link

Splines commented Oct 30, 2024

Hey there, thanks so much for creating this tool. I'm wondering how all the people programmed HTML all those years without a proper HTML linter? Like as if the W3C Validator is one of the top results when I search for "HTML linter" on the web 😂


I'd like to lint the HTML part of .html.erb files. ERB is a templating language used in Ruby on Rails projects. In order to lint .js.erb files, I've written a custom ESLint plugin, which is essentially a preprocessor (and then also a postprocessor) to strip away all the ERB parts in the code (replacing them with a dummy comment), while keeping track of the positions to correctly map line and colon numbers of ESLint messages. Here's what you can achieve with it (image should be animated):

293907467-623d6007-b4f5-41ce-be76-5bc0208ed636

I was wondering if your project also supports some kind of mechanism such that I could use this preprocessor again (maybe with only slight changes) for .html.erb files. The recommended ESLint configuration basically just declares the processor.

This would allow me to lint the HTML part of these files. I would then use a tool like ERB Lint to lint the ERB parts.

@Splines Splines changed the title Preprocessor to support .html.erb files Preprocessor to support .html.erb files (Ruby on Rails, ERB) Oct 30, 2024
@yeonjuan yeonjuan added the enhancement New feature or request label Nov 1, 2024
@yeonjuan
Copy link
Owner

yeonjuan commented Nov 1, 2024

@Splines hello. Thanks for the suggestion, I agree with adding a preprocessor. However, I am not very familiar with ERB, so if I were to implement it, it might take some time. I will refer to the code you shared. Thank you.

@Splines
Copy link
Author

Splines commented Dec 15, 2024

Hey, I noticed that I didn't have too much work to do to support html-eslint with my processor. So I'm happy to announce that with my new release 2.1.0, you can now also lint .html.erb files with it 🙌

eslint-html-erb


Keep in mind that it comes with the same caveats like the JS linting with my processor, i.e.

<%= if @var %>
  <div class="foo" data-foo="bar">
    <p>Hello</p>
  </div>
<% end %>

will be autocorrected to

<%= if @var %>
<div class="foo" data-foo="bar">
  <p>Hello</p>
</div>
<% end %>

But this is something I can live with and it motivates to outsource more logic to Rails helpers or to do more logic in Rails in general ;)

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

2 participants