« PerlNitpick/Rule/RemoveEffectlessUTF8Pragma.pm
↑ Summary
PerlNitpick/Rule/DedupeIncludeStatements.pm »
| 1 | package App::PerlNitpick::Rule::RemoveUnusedInclude; | ||
| 2 | # ABSTRACT: Remove unused include statements. | ||
| 3 | |||
| 4 | =encoding UTF-8 | ||
| 5 | |||
| 6 | =head2 DESCRIPTION | ||
| 7 | |||
| 8 | This nitpicking rules removes include statements that are actually un-used. | ||
| 9 | |||
| 10 | =cut | ||
| 11 | |||
| 12 | use Moose; | ||
| 13 | use PPI::Document; | ||
| 14 | use Perl::Critic::Document; | ||
| 15 | use Perl::Critic::Policy::TooMuchCode::ProhibitUnusedInclude; | ||
| 16 | use App::PerlNitpick::PCPWrap; | ||
| 17 | |||
| 18 | no Moose; | ||
| 19 | |||
| 20 | sub rewrite { | ||
| 21 | my ($self, $doc) = @_; | ||
| 22 | |||
| 23 | my $o = App::PerlNitpick::PCPWrap->new('Perl::Critic::Policy::TooMuchCode::ProhibitUnusedInclude'); | ||
| 24 | |||
| 25 | my @vio = $o->violates( | ||
| 26 | $doc, | ||
| 27 | Perl::Critic::Document->new(-source => $doc) | ||
| 28 | ); | ||
| 29 | |||
| 30 | for (@vio) { | ||
| 31 | my ($msg, $explain, $el) = @$_; | ||
| 32 | my $next_space = $el->next_sibling; | ||
| 33 | $next_space = undef unless $next_space && $next_space->isa('PPI::Token::Whitespace'); | ||
| 34 | |||
| 35 | if ($next_space && $next_space->content eq "\n") { | ||
| 36 | $next_space->remove; | ||
| 37 | } | ||
| 38 | $el->remove; | ||
| 39 | } | ||
| 40 | |||
| 41 | return $doc; | ||
| 42 | } | ||
| 43 | |||
| 44 | 1; |