Skip to content

Commit 33b7576

Browse files
committed
fix trailing comment bug
1 parent aa12f6f commit 33b7576

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

lib/herb/engine.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,25 @@ def add_expression_block_end(code, escaped: false)
286286

287287
@src.chomp! if @src.end_with?("\n") && code_stripped.start_with?(" ")
288288

289-
@src << " " << code_stripped
290-
@src << (escaped ? "))" : ")")
291-
292-
@src << if code.include?("#") || trailing_newline
293-
"\n"
294-
else
295-
";"
296-
end
289+
has_comment = code.include?("#")
290+
closing = escaped ? "))" : ")"
291+
292+
if has_comment
293+
comment_index = code_stripped.index("#")
294+
code_part = code_stripped[0...comment_index].rstrip
295+
comment_part = code_stripped[comment_index..]
296+
297+
@src << " " << code_part << closing << " " << comment_part << "\n"
298+
else
299+
@src << " " << code_stripped
300+
@src << closing
301+
302+
@src << if trailing_newline
303+
"\n"
304+
else
305+
";"
306+
end
307+
end
297308

298309
@buffer_on_stack = false
299310
end

test/engine/engine_block_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,11 @@ class EngineBlockTest < Minitest::Spec
107107

108108
assert_compiled_snapshot(template)
109109
end
110+
111+
test "inline comment on end inside output block does not break parens" do
112+
template = '<%= render Foo.new do %>hello<% end # comment %>'
113+
114+
assert_compiled_snapshot(template)
115+
end
110116
end
111117
end

test/snapshots/engine/engine_block_test/test_0014_inline_comment_on_end_inside_output_block_does_not_break_parens_1c140392798ee0e334ac05bafcebe13b.txt

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)