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   69226 use Moo;
  1         7146  
  1         5  
4              
5             sub transform_to_plain {
6 6     6 0 8083 my ($self, $top) = @_;
7 6         33 $top->remove_use_argument(feature => 'state');
8 12     12   39 my $make_tf = sub { my ($lead) = @_; sub {
9 6         17 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         17 my ($m) = @_;
23 6         35 my $st = $m->subtexts;
24 6         19 push @states, $st;
25 6 100       25 if (my $assigns = $st->{assigns}) {
26 2         49 my $genlex = '$'.$m->gensym;
27             my $text = '('
28             .$genlex
29             .' ? '.$st->{declares}
30 2         11 .' : ++'.$genlex.' and '.$st->{declares}.' = '.$assigns
31             .')';
32 2         6 push @gensym, $genlex;
33 2         12 $m->replace_text($text);
34 2         6 return;
35             }
36 4         34 $m->replace_text('do { no warnings qw(void); '.$st->{declares}.' }');
37 6         79 });
38 6 50       63 if (@states) {
39             my $state_statements = join ' ',
40             (@gensym ? 'my ('.join(', ', @gensym).');' : ()),
41             (map {
42 6 100       32 'my '.$_->{type}.$_->{declares}
43 6 100       51 .($_->{attributes} ? ' '.$_->{attributes} : '')
44             .';'
45             } @states);
46             $m->transform_text(sub {
47 6         50 s/\A/${lead}{ ${state_statements} /;
48 6         33 s/\Z/ }/;
49 6         53 });
50             }
51 6         99 } };
  12         90  
52 6         23 $top->each_match_of(AnonymousSubroutine => $make_tf->('do '));
53 6         70 $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