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

Fix parsing erb with bare yield #2444

Merged
merged 1 commit into from
Aug 14, 2024
Merged

Conversation

Earlopain
Copy link
Contributor

@Earlopain Earlopain commented Aug 14, 2024

Motivation

Some things that are valid in erb would not be valid in a standalone ruby file. This particularly impacts yield.
Instead treat the provided code as being evaled

Closes #2442

Implementation

Switch prism into eval mode. Prior work for the prism parser translator at ruby/prism#2741

Automated Tests

Yup

Manual Tests

I'd guess that this is neovim/editor specific since I'm not seeing anything for syntax errors in vscode but the added test does fail previously.

@Earlopain Earlopain requested a review from a team as a code owner August 14, 2024 15:04
@Earlopain Earlopain requested review from andyw8 and vinistock August 14, 2024 15:04
@andyw8 andyw8 added the bugfix This PR will fix an existing bug label Aug 14, 2024
@andyw8
Copy link
Contributor

andyw8 commented Aug 14, 2024

Are there any possible downsides to using eval mode here? (cc @kddnewton)

@st0012 st0012 added the server This pull request should be included in the server gem's release notes label Aug 14, 2024
Copy link
Member

@st0012 st0012 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. Let's add a comment to explain what it does as well.

lib/ruby_lsp/erb_document.rb Show resolved Hide resolved
Some things that are valid in erb would not be valid in a standalone ruby file.
This particularly impacts `yield`.
Instead treat the provided code as being evaled

Closes Shopify#2442
@kddnewton
Copy link
Contributor

Are there any possible downsides to using eval mode here? (cc @kddnewton)

It will also turn off unused local variable warnings and void statement warnings, which is probably fine.

@st0012 st0012 merged commit e18d456 into Shopify:main Aug 14, 2024
18 checks passed
@Earlopain Earlopain deleted the erb-yield-error branch August 14, 2024 16:57
@natematykiewicz
Copy link
Contributor

It will also turn off unused local variable warnings and void statement warnings, which is probably fine.

From what I've seen of using Rubocop on ERB, this is actually preferred, because if you assign a variable in one ERB block and use it in another, Rubocop thinks the variable is unused, because it checks each ERB block independently.

For example:

<% foos = [1,2,3] %>

<div>
  <% foos.each do |foo| %>
    <span><%= foo %></span>
  <% end %>
</div>

Rubocop via ERB Lint will complain on line 1 that foos is unused.

@vinistock
Copy link
Member

That wouldn't be the case for Kevin's comment though. We strip out the Ruby code from the ERB template and parse it with Prism as if it were a regular Ruby file, so in that particular example it would be able to tell you if the local variable was indeed unused inside the entire template.

We don't yet run RuboCop or ERB Lint on ERB files, so all of the existing diagnostics were coming from Prism itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix This PR will fix an existing bug server This pull request should be included in the server gem's release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Invalid yield" diagnostic in erb
6 participants