Skip to content

Don't extract ERB comments as Ruby comments in herb_extract_ruby#98

Merged
marcoroth merged 3 commits intomainfrom
multiline-erb-comments
May 3, 2025
Merged

Don't extract ERB comments as Ruby comments in herb_extract_ruby#98
marcoroth merged 3 commits intomainfrom
multiline-erb-comments

Conversation

@marcoroth
Copy link
Owner

@marcoroth marcoroth commented May 2, 2025

This pull request updates the herb_extract_ruby and herb_extract_ruby_to_buffer_with_semicolons methods in extract.c to not extract the content of ERB Comment Nodes (<%#) as Ruby code.

So a source file like this:

<%# This is a comment %>
<h1><%= title %></h1>

Was extracted to Ruby code as:

  # This is a comment   
        title        

With the changes included in this pull request it's going to be:

                        
        title  

This is in order to resolve #91. It's valid to have multi-line ERB Comments like:

<%#
  This is 
  a comment
  over multiple
  lines
%>

Which before this pull request was extracted to Ruby as:

  #
  This is 
  a comment
  over multiple
  lines

Which is not a valid Ruby comment anymore, but treated as actual Ruby code from the second line on. If the comment itself included Ruby keywords it would cause syntax errors.

For now, we don't extract the ERB Comments at all - which is the change this pull request introduces.

In the future, we can implement #100 (and/or #102) and also make sure that multi-line ERB Comments get extracted to multi-line Ruby Comments, like:

  
# This is 
# a comment
# over multiple
# lines

or maybe even cleverer: replace the <% with a =begin and the %> with a =end:

=begin
  This is 
  a comment
  over multiple
  lines
=end

Another case where it would break Ruby syntax is in this example:

<% if true %><%# Comment here %><% end %>

Which is going to comment out the end as well:

   if true     # Comment here      end  

This use-case is also fixed with this pull request, since we just skip over the ERB Comments content:

   if true                         end   

This last example could be solved even more elegantly if Ruby shipped the Inline Comments feature:
https://bugs.ruby-lang.org/issues/20405

This following example is still broken and this pull request does not address that use-case. I opened #101 for this.

<% if true %><% # Comment here %><% end %>

Currently it does not address the case, where the comment is part of the Ruby Code itself, so the comment is not seen as a "ERB Comment Node":

@marcoroth marcoroth marked this pull request as ready for review May 3, 2025 09:55
@marcoroth marcoroth changed the title Don't extract ERB Comments as Ruby code Don't extract ERB Comments as Ruby code in herb_extract_ruby May 3, 2025
@marcoroth marcoroth changed the title Don't extract ERB Comments as Ruby code in herb_extract_ruby Don't extract ERB Comments as Ruby comments in herb_extract_ruby May 3, 2025
@marcoroth marcoroth changed the title Don't extract ERB Comments as Ruby comments in herb_extract_ruby Don't extract ERB comments as Ruby comments in herb_extract_ruby May 3, 2025
@marcoroth marcoroth merged commit 9ee04ae into main May 3, 2025
9 checks passed
@marcoroth marcoroth deleted the multiline-erb-comments branch May 3, 2025 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multiline ERB Comment Nodes are being parsed as Ruby Code

1 participant