Skip to content

Commit 314f847

Browse files
committed
Prevent the transformation of multiline do-end blocks to single-line blocks with curly braces by syntax_tree
1 parent 797550a commit 314f847

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

lib/erb/formatter.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,11 @@ def format_code_with_rubocop(code, line_width)
268268

269269
def format_ruby(code, autoclose: false, extra_indent: 0)
270270
if autoclose
271-
code += "\nend" unless ERB_OPEN_BLOCK["#{code}\nend"]
272-
code += "\n}" unless ERB_OPEN_BLOCK["#{code}\n}"]
271+
# A long placeholder is added to prevent the transformation of
272+
# multiline do-end blocks to single-line blocks with curly braces by syntax_tree.
273+
placeholder = "placeholder_#{SecureRandom.hex(@line_width / 2)}"
274+
code += "\n#{placeholder}\nend" unless ERB_OPEN_BLOCK["#{code}\nend"]
275+
code += "\n#{placeholder}\n}" unless ERB_OPEN_BLOCK["#{code}\n}"]
273276
end
274277

275278
p RUBY_IN_: code if @debug
@@ -279,8 +282,8 @@ def format_ruby(code, autoclose: false, extra_indent: 0)
279282
@line_width - (indentation_width + extra_indent)
280283
)
281284

285+
formatted = formatted.split(placeholder).first.rstrip if autoclose
282286
lines = formatted.lines(chomp: true)
283-
lines.delete_at(-1) if autoclose
284287

285288
indent = "\s" * extra_indent
286289

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<% [1, 2, 3].each do |i| %>
2+
<%= i %>
3+
<% end %>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<% [1, 2, 3].each do |i| %>
2+
<%= i %>
3+
<% end %>

0 commit comments

Comments
 (0)