diff --git a/lib/herb/engine.rb b/lib/herb/engine.rb index 69a9f8ee0..ce962d151 100644 --- a/lib/herb/engine.rb +++ b/lib/herb/engine.rb @@ -287,13 +287,9 @@ def add_expression_block_end(code, escaped: false) @src.chomp! if @src.end_with?("\n") && code_stripped.start_with?(" ") @src << " " << code_stripped + @src << "\n" if self.class.comment?(code_stripped) @src << (escaped ? "))" : ")") - - @src << if code.include?("#") || trailing_newline - "\n" - else - ";" - end + @src << (trailing_newline ? "\n" : ";") @buffer_on_stack = false end diff --git a/test/engine/engine_block_test.rb b/test/engine/engine_block_test.rb index 641af9e44..417fca2b4 100644 --- a/test/engine/engine_block_test.rb +++ b/test/engine/engine_block_test.rb @@ -107,5 +107,11 @@ class EngineBlockTest < Minitest::Spec assert_compiled_snapshot(template) end + + test "inline comment on end inside output block does not break parens" do + template = "<%= render Foo.new do %>hello<% end # comment %>" + + assert_compiled_snapshot(template) + end end end diff --git a/test/snapshots/engine/engine_block_test/test_0014_inline_comment_on_end_inside_output_block_does_not_break_parens_1c140392798ee0e334ac05bafcebe13b.txt b/test/snapshots/engine/engine_block_test/test_0014_inline_comment_on_end_inside_output_block_does_not_break_parens_1c140392798ee0e334ac05bafcebe13b.txt new file mode 100644 index 000000000..fee742d04 --- /dev/null +++ b/test/snapshots/engine/engine_block_test/test_0014_inline_comment_on_end_inside_output_block_does_not_break_parens_1c140392798ee0e334ac05bafcebe13b.txt @@ -0,0 +1,7 @@ +--- +source: "Engine::EngineBlockTest#test_0014_inline comment on end inside output block does not break parens" +input: "{source: \"<%= render Foo.new do %>hello<% end # comment %>\", options: {}}" +--- +_buf = ::String.new; _buf << (render Foo.new do; _buf << 'hello'.freeze; end # comment +); +_buf.to_s