Skip to content

Commit f89f7a0

Browse files
authored
v0.9.5 - Fixes formatting of ruby comment in ERB-tag (#45)
Example: ```erb <% # this is a comment %> ``` Output: ```diff -<% - - # this is a comment -%> +<% # this is a comment %> ```
1 parent aa97395 commit f89f7a0

File tree

7 files changed

+40
-7
lines changed

7 files changed

+40
-7
lines changed

CHANGELOG.md

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

77
## [Unreleased]
88

9+
## [0.9.5] - 2023-07-02
10+
11+
- Fixes ruby comment in ERB-tag included VoidStatement
12+
Example:
13+
14+
```erb
15+
<% # this is a comment %>
16+
```
17+
18+
Output:
19+
20+
```diff
21+
-<%
22+
-
23+
- # this is a comment
24+
-%>
25+
+<% # this is a comment %>
26+
```
27+
28+
- Updates versions in Bundler
29+
930
## [0.9.4] - 2023-07-01
1031

1132
- Inline even more empty HTML-tags

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
w_syntax_tree-erb (0.9.4)
4+
w_syntax_tree-erb (0.9.5)
55
prettier_print (~> 1.2, >= 1.2.0)
66
syntax_tree (~> 6.1, >= 6.1.1)
77

lib/syntax_tree/erb/format.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,19 @@ def visit_erb_content(node)
118118
if node.value.is_a?(String)
119119
output_rows(node.value.split("\n"))
120120
else
121-
child_nodes = node.value&.statements&.child_nodes || []
121+
nodes = node.value&.statements&.child_nodes || []
122+
nodes = nodes.reject { |node| node.is_a?(SyntaxTree::VoidStmt) }
122123

123-
if child_nodes.size == 1
124+
if nodes.size == 1
124125
q.text(" ")
125-
q.seplist(child_nodes, -> { q.breakable("") }) do |child_node|
126+
q.seplist(nodes, -> { q.breakable("") }) do |child_node|
126127
format_statement(child_node)
127128
end
128129
q.text(" ")
129-
elsif child_nodes.size > 1
130+
elsif nodes.size > 1
130131
q.indent do
131132
q.breakable("")
132-
q.seplist(child_nodes, -> { q.breakable("") }) do |child_node|
133+
q.seplist(nodes, -> { q.breakable("") }) do |child_node|
133134
format_statement(child_node)
134135
end
135136
end

lib/syntax_tree/erb/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module SyntaxTree
44
module ERB
5-
VERSION = "0.9.4"
5+
VERSION = "0.9.5"
66
end
77
end

test/erb_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ def test_erb_with_comment
6262
assert_equal(source, formatted_twice)
6363
end
6464

65+
def test_erb_only_comment
66+
source = "<% # This should be written on one line %>\n"
67+
formatted_once = ERB.format(source)
68+
formatted_twice = ERB.format(formatted_once)
69+
70+
assert_equal(source, formatted_once)
71+
assert_equal(source, formatted_twice)
72+
end
73+
6574
def test_erb_ternary_as_argument_without_parentheses
6675
source =
6776
"<%= f.submit f.object.id.present? ? t('buttons.titles.save'):t('buttons.titles.create') %>"

test/fixture/erb_syntax_formatted.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<%# This is an ERB-comment https://stackoverflow.com/a/25626629 this answer describes ERB and erubis syntax%>
33
<%== rails_raw_output %>
44
<%- "this only works in ERB not erubis" %>
5+
<% # This should be written on one line %>
56

67
<% if this -%>
78
<%= form.submit -%>

test/fixture/erb_syntax_unformatted.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<%# This is an ERB-comment https://stackoverflow.com/a/25626629 this answer describes ERB and erubis syntax%>
33
<%== rails_raw_output%>
44
<%-"this only works in ERB not erubis"%>
5+
<% # This should be written on one line %>
56

67
<% if this -%>
78
<%= form.submit -%>

0 commit comments

Comments
 (0)