Skip to content
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

Ruby: ERB parse failure with trailing implicit keyword argument #16006

Open
aibaars opened this issue Mar 21, 2024 · 1 comment
Open

Ruby: ERB parse failure with trailing implicit keyword argument #16006

aibaars opened this issue Mar 21, 2024 · 1 comment
Labels
acknowledged GitHub staff acknowledges this issue bug Something isn't working Ruby

Comments

@aibaars
Copy link
Contributor

aibaars commented Mar 21, 2024

The following example causes a syntax error, but it shouldn't. I think it is due to the way CodeQL interprets ERB. Instead of inserting newlines it may be better for CodeQL to insert explicit semicolons in most cases. There is not really a right way to interpret ERB, because ERB has no specification and there are many ERB interpreters each with their own flavours. We should probably emulate the most often used Rails flavour as closely as possible.

<% def foo bar:; end %>
<% bar = 1 %>
<%= foo bar:  %> 
<% print "hello" %>

What's happening is roughly, that the above gets parsed as follows (this is also invalid according to the real Ruby interpreter)

def foo bar:; end 
bar = 1
foo bar:
print "hello"

instead of as (note the extra ;)

def foo bar:; end 
bar = 1
foo bar: ;
print "hello"

The ERB interpreter of Rails usually inserts ; symbols between tags, but sometimes it inserts a newline (to make using blocks easier). CodeQL instead always inserts a newline. This difference is hardly ever a problem because Ruby treats newlines as implicit ; symbols in almost all cases. The trouble is the "almost", there are a few corner cases, and newlines after a keyword argument are one of them.

@aibaars aibaars added bug Something isn't working Ruby labels Mar 21, 2024
@Malikrehman00107
Copy link

you can try to adjust your ERB templates to work with CodeQL's interpretation or provide explicit semicolons where needed to ensure compatibility.

<% def foo(bar:); end %>
<% bar = 1 %>
<%= foo(bar:) %>
<% print "hello" %>

@sidshank sidshank added the acknowledged GitHub staff acknowledges this issue label Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
acknowledged GitHub staff acknowledges this issue bug Something isn't working Ruby
Projects
None yet
Development

No branches or pull requests

3 participants