blib/lib/Perl/Critic/Policy/CodeLayout/ProhibitTrailingWhitespace.pm

filter: Statementstmt 97.8% 46 / 47 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 80.0% 4 / 5 totaltotal 95.8% 69 / 72 CC 4 SCAR 3.9
CC
4
Cov
96%
_escape
3.1
violates
2.0
BEGIN
1.0
CRAP 1.5
« blib/lib/Perl/Critic/ThemeListing.pm ↑ Summary blib/lib/Perl/Critic/Policy/Modules/RequireBarewordIncludes.pm »
1 package Perl::Critic::Policy::CodeLayout::ProhibitTrailingWhitespace;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use charnames qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 40 ▶use PPI::Token::Whitespace;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use Perl::Critic::Utils qw{ :characters :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 40 our $VERSION = '1.150';
16  
17 #-----------------------------------------------------------------------------
18  
19 40 Readonly::Scalar my $EXPL => q{Don't use whitespace at the end of lines};
20  
21 ## no critic (RequireInterpolationOfMetachars)
22 40 Readonly::Hash my %C_STYLE_ESCAPES =>
23 (
24 ord "\t" => q{\t},
25 ord "\n" => q{\n},
26 ord "\r" => q{\r},
27 ord "\f" => q{\f},
28 ord "\b" => q{\b},
29 ord "\a" => q{\a},
30 ord "\e" => q{\e},
31 );
32 ## use critic
33  
34 #-----------------------------------------------------------------------------
35  
36 93 ▶sub supported_parameters { return qw{ } }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
37 81 ▶sub default_severity { return $SEVERITY_LOWEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
38 74 ▶sub default_themes { return qw( core maintenance ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
39 34 ▶sub applies_to { return 'PPI::Token::Whitespace' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
40  
41 #-----------------------------------------------------------------------------
42  
43 sub violates {
44 1011 ▶ my ( $self, $token, undef ) = @_;
Subroutine violates: called CC=2 SCAR=6.9
Pod documented
45  
46 1011 ▶ if ( $token->content() =~ m< ( (?! \n) \s )+ \n >xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->content =~ / ( (?! \n) \s )+ \n /msx) 6 1005 1011
47 6 my $extra_whitespace = $1;
48  
49 6 my $description = q{Found "};
50 $description .=
51 join
52 $EMPTY,
53 6 map { _escape($_) } split $EMPTY, $extra_whitespace;
54 6 $description .= q{" at the end of the line};
55  
56 6 return $self->violation( $description, $EXPL, $token );
57 }
58  
59 1005 return;
60 }
61  
62 sub _escape {
63 6 ▶ my $character = shift;
Subroutine _escape: called CC=3 SCAR=11.2
64 6 my $ordinal = ord $character;
65  
66 6 ▶ if (my $c_escape = $C_STYLE_ESCAPES{$ordinal}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c_escape = $C_STYLE_ESCAPES{$ordinal}) 3 3 6
67 3 return $c_escape;
68 }
69  
70  
71 # Apparently, the charnames.pm that ships with older perls does not
72 # support the C<viacode> function, and newer versions of the module are
73 # not distributed separately from perl itself So if the C<viacode> method
74 # is not supported, then just substitute something.
75  
76  
77 ## no critic (RequireInterpolationOfMetachars)
78 3 ▶ if ( charnames->can( 'viacode' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('charnames'->can('viacode')) { } 3 0 3
79 3 return q/\N{/ . charnames::viacode($ordinal) . q/}/;
80 }
81 else {
82 0 return '\N{WHITESPACE CHAR}';
83 }
84 }
85  
86 40 1;
87  
88 #-----------------------------------------------------------------------------
89  
90 __END__
91  
92 =pod
93  
94 =for stopwords
95  
96 =head1 NAME
97  
98 Perl::Critic::Policy::CodeLayout::ProhibitTrailingWhitespace - Don't use whitespace at the end of lines.
99  
100  
101 =head1 AFFILIATION
102  
103 This Policy is part of the core L<Perl::Critic|Perl::Critic>
104 distribution.
105  
106  
107 =head1 DESCRIPTION
108  
109 Anything that is not readily visually detectable is a bad thing in
110 general, and more specifically, as different people edit the same
111 code, their editors may automatically strip out trailing whitespace,
112 causing spurious differences between different versions of the same
113 file (i.e. code in a source control system).
114  
115  
116 =head1 CONFIGURATION
117  
118 This Policy is not configurable except for the standard options.
119  
120  
121 =head1 AUTHOR
122  
123 Elliot Shank C<< <perl@galumph.com> >>
124  
125 =head1 COPYRIGHT
126  
127 Copyright (c) 2007-2023 Elliot Shank
128  
129 This program is free software; you can redistribute it and/or modify
130 it under the same terms as Perl itself. The full text of this license
131 can be found in the LICENSE file included with this module.
132  
133 =cut
134  
135 # Local Variables:
136 # mode: cperl
137 # cperl-indent-level: 4
138 # fill-column: 78
139 # indent-tabs-mode: nil
140 # c-indentation-style: bsd
141 # End:
142 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/ThemeListing.pm ↑ Summary blib/lib/Perl/Critic/Policy/Modules/RequireBarewordIncludes.pm »