r7kamura / rubocop-erb

RuboCop plugin for ERB template.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Leading/Trailing whitespace not detected

Earlopain opened this issue · comments

Thanks for working on this, it's nice to finally see this integrated properly into rubocop.

I have the following example code <% page_title "Test" %> but no offences are detected. I don't know if this a limitation of how the source code is extracted, but it would be nice for this to be detectable.

Thank you for trying this gem so quickly 🙌

Internally, code like ' page_title "Test" '' would be inspected by RuboCop in that case, so I think we can achieve this by having a dedicated cop on rubocop-erb side for checking leading and trailing whitespaces in erb.

I would consider adding the following cops:

  • Erb/LeadingWhitespace
  • Erb/TrailingWhitespace

The reason why we can't inspect it by Layout/InitialIndentation and Layout/TrailingWhitespace cops is that we need to be able to set up a detailed preference for which erb code is preferred, such as:

<%page_title "Test"%>
<% page_title "Test" %>
<%    page_title "Test"    %>

I can take a look at how these two cops could be implemented. Just to make sure, you want configuration for the amount of allowed whitespace?

I tried with #9, but the current mechanism does not seem to be able to achieve this, so I have decided to give up on this feature.
Template languages like ERB will embed Ruby code fragments that are invalid on their own. e.g. if a in <% if a %> is invalid on its own. So to take care of this we have narrowed the scope of code to be checked (e.g. if a -> a). This mechanism prevents cops from accurately detecting embedded code, since only a portion of the code may be passed to cops, but cops cannot detect it.

In addition, plugins for template languages such as rubocop-erb, rubocop-haml, and rubocop-slim intend to inspect only the embedded Ruby code and not the syntax of the template language. The whitespace that is the subject of discussion is not a Ruby syntax but an area of ERB syntax, so I think it is better not to support this part as well for consistency.