Skip to content

Commit d4f5183

Browse files
committed
Allow zero byte documents to return location data
1 parent 9fbbd13 commit d4f5183

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

Changes

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Revision history for Perl extension PPI
22

33
{{$NEXT}}
4+
- Allow zero byte documents to have a location
45

56
1.281 2024-12-27 14:44:47Z
67
Summary:

lib/PPI/Document.pm

+15-8
Original file line numberDiff line numberDiff line change
@@ -658,14 +658,10 @@ sub index_locations {
658658

659659
# Found the first Token without a location
660660
# Calculate the new location if needed.
661-
if ($_) {
662-
$location =
663-
$self->_add_location( $location, $tokens[$_ - 1], \$heredoc );
664-
} else {
665-
my $logical_file =
666-
$self->can('filename') ? $self->filename : undef;
667-
$location = [ 1, 1, 1, 1, $logical_file ];
668-
}
661+
$location =
662+
$_
663+
? $self->_add_location( $location, $tokens[ $_ - 1 ], \$heredoc )
664+
: $self->_default_location;
669665
$first = $_;
670666
last;
671667
}
@@ -687,6 +683,17 @@ sub index_locations {
687683
1;
688684
}
689685

686+
sub _default_location {
687+
my ($self) = @_;
688+
my $logical_file = $self->can('filename') ? $self->filename : undef;
689+
return [ 1, 1, 1, 1, $logical_file ];
690+
}
691+
692+
sub location {
693+
my ($self) = @_;
694+
return $self->SUPER::location || $self->_default_location;
695+
}
696+
690697
sub _add_location {
691698
my ($self, $start, $Token, $heredoc) = @_;
692699
my $content = $Token->{content};

t/03_document.t

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use lib 't/lib';
66
use PPI::Test::pragmas;
7-
use Test::More tests => 19 + ($ENV{AUTHOR_TESTING} ? 1 : 0);
7+
use Test::More tests => 22 + ( $ENV{AUTHOR_TESTING} ? 1 : 0 );
88

99
use File::Spec::Functions qw( catfile );
1010
use PPI ();
@@ -61,9 +61,7 @@ NEW_EMPTY: {
6161
my $string = $doc1->serialize;
6262
is( $string, '', '->serialize ok' );
6363

64-
# Check for warnings on null document index_locations
65-
{
66-
local $^W = 1;
67-
$doc1->index_locations();
68-
}
64+
ok $doc1->location;
65+
ok $doc2->location;
66+
ok $doc3->location;
6967
}

0 commit comments

Comments
 (0)