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   27137 use 5.010001;
  40         202  
4 40     40   278 use strict;
  40         115  
  40         885  
5 40     40   262 use warnings;
  40         106  
  40         1287  
6 40     40   274 use English qw(-no_match_vars);
  40         132  
  40         319  
7 40     40   15111 use Readonly;
  40         133  
  40         2176  
8              
9 40     40   22587 use charnames qw{};
  40         1288198  
  40         1241  
10              
11 40     40   347 use PPI::Token::Whitespace;
  40         117  
  40         1254  
12 40     40   255 use Perl::Critic::Utils qw{ :characters :severities };
  40         101  
  40         3373  
13              
14 40     40   13005 use parent 'Perl::Critic::Policy';
  40         128  
  40         343  
15              
16             our $VERSION = '1.146';
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 1654 sub supported_parameters { return qw{ } }
38 80     80 1 409 sub default_severity { return $SEVERITY_LOWEST }
39 74     74 1 334 sub default_themes { return qw( core maintenance ) }
40 33     33 1 105 sub applies_to { return 'PPI::Token::Whitespace' }
41              
42             #-----------------------------------------------------------------------------
43              
44             sub violates {
45 1007     1007 1 1857 my ( $self, $token, undef ) = @_;
46              
47 1007 100       1974 if ( $token->content() =~ m< ( (?! \n) \s )+ \n >xms ) {
48 6         64 my $extra_whitespace = $1;
49              
50 6         18 my $description = q{Found "};
51             $description .=
52             join
53             $EMPTY,
54 6         60 map { _escape($_) } split $EMPTY, $extra_whitespace;
  6         20  
55 6         12179 $description .= q{" at the end of the line};
56              
57 6         27 return $self->violation( $description, $EXPL, $token );
58             }
59              
60 1001         4625 return;
61             }
62              
63             sub _escape {
64 6     6   13 my $character = shift;
65 6         14 my $ordinal = ord $character;
66              
67 6 100       35 if (my $c_escape = $C_STYLE_ESCAPES{$ordinal}) {
68 3         59 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         14 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 :