Skip to content

Commit

Permalink
Prevent the transformation of multiline do-end blocks to single-line …
Browse files Browse the repository at this point in the history
…blocks with curly braces by syntax_tree
  • Loading branch information
i-tsvetkov authored and elia committed Jul 7, 2023
1 parent 9645bf3 commit 72d613e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/erb/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,12 @@ def format_text(text)

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

code += "\n#{placeholder}\nend" unless RUBY_OPEN_BLOCK["#{code}\nend"]
code += "\n#{placeholder}\n}" unless RUBY_OPEN_BLOCK["#{code}\n}"]
end

p RUBY_IN_: code if @debug
Expand All @@ -229,8 +233,8 @@ def format_ruby(code, autoclose: false, extra_indent: 0)
code
end

code = code.split(placeholder).first.rstrip if autoclose
lines = code.lines(chomp: true)
lines.delete_at(-1) if autoclose

indent = "\s" * extra_indent

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/short-do-end-block.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% [1, 2, 3].each do |i| %>
<%= i %>
<% end %>
3 changes: 3 additions & 0 deletions test/fixtures/short-do-end-block.html.expected.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% [1, 2, 3].each do |i| %>
<%= i %>
<% end %>

0 comments on commit 72d613e

Please sign in to comment.