| 1 | package App::perlimports::Sorter; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 107 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 107 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 107 | ▶ | use App::perlimports::Annotations (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 107 | ▶ | use PPI::Document (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 107 | ▶ | use Types::Standard qw( Str ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 107 | our $VERSION = '0.000067'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 107 | with 'App::perlimports::Role::Logger'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 107 | has _source => ( | |||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||
| 16 | isa => Str, | ||||||||||||||||||||||||||||
| 17 | init_arg => 'source', | ||||||||||||||||||||||||||||
| 18 | required => 1, | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 36 | ▶ | sub sorted_document { | ||||||||||||||||||||||||||
|
Subroutine
sorted_document: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 36 | my $self = shift; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 36 | my $source = $self->_source; | |||||||||||||||||||||||||||
| 25 | 36 | my $doc = PPI::Document->new( \$source ); | |||||||||||||||||||||||||||
| 26 | 36 | ▶ | return $source unless $doc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 35 | $doc->index_locations; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | my $found | ||||||||||||||||||||||||||||
| 30 | 35 | ▶ | = $doc->find( sub { $_[1]->isa('PPI::Statement::Include') } ) || []; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Condition100%2/2
$doc->find(sub {
$_[1]->isa('PPI::Statement::Include');
}
) || []
MC/DC100%1/1
$doc->find(sub {
$_[1]->isa('PPI::Statement::Include');
}
) || []
$doc->find(sub {
$_[1]->isa('PPI::Statement::Include');
}
)
Truth table100%2/2
A
A= $doc->find(sub {
$_[1]->isa('PPI::Statement::Include');
}
)
| |||||||||||||||||||||||||||||
| 31 | 35 | ▶ | return $source unless @{$found}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 34 | my $annotations = App::perlimports::Annotations->new( | |||||||||||||||||||||||||||
| 34 | ppi_document => $doc, | ||||||||||||||||||||||||||||
| 35 | logger => $self->logger, | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Each element retains its trailing "\n"; line N is $lines[N - 1]. | ||||||||||||||||||||||||||||
| 39 | 34 | my @lines = split /(?<=\n)/, $source; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 34 | my @units = map { _unit( $_, $annotations, \@lines ) } @{$found}; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 34 | my @sections = _sections( \@units, \@lines ); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # Rewrite bottom-up so earlier line numbers stay valid across splices. | ||||||||||||||||||||||||||||
| 46 | 34 | my $changed = 0; | |||||||||||||||||||||||||||
| 47 | 34 | for my $section ( reverse @sections ) { | |||||||||||||||||||||||||||
| 48 | 41 | my ( $new_text, $section_changed ) | |||||||||||||||||||||||||||
| 49 | = _reorder_section( $section, \@lines ); | ||||||||||||||||||||||||||||
| 50 | 41 | ▶ | next unless $section_changed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 27 | $changed = 1; | |||||||||||||||||||||||||||
| 53 | 27 | my $first = $section->[0]{lead}; | |||||||||||||||||||||||||||
| 54 | 27 | my $end = $section->[-1]{end}; | |||||||||||||||||||||||||||
| 55 | 27 | splice @lines, $first - 1, $end - $first + 1, $new_text; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 34 | ▶ | return $changed ? join( q{}, @lines ) : $source; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Build a unit hashref describing one include and the source lines it owns. | ||||||||||||||||||||||||||||
| 62 | 99 | ▶ | sub _unit { | ||||||||||||||||||||||||||
|
Subroutine
_unit: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 63 | 99 | my ( $node, $annotations, $lines ) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 99 | my $start = $node->line_number; | |||||||||||||||||||||||||||
| 66 | 99 | my $newlines = () = $node->content =~ /\n/g; | |||||||||||||||||||||||||||
| 67 | 99 | my $end = $start + $newlines; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # Absorb full-line comments directly above the statement (no blank line | ||||||||||||||||||||||||||||
| 70 | # can appear here: a blank line would have ended the section). | ||||||||||||||||||||||||||||
| 71 | 99 | my $lead = $start; | |||||||||||||||||||||||||||
| 72 | 99 | ▶ | while ( $lead - 1 >= 1 && $lines->[ $lead - 2 ] =~ /\A\s*#/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$lead - 1 >= 1 and $$lines[$lead - 2] =~ /\A\s*#/
MC/DC100%2/2
$lead - 1 >= 1 and $$lines[$lead - 2] =~ /\A\s*#/
$lead - 1 >= 1
$$lines[$lead - 2] =~ /\A\s*#/
Truth table100%3/3
A and B
A= $lead - 1 >= 1B= $$lines[$lead - 2] =~ /\A\s*#/
| |||||||||||||||||||||||||||||
| 73 | 6 | $lead--; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | return { | ||||||||||||||||||||||||||||
| 77 | 99 | lead => $lead, | |||||||||||||||||||||||||||
| 78 | start => $start, | ||||||||||||||||||||||||||||
| 79 | end => $end, | ||||||||||||||||||||||||||||
| 80 | class => _classify( $node, $annotations ), | ||||||||||||||||||||||||||||
| 81 | key => lc( $node->module ), | ||||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 99 | ▶ | sub _classify { | ||||||||||||||||||||||||||
|
Subroutine
_classify: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 86 | 99 | my ( $node, $annotations ) = @_; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 99 | ▶ | return 'hoist' if $node->pragma; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 85 | ▶ | return 'hoist' if $node->version; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 84 | ▶ | return 'anchor' if $annotations->is_ignored($node); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 79 | ▶ | return 'anchor' if $node->type eq 'no'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 78 | ▶ | return 'anchor' unless length $node->module; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 77 | return 'sortable'; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Group units into contiguous sections. | ||||||||||||||||||||||||||||
| 97 | 34 | ▶ | sub _sections { | ||||||||||||||||||||||||||
|
Subroutine
_sections: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 98 | 34 | my ( $units, $lines ) = @_; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 34 | my @sections; | |||||||||||||||||||||||||||
| 101 | my @current; | ||||||||||||||||||||||||||||
| 102 | 34 | for my $u ( @{$units} ) { | |||||||||||||||||||||||||||
| 103 | 99 | ▶ | if (@current) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 65 | my $prev = $current[-1]; | |||||||||||||||||||||||||||
| 105 | 65 | my $same = 1; | |||||||||||||||||||||||||||
| 106 | 65 | for my $ln ( $prev->{end} + 1 .. $u->{start} - 1 ) { | |||||||||||||||||||||||||||
| 107 | 8 | ▶ | unless ( $lines->[ $ln - 1 ] =~ /\A\s*#/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 7 | $same = 0; | |||||||||||||||||||||||||||
| 109 | 7 | last; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 65 | ▶ | if ( !$same ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 7 | push @sections, [@current]; | |||||||||||||||||||||||||||
| 114 | 7 | @current = (); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 99 | push @current, $u; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # @current always holds at least the final unit: sorted_document returns | ||||||||||||||||||||||||||||
| 121 | # early when there are no includes, so this loop always runs at least once. | ||||||||||||||||||||||||||||
| 122 | 34 | push @sections, [@current]; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 34 | return @sections; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Return ( $new_section_text, $changed_bool ) for one section. | ||||||||||||||||||||||||||||
| 128 | 41 | ▶ | sub _reorder_section { | ||||||||||||||||||||||||||
|
Subroutine
_reorder_section: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 129 | 41 | my ( $section, $lines ) = @_; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # If any two units share or overlap a physical line, reordering would | ||||||||||||||||||||||||||||
| 132 | # duplicate or merge the user's source. We cannot safely sort statements | ||||||||||||||||||||||||||||
| 133 | # that share a line, so leave the whole section byte-identical. | ||||||||||||||||||||||||||||
| 134 | 41 | for my $i ( 0 .. $#{$section} - 1 ) { | |||||||||||||||||||||||||||
| 135 | return ( q{}, 0 ) | ||||||||||||||||||||||||||||
| 136 | 57 | ▶ | if $section->[$i]{end} >= $section->[ $i + 1 ]{lead}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 39 | my @hoist = grep { $_->{class} eq 'hoist' } @{$section}; | |||||||||||||||||||||||||||
| 140 | 39 | my @modules = grep { $_->{class} ne 'hoist' } @{$section}; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 45 | my @sortable = sort { $a->{key} cmp $b->{key} } | |||||||||||||||||||||||||||
| 143 | 39 | grep { $_->{class} eq 'sortable' } @modules; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 39 | my @ordered_modules; | |||||||||||||||||||||||||||
| 146 | 39 | my $si = 0; | |||||||||||||||||||||||||||
| 147 | 39 | for my $u (@modules) { | |||||||||||||||||||||||||||
| 148 | push @ordered_modules, | ||||||||||||||||||||||||||||
| 149 | 80 | ▶ | $u->{class} eq 'anchor' ? $u : $sortable[ $si++ ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 188 | ▶ | my $text_of = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 153 | 188 | my $u = shift; | |||||||||||||||||||||||||||
| 154 | 188 | return join q{}, @{$lines}[ $u->{lead} - 1 .. $u->{end} - 1 ]; | |||||||||||||||||||||||||||
| 155 | 39 | }; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # Normalize each unit's text to end in exactly one newline so reordered | ||||||||||||||||||||||||||||
| 158 | # units never jam onto one line when the final unit lacked a trailing "\n". | ||||||||||||||||||||||||||||
| 159 | 94 | ▶ | my $rendered = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | 94 | my $text = $text_of->(shift); | |||||||||||||||||||||||||||
| 161 | 94 | $text =~ s/\n?\z/\n/; | |||||||||||||||||||||||||||
| 162 | 94 | return $text; | |||||||||||||||||||||||||||
| 163 | 39 | }; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 39 | my $new_text = q{}; | |||||||||||||||||||||||||||
| 166 | 39 | $new_text .= $rendered->($_) for @hoist; | |||||||||||||||||||||||||||
| 167 | 39 | ▶ | $new_text .= "\n" if @hoist && @ordered_modules; # set pragmas off | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@hoist and @ordered_modules
MC/DC100%2/2
@hoist and @ordered_modules
@hoist
@ordered_modules
Truth table100%3/3
A and B
A= @hoistB= @ordered_modules
| |||||||||||||||||||||||||||||
| 168 | 39 | $new_text .= $rendered->($_) for @ordered_modules; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 39 | my $old_text = join q{}, map { $text_of->($_) } @{$section}; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # Preserve the section's original trailing-newline state: if the source | ||||||||||||||||||||||||||||
| 173 | # had no final "\n", do not add bytes the user did not have. | ||||||||||||||||||||||||||||
| 174 | 39 | ▶ | $new_text =~ s/\n\z// unless $old_text =~ /\n\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 39 | return ( $new_text, $new_text ne $old_text ); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 107 | 1; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # ABSTRACT: Sort a document's include statements | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||||||||||