From d8b12d131c05918d9a3e94b84561c76fad1ef4d4 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Wed, 24 Dec 2025 18:03:03 +0900 Subject: [PATCH 1/4] Add test cases that causes an error in ERB formatting --- spec/lib/rufo/erb_formatter_spec.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/spec/lib/rufo/erb_formatter_spec.rb b/spec/lib/rufo/erb_formatter_spec.rb index 9ad8816c..fca626c6 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:) %>") @@ -99,5 +109,20 @@ result = subject.format("<% # TODO: fix this later %>") expect(result).to eql("<% # TODO: fix this later %>") 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 From a78ab231b30698a5bb50d5b9c3145388f1de86da Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Wed, 24 Dec 2025 18:07:28 +0900 Subject: [PATCH 2/4] format --- spec/lib/rufo/erb_formatter_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/lib/rufo/erb_formatter_spec.rb b/spec/lib/rufo/erb_formatter_spec.rb index fca626c6..8db16d52 100644 --- a/spec/lib/rufo/erb_formatter_spec.rb +++ b/spec/lib/rufo/erb_formatter_spec.rb @@ -110,7 +110,7 @@ expect(result).to eql("<% # TODO: fix this later %>") end - it 'handles case/when expression' do + it "handles case/when expression" do result = subject.format(<<~ERB) <% case a+b %> <% when c %> @@ -118,11 +118,11 @@ <% end %> ERB expect(result).to eql(<<~ERB) - <% case a + b %> - <% when c %> - <%= d + e %> - <% end %> - ERB + <% case a + b %> + <% when c %> + <%= d + e %> + <% end %> + ERB end end end From c582ed7971f8bcd5bff4476c7fe2acdd146a411d Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Thu, 28 May 2026 21:10:41 +0900 Subject: [PATCH 3/4] Fix ERB formatting for yield with args and case/when blocks Two fixes for code that previously raised SyntaxError in ErbFormatter: - yield with multiple args / parens: Ripper.sexp returns nil for these at top level, so process_code fell through to determine_code_wrappers and then to raise_syntax_error!. raise_syntax_error! calls format_code expecting it to raise, but Rufo::Formatter handles yield via sexp_unparsable_code and instead returned a formatted string, which was then mis-assigned as `prefix` and produced corrupt input. Use Rufo::Parser.sexp_unparsable_code as the Ripper.sexp fallback so these forms skip the wrapper logic entirely. - `case EXPR` alone (split across ERB tags): none of the existing wrappers make a bare case header parse, since case requires a when clause. Add a `\nwhen nil\nend` suffix wrapper so the header tag is parseable in isolation. --- lib/rufo/erb_formatter.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From b10fa7a0af0216b6b6488055c73984ae9c042c5f Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Thu, 28 May 2026 21:12:28 +0900 Subject: [PATCH 4/4] run rufo --- spec/lib/rufo/erb_formatter_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/lib/rufo/erb_formatter_spec.rb b/spec/lib/rufo/erb_formatter_spec.rb index 9951ec88..c7fbfa21 100644 --- a/spec/lib/rufo/erb_formatter_spec.rb +++ b/spec/lib/rufo/erb_formatter_spec.rb @@ -138,11 +138,11 @@ <% end %> ERB expect(result).to eql(<<~ERB) - <% case a + b %> - <% when c %> - <%= d + e %> - <% end %> - ERB + <% case a + b %> + <% when c %> + <%= d + e %> + <% end %> + ERB end end end