File Coverage

blib/lib/Perl/ToPerl6/Transformer/PostfixExpressions/AddWhitespace.pm
Criterion Covered Total %
statement 25 28 89.2
branch 2 2 100.0
condition n/a
subroutine 12 13 92.3
pod 3 5 60.0
total 42 48 87.5


line stmt bran cond sub pod time code
1             package Perl::ToPerl6::Transformer::PostfixExpressions::AddWhitespace;
2              
3 17     17   12235 use 5.006001;
  17         55  
4 17     17   88 use strict;
  17         27  
  17         442  
5 17     17   77 use warnings;
  17         25  
  17         460  
6 17     17   73 use Readonly;
  17         27  
  17         891  
7              
8 17     17   83 use Perl::ToPerl6::Utils qw{ :characters :severities };
  17         32  
  17         1015  
9 17     17   4737 use Perl::ToPerl6::Utils::PPI qw{ is_ppi_token_word };
  17         36  
  17         885  
10              
11 17     17   89 use base 'Perl::ToPerl6::Transformer';
  17         36  
  17         5607  
12              
13             our $VERSION = '0.03';
14              
15             #-----------------------------------------------------------------------------
16              
17             Readonly::Scalar my $DESC => q{Transform 'if()' to 'if ()'};
18             Readonly::Scalar my $EXPL =>
19             q{if(), elsif() and unless() need whitespace in order to not be interpreted as function calls};
20              
21             #-----------------------------------------------------------------------------
22              
23             my %map = (
24             if => 1,
25             elsif => 1,
26             unless => 1,
27             while => 1,
28             until => 1,
29             for => 1,
30             foreach => 1,
31             );
32              
33             #-----------------------------------------------------------------------------
34              
35 40     40 0 1590 sub supported_parameters { return () }
36 28     28 1 109 sub default_severity { return $SEVERITY_HIGHEST }
37 25     25 1 83 sub default_themes { return qw(core bugs) }
38             sub applies_to {
39             return sub {
40 61 100   61   969 is_ppi_token_word($_[1],%map) and
41             $_[1]->next_sibling->isa('PPI::Structure::Condition')
42             }
43 4     4 1 23 }
44              
45             #-----------------------------------------------------------------------------
46              
47             sub transform {
48 0     0 0   my ($self, $elem, $doc) = @_;
49              
50 0           $elem->insert_after(
51             PPI::Token::Whitespace->new(' ')
52             );
53              
54 0           return $self->transformation( $DESC, $EXPL, $elem );
55             }
56              
57             1;
58              
59             #-----------------------------------------------------------------------------
60              
61             __END__
62              
63             =pod
64              
65             =head1 NAME
66              
67             Perl::ToPerl6::Transformer::CompoundStatements::AddWhitespace - Add whitespace between conditionals 'if', 'unless' &c and ()
68              
69              
70             =head1 AFFILIATION
71              
72             This Transformer is part of the core L<Perl::ToPerl6|Perl::ToPerl6>
73             distribution.
74              
75              
76             =head1 DESCRIPTION
77              
78             While Perl6 conditionals allow parentheses, they need whitespace between the bareword C<if> and the opening parenthesis to avoid being interpreted as a function call:
79              
80             if(1) { } elsif(){} --> if (1) { } elsif(){}
81             if (1) { } elsif(){} --> if (1) { } elsif(){}
82              
83             =head1 CONFIGURATION
84              
85             This Transformer is not configurable except for the standard options.
86              
87             =head1 AUTHOR
88              
89             Jeffrey Goff <drforr@pobox.com>
90              
91             =head1 COPYRIGHT
92              
93             Copyright (c) 2015 Jeffrey Goff
94              
95             This program is free software; you can redistribute it and/or modify
96             it under the same terms as Perl itself.
97              
98             =cut
99              
100             ##############################################################################
101             # Local Variables:
102             # mode: cperl
103             # cperl-indent-level: 4
104             # fill-column: 78
105             # indent-tabs-mode: nil
106             # c-indentation-style: bsd
107             # End:
108             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :