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   23427 use 5.010001;
  40         154  
4 40     40   176 use strict;
  40         76  
  40         730  
5 40     40   135 use warnings;
  40         81  
  40         1432  
6 40     40   158 use Readonly;
  40         74  
  40         1662  
7              
8 40     40   16261 use charnames qw{};
  40         295382  
  40         1054  
9              
10 40     40   334 use PPI::Token::Whitespace;
  40         83  
  40         1502  
11 40     40   184 use Perl::Critic::Utils qw{ :characters :severities };
  40         72  
  40         2833  
12              
13 40     40   10257 use parent 'Perl::Critic::Policy';
  40         86  
  40         265  
14              
15             our $VERSION = '1.156';
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 93     93 0 555 sub supported_parameters { return qw{ } }
37 81     81 1 317 sub default_severity { return $SEVERITY_LOWEST }
38 74     74 1 264 sub default_themes { return qw( core maintenance ) }
39 34     34 1 88 sub applies_to { return 'PPI::Token::Whitespace' }
40              
41             #-----------------------------------------------------------------------------
42              
43             sub violates {
44 1011     1011 1 1180 my ( $self, $token, undef ) = @_;
45              
46 1011 100       1422 if ( $token->content() =~ m< ( (?! \n) \s )+ \n >xms ) {
47 6         53 my $extra_whitespace = $1;
48              
49 6         8 my $description = q{Found "};
50             $description .=
51             join
52             $EMPTY,
53 6         50 map { _escape($_) } split $EMPTY, $extra_whitespace;
  6         11  
54 6         18716 $description .= q{" at the end of the line};
55              
56 6         21 return $self->violation( $description, $EXPL, $token );
57             }
58              
59 1005         2994 return;
60             }
61              
62             sub _escape {
63 6     6   9 my $character = shift;
64 6         11 my $ordinal = ord $character;
65              
66 6 100       30 if (my $c_escape = $C_STYLE_ESCAPES{$ordinal}) {
67 3         28 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       48 if ( charnames->can( 'viacode' ) ) {
79 3         10 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 :