From f89f7a08554ce34799d18fdaba52f68148e271c0 Mon Sep 17 00:00:00 2001 From: David Wessman Date: Sun, 2 Jul 2023 12:24:17 +0200 Subject: [PATCH] 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 %> ``` --- CHANGELOG.md | 21 ++++++++++++++++++++ Gemfile.lock | 2 +- lib/syntax_tree/erb/format.rb | 11 +++++----- lib/syntax_tree/erb/version.rb | 2 +- test/erb_test.rb | 9 +++++++++ test/fixture/erb_syntax_formatted.html.erb | 1 + test/fixture/erb_syntax_unformatted.html.erb | 1 + 7 files changed, 40 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50cffd5..26b669c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 7b44902..a1d144e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/lib/syntax_tree/erb/format.rb b/lib/syntax_tree/erb/format.rb index 1af613a..6c9ba41 100644 --- a/lib/syntax_tree/erb/format.rb +++ b/lib/syntax_tree/erb/format.rb @@ -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 diff --git a/lib/syntax_tree/erb/version.rb b/lib/syntax_tree/erb/version.rb index bf2155c..1878b64 100644 --- a/lib/syntax_tree/erb/version.rb +++ b/lib/syntax_tree/erb/version.rb @@ -2,6 +2,6 @@ module SyntaxTree module ERB - VERSION = "0.9.4" + VERSION = "0.9.5" end end diff --git a/test/erb_test.rb b/test/erb_test.rb index dcc7ca6..4445dce 100644 --- a/test/erb_test.rb +++ b/test/erb_test.rb @@ -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') %>" diff --git a/test/fixture/erb_syntax_formatted.html.erb b/test/fixture/erb_syntax_formatted.html.erb index 14e9eca..2552cae 100644 --- a/test/fixture/erb_syntax_formatted.html.erb +++ b/test/fixture/erb_syntax_formatted.html.erb @@ -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 -%> diff --git a/test/fixture/erb_syntax_unformatted.html.erb b/test/fixture/erb_syntax_unformatted.html.erb index a0a25e4..e2005e1 100644 --- a/test/fixture/erb_syntax_unformatted.html.erb +++ b/test/fixture/erb_syntax_unformatted.html.erb @@ -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 -%>