diff --git a/lib/rufo/erb_formatter.rb b/lib/rufo/erb_formatter.rb index 2509c619..fb9d9d53 100644 --- a/lib/rufo/erb_formatter.rb +++ b/lib/rufo/erb_formatter.rb @@ -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 @@ -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 diff --git a/spec/lib/rufo/erb_formatter_spec.rb b/spec/lib/rufo/erb_formatter_spec.rb index f00ed0ba..c7fbfa21 100644 --- a/spec/lib/rufo/erb_formatter_spec.rb +++ b/spec/lib/rufo/erb_formatter_spec.rb @@ -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:) %>") @@ -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