File Coverage

blib/lib/Babble/Plugin/State.pm
Criterion Covered Total %
statement 29 31 93.5
branch 7 8 87.5
condition n/a
subroutine 3 4 75.0
pod 0 2 0.0
total 39 45 86.6


line stmt bran cond sub pod time code
1             package Babble::Plugin::State;
2              
3 1     1   69974 use Moo;
  1         7124  
  1         5  
4              
5             sub transform_to_plain {
6 6     6 0 6529 my ($self, $top) = @_;
7 6         28 $top->remove_use_argument(feature => 'state');
8 12     12   30 my $make_tf = sub { my ($lead) = @_; sub {
9 6         14 my ($m) = @_;
10 6         15 my @states;
11             my @gensym;
12             $m->each_match_within(Assignment => [
13             'state \b (?>(?&PerlOWS))',
14             [ type => '(?: (?&PerlQualifiedIdentifier) (?&PerlOWS) )?+' ],
15             [ declares => '(?>(?&PerlLvalue))' ],
16             '(?>(?&PerlOWS))',
17             [ attributes => '(?&PerlAttributes)?+' ],
18             '(?: (?>(?&PerlOWS)) = (?>(?&PerlOWS))',
19             [ assigns => '(?&PerlConditionalExpression)' ],
20             ')*+',
21             ] => sub {
22 6         22 my ($m) = @_;
23 6         42 my $st = $m->subtexts;
24 6         20 push @states, $st;
25 6 100       27 if (my $assigns = $st->{assigns}) {
26 2         65 my $genlex = '$'.$m->gensym;
27             my $text = '('
28             .$genlex
29             .' ? '.$st->{declares}
30 2         10 .' : ++'.$genlex.' and '.$st->{declares}.' = '.$assigns
31             .')';
32 2         5 push @gensym, $genlex;
33 2         9 $m->replace_text($text);
34 2         6 return;
35             }
36 4         35 $m->replace_text('do { no warnings qw(void); '.$st->{declares}.' }');
37 6         81 });
38 6 50       59 if (@states) {
39             my $state_statements = join ' ',
40             (@gensym ? 'my ('.join(', ', @gensym).');' : ()),
41             (map {
42 6 100       32 'my '.$_->{type}.$_->{declares}
43 6 100       62 .($_->{attributes} ? ' '.$_->{attributes} : '')
44             .';'
45             } @states);
46             $m->transform_text(sub {
47 6         60 s/\A/${lead}{ ${state_statements} /;
48 6         33 s/\Z/ }/;
49 6         52 });
50             }
51 6         93 } };
  12         80  
52 6         20 $top->each_match_of(AnonymousSubroutine => $make_tf->('do '));
53 6         62 $top->each_match_of(SubroutineDeclaration => $make_tf->(''));
54             }
55              
56             sub check_bail_out_early {
57 0     0 0   my ($self, $top) = @_;
58 0           $top->text !~ m/ \b state \b /xs;
59             }
60              
61             1;
62             __END__
63              
64             =head1 NAME
65              
66             Babble::Plugin::State - Plugin for state keyword
67              
68             =head1 SYNOPSIS
69              
70             Converts usage of C<state> variable to C<my>.
71              
72             =head1 SEE ALSO
73              
74             L<state feature|feature/"The 'state' feature">
75              
76             =cut