File Coverage

blib/lib/Perl/ToPerl6/Transformer/CompoundStatements/RewriteMapGreps.pm
Criterion Covered Total %
statement 22 35 62.8
branch 0 10 0.0
condition 0 15 0.0
subroutine 9 14 64.2
pod 3 5 60.0
total 34 79 43.0


line stmt bran cond sub pod time code
1             package Perl::ToPerl6::Transformer::CompoundStatements::RewriteMapGreps;
2              
3 1     1   734 use 5.006001;
  1         3  
4 1     1   5 use strict;
  1         2  
  1         18  
5 1     1   5 use warnings;
  1         1  
  1         21  
6 1     1   4 use Readonly;
  1         2  
  1         41  
7              
8 1     1   5 use Perl::ToPerl6::Utils qw{ :severities };
  1         1  
  1         48  
9 1         54 use Perl::ToPerl6::Utils::PPI qw{
10             is_ppi_token_word
11             replace_remainder_with_block
12 1     1   106 };
  1         2  
13              
14 1     1   5 use base 'Perl::ToPerl6::Transformer';
  1         2  
  1         398  
15              
16             #-----------------------------------------------------------------------------
17              
18             Readonly::Scalar my $DESC => q{Transform 'given()' to 'given ()'};
19             Readonly::Scalar my $EXPL =>
20             q{unless() needs whitespace in order to not be interpreted as a function call};
21              
22             #-----------------------------------------------------------------------------
23              
24             my %map = (
25             map => 1,
26             grep => 1
27             );
28              
29             #-----------------------------------------------------------------------------
30              
31 1     1 0 3 sub supported_parameters { return () }
32 1     1 1 5 sub default_necessity { return $NECESSITY_HIGHEST }
33 0     0 1   sub default_themes { return qw( core ) }
34             sub applies_to {
35             return sub {
36 0 0   0     is_ppi_token_word($_[1], %map) and
37             $_[1]->snext_sibling
38             }
39 0     0 1   }
40              
41             #-----------------------------------------------------------------------------
42              
43             sub _is_end_of_expression {
44 0 0   0     $_[1]->isa('PPI::Token::Operator') and
45             $_[1]->content eq ',';
46             }
47              
48             sub transform {
49 0     0 0   my ($self, $elem, $doc) = @_;
50              
51             #
52             # XXX This is worrisome, as this test should not need to be done.
53             # XXX The applies_to() method above implies that $elem should have an
54             # XXX snext_sibling by the time it gets here.
55             #
56 0 0         return unless $elem->snext_sibling;
57 0           my $token = $elem->snext_sibling;
58              
59 0 0 0       if ( $token->isa('PPI::Structure::Block') and
      0        
      0        
      0        
60             $token->start and
61             $token->start eq '{' and
62             $token->finish and
63             $token->finish eq '}' ) {
64 0 0 0       return if $token->snext_sibling and
65             _is_end_of_expression(undef,$token->snext_sibling);
66 0           my $comma = PPI::Token::Operator->new(',');
67 0           $token->insert_after( $comma );
68             }
69             else {
70 0           replace_remainder_with_block($token, \&_is_end_of_expression );
71             }
72              
73 0           return $self->transformation( $DESC, $EXPL, $elem );
74             }
75              
76             1;
77              
78             #-----------------------------------------------------------------------------
79              
80             __END__
81              
82             =pod
83              
84             =head1 NAME
85              
86             Perl::ToPerl6::Transformer::CompoundStatements::RewriteMapGreps - Format map{}, grep{}
87              
88              
89             =head1 AFFILIATION
90              
91             This Transformer is part of the core L<Perl::ToPerl6|Perl::ToPerl6>
92             distribution.
93              
94              
95             =head1 DESCRIPTION
96              
97             Perl6 unifies C<map{}> and C<grep{}> with the rest of the function calls, in that the first argument must be a block, and the arguments must be separated with commas. This transformer adds the block where needed, and inserts the comma as required:
98              
99             map {$_++} @x; --> map {$_++}, @x;
100             map /x/ @x; --> map {/x/}, @x;
101             grep {$_++} @x; --> grep {$_++}, @x;
102             grep /x/ @x; --> grep {/x/}, @x;
103              
104             =head1 CONFIGURATION
105              
106             This Transformer is not configurable except for the standard options.
107              
108             =head1 AUTHOR
109              
110             Jeffrey Goff <drforr@pobox.com>
111              
112             =head1 COPYRIGHT
113              
114             Copyright (c) 2015 Jeffrey Goff
115              
116             This program is free software; you can redistribute it and/or modify
117             it under the same terms as Perl itself.
118              
119             =cut
120              
121             ##############################################################################
122             # Local Variables:
123             # mode: cperl
124             # cperl-indent-level: 4
125             # fill-column: 78
126             # indent-tabs-mode: nil
127             # c-indentation-style: bsd
128             # End:
129             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :