« PerlNitpick/Rule/RewriteWithAssignmentOperators.pm
↑ Summary
PerlNitpick/Rule/RemoveEffectlessUTF8Pragma.pm »
| 1 | package App::PerlNitpick::Rule::RemoveUnusedImport; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Remove unused import | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | This nitpicking rule removes imports that are explicitly put there, but | ||||||||||||||||||||||||||||||||||||||||||
| 9 | not used in the same file. | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | For example, C<Dumper> is not used this simple program: | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | use Data::Dumper 'Dumper'; | ||||||||||||||||||||||||||||||||||||||||||
| 14 | print 42; | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | And it will be removed by this program. | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use PPI::Document; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use PPIx::Utils qw(is_function_call); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | has idx => ( | |||||||||||||||||||||||||||||||||||||||||
| 25 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | required => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 2 | ▶ | sub rewrite { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rewrite: called CC=4 SCAR=14.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 2 | my ($self, $doc) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | $self->_build_idx($doc); | |||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | my @violations = $self->find_violations($doc); | |||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | for my $tuple (@violations) { | |||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | my ($word, $import) = @$tuple; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | my @args_literal = $import->{expr_qw}->literal; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | my @new_args_literal = grep { $_ ne $word } @args_literal; | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | if (@new_args_literal == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | $import->{expr_qw}{content} = 'qw()'; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | $import->{expr_qw}{sections}[0]{size} = length($import->{expr_qw}{content}); | |||||||||||||||||||||||||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # These 3 lines should probably be moved to the internal of PPI::Token::QuoteLike::Word | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 3 | $import->{expr_qw}{content} =~ s/\s ${word} \s/ /gsx; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 3 | $import->{expr_qw}{content} =~ s/\b ${word} \s//gsx; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 3 | $import->{expr_qw}{content} =~ s/\s ${word} \b//gsx; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | $import->{expr_qw}{content} =~ s/\b ${word} \b//gsx; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | $import->{expr_qw}{sections}[0]{size} = length($import->{expr_qw}{content}); | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | my @new_args_literal = $import->{expr_qw}->literal; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | if (@new_args_literal == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | $import->{expr_qw}{content} = 'qw()'; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | $import->{expr_qw}{sections}[0]{size} = length($import->{expr_qw}{content}); | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | return $doc; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | sub _build_idx { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_idx: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | my ($self, $doc) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | my $idx = { | |||||||||||||||||||||||||||||||||||||||||
| 64 | used_count => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 65 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | for my $el (@{ $doc->find( sub { $_[1]->isa('PPI::Token::Word') }) ||[]}) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition50%1/2
$doc->find(sub {
$_[1]->isa('PPI::Token::Word');
}
) || []
MC/DC0%0/1
$doc->find(sub {
$_[1]->isa('PPI::Token::Word');
}
) || []
$doc->find(sub {
$_[1]->isa('PPI::Token::Word');
}
)
Truth table50%1/2
A
A= $doc->find(sub {
$_[1]->isa('PPI::Token::Word');
}
)
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 9 | ▶ | unless ($el->parent->isa('PPI::Statement::Include') && (!$el->sprevious_sibling || $el->sprevious_sibling eq "use")) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$el->parent->isa("PPI::Statement::Include") and !$el->sprevious_sibling || $el->sprevious_sibling eq "use"
not $el->sprevious_sibling or $el->sprevious_sibling eq "use"
MC/DC33%1/3
$el->parent->isa("PPI::Statement::Include") and !$el->sprevious_sibling || $el->sprevious_sibling eq "use"
$el->parent->isa("PPI::Statement::Include")
not $el->sprevious_sibling
$el->sprevious_sibling eq "use"
Truth table75%3/4
A and not(B) or C
A= $el->parent->isa("PPI::Statement::Include")B= not $el->sprevious_siblingC= $el->sprevious_sibling eq "use"
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 5 | $idx->{used_count}{"$el"}++; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 5 | ▶ | if ($el =~ /::/ && is_function_call($el)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$el =~ /::/ and is_function_call($el)
MC/DC50%1/2
$el =~ /::/ and is_function_call($el)
$el =~ /::/
is_function_call($el)
Truth table66%2/3
A and B
A= $el =~ /::/B= is_function_call($el)
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | my ($module_name, $func_name) = $el =~ m/\A(.+)::([^:]+)\z/; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | $idx->{used_count}{$module_name}++; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | $idx->{used_count}{$func_name}++; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | $self->idx($idx); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | return $idx; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub looks_like_unused { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
looks_like_unused: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my ($self, $module_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | return ! $self->idx->{used_count}{$module_name}; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | ▶ | sub find_violations { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_violations: called CC=15 SCAR=28.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | my ($self, $elem) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 2 | my %imported; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 2 | my %is_special = map { $_ => 1 } qw(MouseX::Foreign); | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 2 | ▶ | my $include_statements = $elem->find(sub { $_[1]->isa('PPI::Statement::Include') }) || []; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Condition50%1/2
$elem->find(sub {
$_[1]->isa('PPI::Statement::Include');
}
) || []
MC/DC0%0/1
$elem->find(sub {
$_[1]->isa('PPI::Statement::Include');
}
) || []
$elem->find(sub {
$_[1]->isa('PPI::Statement::Include');
}
)
Truth table50%1/2
A
A= $elem->find(sub {
$_[1]->isa('PPI::Statement::Include');
}
)
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 2 | for my $st (@$include_statements) { | |||||||||||||||||||||||||||||||||||||||||
| 94 | 2 | ▶ | next unless $st->type eq 'use'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | my $included_module = $st->module; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 2 | ▶ | next if $included_module =~ /\A[a-z0-9:]+\Z/ || $is_special{"$included_module"}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$included_module =~ /\A[a-z0-9:]+\Z/ or $is_special{"$included_module"}
MC/DC0%0/2
$included_module =~ /\A[a-z0-9:]+\Z/ or $is_special{"$included_module"}
$included_module =~ /\A[a-z0-9:]+\Z/
$is_special{"$included_module"}
Truth table33%1/3
A or B
A= $included_module =~ /\A[a-z0-9:]+\Z/B= $is_special{"$included_module"}
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | ▶ | my $expr_qw = $st->find( sub { $_[1]->isa('PPI::Token::QuoteLike::Words'); }) or next; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | ▶ | if (@$expr_qw == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | my $expr = $expr_qw->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 2 | my $expr_str = "$expr"; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # Remove the quoting characters. | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 2 | substr($expr_str, 0, 3) = ''; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 2 | substr($expr_str, -1, 1) = ''; | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 2 | my @words = split ' ', $expr_str; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 2 | for my $w (@words) { | |||||||||||||||||||||||||||||||||||||||||
| 111 | 4 | ▶ | next if $w =~ /\A [:\-\+\$@%]/x; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | ▶ | push @{ $imported{$w} //=[] }, { | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$imported{$w} //= []
MC/DC0%0/1
$imported{$w} //= []
$imported{$w}
Truth table50%1/2
A
A= $imported{$w}
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | statement => $st, | ||||||||||||||||||||||||||||||||||||||||||
| 114 | expr_qw => $expr, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 2 | my %used; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | ▶ | for my $el_word (@{ $elem->find( sub { $_[1]->isa('PPI::Token::Word') }) ||[]}) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition50%1/2
$elem->find(sub {
$_[1]->isa('PPI::Token::Word');
}
) || []
MC/DC0%0/1
$elem->find(sub {
$_[1]->isa('PPI::Token::Word');
}
) || []
$elem->find(sub {
$_[1]->isa('PPI::Token::Word');
}
)
Truth table50%1/2
A
A= $elem->find(sub {
$_[1]->isa('PPI::Token::Word');
}
)
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 9 | $used{"$el_word"}++; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 2 | my @violations; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | my @to_report = grep { !$used{$_} } (sort keys %imported); | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 2 | for my $tok (@to_report) { | |||||||||||||||||||||||||||||||||||||||||
| 129 | 3 | for my $import (@{ $imported{$tok} }) { | |||||||||||||||||||||||||||||||||||||||||
| 130 | 3 | push @violations, [ $tok, $import ]; | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 2 | return @violations; | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||