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   26407 use 5.010001;
  40         190  
4 40     40   292 use strict;
  40         133  
  40         947  
5 40     40   252 use warnings;
  40         109  
  40         1035  
6 40     40   284 use Readonly;
  40         117  
  40         2298  
7              
8 40     40   314 use Perl::Critic::Utils qw{ :severities };
  40         196  
  40         2009  
9 40     40   5287 use PPI::Token::Whitespace;
  40         153  
  40         1802  
10 40     40   290 use English qw(-no_match_vars);
  40         148  
  40         443  
11 40     40   16680 use parent 'Perl::Critic::Policy';
  40         134  
  40         271  
12              
13             our $VERSION = '1.148';
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 1751 sub supported_parameters { return () }
25 138     138 1 548 sub default_severity { return $SEVERITY_HIGH }
26 74     74 1 337 sub default_themes { return qw( core bugs ) }
27 61     61 1 196 sub applies_to { return 'PPI::Document' }
28              
29             #-----------------------------------------------------------------------------
30              
31             sub violates {
32 61     61 1 185 my ( $self, undef, $doc ) = @_;
33              
34 61         204 my $filename = $doc->filename();
35 61 100       477 return if !$filename;
36              
37 27         58 my $fh;
38 27 50       1252 return if !open $fh, '<', $filename;
39 27         182 local $RS = undef;
40 27         819 my $source = <$fh>;
41 27 50       337 close $fh or return;
42              
43 27         86 my $newline; # undef until we find the first one
44 27         53 my $line = 1;
45 27         71 my @v;
46 27         400 while ( $source =~ m/\G([^\012\015]*)($LINE_END)/cgmxs ) {
47 561         1255 my $code = $1;
48 561         898 my $nl = $2;
49 561         899 my $col = length $code;
50 561   66     1105 $newline ||= $nl;
51 561 100       1071 if ( $nl ne $newline ) {
52 64         331 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         509 $token->{_location} = [$line, $col, $col, $line, $filename];
57 64         219 push @v, $self->violation( $DESC, $EXPL, $token );
58             }
59 561         2841 $line++;
60             }
61 27         220 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 :