Skip to content

Commit 605300d

Browse files
authored
Avoid grouping single ERB-tags and handle ERB-comments with newlines (#62)
* Avoid grouping single elements * Handles ERB-comments with newlines
1 parent f8559fe commit 605300d

File tree

6 files changed

+57
-29
lines changed

6 files changed

+57
-29
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
66

77
## [Unreleased]
88

9+
- Avoid grouping single tags
10+
- Handle multiline ERB-comments
11+
912
## [0.10.3] - 2023-08-27
1013

1114
## Fixes

lib/syntax_tree/erb/format.rb

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def visit_erb(node)
114114
q.text(" ")
115115
visit(node.keyword)
116116
end
117+
117118
node.content.blank? ? q.text(" ") : visit(node.content)
118119

119120
visit(node.closing_tag)
@@ -137,27 +138,25 @@ def visit_erb_end(node)
137138
end
138139

139140
def visit_erb_content(node)
140-
if node.value.is_a?(String)
141-
output_rows(node.value.split("\n"))
142-
else
143-
nodes = node.value&.statements&.child_nodes || []
144-
nodes = nodes.reject { |node| node.is_a?(SyntaxTree::VoidStmt) }
141+
# Reject all VoidStmt to avoid empty lines
142+
nodes =
143+
(node.value&.statements&.child_nodes || []).reject do |node|
144+
node.is_a?(SyntaxTree::VoidStmt)
145+
end
145146

146-
if nodes.size == 1
147-
q.text(" ")
147+
if nodes.size == 1
148+
q.text(" ")
149+
format_statement(nodes.first)
150+
q.text(" ")
151+
elsif nodes.size > 1
152+
q.indent do
153+
q.breakable("")
148154
q.seplist(nodes, -> { q.breakable("") }) do |child_node|
149155
format_statement(child_node)
150156
end
151-
q.text(" ")
152-
elsif nodes.size > 1
153-
q.indent do
154-
q.breakable("")
155-
q.seplist(nodes, -> { q.breakable("") }) do |child_node|
156-
format_statement(child_node)
157-
end
158-
end
159-
q.breakable
160157
end
158+
159+
q.breakable
161160
end
162161
end
163162

@@ -341,20 +340,26 @@ def handle_child_nodes(child_nodes)
341340
end
342341

343342
def handle_group(nodes, break_after:)
344-
return unless nodes.any?
345-
346-
q.group do
347-
nodes.each_with_index do |node, group_index|
348-
visit(node)
349-
next_node = nodes[group_index + 1]
350-
next if next_node.nil?
351-
breakable_between_group(node, next_node)
352-
end
343+
if nodes.size == 1
344+
handle_group_nodes(nodes)
345+
elsif nodes.size > 1
346+
q.group { handle_group_nodes(nodes) }
347+
else
348+
return
353349
end
354350

355351
breakable_between_group(nodes.last, nil) if break_after
356352
end
357353

354+
def handle_group_nodes(nodes)
355+
nodes.each_with_index do |node, group_index|
356+
visit(node)
357+
next_node = nodes[group_index + 1]
358+
next if next_node.nil?
359+
breakable_between_group(node, next_node)
360+
end
361+
end
362+
358363
def node_should_group(node)
359364
node.is_a?(SyntaxTree::ERB::CharData) ||
360365
node.is_a?(SyntaxTree::ERB::ErbNode)

lib/syntax_tree/erb/parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def make_tokens
9494
# <!DOCTYPE
9595
enum.yield :doctype, $&, index, line
9696
state << :inside
97-
when /\A<%#.*%>/
97+
when /\A<%#[\s\S]*?%>/
9898
# An ERB-comment
9999
# <%# this is an ERB comment %>
100100
enum.yield :erb_comment, $&, index, line

test/erb_test.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ def test_if_and_end_in_same_output_tag_short
4343
end
4444

4545
def test_if_and_end_in_same_tag
46-
source = "<% if true then this elsif false then that else maybe end %>"
46+
source =
47+
"Hello\n<% if true then this elsif false then that else maybe end %>\n<h1>Hey</h1>"
4748
expected =
48-
"<% if true\n this\nelsif false\n that\nelse\n maybe\nend %>\n"
49+
"Hello\n<% if true\n this\nelsif false\n that\nelse\n maybe\nend %>\n<h1>Hey</h1>\n"
4950

5051
assert_formatting(source, expected)
5152
end
@@ -98,12 +99,21 @@ def test_erb_only_ruby_comment
9899
assert_formatting(source, source)
99100
end
100101

101-
def test_erb_only_erb_comment
102+
def test_erb_comment
102103
source = "<%# This should be written on one line %>\n"
103104

104105
assert_formatting(source, source)
105106
end
106107

108+
def test_erb_multiline_comment
109+
source =
110+
"<%#\n This is the first\nThis is the second\nThis is the third %>"
111+
expected =
112+
"<%#\n This is the first\nThis is the second\nThis is the third %>\n"
113+
114+
assert_formatting(source, expected)
115+
end
116+
107117
def test_erb_ternary_as_argument_without_parentheses
108118
source =
109119
"<%= f.submit( f.object.id.present? ? t('buttons.titles.save'):t('buttons.titles.create')) %>"

test/fixture/erb_syntax_formatted.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
<%== rails_raw_output %>
44
<%- "this only works in ERB not erubis" %>
55
<% # This should be written on one line %>
6+
<%#
7+
This is a comment
8+
It can be mutiline
9+
Treat it as a comment
10+
%>
611

712
<% if this -%>
813
<%= form.submit -%>

test/fixture/erb_syntax_unformatted.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
<%== rails_raw_output%>
44
<%-"this only works in ERB not erubis"%>
55
<% # This should be written on one line %>
6+
<%#
7+
This is a comment
8+
It can be mutiline
9+
Treat it as a comment
10+
%>
611

712
<% if this -%>
813
<%= form.submit -%>

0 commit comments

Comments
 (0)