File Coverage

blib/lib/Babble/Plugin/State.pm
Criterion Covered Total %
statement 29 29 100.0
branch 7 8 87.5
condition n/a
subroutine 3 3 100.0
pod 0 1 0.0
total 39 41 95.1


line stmt bran cond sub pod time code
1             package Babble::Plugin::State;
2              
3 1     1   70799 use Moo;
  1         7325  
  1         5  
4              
5             sub transform_to_plain {
6 6     6 0 6644 my ($self, $top) = @_;
7 6         33 $top->remove_use_argument(feature => 'state');
8 12     12   36 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         29 my ($m) = @_;
23 6         66 my $st = $m->subtexts;
24 6         20 push @states, $st;
25 6 100       30 if (my $assigns = $st->{assigns}) {
26 2         52 my $genlex = '$'.$m->gensym;
27             my $text = '('
28             .$genlex
29             .' ? '.$st->{declares}
30 2         12 .' : ++'.$genlex.' and '.$st->{declares}.' = '.$assigns
31             .')';
32 2         6 push @gensym, $genlex;
33 2         11 $m->replace_text($text);
34 2         6 return;
35             }
36 4         32 $m->replace_text('do { no warnings qw(void); '.$st->{declares}.' }');
37 6         101 });
38 6 50       65 if (@states) {
39             my $state_statements = join ' ',
40             (@gensym ? 'my ('.join(', ', @gensym).');' : ()),
41             (map {
42 6 100       33 'my '.$_->{type}.$_->{declares}
43 6 100       52 .($_->{attributes} ? ' '.$_->{attributes} : '')
44             .';'
45             } @states);
46             $m->transform_text(sub {
47 6         53 s/\A/${lead}{ ${state_statements} /;
48 6         37 s/\Z/ }/;
49 6         60 });
50             }
51 6         98 } };
  12         101  
52 6         26 $top->each_match_of(AnonymousSubroutine => $make_tf->('do '));
53 6         80 $top->each_match_of(SubroutineDeclaration => $make_tf->(''));
54             }
55              
56             1;
57             __END__
58              
59             =head1 NAME
60              
61             Babble::Plugin::State - Plugin for state keyword
62              
63             =head1 SYNOPSIS
64              
65             Converts usage of C<state> variable to C<my>.
66              
67             =head1 SEE ALSO
68              
69             L<state feature|feature/"The 'state' feature">
70              
71             =cut