File Coverage

blib/lib/Perl/ToPerl6/Transformer/CompoundStatements/FormatGivenWhens.pm
Criterion Covered Total %
statement 22 25 88.0
branch 1 2 50.0
condition 2 6 33.3
subroutine 11 12 91.6
pod 3 5 60.0
total 39 50 78.0


line stmt bran cond sub pod time code
1             package Perl::ToPerl6::Transformer::CompoundStatements::FormatGivenWhens;
2              
3 17     17   11598 use 5.006001;
  17         54  
4 17     17   82 use strict;
  17         28  
  17         402  
5 17     17   77 use warnings;
  17         23  
  17         454  
6 17     17   95 use Readonly;
  17         25  
  17         1001  
7              
8 17     17   89 use Perl::ToPerl6::Utils qw{ :characters :severities };
  17         24  
  17         1017  
9              
10 17     17   4441 use base 'Perl::ToPerl6::Transformer';
  17         30  
  17         5487  
11              
12             our $VERSION = '0.03';
13              
14             #-----------------------------------------------------------------------------
15              
16             Readonly::Scalar my $DESC => q{Transform 'given()' to 'given ()'};
17             Readonly::Scalar my $EXPL =>
18             q{unless() needs whitespace in order to not be interpreted as a function call};
19              
20             #-----------------------------------------------------------------------------
21              
22             my %map = (
23             given => 1,
24             when => 1
25             );
26              
27             #-----------------------------------------------------------------------------
28              
29 40     40 0 1377 sub supported_parameters { return () }
30 29     29 1 116 sub default_severity { return $SEVERITY_HIGHEST }
31 25     25 1 84 sub default_themes { return qw(core bugs) }
32             sub applies_to {
33             return sub {
34             ( $_[1]->isa('PPI::Statement::Given') or
35             $_[1]->isa('PPI::Statement::When') ) and
36 61 50 33 61   1017 exists $map{$_[1]->first_element->content} and
      33        
37             not $_[1]->first_element->next_sibling->isa('PPI::Token::Whitespace');
38             }
39 4     4 1 23 }
40              
41             #-----------------------------------------------------------------------------
42              
43             #
44             # Note to the reader:
45             #
46             # PPI::Statement::{Given,When}
47             # \
48             # \ 0 1 2 # count by child()
49             # \0 1 2 # count by schild()
50             # +-----+-----+
51             # | | |
52             # given (...) {...}
53             #
54             # After insertion, the tree looks like this:
55             #
56             # PPI::Statement::{Given,When}
57             # \
58             # \ 0 1 2 3 # count by child()
59             # \0 1 2 # count by schild()
60             # +-----+-----+-----+
61             # | | | |
62             # given ' ' (...) {...}
63              
64             sub transform {
65 0     0 0   my ($self, $elem, $doc) = @_;
66              
67 0           $elem->first_element->insert_after(
68             PPI::Token::Whitespace->new(' ')
69             );
70              
71 0           return $self->transformation( $DESC, $EXPL, $elem );
72             }
73              
74             1;
75              
76             #-----------------------------------------------------------------------------
77              
78             __END__
79              
80             =pod
81              
82             =head1 NAME
83              
84             Perl::ToPerl6::Transformer::CompoundStatements::FormatGivenWhens - Format given(), when()
85              
86              
87             =head1 AFFILIATION
88              
89             This Transformer is part of the core L<Perl::ToPerl6|Perl::ToPerl6>
90             distribution.
91              
92              
93             =head1 DESCRIPTION
94              
95             While Perl6 conditionals allow parentheses, they need whitespace between the bareword C<given> and the opening parenthesis to avoid being interpreted as a function call:
96              
97             given(1) { } --> given (1) { }
98             when(1) { } --> when (1) { }
99             given (1) { } --> given (1) { }
100             when (1) { } --> when (1) { }
101              
102             =head1 CONFIGURATION
103              
104             This Transformer is not configurable except for the standard options.
105              
106             =head1 AUTHOR
107              
108             Jeffrey Goff <drforr@pobox.com>
109              
110             =head1 COPYRIGHT
111              
112             Copyright (c) 2015 Jeffrey Goff
113              
114             This program is free software; you can redistribute it and/or modify
115             it under the same terms as Perl itself.
116              
117             =cut
118              
119             ##############################################################################
120             # Local Variables:
121             # mode: cperl
122             # cperl-indent-level: 4
123             # fill-column: 78
124             # indent-tabs-mode: nil
125             # c-indentation-style: bsd
126             # End:
127             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :