diff --git a/lib/erb/formatter.rb b/lib/erb/formatter.rb
index b4d49c4..76459e9 100644
--- a/lib/erb/formatter.rb
+++ b/lib/erb/formatter.rb
@@ -11,16 +11,6 @@
require 'syntax_tree'
class ERB::Formatter
- module SyntaxTreeCommandPatch
- def format(q)
- q.group do
- q.format(message)
- q.text(" ")
- q.format(arguments) # WAS: q.nest(message.value.length + 1) { q.format(arguments) }
- end
- end
- end
-
autoload :IgnoreList, 'erb/formatter/ignore_list'
class Error < StandardError; end
@@ -177,10 +167,14 @@ def raise(message)
def indented(string, strip: true)
string = string.strip if strip
- indent = " " * tag_stack.size
+ indent = "\s" * indentation_width
"\n#{indent}#{string}"
end
+ def indentation_width
+ 2 * tag_stack.size
+ end
+
def format_text(text)
p format_text: text if @debug
return unless text
@@ -217,26 +211,37 @@ def format_text(text)
end
end
- def format_ruby(code, autoclose: false)
+ 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}"]
end
- p RUBY_IN_: code if @debug
- SyntaxTree::Command.prepend SyntaxTreeCommandPatch
+ p RUBY_IN_: code if @debug
code = begin
- SyntaxTree.format(code, @line_width)
+ SyntaxTree.format(
+ code,
+ @line_width - (indentation_width + extra_indent)
+ )
rescue SyntaxTree::Parser::ParseError => error
p RUBY_PARSE_ERROR: error if @debug
code
end
- lines = code.strip.lines
- lines = lines[0...-1] if autoclose
- code = lines.map { |l| indented(l.chomp("\n"), strip: false) }.join.strip
+ lines = code.lines(chomp: true)
+ lines.delete_at(-1) if autoclose
+
+ indent = "\s" * extra_indent
+
+ code = lines.reduce(String.new) do |string, line|
+ string << indented("#{indent}#{line}", strip: false)
+ end
+
+ code.lstrip!
+
p RUBY_OUT: code if @debug
+
code
end
@@ -274,11 +279,12 @@ def format_erb_tags(string)
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
tag_stack_push('%erb%', ruby_code)
when RUBY_OPEN_BLOCK
+ ruby_code = format_ruby(ruby_code, autoclose: true, extra_indent: erb_open.size)
full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
tag_stack_push('%erb%', ruby_code)
else
- ruby_code = format_ruby(ruby_code, autoclose: false)
+ ruby_code = format_ruby(ruby_code, autoclose: false, extra_indent: erb_open.size)
full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
end
diff --git a/test/erb/test_formatter.rb b/test/erb/test_formatter.rb
index ab34c34..4052bde 100644
--- a/test/erb/test_formatter.rb
+++ b/test/erb/test_formatter.rb
@@ -15,7 +15,8 @@ def test_fixtures
Dir["#{__dir__}/../fixtures/*.html.erb"].each do |erb_path|
expected_path = erb_path.chomp('.erb') + '.expected.erb'
- # File.write expected_path, ERB::Formatter.format(File.read(erb_path))
+ File.write(expected_path, ERB::Formatter.format(File.read(erb_path))) if ENV.key?("UPDATE_TEST_FIXTURES")
+
assert_equal(File.read(expected_path), ERB::Formatter.format(File.read(erb_path)), "Formatting of #{erb_path} failed")
end
end
@@ -127,9 +128,9 @@ def test_format_ruby
assert_equal(
"
\n" \
" <%= render MyComponent.new(\n" \
- " foo: barbarbarbarbarbarbarbar,\n" \
- " bar: bazbazbazbazbazbazbazbaz\n" \
- " ) %>\n" \
+ " foo: barbarbarbarbarbarbarbar,\n" \
+ " bar: bazbazbazbazbazbazbazbaz\n" \
+ " ) %>\n" \
"
\n",
ERB::Formatter.format(" <%=render MyComponent.new(foo:barbarbarbarbarbarbarbar,bar:bazbazbazbazbazbazbazbaz)%>
"),
)
diff --git a/test/fixtures/comments.html.expected.erb b/test/fixtures/comments.html.expected.erb
index a4b7c62..16f1ccf 100644
--- a/test/fixtures/comments.html.expected.erb
+++ b/test/fixtures/comments.html.expected.erb
@@ -1,64 +1,64 @@
<%#
-This fails
-hey
-hey
-hey
-hey %>
+ This fails
+ hey
+ hey
+ hey
+ hey %>
<%#
-This fails
-hey
-hey
-hey
-hey %>
+ This fails
+ hey
+ hey
+ hey
+ hey %>
<%#
-This fails
-hey
-hey
-hey
-hey %>
+ This fails
+ hey
+ hey
+ hey
+ hey %>
<%# This fails
-This fails
-hey
-hey
-hey
-hey %>
+ This fails
+ hey
+ hey
+ hey
+ hey %>
<%# This fails
-This fails
-hey
-hey
-hey
-hey %>
+ This fails
+ hey
+ hey
+ hey
+ hey %>
<%# This fails
-This fails
-hey
-hey
-hey
-hey %>
+ This fails
+ hey
+ hey
+ hey
+ hey %>
<%#This fails
-This fails
-hey
-hey
-hey
-hey %>
+ This fails
+ hey
+ hey
+ hey
+ hey %>
<%# This fails
-This fails
-hey
-hey
-hey
-hey %>
+ This fails
+ hey
+ hey
+ hey
+ hey %>
<%#
-hey %>
+ hey %>
<%#
-hey %>
+ hey %>
<%#
-hey %>
+ hey %>
diff --git a/test/fixtures/complex_case_when.html.expected.erb b/test/fixtures/complex_case_when.html.expected.erb
index efb2be4..e0b31b0 100644
--- a/test/fixtures/complex_case_when.html.expected.erb
+++ b/test/fixtures/complex_case_when.html.expected.erb
@@ -15,10 +15,10 @@
<%= t(".payment.stripe_invoice") %>
<% else %>
<% Rails.logger.error.report(
- StandardError.new(
- "No human readable name found for payment method #{payment_method.class}"
- )
- ) %>
+ StandardError.new(
+ "No human readable name found for payment method #{payment_method.class}"
+ )
+ ) %>
<% end %>
<% else %>
<%= t(".payment.no_payment_method_found") %>
diff --git a/test/fixtures/formatted-2.html.expected.erb b/test/fixtures/formatted-2.html.expected.erb
index dfda216..c7cd3a9 100644
--- a/test/fixtures/formatted-2.html.expected.erb
+++ b/test/fixtures/formatted-2.html.expected.erb
@@ -6,11 +6,11 @@
<%= react_component("HelloWorld", { greeting: "Hello from react-rails." }) %>
<%= react_component(
- "HelloWorld",
- { greeting: "Hello from react-rails." },
- { greeting: "Hello from react-rails." },
- { greeting: "Hello from react-rails." },
- { greeting: "Hello from react-rails." }
- ) %>
+ "HelloWorld",
+ { greeting: "Hello from react-rails." },
+ { greeting: "Hello from react-rails." },
+ { greeting: "Hello from react-rails." },
+ { greeting: "Hello from react-rails." }
+ ) %>
diff --git a/test/fixtures/formatted.html.expected.erb b/test/fixtures/formatted.html.expected.erb
index 2adfd9c..9dae1c7 100644
--- a/test/fixtures/formatted.html.expected.erb
+++ b/test/fixtures/formatted.html.expected.erb
@@ -1,6 +1,6 @@
<% link_to "Very long string here and there",
-very_very_very_long_long_long_pathhhhhh_here,
-opt: "212",
-options: "222sdasdasd",
-class: " 322 ",
-dis: diss %>
+ very_very_very_long_long_long_pathhhhhh_here,
+ opt: "212",
+ options: "222sdasdasd",
+ class: " 322 ",
+ dis: diss %>
diff --git a/test/fixtures/if_then_else.html.expected.erb b/test/fixtures/if_then_else.html.expected.erb
index f26f63f..1d34062 100644
--- a/test/fixtures/if_then_else.html.expected.erb
+++ b/test/fixtures/if_then_else.html.expected.erb
@@ -2,16 +2,16 @@
<% eeee ? a : c %>
<% if longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
- a
-else
- c
-end %>
+ a
+ else
+ c
+ end %>
<% if longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
- "a"
-else
- "c"
-end %>
+ "a"
+ else
+ "c"
+ end %>
>
>
diff --git a/test/fixtures/long_deep_nested-2.html.expected.erb b/test/fixtures/long_deep_nested-2.html.expected.erb
index c3c50d3..1fb4b59 100644
--- a/test/fixtures/long_deep_nested-2.html.expected.erb
+++ b/test/fixtures/long_deep_nested-2.html.expected.erb
@@ -17,18 +17,22 @@
<%= react_component(
- "HelloWorld",
- {
- greeting:
- "Hello from react-rails react-rails react-rails react-rails react-rails react-rails react-rails."
- },
- {
- greeting:
- "Hello from react-rails react-rails react-rails react-rails react-rails react-rails react-rails."
- },
- { greeting: "Hello from react-rails." },
- { greeting: "Hello from react-rails." }
- ) %>
+ "HelloWorld",
+ {
+ greeting:
+ "Hello from react-rails react-rails react-rails react-rails react-rails react-rails react-rails."
+ },
+ {
+ greeting:
+ "Hello from react-rails react-rails react-rails react-rails react-rails react-rails react-rails."
+ },
+ {
+ greeting: "Hello from react-rails."
+ },
+ {
+ greeting: "Hello from react-rails."
+ }
+ ) %>
diff --git a/test/fixtures/long_deep_nested.html.expected.erb b/test/fixtures/long_deep_nested.html.expected.erb
index 17d219d..baff9ba 100644
--- a/test/fixtures/long_deep_nested.html.expected.erb
+++ b/test/fixtures/long_deep_nested.html.expected.erb
@@ -17,19 +17,24 @@
<% link_to "Very long long long long long long long long string here and there",
- very_very_very_long_long_long_pathhhhhh_here,
- opt: "212",
- options: "222sdasdasd",
- class: " 322 ",
- dis: diss %>
+ very_very_very_long_long_long_pathhhhhh_here,
+ opt: "212",
+ options: "222sdasdasd",
+ class: " 322 ",
+ dis: diss %>
- <% link_to "string", path, opt: "212", options: "222sdasdasd" %>
+ <% link_to "string",
+ path,
+ opt: "212",
+ options: "222sdasdasd" %>
- <%= react_component({ greeting: "react-rails." }) %>
+ <%= react_component(
+ { greeting: "react-rails." }
+ ) %>