File Coverage

blib/lib/Perl/Critic/Policy/CodeLayout/RequireTrailingCommas.pm
Criterion Covered Total %
statement 23 35 65.7
branch 1 14 7.1
condition 0 6 0.0
subroutine 11 11 100.0
pod 4 5 80.0
total 39 71 54.9


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::CodeLayout::RequireTrailingCommas;
2              
3 40     40   26850 use 5.010001;
  40         195  
4 40     40   271 use strict;
  40         151  
  40         893  
5 40     40   224 use warnings;
  40         173  
  40         1068  
6 40     40   241 use Readonly;
  40         173  
  40         2217  
7              
8 40     40   301 use Perl::Critic::Utils qw{ :characters :severities };
  40         143  
  40         2119  
9 40     40   11747 use parent 'Perl::Critic::Policy';
  40         135  
  40         286  
10              
11             our $VERSION = '1.150';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{List declaration without trailing comma};
16             Readonly::Scalar my $EXPL => [ 17 ];
17              
18             #-----------------------------------------------------------------------------
19              
20 89     89 0 1619 sub supported_parameters { return () }
21 74     74 1 316 sub default_severity { return $SEVERITY_LOWEST }
22 84     84 1 344 sub default_themes { return qw(core pbp cosmetic) }
23 30     30 1 77 sub applies_to { return 'PPI::Structure::List' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub violates {
28 21     21 1 38 my ( $self, $elem, undef ) = @_;
29 21 50       50 $elem =~ m{ \n }xms || return;
30              
31             # Is it an assignment of some kind?
32 0           my $sib = $elem->sprevious_sibling();
33 0 0         return if !$sib;
34 0 0 0       $sib->isa('PPI::Token::Operator') && $sib =~ m{ = }xms || return;
35              
36             # List elements are children of an expression
37 0           my $expr = $elem->schild(0);
38 0 0         return if !$expr;
39              
40             # Does the list have more than 1 element?
41             # This means list element, not PPI element.
42 0           my @children = $expr->schildren();
43 0 0         return if 1 >= grep { $_->isa('PPI::Token::Operator')
  0 0          
44             && $_ eq $COMMA } @children;
45              
46             # Is the final element a comma?
47 0           my $final = $children[-1];
48 0 0 0       if ( ! ($final->isa('PPI::Token::Operator') && $final eq $COMMA) ) {
49 0           return $self->violation( $DESC, $EXPL, $elem );
50             }
51              
52 0           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 :