Skip to content

Commit 6aba510

Browse files
Fix ERB expression compilation when code contains a heredoc
When an ERB output tag wraps code in parentheses and that code contains a heredoc (e.g. `method_call <<~GRAPHQL, variables`), the closing parenthesis must appear on its own line after the heredoc terminator. Without this, the compiled Ruby is syntactically invalid. Includes a snapshot test for the compiled output. Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID: https://ampcode.com/threads/T-019c7333-8beb-715a-9183-8434cd5fb15a
1 parent f0aa35a commit 6aba510

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

lib/reactionview/template/handlers/herb/herb.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def add_rails_expression(indicator, code, wrap_parentheses:)
6666
end
6767

6868
if wrap_parentheses
69-
@src << "(" << code << ")"
69+
@src << "(" << code
70+
@src << "\n" if code.match?(/<<[~-]?\s*['"`]?\w/)
71+
@src << ")"
7072
else
7173
@src << " " << code
7274
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@output_buffer.append=(method_call <<~GRAPHQL, variables
2+
query {
3+
field
4+
}
5+
GRAPHQL
6+
);
7+
@output_buffer.safe_append='
8+
'.freeze;@output_buffer

test/template/handlers/herb_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,17 @@ def @view_context.ui_badge(count, **_options)
342342
assert_compiled_snapshot(template)
343343
assert_evaluated_snapshot(template, ivars: { config: { key: "value" } })
344344
end
345+
346+
test "heredoc with trailing arguments compiles to valid Ruby" do
347+
template = <<~ERB
348+
<%= method_call <<~GRAPHQL, variables
349+
query {
350+
field
351+
}
352+
GRAPHQL
353+
%>
354+
ERB
355+
356+
assert_compiled_snapshot(template)
357+
end
345358
end

0 commit comments

Comments
 (0)