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

Case statements not working correctly #4

Closed
MichaelHoste opened this issue Dec 7, 2021 · 3 comments
Closed

Case statements not working correctly #4

MichaelHoste opened this issue Dec 7, 2021 · 3 comments

Comments

@MichaelHoste
Copy link

We found some issues with case/when statements when parsing ERB templates:

What doesn't work

erb_content = <<~EOS
  <% case 1 %>
  <% when 1 %>foo!
  <% end %>
EOS

src = ERB.new(erb_content).src

# #coding:UTF-8
# _erbout = +'';  case 1 ; _erbout.<< "\n".freeze
# ;  when 1 ; _erbout.<< "foo!\n".freeze
# ;  end ; _erbout.<< "\n".freeze
# ; _erbout

eval(src)

# (eval):2: syntax error, unexpected local variable or method, expecting `when' (SyntaxError)
# ...erbout = +'';  case 1 ; _erbout.<< "\n".freeze
# ...                        ^~~~~~~
# (eval):3: syntax error, unexpected `when', expecting end-of-input
# ;  when 1 ; _erbout.<< "foo!\n".free...
#    ^~~~

What works

erb_content = <<~EOS
  <% case 1 
  when 1 %>foo!
  <% end %>
EOS

src = ERB.new(erb_content).src

# #coding:UTF-8
# _erbout = +'';  case 1 
# when 1 ; _erbout.<< "foo!\n".freeze
# ;  end ; _erbout.<< "\n".freeze
# ; _erbout

eval(src)

# "foo!\n\n"

It was tested on Ruby 3.0.3 and 2.7.2

@k0kubun
Copy link
Member

k0kubun commented Dec 7, 2021

I'm not sure if we've ever supported it. Unlike Haml, ERB doesn't do anything clever whether you embed case-when or not. You have to carefully write your template or choose a proper option to make it work. e.g. This should work:

erb_content = <<~EOS
  <% case 1 %>
  <% when 1 %>foo!
  <% end %>
EOS

src = ERB.new(erb_content, trim_mode: '>').src

@k0kubun k0kubun closed this as completed Dec 7, 2021
@MichaelHoste
Copy link
Author

Thank you for looking at this.

@sandstrom
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants