Skip to content

Commit

Permalink
Format empty html-tags on one line if possible (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwessman authored Jun 30, 2023
1 parent 3dbc3d9 commit 98227e3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

## [0.9.3] - 2023-06-30

- Print empty html-tags on one line if possible

## [0.9.2] - 2023-06-30

- Handle whitespace in HTML-strings using ERB-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.2)
w_syntax_tree-erb (0.9.3)
prettier_print (>= 1.2.0)
syntax_tree (>= 6.1.1)

Expand Down
7 changes: 6 additions & 1 deletion lib/syntax_tree/erb/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ def visit_block(node)
end

def visit_html(node)
visit_block(node)
# Make sure to group the tags together if there is no child nodes.
if node.elements.size == 0
q.group { visit_block(node) }
else
visit_block(node)
end
end

def visit_erb_block(node)
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.2"
VERSION = "0.9.3"
end
end
3 changes: 1 addition & 2 deletions test/fixture/erb_syntax_formatted.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@
assign_c ||= "c"
%>

<div class="card mb-2 <%= condition ? "pb-3" : "pb-0" %> mt-<%= 5 * 5 %>">
</div>
<div class="card mb-2 <%= condition ? "pb-3" : "pb-0" %> mt-<%= 5 * 5 %>"></div>
8 changes: 8 additions & 0 deletions test/html_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,13 @@ def test_html_attribute_without_quotes
formatted = ERB.format(source)
assert_equal("<div class=\"card\">\n Hello World\n</div>\n", formatted)
end

def test_html_attribute_without_content
source = "<component-without-content>\n</component-without-content>\n"
expected = "<component-without-content></component-without-content>\n"

formatted = ERB.format(source)
assert_equal(expected, formatted)
end
end
end

0 comments on commit 98227e3

Please sign in to comment.