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   26693 use 5.010001;
  40         139  
4 40     40   232 use strict;
  40         86  
  40         826  
5 40     40   152 use warnings;
  40         71  
  40         1729  
6 40     40   192 use Readonly;
  40         87  
  40         2283  
7              
8 40     40   250 use Perl::Critic::Utils qw{ :severities };
  40         84  
  40         2025  
9 40     40   4637 use PPI::Token::Whitespace;
  40         88  
  40         1112  
10 40     40   192 use English qw(-no_match_vars);
  40         79  
  40         286  
11 40     40   13057 use parent 'Perl::Critic::Policy';
  40         79  
  40         218  
12              
13             our $VERSION = '1.156';
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 119     119 0 636 sub supported_parameters { return () }
25 139     139 1 477 sub default_severity { return $SEVERITY_HIGH }
26 74     74 1 243 sub default_themes { return qw( core bugs ) }
27 62     62 1 131 sub applies_to { return 'PPI::Document' }
28              
29             #-----------------------------------------------------------------------------
30              
31             sub violates {
32 62     62 1 136 my ( $self, undef, $doc ) = @_;
33              
34 62         263 my $filename = $doc->filename();
35 62 100       329 return if !$filename;
36              
37 27         38 my $fh;
38 27 50       1244 return if !open $fh, '<', $filename;
39 27         149 local $RS = undef;
40 27         630 my $source = <$fh>;
41 27 50       285 close $fh or return;
42              
43 27         43 my $newline; # undef until we find the first one
44 27         50 my $line = 1;
45 27         39 my @v;
46 27         396 while ( $source =~ m/\G([^\012\015]*)($LINE_END)/cgmxs ) {
47 561         871 my $code = $1;
48 561         754 my $nl = $2;
49 561         718 my $col = length $code;
50 561   66     867 $newline ||= $nl;
51 561 100       772 if ( $nl ne $newline ) {
52 64         375 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         490 $token->{_location} = [$line, $col, $col, $line, $filename];
57 64         267 push @v, $self->violation( $DESC, $EXPL, $token );
58             }
59 561         2323 $line++;
60             }
61 27         203 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 :