Skip to content

Commit 8d964bf

Browse files
authored
Engine: Inline comments on <% end %> on output blocks produce invalid Ruby (#1363)
## Version herb 0.8.10 (commit `3a36f815b05b`) ## Summary When `<% end # comment %>` closes a block opened inside a `<%= %>` output tag, Herb appends the closing `))` *after* the comment text in compiled Ruby. Since `#` starts a Ruby comment, the `))` become part of the comment, leaving `__herb.h((` unclosed. ## Minimal reproduction ```erb <%= render Foo.new do %> hello <% end # comment %> ``` ### Actual compiled Ruby (broken) ```ruby _buf << __herb.h((render Foo.new do; _buf << ' hello '.freeze; end # comment )) ``` The `))` after `# comment` is commented out — parens never close. ### Expected The `))` should be placed *before* the comment, or the comment should be stripped: ```ruby _buf << __herb.h((render Foo.new do; _buf << ' hello '.freeze; end )) # comment ``` ### Prism parse errors ``` - unexpected end-of-input, assuming it is closing the parent top level context (line 4) - expected a matching `)` (line 5) - unexpected end-of-input; expected a `)` to close the arguments (line 4) ``` ## Scope - **Affected:** `<% end # ... %>` closing a block started in `<%= %>` (where Herb wraps in `__herb.h((...))`) - **Not affected:** `<% end # ... %>` inside non-output `<% %>` tags — no wrapping parens, so comment is harmless ## Workaround Remove inline comments from `end` tags that close output-tag blocks. --------- Signed-off-by: Joel Hawksley <joelhawksley@github.com>
1 parent aa12f6f commit 8d964bf

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

lib/herb/engine.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,9 @@ def add_expression_block_end(code, escaped: false)
287287
@src.chomp! if @src.end_with?("\n") && code_stripped.start_with?(" ")
288288

289289
@src << " " << code_stripped
290+
@src << "\n" if self.class.comment?(code_stripped)
290291
@src << (escaped ? "))" : ")")
291-
292-
@src << if code.include?("#") || trailing_newline
293-
"\n"
294-
else
295-
";"
296-
end
292+
@src << (trailing_newline ? "\n" : ";")
297293

298294
@buffer_on_stack = false
299295
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: 7 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)