« blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitUnreachableCode.pm
↑ Summary
blib/lib/Perl/Critic/Policy/InputOutput/ProhibitBarewordFileHandles.pm »
| 1 | package Perl::Critic::Policy::CodeLayout::RequireTrailingCommas; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 40 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use Perl::Critic::Utils qw{ :characters :severities }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 40 | our $VERSION = '1.150'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 40 | Readonly::Scalar my $DESC => q{List declaration without trailing comma}; | |||||||||||||||||||||||||||
| 16 | 40 | Readonly::Scalar my $EXPL => [ 17 ]; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 90 | ▶ | sub supported_parameters { return () } | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 75 | ▶ | sub default_severity { return $SEVERITY_LOWEST } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 84 | ▶ | sub default_themes { return qw(core pbp cosmetic) } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 31 | ▶ | sub applies_to { return 'PPI::Structure::List' } | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub violates { | ||||||||||||||||||||||||||||
| 28 | 22 | ▶ | my ( $self, $elem, undef ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=10 SCAR=44.5
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 22 | ▶ | $elem =~ m{ \n }xms || return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Is it an assignment of some kind? | ||||||||||||||||||||||||||||
| 32 | 0 | my $sib = $elem->sprevious_sibling(); | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | return if !$sib; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | $sib->isa('PPI::Token::Operator') && $sib =~ m{ = }xms || return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$sib->isa("PPI::Token::Operator") and $sib =~ / = /msx
MC/DC0%0/2
$sib->isa("PPI::Token::Operator") and $sib =~ / = /msx
$sib->isa("PPI::Token::Operator")
$sib =~ / = /msx
Truth table0%0/3
A and B
A= $sib->isa("PPI::Token::Operator")B= $sib =~ / = /msx
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # List elements are children of an expression | ||||||||||||||||||||||||||||
| 37 | 0 | my $expr = $elem->schild(0); | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | return if !$expr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | return if 1 >= grep { $_->isa('PPI::Token::Operator') | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 44 | && $_ eq $COMMA } @children; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Is the final element a comma? | ||||||||||||||||||||||||||||
| 47 | 0 | my $final = $children[-1]; | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | if ( ! ($final->isa('PPI::Token::Operator') && $final eq $COMMA) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$final->isa("PPI::Token::Operator") and $final eq $Perl::Critic::Policy::CodeLayout::RequireTrailingCommas::COMMA
MC/DC0%0/2
$final->isa("PPI::Token::Operator") and $final eq $Perl::Critic::Policy::CodeLayout::RequireTrailingCommas::COMMA
$final->isa("PPI::Token::Operator")
$final eq $Perl::Critic::Policy::CodeLayout::RequireTrailingCommas::COMMA
Truth table0%0/3
A and B
A= $final->isa("PPI::Token::Operator")B= $final eq $Perl::Critic::Policy::CodeLayout::RequireTrailingCommas::COMMA
| |||||||||||||||||||||||||||||
| 49 | 0 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | return; #ok! | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 40 | 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 : | ||||||||||||||||||||||||||||