File Coverage

blib/lib/App/PerlNitpick/Rule/MoreOrLessSpaces.pm
Criterion Covered Total %
statement 36 37 97.3
branch 20 30 66.6
condition 7 9 77.7
subroutine 5 5 100.0
pod 0 1 0.0
total 68 82 82.9


line stmt bran cond sub pod time code
1             use Moose;
2 1     1   289799 use PPI::Document;
  1         389806  
  1         6  
3 1     1   6566 use PPI::Token::Whitespace;
  1         2  
  1         23  
4 1     1   4  
  1         3  
  1         36  
5             no Moose;
6 1     1   5  
  1         2  
  1         4  
7             my ($self, $document) = @_;
8              
9 3     3 0 13874 for my $el (@{ $document->find('PPI::Token::Whitespace') ||[]}) {
10             next if $el->parent->isa('PPI::Statement');
11 3 50       7 next unless $el->content =~ m/\A +\n( *)/;
  3         10  
12 13 100       1747 $el->set_content("\n$1");
13 10 100       64 }
14 1         16  
15             for my $el (@{ $document->find('PPI::Token::Whitespace') ||[]}) {
16             next if $el->parent->isa('PPI::Statement');
17 3 50       14  
  3         12  
18 13 100       1755 my $prev1 = $el->previous_sibling or next;
19             my $prev2 = $prev1->previous_sibling or next;
20 10 100       59 next unless $prev1->isa('PPI::Token::Whitespace') && $prev1->content eq "\n" && $prev2->isa('PPI::Token::Whitespace') && $prev2->content eq "\n";
21 8 50       146 if ($el->content eq "\n") {
22 8 100 66     141 $el->delete;
      100        
      66        
23 2 50       23 } elsif ($el->content =~ m/\A\n( +)\z/) {
    0          
24 2         23 $el->set_content("$1");
25             }
26 0         0 }
27              
28             for my $el0 (@{ $document->find('PPI::Structure::List') ||[]}) {
29             for my $el (@{ $el0->find('PPI::Token::Operator') ||[]}) {
30 3 100       15 next unless $el->content eq ',';
  3         16  
31 2 50       732 my $next_el = $el->next_sibling or next;
  2         9  
32 4 50       583 unless ($next_el->isa('PPI::Token::Whitespace')) {
33 4 50       27 # Insert a new one.
34 4 50       86 my $wht = PPI::Token::Whitespace->new(' ');
35             $el->insert_after($wht);
36 4         27 }
37 4         40 }
38             }
39              
40             return $document;
41             }
42 3         849  
43             1;
44              
45              
46             =head1 MoreOrLessSpaces
47              
48             In this rule, space characters is inserted or removed within one line
49             between punctuation boundaries.
50