Perl-Critic / PPI

53 stars 44 forks source link

Indented here-doc loses whitespace before final delimiter #251

Closed ssimms closed 2 years ago

ssimms commented 3 years ago

When an indented here-doc is passed into PPI and then serialized, the leading whitespace from the final delimiter is lost, as shown in this test:

use v5.26;
use PPI;
use Test::More tests => 2;

my $basic = q{
my $message = <<~'EOF';
    This is a test.
    EOF
};

my $doc_basic = PPI::Document->new(\$basic);
is($doc_basic->serialize(), $basic,
     q{Indented here-doc delimiter is indented});

my $blank_line = q{
my $message = <<~'EOF';
    With indented here-docs, all lines *must* have at least the same whitespace

    (except lines only containing a newline)
    -- perldoc perlop
    EOF
};

my $doc_blank_line = PPI::Document->new(\$blank_line);
is($doc_blank_line->serialize(), $blank_line,
     q{Indented here-doc delimiter is indented when blank line is present});

The final EOF loses its indentation in both cases. In the first case, the contents of the here-doc also lose their leading whitespace. In the second case, they remain indented, resulting in unexpected whitespace when the serialized code is then run.

As far as I can tell, this is a separate issue from #203, which was resolved by pull request #209, but the fix will likely involve the same area of the code as that pull request.