Skip to content

Nested heredocs are formatted incorrectly when text follows closing bracket #286

@phiggins

Description

@phiggins

If I add text to the line with nested heredocs in the fixtures/small/pathological_heredocs_actual.rb test case like this:

pete@hexagon:~/projects/rubyfmt$ git diff
diff --git a/fixtures/small/pathological_heredocs_actual.rb b/fixtures/small/pathological_heredocs_actual.rb
index 60717a3..28e75e8 100644
--- a/fixtures/small/pathological_heredocs_actual.rb
+++ b/fixtures/small/pathological_heredocs_actual.rb
@@ -1,5 +1,5 @@
 a = <<EOD
-part 1 of heredoc #{ "not a heredoc" + <<EOM }
+part 1 of heredoc #{ "not a heredoc" + <<EOM } after brace before newline
 eom part
 EOM
 part 2 of heredoc

The output of rubyfmt changes a lot:

pete@hexagon:~/projects/rubyfmt$ FIXTURE_NAME=pathological_heredocs ./script/tests/test_fixtures.sh
+ source ./script/functions.sh
+++ git rev-parse --show-toplevel
++ REPO_BASE=/home/pete/projects/rubyfmt
+ make
git submodule init
git submodule update
+ test_fixtures_folder fixtures/small
+ current_dir=fixtures/small
+ fixture_name=pathological_heredocs
+ find fixtures/small -name pathological_heredocs_expected.rb -maxdepth 1
+ read -r expected_file
find: warning: you have specified the global option -maxdepth after the argument -name, but global options are not positional, i.e., -maxdepth affects tests specified before it as well as those specified after it.  Please specify global options before other arguments.
+ actual_file=fixtures/small/pathological_heredocs_actual.rb
+ f_rubyfmt fixtures/small/pathological_heredocs_actual.rb
+ /home/pete/projects/rubyfmt/target/release/rubyfmt-main fixtures/small/pathological_heredocs_actual.rb

real    0m0.028s
user    0m0.016s
sys     0m0.012s
+ diff_files /tmp/out.rb fixtures/small/pathological_heredocs_expected.rb
+ ACTUAL=/tmp/out.rb
+ EXPECTED=fixtures/small/pathological_heredocs_expected.rb
+ diff -u /tmp/out.rb fixtures/small/pathological_heredocs_expected.rb
--- /tmp/out.rb 2021-05-05 22:59:49.935353109 -0700
+++ fixtures/small/pathological_heredocs_expected.rb    2021-04-12 12:23:41.706772058 -0700
@@ -1,9 +1,8 @@
 a = <<EOD
-part 1 of heredoc #{"not a heredoc" + <<EOM} after brace before newline
-part 2 of heredoc
-
+part 1 of heredoc #{"not a heredoc" + <<EOM}
 eom part
 EOM
+part 2 of heredoc
 EOD

 b = <<-EOD
+ echo 'got diff between formated formatted actual and expected'
got diff between formated formatted actual and expected
+ exit 1

This is originally from #275.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions