| 1 | # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | # This file is part of Perl-Critic-Pulp. | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | # Perl-Critic-Pulp is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||
| 6 | # it under the terms of the GNU General Public License as published by the | ||||||||||||||||||||||||||||||||
| 7 | # Free Software Foundation; either version 3, or (at your option) any later | ||||||||||||||||||||||||||||||||
| 8 | # version. | ||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||
| 10 | # Perl-Critic-Pulp is distributed in the hope that it will be useful, but | ||||||||||||||||||||||||||||||||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||||||||||||||||||||||||||||||||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||||||||||||||||||||||||||||||||
| 13 | # for more details. | ||||||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||||||
| 15 | # You should have received a copy of the GNU General Public License along | ||||||||||||||||||||||||||||||||
| 16 | # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | # perlcritic -s ProhibitUnbalancedParens ProhibitUnbalancedParens.pm | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | # unclosed: | ||||||||||||||||||||||||||||||||
| 22 | # perlcritic -s ProhibitUnbalancedParens /usr/share/perl/5.12/CGI.pm | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | # smiley close: | ||||||||||||||||||||||||||||||||
| 25 | # perlcritic -s ProhibitUnbalancedParens /usr/share/perl5/accessors.pm | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | package Perl::Critic::Policy::Documentation::ProhibitUnbalancedParens; | ||||||||||||||||||||||||||||||||
| 29 | 40 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 30 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 31 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 32 | 40 | ▶ | use base 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 33 | 40 | ▶ | use Perl::Critic::Utils; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||||||
| 36 | # use Smart::Comments; | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 40 | our $VERSION = 100; | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 40 | ▶ | use constant supported_parameters => (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 41 | 40 | ▶ | use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 42 | 40 | ▶ | use constant default_themes => qw(pulp cosmetic); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 43 | 40 | ▶ | use constant applies_to => 'PPI::Document'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 75 | ▶ | sub violates { | ||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 46 | 75 | my ($self, $elem, $document) = @_; | |||||||||||||||||||||||||||||||
| 47 | ### ProhibitUnbalancedParens on: $elem->content | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 75 | my $parser = Perl::Critic::Pulp::PodParser::ProhibitUnbalancedParens->new | |||||||||||||||||||||||||||||||
| 50 | (policy => $self); | ||||||||||||||||||||||||||||||||
| 51 | 75 | $parser->parse_from_elem ($elem); | |||||||||||||||||||||||||||||||
| 52 | 75 | return $parser->violations; | |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | package Perl::Critic::Pulp::PodParser::ProhibitUnbalancedParens; | ||||||||||||||||||||||||||||||||
| 56 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 57 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 58 | 40 | ▶ | use Pod::ParseLink; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 59 | 40 | ▶ | use base 'Perl::Critic::Pulp::PodParser'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 79 | ▶ | sub command { | ||||||||||||||||||||||||||||||
|
Subroutine
command: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||
| 62 | 79 | my $self = shift; | |||||||||||||||||||||||||||||||
| 63 | 79 | my ($command, $text, $linenum, $paraobj) = @_; | |||||||||||||||||||||||||||||||
| 64 | 79 | $self->SUPER::command(@_); # maintain 'in_begin' | |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 79 | ▶ | if ($command eq 'for' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$command eq "for" and $text =~ /^ProhibitUnbalancedParens\b\s*(.*)/
MC/DC50%1/2
$command eq "for" and $text =~ /^ProhibitUnbalancedParens\b\s*(.*)/
$command eq "for"
$text =~ /^ProhibitUnbalancedParens\b\s*(.*)/
Truth table66%2/3
A and B
A= $command eq "for"B= $text =~ /^ProhibitUnbalancedParens\b\s*(.*)/
| |||||||||||||||||||||||||||||||||
| 67 | && $text =~ /^ProhibitUnbalancedParens\b\s*(.*)/) { | ||||||||||||||||||||||||||||||||
| 68 | 4 | my $directive = $1; | |||||||||||||||||||||||||||||||
| 69 | ### $directive | ||||||||||||||||||||||||||||||||
| 70 | 4 | ▶ | if ($directive =~ /^allow next( (\d+))?/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | # numbered "allow next 5" means up to that many following | ||||||||||||||||||||||||||||||||
| 72 | # unnumbered "allow next" means one following | ||||||||||||||||||||||||||||||||
| 73 | 4 | ▶ | $self->{'allow_next'} = (defined $2 ? $2 : 1); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | 79 | return $self->command_as_textblock(@_); | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 40 | my %open_to_close = ('(' => ')', | |||||||||||||||||||||||||||||||
| 80 | '[' => ']', | ||||||||||||||||||||||||||||||||
| 81 | '{' => '}'); | ||||||||||||||||||||||||||||||||
| 82 | 40 | my %close_to_open = reverse %open_to_close; | |||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 152 | ▶ | sub textblock { | ||||||||||||||||||||||||||||||
|
Subroutine
textblock: called CC=19 SCAR=29.5
| |||||||||||||||||||||||||||||||||
| 85 | 152 | my ($self, $text, $linenum, $paraobj) = @_; | |||||||||||||||||||||||||||||||
| 86 | ### textblock: "linenum=$linenum" | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 152 | ▶ | if (($self->{'allow_next'}||0) > 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->{'allow_next'} || 0
MC/DC100%1/1
$self->{'allow_next'} || 0
$self->{'allow_next'}
Truth table100%2/2
A
A= $self->{'allow_next'}
| |||||||||||||||||||||||||||||||||
| 89 | 6 | $self->{'allow_next'}--; | |||||||||||||||||||||||||||||||
| 90 | 6 | return ''; | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | # process outside =begin, and inside =begin which is ":" markup | ||||||||||||||||||||||||||||||||
| 94 | 146 | ▶ | unless ($self->{'in_begin'} eq '' || $self->{'in_begin'} =~ /^:/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/
MC/DC50%1/2
$self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/
$self->{"in_begin"} eq ""
$self->{"in_begin"} =~ /^:/
Truth table66%2/3
A or B
A= $self->{"in_begin"} eq ""B= $self->{"in_begin"} =~ /^:/
| |||||||||||||||||||||||||||||||||
| 95 | 1 | return ''; | |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 145 | my $interpolated = $self->interpolate($text, $linenum); | |||||||||||||||||||||||||||||||
| 99 | ### $text | ||||||||||||||||||||||||||||||||
| 100 | ### $interpolated | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 145 | my @parens; | |||||||||||||||||||||||||||||||
| 103 | 145 | ▶ | while ($interpolated | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 104 | =~ m/ | ||||||||||||||||||||||||||||||||
| 105 | ([][({})]) # $1 open or close | ||||||||||||||||||||||||||||||||
| 106 | |([:;]-?\) # $2 smiley face optional close | ||||||||||||||||||||||||||||||||
| 107 | |\b[a-zA-Z1-9]\) # "middle a) or 1) item" | ||||||||||||||||||||||||||||||||
| 108 | |(?<!\$)\$\) # perlvar $), and not $$ | ||||||||||||||||||||||||||||||||
| 109 | ) | ||||||||||||||||||||||||||||||||
| 110 | |(["'])[][(){}]+\3 # $3 "(" quoted | ||||||||||||||||||||||||||||||||
| 111 | |[:;]-?[(] # smiley face not an open | ||||||||||||||||||||||||||||||||
| 112 | |(?<!\$)\$\$ # perlvar $$ consumed | ||||||||||||||||||||||||||||||||
| 113 | |\$\(\w*\) # makefile var $(abc) | ||||||||||||||||||||||||||||||||
| 114 | |\$\[\w*\] # perhaps template $[abc] | ||||||||||||||||||||||||||||||||
| 115 | |(?<!\$)\$[][(] # perlvars $[, $(, $], and not $$ | ||||||||||||||||||||||||||||||||
| 116 | |^\s*(\d+|[A-Za-z])\.?\) # initial "1.5) something" | ||||||||||||||||||||||||||||||||
| 117 | /xg) { | ||||||||||||||||||||||||||||||||
| 118 | ### match: $& | ||||||||||||||||||||||||||||||||
| 119 | ### $1 | ||||||||||||||||||||||||||||||||
| 120 | ### $2 | ||||||||||||||||||||||||||||||||
| 121 | ### $3 | ||||||||||||||||||||||||||||||||
| 122 | 117 | ▶ | if (defined $1) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 123 | 65 | push @parens, { char => $1, | |||||||||||||||||||||||||||||||
| 124 | pos => pos($interpolated)-1, | ||||||||||||||||||||||||||||||||
| 125 | }; | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | } elsif (defined $2) { | ||||||||||||||||||||||||||||||||
| 128 | 25 | push @parens, { char => ')', | |||||||||||||||||||||||||||||||
| 129 | pos => pos($interpolated)-1, | ||||||||||||||||||||||||||||||||
| 130 | optional => 1, | ||||||||||||||||||||||||||||||||
| 131 | }; | ||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | ### @parens | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | # sort optional closes to after hard closes | ||||||||||||||||||||||||||||||||
| 137 | { | ||||||||||||||||||||||||||||||||
| 138 | 145 | my @optional; | |||||||||||||||||||||||||||||||
| 139 | my @new; | ||||||||||||||||||||||||||||||||
| 140 | 145 | foreach my $p (@parens) { | |||||||||||||||||||||||||||||||
| 141 | 90 | ▶ | if (@optional && $optional[0]->{'char'} ne $p->{'char'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@optional and $optional[0]{"char"} ne $p->{"char"}
MC/DC100%2/2
@optional and $optional[0]{"char"} ne $p->{"char"}
@optional
$optional[0]{"char"} ne $p->{"char"}
Truth table100%3/3
A and B
A= @optionalB= $optional[0]{"char"} ne $p->{"char"}
| |||||||||||||||||||||||||||||||||
| 142 | 5 | push @new, splice @optional; | |||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||
| 144 | 90 | ▶ | if ($p->{'optional'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | 25 | push @optional, $p; | |||||||||||||||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||||||||||||||
| 147 | 65 | push @new, $p; | |||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | 145 | @parens = (@new, @optional); | |||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||
| 152 | ### sorted: @parens | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | 145 | my @opens; | |||||||||||||||||||||||||||||||
| 155 | 145 | foreach my $p (@parens) { | |||||||||||||||||||||||||||||||
| 156 | ### $p | ||||||||||||||||||||||||||||||||
| 157 | 90 | my $char = $p->{'char'}; | |||||||||||||||||||||||||||||||
| 158 | 90 | ▶ | if (my $want_openchar = $close_to_open{$char}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 159 | # a close | ||||||||||||||||||||||||||||||||
| 160 | 42 | ▶ | if (my $o = pop @opens) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 161 | 28 | my $openchar = $o->{'char'}; | |||||||||||||||||||||||||||||||
| 162 | 28 | ▶ | if ($openchar ne $want_openchar) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 163 | 2 | ▶ | if ($p->{'optional'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 164 | ### mismatched optional close, skip | ||||||||||||||||||||||||||||||||
| 165 | 2 | push @opens, $o; | |||||||||||||||||||||||||||||||
| 166 | 2 | next; | |||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||
| 168 | $self->violation_at_linenum_and_textpos | ||||||||||||||||||||||||||||||||
| 169 | ("Mismatched closing paren \"$char\" expected \"$open_to_close{$openchar}\"", | ||||||||||||||||||||||||||||||||
| 170 | 0 | $linenum, $interpolated, $p->{'pos'}); | |||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | } else { | ||||||||||||||||||||||||||||||||
| 174 | 14 | ▶ | if ($p->{'optional'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 175 | ### unopened optional close, skip | ||||||||||||||||||||||||||||||||
| 176 | 12 | next; | |||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||
| 178 | $self->violation_at_linenum_and_textpos | ||||||||||||||||||||||||||||||||
| 179 | ("Unopened close paren \"$char\"", | ||||||||||||||||||||||||||||||||
| 180 | 2 | $linenum, $interpolated, $p->{'pos'}); | |||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | } else { | ||||||||||||||||||||||||||||||||
| 184 | # an open | ||||||||||||||||||||||||||||||||
| 185 | 48 | push @opens, $p; | |||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||
| 188 | 145 | foreach my $p (@opens) { | |||||||||||||||||||||||||||||||
| 189 | $self->violation_at_linenum_and_textpos | ||||||||||||||||||||||||||||||||
| 190 | ("Unclosed paren \"$p->{'char'}\"", | ||||||||||||||||||||||||||||||||
| 191 | 22 | $linenum, $interpolated, $p->{'pos'}); | |||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||
| 193 | 145 | return ''; | |||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||
| 196 | 40 | *interior_sequence = \&interior_sequence_as_displayed_noncode_text; | |||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | 12 | ▶ | sub interior_sequence_as_displayed_noncode_text { | ||||||||||||||||||||||||||||||
|
Subroutine
interior_sequence_as_displayed_noncode_text: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 199 | 12 | my ($self, $cmd, $text, $pod_seq) = @_; | |||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | 12 | ▶ | if ($cmd eq 'X' || $cmd eq 'C') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$cmd eq "X" or $cmd eq "C"
MC/DC50%1/2
$cmd eq "X" or $cmd eq "C"
$cmd eq "X"
$cmd eq "C"
Truth table66%2/3
A or B
A= $cmd eq "X"B= $cmd eq "C"
| |||||||||||||||||||||||||||||||||
| 202 | ### $cmd | ||||||||||||||||||||||||||||||||
| 203 | ### X,C keep only the newlines: $text | ||||||||||||||||||||||||||||||||
| 204 | 7 | $text =~ tr/\n//cd; | |||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | } elsif ($cmd eq 'L') { | ||||||||||||||||||||||||||||||||
| 207 | 3 | my ($display, $inferred, $name, $section, $type) | |||||||||||||||||||||||||||||||
| 208 | = Pod::ParseLink::parselink ($text); | ||||||||||||||||||||||||||||||||
| 209 | ### $text | ||||||||||||||||||||||||||||||||
| 210 | ### $display | ||||||||||||||||||||||||||||||||
| 211 | ### $inferred | ||||||||||||||||||||||||||||||||
| 212 | ### $name | ||||||||||||||||||||||||||||||||
| 213 | 3 | return $inferred; # the display part, or the name part if no display | |||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||
| 215 | 9 | return $text; | |||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||
| 218 | 40 | 1; | |||||||||||||||||||||||||||||||
| 219 | __END__ | ||||||||||||||||||||||||||||||||