From f4a2ba19a9c0b2fb73df225229d4fdd5ca5d777c Mon Sep 17 00:00:00 2001 From: David Wessman Date: Wed, 30 Aug 2023 22:09:23 +0200 Subject: [PATCH] Handles indentation for multiline ERB-comments --- CHANGELOG.md | 1 + lib/syntax_tree/erb/format.rb | 4 +++- test/erb_test.rb | 4 ++-- test/fixture/erb_syntax_formatted.html.erb | 6 +++--- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26ea725..992b56e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ## [Unreleased] - Handle ERB-tags inside HTML-tags, like `
>` +- Handles indentation for multiline ERB-comment ## [0.10.4] - 2023-08-28 diff --git a/lib/syntax_tree/erb/format.rb b/lib/syntax_tree/erb/format.rb index dce7a1d..0128843 100644 --- a/lib/syntax_tree/erb/format.rb +++ b/lib/syntax_tree/erb/format.rb @@ -236,7 +236,9 @@ def visit_html_comment(node) end def visit_erb_comment(node) - visit(node.token) + q.seplist(node.token.value.split("\n"), -> { q.breakable }) do |line| + q.text(line.lstrip) + end end # Visit a CharData node. diff --git a/test/erb_test.rb b/test/erb_test.rb index 7d4b152..44c5015 100644 --- a/test/erb_test.rb +++ b/test/erb_test.rb @@ -114,9 +114,9 @@ def test_erb_comment def test_erb_multiline_comment source = - "<%#\n This is the first\nThis is the second\nThis is the third %>" + "<%#\n This is the first\n This is the second\n This is the third %>" expected = - "<%#\n This is the first\nThis is the second\nThis is the third %>\n" + "<%#\nThis is the first\nThis is the second\nThis is the third %>\n" assert_formatting(source, expected) end diff --git a/test/fixture/erb_syntax_formatted.html.erb b/test/fixture/erb_syntax_formatted.html.erb index 3a9ef31..835750a 100644 --- a/test/fixture/erb_syntax_formatted.html.erb +++ b/test/fixture/erb_syntax_formatted.html.erb @@ -4,9 +4,9 @@ <%- "this only works in ERB not erubis" %> <% # This should be written on one line %> <%# - This is a comment - It can be mutiline - Treat it as a comment +This is a comment +It can be mutiline +Treat it as a comment %> <% if this -%>