File Coverage

blib/lib/Perl/Lint/Policy/CodeLayout/RequireTrailingCommas.pm
Criterion Covered Total %
statement 47 47 100.0
branch 18 18 100.0
condition 11 12 91.6
subroutine 7 7 100.0
pod 0 1 0.0
total 83 85 97.6


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::CodeLayout::RequireTrailingCommas;
2 134     134   69759 use strict;
  134         185  
  134         3177  
3 134     134   420 use warnings;
  134         148  
  134         2596  
4 134     134   859 use Perl::Lint::Constants::Type;
  134         154  
  134         59631  
5 134     134   995 use Perl::Lint::Constants::Kind;
  134         162  
  134         7173  
6 134     134   596 use parent "Perl::Lint::Policy";
  134         221  
  134         1980  
7              
8             use constant {
9 134         40728 DESC => 'List declaration without trailing comma',
10             EXPL => [17],
11 134     134   6795 };
  134         347  
12              
13             sub evaluate {
14 8     8 0 13 my ($class, $file, $tokens, $src, $args) = @_;
15              
16 8         9 my @violations;
17 8         28 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
18 136         83 my $token_type = $token->{type};
19              
20 136 100       230 if ($token_type == ASSIGN) {
21 24         22 $token = $tokens->[++$i];
22              
23 24 100       39 if ($token->{type} == LEFT_PAREN) { # TODO enough?
24 22         20 my $begin_line = $token->{line};
25              
26 22         9 my $left_paren_num = 1;
27 22         18 my $num_of_item = 0;
28 22         13 my $is_nested = 0;
29 22         13 my $does_exist_procedure = 0;
30 22         15 my $does_exist_any_comma = 0;
31              
32 22         31 for ($i++; $token = $tokens->[$i]; $i++) {
33 138         97 $token_type = $token->{type};
34              
35 138 100       244 if ($token_type == LEFT_PAREN) {
    100          
    100          
    100          
36 2         2 $left_paren_num++;
37 2         3 $is_nested = 1;
38             }
39             elsif ($token_type == RIGHT_PAREN) {
40 24 100       39 if (--$left_paren_num <= 0) {
41 22         17 my $end_line = $token->{line};
42 22 100 100     116 if (
      66        
      100        
      100        
43             !$is_nested &&
44             (!$does_exist_procedure || $does_exist_any_comma) &&
45             $num_of_item > 1 &&
46             $end_line - $begin_line > 0
47             ) {
48 12         11 my $just_before_token = $tokens->[$i-1];
49 12 100       18 if ($just_before_token->{type} != COMMA) {
50             push @violations, {
51             filename => $file,
52             line => $token->{line},
53 9         28 description => DESC,
54             explanation => EXPL,
55             policy => __PACKAGE__,
56             };
57             }
58             }
59              
60 22         42 last;
61             }
62             }
63             elsif (
64             $token->{kind} == KIND_OP
65             ) {
66 9         12 $does_exist_procedure = 1;
67             }
68             elsif ($token_type == COMMA) {
69 34         45 $does_exist_any_comma = 1;
70             }
71             else {
72 69         89 $num_of_item++;
73             }
74             }
75             }
76             }
77             }
78              
79 8         26 return \@violations;
80             }
81              
82             1;
83