1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
# Copyright 2013, 2014, 2015, 2016, 2017 Kevin Ryde
# This file is part of Perl-Critic-Pulp.
# Perl-Critic-Pulp is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# Perl-Critic-Pulp is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
# perlcritic -s ProhibitDuplicateHeadings ProhibitDuplicateHeadings.pm
#
# duplicate BUGS
# perlcritic -s ProhibitDuplicateHeadings /usr/share/perl5/Acme/Tie/Eleet.pm
# duplicate toplevel CLASS METHODS
# perlcritic -s ProhibitDuplicateHeadings /usr/share/perl5/Games/Euchre/Trick.pm
# duplicate =head2 serialise
# perlcritic -s ProhibitDuplicateHeadings /usr/share/perl5/SVG/Extension.pm
package Perl::Critic::Policy::Documentation::ProhibitDuplicateHeadings;
use 5.006;
use strict;
use warnings;
use base 'Perl::Critic::Policy';
use Perl::Critic::Utils;
# uncomment this to run the ### lines
# use Smart::Comments;
our $VERSION = 96;
use constant supported_parameters =>
({ name => 'uniqueness',
description => 'The scope for headings names, meaning to what extent they must not be duplicates. Choices nested, all.',
behavior => 'string',
default_string => 'default',
parser => \&_parse_uniqueness,
});
use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM;
use constant default_themes => qw(pulp bugs);
use constant applies_to => 'PPI::Document';
sub violates {
my ($self, $elem, $document) = @_;
### ProhibitDuplicateHeadings ...
### _uniqueness: $self->{'_uniqueness'}
# ### content: $elem->content
my $parser = Perl::Critic::Pulp::PodParser::ProhibitDuplicateHeadings->new
(policy => $self);
$parser->parse_from_elem ($elem);
### violations return: [ $parser->violations ]
return $parser->violations;
}
my %known_uniqueness = ('' => 1, # for trailing comma
all => 1,
ancestor => 1,
sibling => 1,
adjacent => 1,
default => 1,
);
my %uniqueness_expand = (default => [ 'ancestor', 'sibling', 'adjacent' ],
);
sub _parse_uniqueness {
my ($self, $parameter, $str) = @_;
### _parse_uniqueness ...
### $parameter
### $str
if (! defined $str) {
$str = $parameter->get_default_string;
### default: $str
}
my %uhash;
foreach my $key (split /,/, $str) {
$key =~ s/^\s+//;
$key =~ s/\s+$//;
if (! $known_uniqueness{$key}) {
$self->throw_parameter_value_exception
($parameter->get_name,
$str,
undef, # source
'unrecognised uniqueness');
}
if (my $aref = $uniqueness_expand{$key}) {
foreach my $key (@$aref) {
$uhash{$key} = 1;
}
} else {
$uhash{$key} = 1;
}
}
### %uhash
$self->__set_parameter_value ($parameter, \%uhash);
}
#------------------------------------------------------------------------------
package Perl::Critic::Pulp::PodParser::ProhibitDuplicateHeadings;
use strict;
use warnings;
use base 'Perl::Critic::Pulp::PodParser';
sub command {
my $self = shift;
my ($command, $text, $linenum, $paraobj) = @_;
### $command
if ($command =~ /^head(\d*)$/) {
my $level = $1 || 0;
$text =~ s/^\s+//; # leading whitespace
$text =~ s/\s+$//; # trailing whitespace
$text =~ s/\s+/ /; # collapse whitespace to single space each
### $text
### $level
my $uniqueness = $self->{'policy'}->{'_uniqueness'};
my $seen_linenum;
my $seen_type;
if ($uniqueness->{'all'}) {
unless (defined $seen_linenum) {
$seen_linenum = $self->{'seen_all'}->{$text};
$seen_type = ' ';
}
$self->{'seen_all'}->{$text} = $linenum;
}
if ($uniqueness->{'adjacent'}) {
unless (defined $seen_linenum) {
if (defined $self->{'seen_adjacent'}
&& $text eq $self->{'seen_adjacent'}) {
$seen_linenum = $self->{'seen_adjacent_linenum'};
$seen_type = ' adjacent ';
}
}
$self->{'seen_adjacent'} = $text;
$self->{'seen_adjacent_linenum'} = $linenum;
}
if ($uniqueness->{'sibling'}) {
### seen_sibling: $self->{'seen_sibling'}
unless (defined $seen_linenum) {
$seen_linenum = $self->{'seen_sibling'}->{$level}->{$text};
$seen_type = ' sibling ';
}
# discard anything > $level
foreach my $l (keys %{$self->{'seen_sibling'}}) {
if ($l > $level) {
delete $self->{'seen_sibling'}->{$l};
}
}
$self->{'seen_sibling'}->{$level}->{$text} = $linenum;
}
if ($uniqueness->{'ancestor'}) {
foreach my $l (sort {$a<=>$b} # biggest to smallest
keys %{$self->{'seen_ancestor'}}) {
if ($l < $level) {
if ($text eq $self->{'seen_ancestor'}->{$l}) {
unless (defined $seen_linenum) {
$seen_linenum = $self->{'seen_ancestor_linenum'}->{$l};
$seen_type = ' ancestor ';
}
}
} else {
delete $self->{'seen_ancestor'}->{$l};
}
}
$self->{'seen_ancestor'}->{$level} = $text;
$self->{'seen_ancestor_linenum'}->{$level} = $linenum;
}
### $seen_linenum
### $seen_type
if (defined $seen_linenum) {
$self->violation_at_linenum
("Duplicate$seen_type=head \"$text\", previously seen at line $seen_linenum",
$linenum);
### violation at line: $linenum
}
}
return '';
}
1;
__END__
# within a
# nested tree scope. This is designed to be how A subheading can be repeated if under a
# different containing heading.
#
# Headings are thought of as a tree and a given heading must not duplicate a
# sibling or an ancestor.
#
# head1 head2 head3 no duplicate
# ----- ----- ----- ------------
#
# A--+--B A,J head1 siblings
# |
# +--C--+--D B,C,F,I,A head2 siblings and parent
# | |
# | +--E D,E,A,C head3 siblings and ancestors
# |
# +--F--+--G G,H,A,F head3 siblings and ancestors
# | |
# | +--H
# |
# +--I
#
# J--+--K K,L,M,J head2 siblings and parent
# |
# +--L
# |
# +--M
#
# "B" must be unique to its siblings C,F,I and its parent A.
#
# "D" must be unique to its sibling E and its ancestors A,C. But "D" doesn't
# have to be unique to F,G,H since F is not a direct ancestor and G,H are not
# siblings but cousins under the different branch F.
#
# This rule suits a construction like "A+C+D" to make a path to identify a
# point in the document (with some suitable separator between the parts).
=for stopwords Ryde
=head1 NAME
Perl::Critic::Policy::Documentation::ProhibitDuplicateHeadings - don't duplicate =head names
=head1 DESCRIPTION
This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
add-on. It asks you not to duplicate heading names in C<=head> POD
commands.
=head1 SOMETHING
=head1 SOMETHING # bad, duplicate
Duplication is usually a mistake, perhaps too much cut-and-paste, or a
leftover from a template, or perhaps text in two places which ought to be
together. On that basis this policy is medium severity and under the "bugs"
theme (see L<Perl::Critic/POLICY THEMES>).
=head2 Default Uniqueness
The policy default is to demand that a given heading is unique to its
ancestors, siblings, and to the immediately adjacent heading irrespective of
level. This is designed to be how human readers perceive the scope of
headings and subheadings, plus adjacency in case a mixture of heading levels
would let a duplicate otherwise go undetected. For example
=head1 Top
=head2 Subhead
=head3 Top # bad, duplicates its ancestor head1
Or siblings
=head1 Top
=head2 Down
=head2 Another
=head2 Down # bad, duplicates sibling head2
Or adjacent
=head2 Blah
=head1 Blah # bad, duplicates adjacent
A subheading can be repeated if it's under a different higher heading. For
example the following two "Details" are cousins, so allowed.
=head1 One
=head2 Details
=head1 Two
=head2 Details # ok
=head2 All Unique
Option C<uniqueness=all> (see L</CONFIGURATION> below) applies a stricter
rule so that all C<=head> names must be unique throughout the document,
irrespective of levels and structure.
=head3 Foo
=head1 Bar
=head3 Foo # bad
One use for this is to ensure all headings can be reached by an
C<LE<lt>E<gt>> link. An C<LE<lt>E<gt>> only has the heading name, no level
or path, so if there's any duplication among the names then only the first
of each duplicate will be reachable. (The POD browsers usually go to the
first among duplicates.)
This rule is often too strict. It can be good to have similar subheadings
like "Details" as shown above, with no need to make such sub-parts reachable
by a link.
=head2 Disabling
If you don't care at all about this you can disable
C<ProhibitDuplicateHeadings> from your F<.perlcriticrc> in the usual way (see
L<Perl::Critic/CONFIGURATION>),
[-Documentation::ProhibitDuplicateHeadings]
=head1 CONFIGURATION
=over 4
=item C<uniqueness> (string, default "default")
The uniqueness to be enforced on each heading. The value is a
comma-separated list of
default currently "ancestor,sibling,adjacent"
ancestor don't duplicate parent, grandparent, etc
sibling same level and parent
adjacent immediately preceding, irrespective of level
all all headings
The default is "default" and the intention is to have default mean a
sensible set of restrictions, though precisely what it might be could
change.
For example in your F<.perlcriticrc> file
[Documentation::ProhibitDuplicateHeadings]
uniqueness=ancestor,adjacent
=back
=head1 SEE ALSO
L<Perl::Critic::Pulp>, L<Perl::Critic>
L<Perl::Critic::Policy::Documentation::ProhibitDuplicateSeeAlso>,
L<Perl::Critic::Policy::Documentation::RequirePodSections>
=head1 HOME PAGE
http://user42.tuxfamily.org/perl-critic-pulp/index.html
=head1 COPYRIGHT
Copyright 2013, 2014, 2015, 2016, 2017 Kevin Ryde
Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
Perl-Critic-Pulp is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
=cut
|