Templating Lint/Syntax error
Earlopain opened this issue · comments
I have this file from the simple_form
gem in my templates folder:
https://github.com/heartcombo/simple_form/blob/921ce9c3fc100e1737125ba1b5a2037c03f2de1b/lib/generators/simple_form/templates/_form.html.erb
<%%= simple_form_for(@<%= singular_table_name %>) do |f| %>
<%%= f.error_notification %>
<div class="form-inputs">
<%- attributes.each do |attribute| -%>
<%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> %>
<%- end -%>
</div>
<div class="form-actions">
<%%= f.button :submit %>
</div>
<%% end %>
which yields me this output:
lib/templates/erb/scaffold/_form.html.erb:1:4: E: Lint/Syntax: unexpected token tEQL
(Using Ruby 3.1 parser; configure using TargetRubyVersion parameter, under AllCops)
<%%= simple_form_for(@<%= singular_table_name %>) do |f| %>
^
lib/templates/erb/scaffold/_form.html.erb:2:6: E: Lint/Syntax: unexpected token tEQL
(Using Ruby 3.1 parser; configure using TargetRubyVersion parameter, under AllCops)
<%%= f.error_notification %>
^
lib/templates/erb/scaffold/_form.html.erb:6:8: E: Lint/Syntax: unexpected token tEQL
(Using Ruby 3.1 parser; configure using TargetRubyVersion parameter, under AllCops)
<%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> %>
^
lib/templates/erb/scaffold/_form.html.erb:11:8: E: Lint/Syntax: unexpected token tEQL
(Using Ruby 3.1 parser; configure using TargetRubyVersion parameter, under AllCops)
<%%= f.button :submit %>
I can exclude this file manually since I don't really care about linting this and the problem goes away. I'm not sure on the meaing of this syntax, but erubi has tests for this: https://github.com/jeremyevans/erubi/blob/7fedb8c9657a2ca3bd1269b6695a2ea059690d29/test/test.rb#L688
I've never seen this ERB format with two %
's before. rubocop-erb uses the better_html gem as ERB parser. This is probably because the better_html parser does not support this format (by default?), so it does not work as intended.
The ability to generate ERB template with this %%
format seems to have been added in the following commits.
If there is no particular reason to use %%
, how about sending a pull request to change %%
to %
?
I asked the question in an issue in the simple_form repository.
I am not familiar with ERB format, so it is possible that I am misunderstanding something...
Oh, I see now. Shopify/better-html@aa670a1
It escapes <%=
so you can have it in your output even after it got processed. It probably won't do to remove that syntax from the file.
I can use this file with rails g erb:scaffold Form
which outputs this, among other things:
<%= simple_form_for(@form) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
So the problem is that better_html's ERB parser doesn't support the <%% ... %%>
escapes?
Uh, no, it's not... the fact that better_html has that test means that it supports that escaping, so it may be that rubocop-erb is not handling the parsed results provided by better_html well. Even though some metadata is given to the parsed result nodes, rubocop-erb may be treating <% ... %>
and <%% ... %%>
the same.
s(:indicator, "%")
seems to signify that something is going on. I would probably ignore these lines, the same way that comments are already excluded.
erb.children.first&.to_a&.first&.in?('#', '%')
One could also try to remove the indicators from the result <%%= foo %%>
=> = foo %
, but I'm not sure if trying to do that would be worth it.
@Earlopain I fixed it in v0.2.4, can you please try it?
Works perfectly, thanks!