Skip to content

Transform multi-line ERB Comment to multi-line Ruby comment when extracting Ruby code in herb_extract_ruby #102

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

Open
marcoroth opened this issue May 3, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@marcoroth
Copy link
Owner

In #98 we skipped the extract of ERB Comments when extracting Ruby code, but ideally we would also convert the ERB comment to Ruby comments in all cases.

Given this source:

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

We would want it to extract the following Ruby code:

  
# This is 
# a comment
# over multiple
# lines

and/or:

=begin
  This is 
  a comment
  over multiple
  lines
=end
@marcoroth marcoroth added the enhancement New feature or request label May 3, 2025
@marcoroth marcoroth changed the title Transform multi-line ERB Comment to multi-line Ruby comment when extract Ruby code in herb_extract_ruby Transform multi-line ERB Comment to multi-line Ruby comment when extracting Ruby code in herb_extract_ruby May 3, 2025
marcoroth added a commit that referenced this issue May 3, 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:

```html+erb
<%# This is a comment %>
<h1><%= title %></h1>
```

Was extracted to Ruby code as:

```ruby
  # This is a comment   
        title        
```

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

```ruby
                        
        title  
```        

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

```html+erb
<%#
  This is 
  a comment
  over multiple
  lines
%>
```

Which before this pull request was extracted to Ruby as:

```ruby
  #
  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:

```ruby
  
# This is 
# a comment
# over multiple
# lines

```

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

```ruby
=begin
  This is 
  a comment
  over multiple
  lines
=end
```

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

```html+erb
<% if true %><%# Comment here %><% end %>
```

Which is going to comment out the `end` as well:
```ruby
   if true     # Comment here      end  
```

This use-case is also fixed with this pull request, since we just skip
over the ERB Comments content:
```ruby
   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.

```html+erb
<% 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":
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

1 participant