File Coverage

blib/lib/App/perlimports/Annotations.pm
Criterion Covered Total %
statement 36 37 97.3
branch 10 12 83.3
condition 11 17 64.7
subroutine 4 4 100.0
pod 1 1 100.0
total 62 71 87.3


line stmt bran cond sub pod time code
1             package App::perlimports::Annotations;
2              
3             # Some of this logic was lifted directly from Perl::Critic::Annotation
4              
5 73     73   2723 use Moo;
  73         10146  
  73         361  
6              
7             our $VERSION = '0.000051';
8              
9 73     73   68019 use Types::Standard qw( ArrayRef Object );
  73         8026974  
  73         738  
10              
11             has _annotations => (
12             is => 'ro',
13             isa => ArrayRef,
14             lazy => 1,
15             builder => '_build_annotations',
16             );
17              
18             has _ppi_document => (
19             is => 'ro',
20             isa => Object,
21             init_arg => 'ppi_document',
22             required => 1,
23             );
24              
25             sub _build_annotations {
26 47     47   919 my $self = shift;
27              
28 47         157 my @found = ();
29 47   100     449 my $comments
30             = $self->_ppi_document->find('PPI::Token::Comment') || return [];
31 8         19792 my $disable_rx = qr{[#][#] \s* no \s+ perlimports}xms;
32 8         38 my $enable_rx = qr{[#][#] \s* use \s+ perlimports}xms;
33              
34 8         20 my @enabled = ( grep { $_ =~ $enable_rx } @{$comments} );
  14         90  
  8         26  
35              
36 8         65 for my $element ( grep { $_ =~ $disable_rx } @{$comments} ) {
  14         68  
  8         23  
37              
38 7 100       40 my %found = (
39             column_number => $element->column_number,
40             line_number => $element->logical_line_number,
41             range => [
42             $element->logical_line_number,
43             $element->column_number > 1
44             ? ( $element->logical_line_number )
45             : ()
46             ],
47             );
48              
49             # Seek ahead to see if/when perlimpts is re-enabled
50 7 100       401 if ( $element->column_number == 1 ) {
51 3         45 for my $on (@enabled) {
52 2 50 33     5 if ( $on->column_number == 1
53             && $on->logical_line_number
54             > $element->logical_line_number ) {
55 2         85 $found{range}->[1] = $on->logical_line_number;
56             }
57             }
58             }
59 7         93 push @found, \%found;
60             }
61              
62 8         209 return \@found;
63             }
64              
65             sub is_ignored {
66 81     81 1 202137 my $self = shift;
67 81         216 my $element = shift;
68 81 50 33     1057 unless ( $element && ref($element) && $element->isa('PPI::Element') ) {
      33        
69 0         0 die 'PPI::Element required';
70             }
71              
72 81         606 my $line = $element->logical_line_number;
73 81         62895 for my $a ( @{ $self->_annotations } ) {
  81         2058  
74 32         256 my ( $min, $max ) = @{ $a->{range} };
  32         74  
75 32 100 100     138 if ( $line >= $min && ( !$max || $line <= $max ) ) {
      100        
76 14         84 return 1;
77             }
78              
79             # Any further annotations do not apply to this element
80 18 100       44 last if $line < $max;
81             }
82              
83 67         55616 return 0;
84             }
85              
86             1;
87              
88             # ABSTRACT: Find line ranges where perlimports has been disabled
89              
90             __END__
91              
92             =pod
93              
94             =encoding UTF-8
95              
96             =head1 NAME
97              
98             App::perlimports::Annotations - Find line ranges where perlimports has been disabled
99              
100             =head1 VERSION
101              
102             version 0.000051
103              
104             =head1 SYNOPSIS
105              
106             my $anno = App::perlimports::Annotations->new(
107             ppi_document => $ppi_doc
108             );
109              
110             print 'skip include' if $anno->is_ignored( $ppi_element );
111              
112             =head2 is_ignored( $ppi_element )
113              
114             Returns true if the provided L<PPI::Element> is in an ignored line range.
115              
116             =head1 AUTHOR
117              
118             Olaf Alders <olaf@wundercounter.com>
119              
120             =head1 COPYRIGHT AND LICENSE
121              
122             This software is copyright (c) 2020 by Olaf Alders.
123              
124             This is free software; you can redistribute it and/or modify it under
125             the same terms as the Perl 5 programming language system itself.
126              
127             =cut