lib/Perl/ToPerl6/Annotation.pm

filter: Statementstmt 87.8% 101 / 115 Branchbran 72.7% 32 / 44 Conditioncond 50.0% 14 / 28 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 89.4% 17 / 19 Podpod 100% 8 / 8 totaltotal 75.8% 176 / 232 CC 34 SCAR 23.8
CC
34
Cov
79%
_init
22.3
_is_single_line_annotation_on_simple_statement
11.2
disables_transformer
5.2
CRAP 10.8
« lib/Perl/ToPerl6/Utils.pm ↑ Summary lib/Perl/ToPerl6/Transformer/BasicTypes/Strings/RewriteHereDocs.pm »
1 package Perl::ToPerl6::Annotation;
2  
3 13 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 ▶use Perl::ToPerl6::TransformerFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Perl::ToPerl6::Utils qw(:characters hashify);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 #-----------------------------------------------------------------------------
15  
16 13 Readonly::Scalar my $LAST_ELEMENT => -1;
17  
18 #=============================================================================
19 # CLASS methods
20  
21 12 ▶sub create_annotations {
Subroutine create_annotations: called CC=3 SCAR=11.0
Pod documented
22 12 my ($class, $doc) = @_;
23  
24 12 my @annotations = ();
25 12 ▶ my $comment_elements_ref = $doc->find('PPI::Token::Comment') || return;
Condition100%2/2
$doc->find('PPI::Token::Comment') || (return)
l!l
75
MC/DC100%1/1
$doc->find('PPI::Token::Comment') || (return)
$doc->find('PPI::Token::Comment')
Truth table100%2/2
A
A= $doc->find('PPI::Token::Comment')
Aresult
11
00
26 7 my $annotation_rx = qr{\A (?: [#]! .*? )? \s* [#][#] \s* no \s+ mogrify}xms;
27 7 for my $annotation_element ( grep { $_ =~ $annotation_rx } @{$comment_elements_ref} ) {
28 13 push @annotations, Perl::ToPerl6::Annotation->new( -element => $annotation_element);
29 }
30  
31 7 return @annotations;
32 }
33  
34 #-----------------------------------------------------------------------------
35  
36 13 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
37 13 my ($class, @args) = @_;
38 13 my $self = bless {}, $class;
39 13 $self->_init(@args);
40 13 return $self;
41 }
42  
43 #=============================================================================
44 # OBJECT methods
45  
46 13 ▶sub _init {
Subroutine _init: called CC=18 SCAR=31.0
47 13 my ($self, %args) = @_;
48 13 ▶ my $annotation_element = $args{-element} || confess '-element argument is required';
Condition50%1/2
$args{'-element'} || confess('-element argument is required')
l!l
130
MC/DC0%0/1
$args{'-element'} || confess('-element argument is required')
$args{'-element'}
Truth table50%1/2
A
A= $args{'-element'}
Aresult
11
00
49 13 $self->{_element} = $annotation_element;
50  
51 13 my %disabled_transformers = _parse_annotation( $annotation_element );
52 13 ▶ $self->{_disables_all_transformers} = %disabled_transformers ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
%disabled_transformers ? : 11 2 13
53 13 $self->{_disabled_transformers} = \%disabled_transformers;
54  
55 # Grab surrounding nodes to determine the context.
56 # This determines whether the annotation applies to
57 # the current line or the block that follows.
58 13 my $annotation_line = $annotation_element->logical_line_number();
59 13 my $parent = $annotation_element->parent();
60 13 ▶ my $grandparent = $parent ? $parent->parent() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$parent ? : 13 0 13
61  
62 # Handle case when it appears on the shebang line. In this
63 # situation, it only affects the first line, not the whole doc
64 13 ▶ if ( $annotation_element =~ m{\A [#]!}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($annotation_element =~ /\A [#]!/msx) 0 13 13
65 0 $self->{_effective_range} = [$annotation_line, $annotation_line];
66 0 return $self;
67 }
68  
69 # Handle single-line usage on simple statements. In this
70 # situation, it only affects the line that it appears on.
71 13 ▶ if ( _is_single_line_annotation_on_simple_statement( $annotation_element )
Branch: 1 branch
BranchTrueFalseTotal
if (_is_single_line_annotation_on_simple_statement($annotation_element)) 1 12 13
72 ) {
73 1 $self->{_effective_range} = [$annotation_line, $annotation_line];
74 1 return $self;
75 }
76  
77 # Handle single-line usage on compound statements. In this
78 # situation -- um -- I'm not sure how this works, but it does.
79 12 ▶ if ( ref $parent eq 'PPI::Structure::Block' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parent eq "PPI::Structure::Block") 1 11 12
80 1 ▶ if ( ref $grandparent eq 'PPI::Statement::Compound'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $grandparent eq "PPI::Statement::Compound" or ref $grandparent eq "PPI::Statement::Sub") 1 0 1
Condition33%1/3
ref $grandparent eq "PPI::Statement::Compound" or ref $grandparent eq "PPI::Statement::Sub"
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $grandparent eq "PPI::Statement::Compound" or ref $grandparent eq "PPI::Statement::Sub"
ref $grandparent eq "PPI::Statement::Compound" ref $grandparent eq "PPI::Statement::Sub"
Truth table33%1/3
A or B
A= ref $grandparent eq "PPI::Statement::Compound"B= ref $grandparent eq "PPI::Statement::Sub"
ABresult
1X1
011
000
81 || ref $grandparent eq 'PPI::Statement::Sub' ) {
82 1 ▶ if ( $parent->logical_line_number() == $annotation_line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->logical_line_number == $annotation_line) 0 1 1
83 0 my $grandparent_line = $grandparent->logical_line_number();
84 0 $self->{_effective_range} = [$grandparent_line, $grandparent_line];
85 0 return $self;
86 }
87 }
88 }
89  
90  
91 # Handle multi-line usage. This is either a "no mogrify" ..
92 # "use mogrify" region or a block where "no mogrify" is in effect
93 # until the end of the scope. The start is the always the "no
94 # mogrify" which we already found. So now we have to search for the end.
95 12 my $end = $annotation_element;
96 12 my $use_mogrify = qr{\A \s* [#][#] \s* use \s+ mogrify}xms;
97  
98 SIB:
99 12 ▶ while ( my $esib = $end->next_sibling() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $esib = $end->next_sibling) 24 4 28
100 24 $end = $esib; # keep track of last sibling encountered in this scope
101 24 ▶ last SIB if $esib->isa('PPI::Token::Comment') && $esib =~ $use_mogrify;
Branch: 1 branch
BranchTrueFalseTotal
if $esib->isa("PPI::Token::Comment") and $esib =~ /$use_mogrify/ 8 16 24
Condition66%2/3
$esib->isa("PPI::Token::Comment") and $esib =~ /$use_mogrify/
!ll&&!rl&&r
1608
MC/DC50%1/2
$esib->isa("PPI::Token::Comment") and $esib =~ /$use_mogrify/
$esib->isa("PPI::Token::Comment") $esib =~ /$use_mogrify/
Truth table66%2/3
A and B
A= $esib->isa("PPI::Token::Comment")B= $esib =~ /$use_mogrify/
ABresult
0X0
100
111
102 }
103  
104 # PPI parses __END__ as a PPI::Statement::End, and everything following is
105 # a child of that statement. That means if we encounter an __END__, we
106 # need to descend into it and continue the analysis.
107 12 ▶ if ( $end->isa( 'PPI::Statement::End' ) and my $kid = $end->child( 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end->isa("PPI::Statement::End") and my $kid = $end->child(0)) 1 11 12
Condition66%2/3
$end->isa("PPI::Statement::End") and my $kid = $end->child(0)
!ll&&!rl&&r
1101
MC/DC50%1/2
$end->isa("PPI::Statement::End") and my $kid = $end->child(0)
$end->isa("PPI::Statement::End") my $kid = $end->child(0)
Truth table66%2/3
A and B
A= $end->isa("PPI::Statement::End")B= my $kid = $end->child(0)
ABresult
0X0
100
111
108 1 $end = $kid;
109 SIB:
110 1 ▶ while ( my $esib = $end->next_sibling() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $esib = $end->next_sibling) 3 1 4
111 3 $end = $esib;
112 3 ▶ last SIB if $esib->isa( 'PPI::Token::Comment' ) &&
Branch: 1 branch
BranchTrueFalseTotal
if $esib->isa("PPI::Token::Comment") and $esib->content =~ /$use_mogrify/ 0 3 3
Condition33%1/3
$esib->isa("PPI::Token::Comment") and $esib->content =~ /$use_mogrify/
!ll&&!rl&&r
300
MC/DC0%0/2
$esib->isa("PPI::Token::Comment") and $esib->content =~ /$use_mogrify/
$esib->isa("PPI::Token::Comment") $esib->content =~ /$use_mogrify/
Truth table33%1/3
A and B
A= $esib->isa("PPI::Token::Comment")B= $esib->content =~ /$use_mogrify/
ABresult
0X0
100
111
113 $esib->content() =~ $use_mogrify;
114 }
115 }
116  
117 # We either found an end or hit the end of the scope.
118 12 my $ending_line = $end->logical_line_number();
119 12 $self->{_effective_range} = [$annotation_line, $ending_line];
120 12 return $self;
121 }
122  
123 #-----------------------------------------------------------------------------
124  
125 0 ▶sub element {
Subroutine element: not called CC=1 SCAR=6.9
Pod documented
126 0 my ($self) = @_;
127 0 return $self->{_element};
128 }
129  
130 #-----------------------------------------------------------------------------
131  
132 12 ▶sub effective_range {
Subroutine effective_range: called CC=1 SCAR=0.0
Pod documented
133 12 my $self = shift;
134 12 return @{ $self->{_effective_range} };
135 }
136  
137 #-----------------------------------------------------------------------------
138  
139 0 ▶sub disabled_transformers {
Subroutine disabled_transformers: not called CC=1 SCAR=6.9
Pod documented
140 0 my $self = shift;
141 0 return keys %{ $self->{_disabled_transformers} };
142 }
143  
144 #-----------------------------------------------------------------------------
145  
146 8 ▶sub disables_transformer {
Subroutine disables_transformer: called CC=3 SCAR=16.5
Pod documented
147 8 my ($self, $transformer_name) = @_;
148 8 ▶ return 1 if $self->{_disabled_transformers}->{$transformer_name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_disabled_transformers"}{$transformer_name} 8 0 8
149 0 ▶ return 1 if $self->disables_all_transformers();
Branch: 1 branch
BranchTrueFalseTotal
if $self->disables_all_transformers 0 0 0
150 0 return 0;
151 }
152  
153 #-----------------------------------------------------------------------------
154  
155 12 ▶sub disables_all_transformers {
Subroutine disables_all_transformers: called CC=1 SCAR=0.0
Pod documented
156 12 my ($self) = @_;
157 12 return $self->{_disables_all_transformers};
158 }
159  
160 #-----------------------------------------------------------------------------
161  
162 13 ▶sub disables_line {
Subroutine disables_line: called CC=3 SCAR=13.3
Pod documented
163 13 my ($self, $line_number) = @_;
164 13 my $effective_range = $self->{_effective_range};
165 13 ▶ return 1 if $line_number >= $effective_range->[0]
Branch: 1 branch
BranchTrueFalseTotal
if $line_number >= $effective_range->[0] and $line_number <= $effective_range->[$LAST_ELEMENT] 13 0 13
Condition33%1/3
$line_number >= $effective_range->[0] and $line_number <= $effective_range->[$LAST_ELEMENT]
!ll&&!rl&&r
0013
MC/DC0%0/2
$line_number >= $effective_range->[0] and $line_number <= $effective_range->[$LAST_ELEMENT]
$line_number >= $effective_range->[0] $line_number <= $effective_range->[$LAST_ELEMENT]
Truth table33%1/3
A and B
A= $line_number >= $effective_range->[0]B= $line_number <= $effective_range->[$LAST_ELEMENT]
ABresult
0X0
100
111
166 and $line_number <= $effective_range->[$LAST_ELEMENT];
167 0 return 0;
168 }
169  
170 #-----------------------------------------------------------------------------
171  
172 # Recognize a single-line annotation on a simple statement.
173 13 ▶sub _is_single_line_annotation_on_simple_statement {
Subroutine _is_single_line_annotation_on_simple_statement: called CC=9 SCAR=24.1
174 13 my ( $annotation_element ) = @_;
175 13 my $annotation_line = $annotation_element->logical_line_number();
176  
177 # If there is no sibling, we are clearly not a single-line annotation of
178 # any sort.
179 13 ▶ my $sib = $annotation_element->sprevious_sibling()
Branch: 1 branch
BranchTrueFalseTotal
my $sib = $annotation_element->sprevious_sibling or return 0 1 12 13
180 or return 0;
181  
182 # The easy case: the sibling (whatever it is) is on the same line as the
183 # annotation.
184 1 ▶ $sib->logical_line_number() == $annotation_line
Branch: 1 branch
BranchTrueFalseTotal
$sib->logical_line_number == $annotation_line and return 1 0 1 1
185 and return 1;
186  
187 # If the sibling is a node, we may have an annotation on one line of a
188 # statement that was split over multiple lines. So we descend through the
189 # children, keeping the last significant child of each, until we bottom
190 # out. If the ultimate significant descendant is on the same line as the
191 # annotation, we accept the annotation as a single-line annotation.
192 1 ▶ if ( $sib->isa( 'PPI::Node' ) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($sib->isa("PPI::Node") and $sib->logical_line_number < $annotation_line) 1 0 1
Condition33%1/3
$sib->isa("PPI::Node") and $sib->logical_line_number < $annotation_line
!ll&&!rl&&r
001
MC/DC0%0/2
$sib->isa("PPI::Node") and $sib->logical_line_number < $annotation_line
$sib->isa("PPI::Node") $sib->logical_line_number < $annotation_line
Truth table33%1/3
A and B
A= $sib->isa("PPI::Node")B= $sib->logical_line_number < $annotation_line
ABresult
0X0
100
111
193 $sib->logical_line_number() < $annotation_line
194 ) {
195 1 my $neighbor = $sib;
196 1 ▶ while ( $neighbor->isa( 'PPI::Node' )
Branch: 1 branch
BranchTrueFalseTotal
if ($neighbor->isa("PPI::Node") and my $kid = $neighbor->schild($LAST_ELEMENT)) 1 1 2
Condition66%2/3
$neighbor->isa("PPI::Node") and my $kid = $neighbor->schild($LAST_ELEMENT)
!ll&&!rl&&r
101
MC/DC50%1/2
$neighbor->isa("PPI::Node") and my $kid = $neighbor->schild($LAST_ELEMENT)
$neighbor->isa("PPI::Node") my $kid = $neighbor->schild($LAST_ELEMENT)
Truth table66%2/3
A and B
A= $neighbor->isa("PPI::Node")B= my $kid = $neighbor->schild($LAST_ELEMENT)
ABresult
0X0
100
111
197 and my $kid = $neighbor->schild( $LAST_ELEMENT ) ) {
198 1 $neighbor = $kid;
199 }
200 1 ▶ if ( $neighbor &&
Branch: 1 branch
BranchTrueFalseTotal
if ($neighbor and $neighbor->logical_line_number == $annotation_line) 1 0 1
Condition33%1/3
$neighbor and $neighbor->logical_line_number == $annotation_line
!ll&&!rl&&r
001
MC/DC0%0/2
$neighbor and $neighbor->logical_line_number == $annotation_line
$neighbor $neighbor->logical_line_number == $annotation_line
Truth table33%1/3
A and B
A= $neighborB= $neighbor->logical_line_number == $annotation_line
ABresult
0X0
100
111
201 $neighbor->logical_line_number() == $annotation_line
202 ) {
203 1 return 1;
204 }
205 }
206  
207 # We do not understand any other sort of single-line annotation. Accepting
208 # the annotation as such (if it is) is Someone Else's Problem.
209 0 return 0;
210 }
211  
212 #-----------------------------------------------------------------------------
213  
214 13 ▶sub _parse_annotation {
Subroutine _parse_annotation: called CC=3 SCAR=11.0
215  
216 13 my ($annotation_element) = @_;
217  
218 #############################################################################
219 # This regex captures the list of Transformer name patterns that are to be
220 # disabled. It is generally assumed that the element has already been
221 # verified as a no-mogrify annotation. So if this regex does not match,
222 # then it implies that all Transformers are to be disabled.
223 #
224 13 my $no_mogrify = qr{\#\# \s* no \s+ mogrify \s* (?:qw)? [("'] ([\s\w:,]+) }xms;
225 # -------------------------- ------- ----- -----------
226 # | | | |
227 # "## no mogrify" with optional spaces | | |
228 # | | |
229 # Transformer list may be prefixed with "qw" | |
230 # | |
231 # Optional Transformer list must begin with one of these |
232 # |
233 # Capture entire Transformer list (with delimiters) here
234 #
235 #############################################################################
236  
237 13 my @disabled_transformer_names = ();
238 13 ▶ if ( my ($patterns_string) = $annotation_element =~ $no_mogrify ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($patterns_string) = $annotation_element =~ /$no_mogrify/) 11 2 13
239  
240 # Compose the specified modules into a regex alternation. Wrap each
241 # in a no-capturing group to permit "|" in the modules specification.
242  
243 11 my @transformer_name_patterns = grep { $_ ne $EMPTY }
244 split m{\s *[,\s] \s*}xms, $patterns_string;
245 11 my $re = join $PIPE, map {"(?:$_)"} @transformer_name_patterns;
246 11 my @site_transformer_names = Perl::ToPerl6::TransformerFactory::site_transformer_names();
247 11 @disabled_transformer_names = grep {m/$re/ixms} @site_transformer_names;
248  
249 # It is possible that the Transformer patterns listed in the annotation do not
250 # match any of the site transformer names. This could happen when running
251 # on a machine that does not have the same set of Transformers as the
252 # author.
253 # So we must return something here, otherwise all Transformers will be
254 # disabled. We probably need to add a mechanism to (optionally) warn
255 # about this, just to help the author avoid writing invalid Transformer names.
256  
257 11 ▶ if (not @disabled_transformer_names) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@disabled_transformer_names) 2 9 11
258 2 @disabled_transformer_names = @transformer_name_patterns;
259 }
260 }
261  
262 13 return hashify(@disabled_transformer_names);
263 }
264  
265 #-----------------------------------------------------------------------------
266  
267 13 1;
268  
269 __END__
« lib/Perl/ToPerl6/Utils.pm ↑ Summary lib/Perl/ToPerl6/Transformer/BasicTypes/Strings/RewriteHereDocs.pm »