From b37e4015285e3080aa2276bf348759ebfdded676 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Wed, 24 Dec 2025 17:06:50 +0900 Subject: [PATCH 1/2] Support ERB native comment fixes: #350 --- lib/rufo/erb_formatter.rb | 6 +++++- spec/lib/rufo/erb_formatter_spec.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/rufo/erb_formatter.rb b/lib/rufo/erb_formatter.rb index 0bc56dbe..76f43f10 100644 --- a/lib/rufo/erb_formatter.rb +++ b/lib/rufo/erb_formatter.rb @@ -34,7 +34,7 @@ def initialize(code, **options) def format out = [] process_erb do |(type, content)| - if type == :code + if type == :code && !erb_comment?(content) formatted_code = process_code(content) indented_code = formatted_code.lines.join(" " * current_column) out << " #{indented_code} " @@ -176,4 +176,8 @@ def enable_code_mode def disable_code_mode @code_mode = false end + + def erb_comment?(code) + code.start_with?('#') + end end diff --git a/spec/lib/rufo/erb_formatter_spec.rb b/spec/lib/rufo/erb_formatter_spec.rb index e45e20e3..67f64fa2 100644 --- a/spec/lib/rufo/erb_formatter_spec.rb +++ b/spec/lib/rufo/erb_formatter_spec.rb @@ -89,5 +89,15 @@ result = subject.format("<%=yield%>") expect(result).to eql("<%= yield %>") end + + it 'handles native erb comments' do + result = subject.format('<%# locals: (item:, variant:) %>') + expect(result).to eql("<%# locals: (item:, variant:) %>") + end + + it 'handles ruby comments' do + result = subject.format('<% # TODO: fix this later %>') + expect(result).to eql("<% # TODO: fix this later %>") + end end end From 9ec4998033bf0837567a4ce72db249167b174882 Mon Sep 17 00:00:00 2001 From: Kazuki Nishikawa Date: Wed, 24 Dec 2025 17:09:31 +0900 Subject: [PATCH 2/2] format with rufo --- lib/rufo/erb_formatter.rb | 2 +- spec/lib/rufo/erb_formatter_spec.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/rufo/erb_formatter.rb b/lib/rufo/erb_formatter.rb index 76f43f10..49cd390d 100644 --- a/lib/rufo/erb_formatter.rb +++ b/lib/rufo/erb_formatter.rb @@ -178,6 +178,6 @@ def disable_code_mode end def erb_comment?(code) - code.start_with?('#') + code.start_with?("#") end end diff --git a/spec/lib/rufo/erb_formatter_spec.rb b/spec/lib/rufo/erb_formatter_spec.rb index 67f64fa2..9ad8816c 100644 --- a/spec/lib/rufo/erb_formatter_spec.rb +++ b/spec/lib/rufo/erb_formatter_spec.rb @@ -90,13 +90,13 @@ expect(result).to eql("<%= yield %>") end - it 'handles native erb comments' do - result = subject.format('<%# locals: (item:, variant:) %>') + it "handles native erb comments" do + result = subject.format("<%# locals: (item:, variant:) %>") expect(result).to eql("<%# locals: (item:, variant:) %>") end - it 'handles ruby comments' do - result = subject.format('<% # TODO: fix this later %>') + it "handles ruby comments" do + result = subject.format("<% # TODO: fix this later %>") expect(result).to eql("<% # TODO: fix this later %>") end end