fables-tales/rubyfmt

Bare (`<<`) heredocs nested in squiggly (`<<~`) heredocs render invalid syntax/semantic changes

Opened this issue · 0 comments

reese commented
  • Ruby version: 2.7.3
  • Rubyfmt git sha: ed99cc4

Input file

(I feel a little bad just writing this example)

class Foo
    def self.example
        p <<~FOO
          abc
          #{[1, 2].map do |n|
            <<BAR
    n is #{n}
BAR
    end
        }
        FOO
        .inspect
    end
end
Foo.example

Rubyfmt's output

class Foo
  def self.example
    p(
      <<~FOO.inspect
        abc
        #{[1, 2].map do |n|
                <<BAR
            n is #{n}
        BAR
              end}
      FOO
    )
  end
end

Foo.example

This breaks because

I mean, this is a pretty cursed way to write this, but this just came up as a cursed test case when I was working on #360. For bare heredocs, the closing BAR tag must be fully left-aligned, but we always bad the contents of squiggly heredocs with spaces no matter what. This also affects dash (<<-) heredocs as well, since it modifies the contents of the heredoc by adding too many spaces.