File Coverage

blib/lib/Perl/Critic/Policy/CodeLayout/RequireConsistentNewlines.pm
Criterion Covered Total %
statement 49 49 100.0
branch 6 8 75.0
condition 2 3 66.6
subroutine 13 13 100.0
pod 4 5 80.0
total 74 78 94.8


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::CodeLayout::RequireConsistentNewlines;
2              
3 40     40   27618 use 5.010001;
  40         198  
4 40     40   276 use strict;
  40         113  
  40         876  
5 40     40   247 use warnings;
  40         132  
  40         995  
6 40     40   280 use Readonly;
  40         159  
  40         2134  
7              
8 40     40   318 use Perl::Critic::Utils qw{ :severities };
  40         173  
  40         2050  
9 40     40   5508 use PPI::Token::Whitespace;
  40         120  
  40         1774  
10 40     40   316 use English qw(-no_match_vars);
  40         135  
  40         367  
11 40     40   17004 use parent 'Perl::Critic::Policy';
  40         162  
  40         270  
12              
13             our $VERSION = '1.146';
14              
15             Readonly::Scalar my $LINE_END => qr/\015{1,2}\012|[\012\015]/mxs;
16              
17             #-----------------------------------------------------------------------------
18              
19             Readonly::Scalar my $DESC => q{Use the same newline through the source};
20             Readonly::Scalar my $EXPL => q{Change your newlines to be the same throughout};
21              
22             #-----------------------------------------------------------------------------
23              
24 118     118 0 1674 sub supported_parameters { return () }
25 138     138 1 563 sub default_severity { return $SEVERITY_HIGH }
26 74     74 1 364 sub default_themes { return qw( core bugs ) }
27 61     61 1 195 sub applies_to { return 'PPI::Document' }
28              
29             #-----------------------------------------------------------------------------
30              
31             sub violates {
32 61     61 1 184 my ( $self, undef, $doc ) = @_;
33              
34 61         230 my $filename = $doc->filename();
35 61 100       466 return if !$filename;
36              
37 27         57 my $fh;
38 27 50       1228 return if !open $fh, '<', $filename;
39 27         201 local $RS = undef;
40 27         831 my $source = <$fh>;
41 27 50       360 close $fh or return;
42              
43 27         70 my $newline; # undef until we find the first one
44 27         50 my $line = 1;
45 27         64 my @v;
46 27         354 while ( $source =~ m/\G([^\012\015]*)($LINE_END)/cgmxs ) {
47 561         1249 my $code = $1;
48 561         916 my $nl = $2;
49 561         855 my $col = length $code;
50 561   66     1151 $newline ||= $nl;
51 561 100       1088 if ( $nl ne $newline ) {
52 64         293 my $token = PPI::Token::Whitespace->new( $nl );
53             # TODO this is a terrible violation of encapsulation, but absent a
54             # mechanism to override the line numbers in the violation, I do
55             # not know what to do about it.
56 64         475 $token->{_location} = [$line, $col, $col, $line, $filename];
57 64         219 push @v, $self->violation( $DESC, $EXPL, $token );
58             }
59 561         2877 $line++;
60             }
61 27         218 return @v;
62             }
63              
64             1;
65              
66             #-----------------------------------------------------------------------------
67              
68             __END__
69              
70             =pod
71              
72             =for stopwords GnuPG
73              
74             =head1 NAME
75              
76             Perl::Critic::Policy::CodeLayout::RequireConsistentNewlines - Use the same newline through the source.
77              
78              
79             =head1 AFFILIATION
80              
81             This Policy is part of the core L<Perl::Critic|Perl::Critic>
82             distribution.
83              
84              
85             =head1 DESCRIPTION
86              
87             Source code files are divided into lines with line endings of C<\r>,
88             C<\n> or C<\r\n>. Mixing these different line endings causes problems
89             in many text editors and, notably, Module::Signature and GnuPG.
90              
91              
92             =head1 CAVEAT
93              
94             This policy works outside of PPI because PPI automatically normalizes
95             source code to local newline conventions. So, this will only work if
96             we know the filename of the source code.
97              
98              
99             =head1 CONFIGURATION
100              
101             This Policy is not configurable except for the standard options.
102              
103              
104             =head1 AUTHOR
105              
106             Chris Dolan <cdolan@cpan.org>
107              
108              
109             =head1 COPYRIGHT
110              
111             Copyright (c) 2006-2011 Chris Dolan.
112              
113             This program is free software; you can redistribute it and/or modify
114             it under the same terms as Perl itself.
115              
116             =cut
117              
118             # Local Variables:
119             # mode: cperl
120             # cperl-indent-level: 4
121             # fill-column: 78
122             # indent-tabs-mode: nil
123             # c-indentation-style: bsd
124             # End:
125             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :