File Coverage

blib/lib/Perl/Critic/Policy/CodeLayout/ProhibitTrailingWhitespace.pm
Criterion Covered Total %
statement 45 46 97.8
branch 5 6 83.3
condition n/a
subroutine 15 15 100.0
pod 4 5 80.0
total 69 72 95.8


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