line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::PerlNitpick::Rule::AppendUnimportStatement; |
2
|
|
|
|
|
|
|
# ABSTRACT: Ensure a 'no Moose;' statement is there if 'use Moose;' is. |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=encoding UTF-8 |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 DESCRIPTION |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
This nitpicking rule ensure a 'no Moose' statement is present in the file if a 'use Moose' is there. |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=cut |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
239154
|
use Moose; |
|
1
|
|
|
|
|
478613
|
|
|
1
|
|
|
|
|
6
|
|
13
|
1
|
|
|
1
|
|
8468
|
use PPI::Document; |
|
1
|
|
|
|
|
117878
|
|
|
1
|
|
|
|
|
402
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub rewrite { |
16
|
6
|
|
|
6
|
0
|
40862
|
my ($self, $doc) = @_; |
17
|
|
|
|
|
|
|
|
18
|
6
|
|
|
|
|
14
|
for my $module ('Moose', 'Mouse', 'Moo', 'Moose::Role', 'Mouse::Role') { |
19
|
30
|
100
|
|
|
|
140
|
if ($self->has_import_but_has_no_unimport($doc, $module)) { |
20
|
5
|
|
|
|
|
17
|
$self->append_unimport($doc, $module); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
6
|
|
|
|
|
76
|
return $doc; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub has_import_but_has_no_unimport { |
28
|
30
|
|
|
30
|
0
|
64
|
my ($self, $doc, $module) = @_; |
29
|
30
|
|
50
|
855
|
|
141
|
my $include_statements = $doc->find(sub { $_[1]->isa('PPI::Statement::Include') }) || []; |
|
855
|
|
|
|
|
9551
|
|
30
|
|
|
|
|
|
|
|
31
|
30
|
|
|
|
|
378
|
my ($has_use, $has_no); |
32
|
30
|
|
|
|
|
63
|
for my $st (@$include_statements) { |
33
|
40
|
100
|
|
|
|
317
|
next unless $st->module eq $module; |
34
|
5
|
50
|
|
|
|
124
|
if ($st->type eq 'use') { |
|
|
0
|
|
|
|
|
|
35
|
5
|
|
|
|
|
104
|
$has_use = 1; |
36
|
|
|
|
|
|
|
} elsif ($st->type eq 'no') { |
37
|
0
|
|
|
|
|
0
|
$has_no = 1; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
30
|
|
66
|
|
|
722
|
return $has_use && !$has_no; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub append_unimport { |
44
|
5
|
|
|
5
|
0
|
15
|
my ($self, $doc, $module) = @_; |
45
|
|
|
|
|
|
|
|
46
|
5
|
|
|
|
|
22
|
my $doc2 = PPI::Document->new(\"no ${module};"); |
47
|
5
|
|
|
|
|
4040
|
my $el = $doc2->find_first('PPI::Statement::Include'); |
48
|
5
|
|
|
|
|
965
|
$el->remove; |
49
|
|
|
|
|
|
|
|
50
|
5
|
|
|
|
|
208
|
my @child = $doc->schildren(); |
51
|
5
|
|
|
|
|
116
|
$child[-1]->insert_before($el); |
52
|
5
|
|
|
|
|
265
|
$child[-1]->insert_before(PPI::Token::Whitespace->new("\n")); |
53
|
|
|
|
|
|
|
|
54
|
5
|
|
|
|
|
283
|
return $doc; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
1; |