File Coverage

blib/lib/Perl/Critic/Policy/CodeLayout/ProhibitTrailingWhitespace.pm
Criterion Covered Total %
statement 42 43 97.6
branch 5 6 83.3
condition n/a
subroutine 14 14 100.0
pod 4 5 80.0
total 65 68 95.5


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::CodeLayout::ProhibitTrailingWhitespace;
2              
3 40     40   26467 use 5.010001;
  40         193  
4 40     40   273 use strict;
  40         117  
  40         3088  
5 40     40   2585 use warnings;
  40         129  
  40         1117  
6 40     40   256 use Readonly;
  40         138  
  40         2095  
7              
8 40     40   22587 use charnames qw{};
  40         1165798  
  40         1176  
9              
10 40     40   379 use PPI::Token::Whitespace;
  40         129  
  40         1169  
11 40     40   252 use Perl::Critic::Utils qw{ :characters :severities };
  40         114  
  40         3149  
12              
13 40     40   12709 use parent 'Perl::Critic::Policy';
  40         151  
  40         339  
14              
15             our $VERSION = '1.150';
16              
17             #-----------------------------------------------------------------------------
18              
19             Readonly::Scalar my $EXPL => q{Don't use whitespace at the end of lines};
20              
21             ## no critic (RequireInterpolationOfMetachars)
22             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 92     92 0 1605 sub supported_parameters { return qw{ } }
37 80     80 1 330 sub default_severity { return $SEVERITY_LOWEST }
38 74     74 1 334 sub default_themes { return qw( core maintenance ) }
39 33     33 1 91 sub applies_to { return 'PPI::Token::Whitespace' }
40              
41             #-----------------------------------------------------------------------------
42              
43             sub violates {
44 1007     1007 1 1488 my ( $self, $token, undef ) = @_;
45              
46 1007 100       1574 if ( $token->content() =~ m< ( (?! \n) \s )+ \n >xms ) {
47 6         63 my $extra_whitespace = $1;
48              
49 6         13 my $description = q{Found "};
50             $description .=
51             join
52             $EMPTY,
53 6         94 map { _escape($_) } split $EMPTY, $extra_whitespace;
  6         21  
54 6         13234 $description .= q{" at the end of the line};
55              
56 6         28 return $self->violation( $description, $EXPL, $token );
57             }
58              
59 1001         3849 return;
60             }
61              
62             sub _escape {
63 6     6   17 my $character = shift;
64 6         17 my $ordinal = ord $character;
65              
66 6 100       67 if (my $c_escape = $C_STYLE_ESCAPES{$ordinal}) {
67 3         58 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 50       75 if ( charnames->can( 'viacode' ) ) {
79 3         15 return q/\N{/ . charnames::viacode($ordinal) . q/}/;
80             }
81             else {
82 0           return '\N{WHITESPACE CHAR}';
83             }
84             }
85              
86             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 :