File Coverage

blib/lib/App/PerlNitpick/Rule/AddTrailingCommas.pm
Criterion Covered Total %
statement 30 30 100.0
branch 1 2 50.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 38 40 95.0


line stmt bran cond sub pod time code
1             package App::PerlNitpick::Rule::AddTrailingCommas;
2             # ABSTRACT: Put a comma at the end of every multi-line list declaration, including the last one.
3              
4 1     1   370093 use Moose;
  1         479212  
  1         7  
5 1     1   8031 use PPI::Document;
  1         3  
  1         41  
6 1     1   9 use PPI::Token::Operator;
  1         2  
  1         36  
7 1     1   755 use Perl::Critic::Policy::CodeLayout::RequireTrailingCommas;
  1         112299  
  1         45  
8 1     1   566 use App::PerlNitpick::PCPWrap;
  1         6  
  1         33  
9 1     1   532 use PPI::Dumper;
  1         1191  
  1         209  
10              
11             sub rewrite {
12 1     1 0 13393 my ($self, $doc) = @_;
13              
14 1         9 my $o = App::PerlNitpick::PCPWrap->new('Perl::Critic::Policy::CodeLayout::RequireTrailingCommas');
15              
16 1 50       11 my $elems = $doc->find( $o->applies_to ) or return $doc;
17 1         930 my @vio = map { $o->violates($_, $doc) } @$elems;
  1         8  
18              
19 1         5 for (@vio) {
20 1         5 my ($msg, $explain, $el) = @$_;
21              
22             # $el is a PPI::Structure::List. In this loop, we must match
23             # what Perl::Critic::Policy::CodeLayout::RequireTrailingCommas
24             # is doing in order to correctly navigate ourself to the final
25             # non-whitespace element inside this list -- which is
26             # something other than a comma.
27 1         3 my $expr = $el->schild(0);
28 1         18 my @children = $expr->schildren();
29 1         16 my $final = $children[-1];
30              
31             # Insert a new comma right after the last non-whitespace
32             # child, and before all trailing whitespace children.
33 1         4 $final->insert_after( PPI::Token::Operator->new(",") );
34             }
35              
36 1         112 return $doc;
37             }
38              
39             1;