1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/CodeLayout/RequireTrailingCommas.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::CodeLayout::RequireTrailingCommas;
15 use Perl::Critic::Utils qw{ :characters :severities };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{List declaration without trailing comma};
23 Readonly::Scalar my $EXPL => [ 17 ];
25 #-----------------------------------------------------------------------------
27 sub supported_parameters { return () }
28 sub default_severity { return $SEVERITY_LOWEST }
29 sub default_themes { return qw(core pbp cosmetic) }
30 sub applies_to { return 'PPI::Structure::List' }
32 #-----------------------------------------------------------------------------
35 my ( $self, $elem, undef ) = @_;
36 $elem =~ m{ \n }mx || return;
38 # Is it an assignment of some kind?
39 my $sib = $elem->sprevious_sibling();
41 $sib->isa('PPI::Token::Operator') && $sib =~ m{ = }mx || return;
43 # List elements are children of an expression
44 my $expr = $elem->schild(0);
47 # Does the list have more than 1 element?
48 # This means list element, not PPI element.
49 my @children = $expr->schildren();
50 return if 1 >= grep { $_->isa('PPI::Token::Operator')
51 && $_ eq $COMMA } @children;
53 # Is the final element a comma?
54 my $final = $children[-1];
55 if ( ! ($final->isa('PPI::Token::Operator') && $final eq $COMMA) ) {
56 return $self->violation( $DESC, $EXPL, $elem );
70 Perl::Critic::Policy::CodeLayout::RequireTrailingCommas - Put a comma at the end of every multi-line list declaration, including the last one.
74 This Policy is part of the core L<Perl::Critic> distribution.
79 Conway suggests that all elements in a multi-line list should be
80 separated by commas, including the last element. This makes it a
81 little easier to re-order the list by cutting and pasting.
94 This Policy is not configurable except for the standard options.
99 In the PPI parlance, a "list" is almost anything with parentheses.
100 I've tried to make this Policy smart by targeting only "lists" that
101 have at least one element and are being assigned to something.
102 However, there may be some edge cases that I haven't covered. If you
103 find one, send me a note.
107 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
111 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
113 This program is free software; you can redistribute it and/or modify
114 it under the same terms as Perl itself. The full text of this license
115 can be found in the LICENSE file included with this module.
121 # cperl-indent-level: 4
123 # indent-tabs-mode: nil
124 # c-indentation-style: bsd
126 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :