Skip to content

Support for Inline Ruby Comments for when extracting Ruby code in herb_extract_ruby #101

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
bug Something isn't working

Comments

@marcoroth
Copy link
Owner

In #98 we updated the herb_extract_ruby method to skip over ERB Comment Nodes (<%#), which works great.

However for this variation it doesn't work, since it's treated as a regular ERB Content Node which has a Ruby comment inside of it:

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

We should find a way to also skip over this.

@marcoroth marcoroth added the bug Something isn't working label 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
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant