| 1 | # Copyright 2009, 2010, 2011, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde | ||||||||||||
| 2 | |||||||||||||
| 3 | # Perl-Critic-Pulp is free software; you can redistribute it and/or modify | ||||||||||||
| 4 | # it under the terms of the GNU General Public License as published by the | ||||||||||||
| 5 | # Free Software Foundation; either version 3, or (at your option) any later | ||||||||||||
| 6 | # version. | ||||||||||||
| 7 | # | ||||||||||||
| 8 | # Perl-Critic-Pulp is distributed in the hope that it will be useful, but | ||||||||||||
| 9 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||||||||||||
| 10 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||||||||||||
| 11 | # for more details. | ||||||||||||
| 12 | # | ||||||||||||
| 13 | # You should have received a copy of the GNU General Public License along | ||||||||||||
| 14 | # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||
| 15 | |||||||||||||
| 16 | |||||||||||||
| 17 | # use strict; | ||||||||||||
| 18 | # $, = "\n"; | ||||||||||||
| 19 | # sub foo { | ||||||||||||
| 20 | # return 123; | ||||||||||||
| 21 | # } | ||||||||||||
| 22 | # sub x { | ||||||||||||
| 23 | # my %h = (-foo | ||||||||||||
| 24 | # => 'abc'); | ||||||||||||
| 25 | # print %h | ||||||||||||
| 26 | # } | ||||||||||||
| 27 | # x(); | ||||||||||||
| 28 | |||||||||||||
| 29 | |||||||||||||
| 30 | package Perl::Critic::Policy::CodeLayout::ProhibitFatCommaNewline; | ||||||||||||
| 31 | 40 | ▶ | use 5.006; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 32 | 40 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 33 | 40 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 34 | 40 | ▶ | use version (); # but don't import qv() | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 35 | 40 | ▶ | use Perl::Critic::Utils; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 36 | |||||||||||||
| 37 | # 1.084 for Perl::Critic::Document highest_explicit_perl_version() | ||||||||||||
| 38 | 40 | ▶ | use Perl::Critic::Policy 1.084; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 39 | 40 | ▶ | use base 'Perl::Critic::Policy'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 40 | |||||||||||||
| 41 | 40 | our $VERSION = 100; | |||||||||||
| 42 | |||||||||||||
| 43 | # uncomment this to run the ### lines | ||||||||||||
| 44 | # use Smart::Comments; | ||||||||||||
| 45 | |||||||||||||
| 46 | |||||||||||||
| 47 | 40 | ▶ | use constant supported_parameters => (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 48 | 40 | ▶ | use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 49 | 40 | ▶ | use constant default_themes => qw(pulp bugs); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 50 | 40 | ▶ | use constant applies_to => ('PPI::Token::Operator'); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 51 | |||||||||||||
| 52 | 40 | my $v5008 = version->new('5.008'); | |||||||||||
| 53 | |||||||||||||
| 54 | 14 | ▶ | sub violates { | ||||||||||
|
Subroutine
violates: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||
| 55 | 14 | my ($self, $elem, $document) = @_; | |||||||||||
| 56 | |||||||||||||
| 57 | 14 | ▶ | $elem->content eq '=>' | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | or return; # some other operator | ||||||||||||
| 59 | |||||||||||||
| 60 | 7 | ▶ | my $prev = $elem->sprevious_sibling || return; | ||||||||||
|
Condition50%1/2
$elem->sprevious_sibling || (return)
MC/DC0%0/1
$elem->sprevious_sibling || (return)
$elem->sprevious_sibling
Truth table50%1/2
A
A= $elem->sprevious_sibling
| |||||||||||||
| 61 | 7 | ▶ | if (! $prev->isa('PPI::Token::Word')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | ### previous not a word, so => acts as a plain comma, ok ... | ||||||||||||
| 63 | 0 | return; | |||||||||||
| 64 | } | ||||||||||||
| 65 | 7 | ▶ | if (! _elems_any_newline_between ($prev, $elem)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | ### no newline before =>, ok ... | ||||||||||||
| 67 | 0 | return; | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 7 | my $word = $prev->content; | |||||||||||
| 71 | |||||||||||||
| 72 | # A builtin is never quoted by newline fat comma. | ||||||||||||
| 73 | # PPI 1.213 gives a word "-print" where it should be a negate of a | ||||||||||||
| 74 | # print(), so check the word "sans dash". | ||||||||||||
| 75 | 7 | ▶ | if (Perl::Critic::Utils::is_perl_builtin(_sans_dash($word))) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 3 | return $self->violation | |||||||||||
| 77 | ("Fat comma after newline doesn't quote Perl builtin \"$word\"", | ||||||||||||
| 78 | '', | ||||||||||||
| 79 | $elem); | ||||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | # In 5.8 up words are quoted by newline fat comma, so ok. | ||||||||||||
| 83 | 4 | ▶ | if (defined (my $doc_version = $document->highest_explicit_perl_version)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | 2 | ▶ | if ($doc_version >= $v5008) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 1 | return; | |||||||||||
| 86 | } | ||||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | # In 5.6 and earlier newline fat comma doesn't quote. | ||||||||||||
| 90 | 3 | return $self->violation | |||||||||||
| 91 | ("Fat comma after newline doesn't quote preceding bareword \"$word\"", | ||||||||||||
| 92 | '', | ||||||||||||
| 93 | $elem); | ||||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | # return $str stripped of a leading "-", if it has one | ||||||||||||
| 97 | 7 | ▶ | sub _sans_dash { | ||||||||||
|
Subroutine
_sans_dash: called CC=1 SCAR=0.0
| |||||||||||||
| 98 | 7 | my ($str) = @_; | |||||||||||
| 99 | 7 | $str =~ s/^-//; | |||||||||||
| 100 | 7 | return $str; | |||||||||||
| 101 | } | ||||||||||||
| 102 | |||||||||||||
| 103 | # $from and $to are PPI::Element | ||||||||||||
| 104 | # Return true if there's a "\n" newline anywhere in between those elements, | ||||||||||||
| 105 | # not including either $from or $to themselves. | ||||||||||||
| 106 | 7 | ▶ | sub _elems_any_newline_between { | ||||||||||
|
Subroutine
_elems_any_newline_between: called CC=5 SCAR=17.3
| |||||||||||||
| 107 | 7 | my ($from, $to) = @_; | |||||||||||
| 108 | 0 | ▶ | if ($from == $to) { return 0; } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 109 | 7 | for (;;) { | |||||||||||
| 110 | 9 | ▶ | $from = $from->next_sibling || return 0; | ||||||||||
|
Condition50%1/2
$from->next_sibling || (return 0)
MC/DC0%0/1
$from->next_sibling || (return 0)
$from->next_sibling
Truth table50%1/2
A
A= $from->next_sibling
| |||||||||||||
| 111 | 0 | ▶ | if ($from == $to) { return 0; } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 9 | ▶ | if ($from =~ /\n/) { return 1; } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | } | ||||||||||||
| 114 | } | ||||||||||||
| 115 | |||||||||||||
| 116 | 40 | 1; | |||||||||||
| 117 | __END__ | ||||||||||||