Login
RT #65663 RequireConsistentNewlines five-part PPI location
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / CodeLayout / RequireConsistentNewlines.pm
CommitLineData
dc93df4f 1##############################################################################
70390a27
CD
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
dc93df4f 6##############################################################################
70390a27
CD
7
8package Perl::Critic::Policy::CodeLayout::RequireConsistentNewlines;
9
df6dee2b 10use 5.006001;
70390a27
CD
11use strict;
12use warnings;
c680a9c9
ES
13use Readonly;
14
bbf4108c 15use Perl::Critic::Utils qw{ :severities };
dc93df4f 16use PPI::Token::Whitespace;
70390a27
CD
17use English qw(-no_match_vars);
18use base 'Perl::Critic::Policy';
19
010173e0 20our $VERSION = '1.112_002';
70390a27 21
5d6f7fbc 22Readonly::Scalar my $LINE_END => qr/\015{1,2}\012|[\012\015]/mxs;
70390a27 23
6036a254 24#-----------------------------------------------------------------------------
70390a27 25
c680a9c9
ES
26Readonly::Scalar my $DESC => q{Use the same newline through the source};
27Readonly::Scalar my $EXPL => q{Change your newlines to be the same throughout};
70390a27 28
6036a254 29#-----------------------------------------------------------------------------
70390a27 30
e178fe38 31sub supported_parameters { return () }
c680a9c9
ES
32sub default_severity { return $SEVERITY_HIGH }
33sub default_themes { return qw( core bugs ) }
34sub applies_to { return 'PPI::Document' }
70390a27 35
6036a254 36#-----------------------------------------------------------------------------
70390a27
CD
37
38sub violates {
39 my ( $self, undef, $doc ) = @_;
40
41 my $filename = $doc->filename();
42 return if !$filename;
43
44 my $fh;
45 return if !open $fh, '<', $filename;
46 local $RS = undef;
47 my $source = <$fh>;
9fb2d1dc 48 close $fh or return;
70390a27
CD
49
50 my $newline; # undef until we find the first one
51 my $line = 1;
52 my @v;
53 while ( $source =~ m/\G([^\012\015]*)($LINE_END)/cgmxs ) {
54 my $code = $1;
55 my $nl = $2;
56 my $col = length $code;
57 $newline ||= $nl;
58 if ( $nl ne $newline ) {
59 my $token = PPI::Token::Whitespace->new( $nl );
6eb1ad2a
TW
60 # TODO this is a terrible violation of encapsulation, but absent a
61 # mechanism to override the line numbers in the violation, I do
62 # not know what to do about it.
63 $token->{_location} = [$line, $col, $col, $line, $filename];
c680a9c9 64 push @v, $self->violation( $DESC, $EXPL, $token );
70390a27
CD
65 }
66 $line++;
67 }
68 return @v;
69}
70
711;
72
6036a254 73#-----------------------------------------------------------------------------
70390a27
CD
74
75__END__
76
77=pod
78
dc93df4f
JRT
79=for stopwords GnuPG
80
70390a27
CD
81=head1 NAME
82
9c338374 83Perl::Critic::Policy::CodeLayout::RequireConsistentNewlines - Use the same newline through the source.
70390a27 84
0cb729f0 85
11f53956 86=head1 AFFILIATION
0cb729f0 87
11f53956
ES
88This Policy is part of the core L<Perl::Critic|Perl::Critic>
89distribution.
90
91
92=head1 DESCRIPTION
93
94Source code files are divided into lines with line endings of C<\r>,
95C<\n> or C<\r\n>. Mixing these different line endings causes problems
96in many text editors and, notably, Module::Signature and GnuPG.
0cb729f0
ES
97
98
70390a27
CD
99=head1 CAVEAT
100
101This policy works outside of PPI because PPI automatically normalizes
102source code to local newline conventions. So, this will only work if
103we know the filename of the source code.
104
af93c316 105
11f53956 106=head1 CONFIGURATION
af93c316 107
11f53956 108This Policy is not configurable except for the standard options.
af93c316 109
70390a27
CD
110
111=head1 AUTHOR
112
113Chris Dolan <cdolan@cpan.org>
114
11f53956 115
70390a27
CD
116=head1 COPYRIGHT
117
53b8903f 118Copyright (c) 2006-2011 Chris Dolan.
70390a27
CD
119
120This program is free software; you can redistribute it and/or modify
121it under the same terms as Perl itself.
122
123=cut
737d3b65
CD
124
125# Local Variables:
126# mode: cperl
127# cperl-indent-level: 4
128# fill-column: 78
129# indent-tabs-mode: nil
130# c-indentation-style: bsd
131# End:
96fed375 132# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :