Skip to content

Commit

Permalink
v0.9.5 - Fixes formatting of ruby comment in ERB-tag (#45)
Browse files Browse the repository at this point in the history
Example:
```erb
<% # this is a comment %>
```

Output:
```diff
-<%
-
-  # this is a comment
-%>
+<% # this is a comment %>
```
  • Loading branch information
davidwessman authored Jul 2, 2023
1 parent aa97395 commit f89f7a0
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 7 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

## [0.9.5] - 2023-07-02

- Fixes ruby comment in ERB-tag included VoidStatement
Example:

```erb
<% # this is a comment %>
```

Output:

```diff
-<%
-
- # this is a comment
-%>
+<% # this is a comment %>
```

- Updates versions in Bundler

## [0.9.4] - 2023-07-01

- Inline even more empty HTML-tags
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
w_syntax_tree-erb (0.9.4)
w_syntax_tree-erb (0.9.5)
prettier_print (~> 1.2, >= 1.2.0)
syntax_tree (~> 6.1, >= 6.1.1)

Expand Down
11 changes: 6 additions & 5 deletions lib/syntax_tree/erb/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,19 @@ def visit_erb_content(node)
if node.value.is_a?(String)
output_rows(node.value.split("\n"))
else
child_nodes = node.value&.statements&.child_nodes || []
nodes = node.value&.statements&.child_nodes || []
nodes = nodes.reject { |node| node.is_a?(SyntaxTree::VoidStmt) }

if child_nodes.size == 1
if nodes.size == 1
q.text(" ")
q.seplist(child_nodes, -> { q.breakable("") }) do |child_node|
q.seplist(nodes, -> { q.breakable("") }) do |child_node|
format_statement(child_node)
end
q.text(" ")
elsif child_nodes.size > 1
elsif nodes.size > 1
q.indent do
q.breakable("")
q.seplist(child_nodes, -> { q.breakable("") }) do |child_node|
q.seplist(nodes, -> { q.breakable("") }) do |child_node|
format_statement(child_node)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/syntax_tree/erb/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module SyntaxTree
module ERB
VERSION = "0.9.4"
VERSION = "0.9.5"
end
end
9 changes: 9 additions & 0 deletions test/erb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ def test_erb_with_comment
assert_equal(source, formatted_twice)
end

def test_erb_only_comment
source = "<% # This should be written on one line %>\n"
formatted_once = ERB.format(source)
formatted_twice = ERB.format(formatted_once)

assert_equal(source, formatted_once)
assert_equal(source, formatted_twice)
end

def test_erb_ternary_as_argument_without_parentheses
source =
"<%= f.submit f.object.id.present? ? t('buttons.titles.save'):t('buttons.titles.create') %>"
Expand Down
1 change: 1 addition & 0 deletions test/fixture/erb_syntax_formatted.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<%# This is an ERB-comment https://stackoverflow.com/a/25626629 this answer describes ERB and erubis syntax%>
<%== rails_raw_output %>
<%- "this only works in ERB not erubis" %>
<% # This should be written on one line %>
<% if this -%>
<%= form.submit -%>
Expand Down
1 change: 1 addition & 0 deletions test/fixture/erb_syntax_unformatted.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<%# This is an ERB-comment https://stackoverflow.com/a/25626629 this answer describes ERB and erubis syntax%>
<%== rails_raw_output%>
<%-"this only works in ERB not erubis"%>
<% # This should be written on one line %>
<% if this -%>
<%= form.submit -%>
Expand Down

0 comments on commit f89f7a0

Please sign in to comment.