| 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 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use PPI::Document; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use PPI::Token::Operator; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Perl::Critic::Policy::CodeLayout::RequireTrailingCommas; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use App::PerlNitpick::PCPWrap; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use PPI::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | sub rewrite { | ||||||||
|
Subroutine
rewrite: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 12 | 1 | my ($self, $doc) = @_; | |||||||||
| 13 | |||||||||||
| 14 | 1 | my $o = App::PerlNitpick::PCPWrap->new('Perl::Critic::Policy::CodeLayout::RequireTrailingCommas'); | |||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | my $elems = $doc->find( $o->applies_to ) or return $doc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 1 | my @vio = map { $o->violates($_, $doc) } @$elems; | |||||||||
| 18 | |||||||||||
| 19 | 1 | for (@vio) { | |||||||||
| 20 | 1 | 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 | my $expr = $el->schild(0); | |||||||||
| 28 | 1 | my @children = $expr->schildren(); | |||||||||
| 29 | 1 | 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 | $final->insert_after( PPI::Token::Operator->new(",") ); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 1 | return $doc; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 1 | 1; | |||||||||