File Coverage

blib/lib/Perl/Critic/Policy/Documentation/ProhibitParagraphEndComma.pm
Criterion Covered Total %
statement 61 62 98.3
branch 9 12 75.0
condition 13 18 72.2
subroutine 18 18 100.0
pod 1 1 100.0
total 102 111 91.8


line stmt bran cond sub pod time code
1             # Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2020 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             # maybe allow comma for
20             # =head1 Foo
21             # And something,
22             # =head2 Item
23              
24             # perlcritic -s ProhibitParagraphEndComma ProhibitParagraphEndComma.pm
25             # perlcritic -s ProhibitParagraphEndComma /usr/share/perl5/IO/Socket/INET6.pm
26             # perlcritic -s ProhibitParagraphEndComma /usr/share/perl5/MIME/Body.pm /usr/share/perl5/XML/Twig.pm
27              
28              
29             package Perl::Critic::Policy::Documentation::ProhibitParagraphEndComma;
30 40     40   32493 use 5.006;
  40         173  
31 40     40   232 use strict;
  40         90  
  40         913  
32 40     40   208 use warnings;
  40         100  
  40         1222  
33 40     40   221 use base 'Perl::Critic::Policy';
  40         112  
  40         5055  
34 40     40   183134 use Perl::Critic::Utils;
  40         113  
  40         798  
35              
36             # uncomment this to run the ### lines
37             # use Smart::Comments;
38              
39             our $VERSION = 98;
40              
41              
42 40     40   35581 use constant supported_parameters => ();
  40         136  
  40         2708  
43 40     40   257 use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOWEST;
  40         111  
  40         2338  
44 40     40   253 use constant default_themes => qw(pulp cosmetic);
  40         103  
  40         2508  
45 40     40   285 use constant applies_to => 'PPI::Document';
  40         94  
  40         5538  
46              
47             sub violates {
48 7     7 1 521263 my ($self, $elem, $document) = @_;
49             ### ProhibitParagraphEndComma on: $elem->content
50              
51 7         47 my $parser = Perl::Critic::Pulp::PodParser::ProhibitParagraphEndComma->new
52             (policy => $self);
53 7         27 $parser->parse_from_elem ($elem);
54 7         24 $parser->check_last;
55 7         24 return $parser->violations;
56             }
57              
58             package Perl::Critic::Pulp::PodParser::ProhibitParagraphEndComma;
59 40     40   311 use strict;
  40         97  
  40         1134  
60 40     40   221 use warnings;
  40         99  
  40         1351  
61 40     40   258 use base 'Perl::Critic::Pulp::PodParser';
  40         131  
  40         21393  
62              
63             sub new {
64 7     7   16 my $class = shift;
65             ### new() ...
66 7         137 return $class->SUPER::new (last_text => '',
67             last_command => '',
68             @_);
69             }
70             sub command {
71 14     14   1320 my ($self, $command, $text, $linenum, $paraobj) = @_;
72             ### command(): $command
73              
74             # "=begin :foo" means pod markup continues. Ignore the =begin and
75             # continue processing POD within it. Any other begin is a new block
76             # something and preceding comma not allowed.
77             #
78 14 100 66     105 if ($command eq 'for'
      100        
      100        
      100        
79             || $command eq 'pod'
80             || ($command eq 'begin' && $text =~ /^\s*:/)
81             || $command eq 'end') {
82 11         103 return; # ignore these completely
83             }
84              
85 3 50 33     17 if ($command eq 'item' && $self->{'last_command'} eq 'item') {
    100          
86             # Paragraphs in =item list can end in successive commas.
87              
88             } elsif ($command eq 'over') {
89              
90             } else {
91 2         6 $self->check_last;
92             }
93 3         8 $self->{'last_text'} = '';
94 3         33 $self->{'last_command'} = $command;
95             }
96             sub textblock {
97 10     10   629 my ($self, $text, $linenum, $paraobj) = @_;
98             ### textblock(): $text
99 10         34 $self->check_last;
100             # sometimes $text=undef from Pod::Parser
101 10 50       30 if (! defined $text) { $text = ''; }
  0         0  
102 10         21 $self->{'last_linenum'} = $linenum;
103 10         143 $self->{'last_text'} = $text;
104             }
105             sub verbatim {
106 3     3   196 my ($self, $text, $linenum, $paraobj) = @_;
107             # anything before a verbatim is ok
108 3         36 $self->{'last_text'} = '';
109             }
110              
111             sub check_last {
112 19     19   41 my ($self) = @_;
113             ### check_last() ...
114             ### in_begin: $self->{'in_begin'}
115              
116 19 50 33     88 if ($self->{'in_begin'} && $self->{'in_begin'} !~ /^:/) {
    100          
117             # =begin block of non-: means not pod markup
118              
119             } elsif ($self->{'last_text'} =~ /(,\s*)$/s) {
120             ### last_text ends comma ...
121 3         10 my $pos = length($self->{'last_text'}) - length($1); # position of comma
122             $self->violation_at_linenum_and_textpos
123             ("Paragraph ends with comma",
124 3         21 $self->{'last_linenum'}, $self->{'last_text'}, $pos);
125             }
126 19         43 $self->{'last_text'} = '';
127             }
128              
129             1;
130             __END__
131              
132             =for stopwords Ryde
133              
134             =head1 NAME
135              
136             Perl::Critic::Policy::Documentation::ProhibitParagraphEndComma - avoid comma at end of section
137              
138             =head1 DESCRIPTION
139              
140             This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
141             add-on. It asks you not to end a POD paragraph with a comma.
142              
143             Some text, # bad, meant to be a full-stop?
144              
145             Some more text.
146              
147             Usually such a comma is meant to be a full-stop, or perhaps omitted at the
148             end of a "SEE ALSO" list
149              
150             =for ProhibitVerbatimMarkup allow next 2
151              
152             =head1 SEE ALSO
153              
154             L<Foo>,
155             L<Bar>, # bad, meant to be omitted?
156              
157             A paragraph is allowed to end with a comma when before an C<=over> or a
158             verbatim block, that being taken as introducing a quotation or example,
159              
160             For example, # ok, introduce an example
161              
162             foo(1+2+3)
163              
164             Or one of, # ok, introduce an itemized list
165              
166             =over
167              
168             =item Foo
169              
170             =head2 Disabling
171              
172             If you don't care about this you can disable C<ProhibitParagraphEndComma>
173             from your F<.perlcriticrc> in the usual way (see
174             L<Perl::Critic/CONFIGURATION>),
175              
176             [-Documentation::ProhibitParagraphEndComma]
177              
178             =head1 SEE ALSO
179              
180             L<Perl::Critic::Pulp>, L<Perl::Critic>
181              
182             L<Perl::Critic::Policy::Documentation::ProhibitParagraphTwoDots>
183              
184             =head1 HOME PAGE
185              
186             http://user42.tuxfamily.org/perl-critic-pulp/index.html
187              
188             =head1 COPYRIGHT
189              
190             Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2020 Kevin Ryde
191              
192             Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
193             under the terms of the GNU General Public License as published by the Free
194             Software Foundation; either version 3, or (at your option) any later
195             version.
196              
197             Perl-Critic-Pulp is distributed in the hope that it will be useful, but
198             WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
199             or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
200             more details.
201              
202             You should have received a copy of the GNU General Public License along with
203             Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
204              
205             =cut