File Coverage

blib/lib/Perl/Critic/Policy/CodeLayout/RequireTrailingCommas.pm
Criterion Covered Total %
statement 35 35 100.0
branch 14 14 100.0
condition 5 6 83.3
subroutine 11 11 100.0
pod 4 5 80.0
total 69 71 97.1


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::CodeLayout::RequireTrailingCommas;
2              
3 40     40   27233 use 5.010001;
  40         191  
4 40     40   266 use strict;
  40         116  
  40         894  
5 40     40   221 use warnings;
  40         114  
  40         1095  
6 40     40   261 use Readonly;
  40         114  
  40         2130  
7              
8 40     40   313 use Perl::Critic::Utils qw{ :characters :severities };
  40         108  
  40         2098  
9 40     40   12354 use parent 'Perl::Critic::Policy';
  40         116  
  40         291  
10              
11             our $VERSION = '1.146';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{List declaration without trailing comma};
16             Readonly::Scalar my $EXPL => [ 17 ];
17              
18             #-----------------------------------------------------------------------------
19              
20 94     94 0 1707 sub supported_parameters { return () }
21 78     78 1 396 sub default_severity { return $SEVERITY_LOWEST }
22 84     84 1 377 sub default_themes { return qw(core pbp cosmetic) }
23 35     35 1 132 sub applies_to { return 'PPI::Structure::List' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub violates {
28 45     45 1 123 my ( $self, $elem, undef ) = @_;
29 45 100       127 $elem =~ m{ \n }xms || return;
30              
31             # Is it an assignment of some kind?
32 18         1393 my $sib = $elem->sprevious_sibling();
33 18 100       543 return if !$sib;
34 17 100 66     80 $sib->isa('PPI::Token::Operator') && $sib =~ m{ = }xms || return;
35              
36             # List elements are children of an expression
37 15         119 my $expr = $elem->schild(0);
38 15 100       2004 return if !$expr;
39              
40             # Does the list have more than 1 element?
41             # This means list element, not PPI element.
42 14         49 my @children = $expr->schildren();
43 14 100       296 return if 1 >= grep { $_->isa('PPI::Token::Operator')
  64 100       527  
44             && $_ eq $COMMA } @children;
45              
46             # Is the final element a comma?
47 7         59 my $final = $children[-1];
48 7 100 100     34 if ( ! ($final->isa('PPI::Token::Operator') && $final eq $COMMA) ) {
49 4         41 return $self->violation( $DESC, $EXPL, $elem );
50             }
51              
52 3         45 return; #ok!
53             }
54              
55             1;
56              
57             __END__
58              
59             =pod
60              
61             =head1 NAME
62              
63             Perl::Critic::Policy::CodeLayout::RequireTrailingCommas - Put a comma at the end of every multi-line list declaration, including the last one.
64              
65              
66             =head1 AFFILIATION
67              
68             This Policy is part of the core L<Perl::Critic|Perl::Critic>
69             distribution.
70              
71              
72             =head1 DESCRIPTION
73              
74             Conway suggests that all elements in a multi-line list should be
75             separated by commas, including the last element. This makes it a
76             little easier to re-order the list by cutting and pasting.
77              
78             my @list = ($foo,
79             $bar,
80             $baz); #not ok
81              
82             my @list = ($foo,
83             $bar,
84             $baz,); #ok
85              
86              
87             =head1 CONFIGURATION
88              
89             This Policy is not configurable except for the standard options.
90              
91              
92             =head1 NOTES
93              
94             In the PPI parlance, a "list" is almost anything with parentheses.
95             I've tried to make this Policy smart by targeting only "lists" that
96             have at least one element and are being assigned to something.
97             However, there may be some edge cases that I haven't covered. If you
98             find one, send me a note.
99              
100              
101             =head1 AUTHOR
102              
103             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
104              
105              
106             =head1 COPYRIGHT
107              
108             Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
109              
110             This program is free software; you can redistribute it and/or modify
111             it under the same terms as Perl itself. The full text of this license
112             can be found in the LICENSE file included with this module.
113              
114             =cut
115              
116             # Local Variables:
117             # mode: cperl
118             # cperl-indent-level: 4
119             # fill-column: 78
120             # indent-tabs-mode: nil
121             # c-indentation-style: bsd
122             # End:
123             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :