Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/rufo/erb_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def process_erb
end

def process_code(code_str)
sexps = Ripper.sexp(code_str)
sexps = Ripper.sexp(code_str) || Rufo::Parser.sexp_unparsable_code(code_str)
if sexps.nil?
prefix, suffix = determine_code_wrappers(code_str)
end
Expand Down Expand Up @@ -157,6 +157,7 @@ def determine_code_wrappers(code_str)
return "begin\n", "\nend" if Ripper.sexp("begin\n#{code_str}\nend")
return "if a\n", "\nend" if Ripper.sexp("if a\n#{code_str}\nend")
return "case a\n", "\nend" if Ripper.sexp("case a\n#{code_str}\nend")
return nil, "\nwhen nil\nend" if Ripper.sexp("#{code_str}\nwhen nil\nend")
raise_syntax_error!(code_str)
end

Expand Down
25 changes: 25 additions & 0 deletions spec/lib/rufo/erb_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@
expect(result).to eql("<%= yield %>")
end

it "formats standalone 'yield' with arguments" do
result = subject.format("<%=yield x,y%>")
expect(result).to eql("<%= yield x, y %>")
end

it "formats standalone 'yield' with arguments and parens" do
result = subject.format("<%=yield(x,y)%>")
expect(result).to eql("<%= yield(x, y) %>")
end

it "handles native erb comments" do
result = subject.format("<%# locals: (item:, variant:) %>")
expect(result).to eql("<%# locals: (item:, variant:) %>")
Expand Down Expand Up @@ -119,5 +129,20 @@
result = subject.format("<% - x * y %>")
expect(result).to eql("<% -x * y %>")
end

it "handles case/when expression" do
result = subject.format(<<~ERB)
<% case a+b %>
<% when c %>
<%= d+e %>
<% end %>
ERB
expect(result).to eql(<<~ERB)
<% case a + b %>
<% when c %>
<%= d + e %>
<% end %>
ERB
end
end
end
Loading