seattlerb / ruby_parser

ruby_parser is a ruby parser written in pure ruby. It outputs s-expressions which can be manipulated and converted back to ruby via the ruby2ruby gem.
http://www.zenspider.com/projects/ruby_parser.html
475 stars 102 forks source link

Support for comments inside ERB #345

Closed johansenja closed 3 months ago

johansenja commented 3 months ago

Background

Issue

This code:

<script>
  // ok
  const a = <%= 1 %>;

  // not ok
  const b = <%= 1 # hello %>;

  // ok
  <% if true %>;
    const c = true;
  <% end%>

  // not ok
  <% if true # hello %>;
    const d = true;
  <% end%>
</script>

leads to this parsing error when running brakeman

== Errors ==

Error: app/views/comments_in_erb.html.erb:8 :: parse error on value "ok" (tIDENTIFIER) Could not parse app/views/comments_in_erb.html.erb
Location: Could not parse <snip>/app/views/comments_in_erb.html.erb

Expected behaviour

I would expect %> after a # to terminate the comment and the tag; currently it seems like %> is parsed as being part of the comment

Working reproduction

You can see the issue here from this repo if you check it out and run brakeman - but sorry I don't have a non brakeman-specific reproduction https://github.com/johansenja/brakeman-erb-test/

johansenja commented 3 months ago

Closing because it actually is a syntax error and my editor misled me 😶‍🌫️ 🤦🏻 Screenshot 2024-06-13 at 16 53 52