| 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 ProhibitLinkToSelf ProhibitLinkToSelf.pm | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | package Perl::Critic::Policy::Documentation::ProhibitLinkToSelf; | ||||||||||||||||||||||||||||
| 23 | 40 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 40 | ▶ | use base 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 40 | ▶ | use Perl::Critic::Utils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 30 | #use Devel::Comments; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 40 | our $VERSION = 100; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 40 | ▶ | use constant supported_parameters => (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 40 | ▶ | use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 40 | ▶ | use constant default_themes => qw(pulp cosmetic); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 40 | ▶ | use constant applies_to => 'PPI::Document'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 6 | ▶ | sub violates { | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 6 | my ($self, $elem, $document) = @_; | |||||||||||||||||||||||||||
| 41 | # ### ProhibitLinkToSelf on: $elem->content | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 6 | my $parser = Perl::Critic::Pulp::PodParser::ProhibitLinkToSelf->new | |||||||||||||||||||||||||||
| 44 | (policy => $self); | ||||||||||||||||||||||||||||
| 45 | 6 | $parser->parse_from_elem ($elem); | |||||||||||||||||||||||||||
| 46 | 6 | return $parser->violations; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | package Perl::Critic::Pulp::PodParser::ProhibitLinkToSelf; | ||||||||||||||||||||||||||||
| 50 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 40 | ▶ | use Pod::ParseLink; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 40 | ▶ | use base 'Perl::Critic::Pulp::PodParser'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 40 | my %command_non_text = (for => 1, | |||||||||||||||||||||||||||
| 56 | begin => 1, | ||||||||||||||||||||||||||||
| 57 | end => 1, | ||||||||||||||||||||||||||||
| 58 | # cut => 1, # not seen unless -process_cut_cmd | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | 8 | ▶ | sub command { | ||||||||||||||||||||||||||
|
Subroutine
command: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 61 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 62 | 8 | my ($command, $text, $linenum, $paraobj) = @_; | |||||||||||||||||||||||||||
| 63 | 8 | $self->SUPER::command(@_); # maintain 'in_begin' | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # if ($command eq 'for' | ||||||||||||||||||||||||||||
| 66 | # && $text =~ /^ProhibitLinkToSelf\b\s*(.*)/) { | ||||||||||||||||||||||||||||
| 67 | # my $directive = $1; | ||||||||||||||||||||||||||||
| 68 | # ### $directive | ||||||||||||||||||||||||||||
| 69 | # if ($directive =~ /^allow next( (\d+))?/) { | ||||||||||||||||||||||||||||
| 70 | # # numbered "allow next 5" means up to that many following, | ||||||||||||||||||||||||||||
| 71 | # # unnumbered "allow next" means one following | ||||||||||||||||||||||||||||
| 72 | # $self->{'allow_next'} = (defined $2 ? $2 : 1); | ||||||||||||||||||||||||||||
| 73 | # } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 8 | ▶ | if ($command eq 'head1') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 7 | $self->{'in_name'} = ($text =~ /^\s*NAME\b/); | |||||||||||||||||||||||||||
| 77 | 7 | $self->{'in_see_also'} = ($text =~ /^\s*SEE\s+ALSO\b/); | |||||||||||||||||||||||||||
| 78 | ### in_name now: $self->{'in_name'} | ||||||||||||||||||||||||||||
| 79 | ### in_see_also: $self->{'in_see_also'} | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 8 | ▶ | unless ($command_non_text{$command}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | # padded for the column number right, the leading spaces do no harm here | ||||||||||||||||||||||||||||
| 84 | 8 | _check_text ($self, | |||||||||||||||||||||||||||
| 85 | (' ' x (length($command)+1)) . $text, | ||||||||||||||||||||||||||||
| 86 | $linenum, | ||||||||||||||||||||||||||||
| 87 | $paraobj); | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 8 | return ''; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 11 | ▶ | sub textblock { | ||||||||||||||||||||||||||
|
Subroutine
textblock: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||
| 94 | 11 | my ($self, $text, $linenum, $paraobj) = @_; | |||||||||||||||||||||||||||
| 95 | ### textblock(): "linenum=$linenum" | ||||||||||||||||||||||||||||
| 96 | ### $text | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # "=begin :foo" is markup, check it. Other =begin is not markup. | ||||||||||||||||||||||||||||
| 99 | 11 | ▶ | unless ($self->{'in_begin'} eq '' || $self->{'in_begin'} =~ /^:/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/
MC/DC0%0/2
$self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/
$self->{"in_begin"} eq ""
$self->{"in_begin"} =~ /^:/
Truth table33%1/3
A or B
A= $self->{"in_begin"} eq ""B= $self->{"in_begin"} =~ /^:/
| |||||||||||||||||||||||||||||
| 100 | 0 | return ''; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 11 | my $str = _check_text ($self, $text, $linenum, $paraobj); | |||||||||||||||||||||||||||
| 104 | ### interpolated: $str | ||||||||||||||||||||||||||||
| 105 | 11 | ▶ | if ($self->{'in_name'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 10 | ▶ | if ($str =~ /^\s*([[:word:]:]+)\s*-/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | ### add own package name: $1 | ||||||||||||||||||||||||||||
| 108 | 6 | $self->{'own_package_names'}->{$1} = 1; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 11 | return ''; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 19 | ▶ | sub _check_text { | ||||||||||||||||||||||||||
|
Subroutine
_check_text: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 19 | my ($self, $text, $linenum, $paraobj) = @_; | |||||||||||||||||||||||||||
| 116 | ### _check_text() ... | ||||||||||||||||||||||||||||
| 117 | ### $linenum | ||||||||||||||||||||||||||||
| 118 | 19 | return $self->interpolate($text, $linenum); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 7 | ▶ | sub interior_sequence { | ||||||||||||||||||||||||||
|
Subroutine
interior_sequence: called CC=7 SCAR=20.1
| |||||||||||||||||||||||||||||
| 122 | 7 | my ($self, $cmd, $text, $paraobj) = @_; | |||||||||||||||||||||||||||
| 123 | ### interior_sequence() ... | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 7 | ▶ | if ($cmd eq 'X') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 126 | # index entry, no text output, but keep newlines for linenum | ||||||||||||||||||||||||||||
| 127 | 0 | $text =~ tr/\n//cd; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | } elsif ($cmd eq 'L') { | ||||||||||||||||||||||||||||
| 130 | 6 | my ($display, $inferred, $name, $section, $type) | |||||||||||||||||||||||||||
| 131 | = Pod::ParseLink::parselink ($text); | ||||||||||||||||||||||||||||
| 132 | ### $display | ||||||||||||||||||||||||||||
| 133 | ### $inferred | ||||||||||||||||||||||||||||
| 134 | ### $name | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 6 | ▶ | if (defined $name && $self->{'own_package_names'}->{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $name and $self->{"own_package_names"}{$name}
MC/DC0%0/2
defined $name and $self->{"own_package_names"}{$name}
defined $name
$self->{"own_package_names"}{$name}
Truth table33%1/3
A and B
A= defined $nameB= $self->{"own_package_names"}{$name}
| |||||||||||||||||||||||||||||
| 137 | 6 | $text =~ /(\s*)$/; | |||||||||||||||||||||||||||
| 138 | 6 | my $pos = length($text) - length($1); # end of $text | |||||||||||||||||||||||||||
| 139 | ### $pos | ||||||||||||||||||||||||||||
| 140 | 6 | (undef, my $linenum) = $paraobj->file_line; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | $self->violation_at_linenum_and_textpos | ||||||||||||||||||||||||||||
| 143 | 6 | ▶ | (($self->{'in_see_also'} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | ? "L<> link to this POD itself in \"SEE ALSO\" section, probable typo" | ||||||||||||||||||||||||||||
| 145 | : "L<> link to this POD itself, suggest just C<> markup is enough"), | ||||||||||||||||||||||||||||
| 146 | $linenum, $text, $pos); | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 6 | ▶ | return (defined $display ? $display : $name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 1 | return $text; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 40 | 1; | |||||||||||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||||||||||