Login
Fix false positive in ProhibitSingleCharAlternation
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / CodeLayout / RequireTidyCode.pm
CommitLineData
6036a254 1##############################################################################
dff08b70
JRT
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6036a254 6##############################################################################
dff08b70 7
59b05e08
JRT
8package Perl::Critic::Policy::CodeLayout::RequireTidyCode;
9
df6dee2b 10use 5.006001;
59b05e08
JRT
11use strict;
12use warnings;
c680a9c9
ES
13use Readonly;
14
bf159007 15use English qw(-no_match_vars);
005d2cb9 16use Perl::Critic::Utils qw{ :booleans :characters :severities };
59b05e08
JRT
17use base 'Perl::Critic::Policy';
18
a8da49fd 19our $VERSION = '1.088';
59b05e08 20
6036a254 21#-----------------------------------------------------------------------------
dff08b70 22
c680a9c9
ES
23Readonly::Scalar my $DESC => q{Code is not tidy};
24Readonly::Scalar my $EXPL => [ 33 ];
59b05e08 25
6036a254 26#-----------------------------------------------------------------------------
59b05e08 27
b42b8ea3
ES
28sub supported_parameters {
29 return (
30 {
31 name => 'perltidyrc',
32 description => 'The Perl::Tidy configuration file to use, if any.',
33 default_string => undef,
34 },
35 );
36}
37
c680a9c9
ES
38sub default_severity { return $SEVERITY_LOWEST }
39sub default_themes { return qw(core pbp cosmetic) }
40sub applies_to { return 'PPI::Document' }
dff08b70 41
6036a254 42#-----------------------------------------------------------------------------
02558daa 43
005d2cb9
ES
44sub initialize_if_enabled {
45 my ($self, $config) = @_;
85da2bf2 46
613f4745
CD
47 # workaround for Test::Without::Module v0.11
48 local $EVAL_ERROR = undef;
49
005d2cb9 50 # If Perl::Tidy is missing, bow out.
49622277 51 eval { require Perl::Tidy; } or return $FALSE;
02558daa
CD
52
53 #Set configuration if defined
fd5bd7b5
JRT
54 if (defined $self->{_perltidyrc} && $self->{_perltidyrc} eq $EMPTY) {
55 $self->{_perltidyrc} = \$EMPTY;
02558daa
CD
56 }
57
005d2cb9 58 return $TRUE;
02558daa
CD
59}
60
6036a254 61#-----------------------------------------------------------------------------
59b05e08
JRT
62
63sub violates {
64 my ( $self, $elem, $doc ) = @_;
bf159007 65
05bf01cd 66 # Perl::Tidy seems to produce slightly different output, depending
871ec032 67 # on the trailing whitespace in the input. As best I can tell,
fd9d73d0
JRT
68 # Perl::Tidy will truncate any extra trailing newlines, and if the
69 # input has no trailing newline, then it adds one. But when you
70 # re-run it through Perl::Tidy here, that final newline gets lost,
71 # which causes the policy to insist that the code is not tidy.
72 # This only occurs when Perl::Tidy is writing the output to a
73 # scalar, but does not occur when writing to a file. I may
74 # investigate further, but for now, this seems to do the trick.
871ec032 75
02558daa 76 my $source = $doc->serialize();
fd9d73d0 77 $source =~ s{ \s+ \Z}{\n}mx;
871ec032 78
44b74300 79 # Remove the shell fix code from the top of program, if applicable
682ce894 80 ## no critic(ProhibitComplexRegexes)
03261933
ES
81 my $shebang_re = qr< [#]! [^\015\012]+ [\015\012]+ >xms;
82 my $shell_re = qr<eval [ ] 'exec [ ] [^\015\012]* [ ] \$0 [ ] \${1[+]"\$@"}'
67ab80cc 83 [ \t]*[\012\015]+ [ \t]* if [^\015\012]+ [\015\012]+ >xms;
44b74300
CD
84 $source =~ s/\A ($shebang_re) $shell_re /$1/xms;
85
59b05e08 86 my $dest = $EMPTY;
59b05e08
JRT
87 my $stderr = $EMPTY;
88
871ec032 89
6bf9b465
JRT
90 # Perl::Tidy gets confused if @ARGV has arguments from
91 # another program. Also, we need to override the
92 # stdout and stderr redirects that the user may have
93 # configured in their .perltidyrc file.
94 local @ARGV = qw(-nst -nse); ## no critic
ee99f507 95
d5fc0818 96 # Trap Perl::Tidy errors, just in case it dies
49622277 97 my $eval_worked = eval {
0a6f07d0
AL
98 Perl::Tidy::perltidy(
99 source => \$source,
100 destination => \$dest,
101 stderr => \$stderr,
02558daa 102 defined $self->{_perltidyrc} ? (perltidyrc => $self->{_perltidyrc}) : (),
d5fc0818 103 );
c713b566 104 1;
d5fc0818
JRT
105 };
106
49622277 107 if ($stderr or not $eval_worked) {
59b05e08 108 # Looks like perltidy had problems
c680a9c9 109 return $self->violation( 'perltidy had errors!!', $EXPL, $elem );
59b05e08
JRT
110 }
111
47639f94 112 if ( $source ne $dest ) {
c680a9c9 113 return $self->violation( $DESC, $EXPL, $elem );
59b05e08
JRT
114 }
115
116 return; #ok!
117}
118
1191;
120
6036a254 121#-----------------------------------------------------------------------------
6bf9b465 122
59b05e08
JRT
123__END__
124
125=pod
126
127=head1 NAME
128
9c338374 129Perl::Critic::Policy::CodeLayout::RequireTidyCode - Must run code through L<perltidy>.
59b05e08 130
af93c316
ES
131=head1 AFFILIATION
132
133This Policy is part of the core L<Perl::Critic> distribution.
134
135
59b05e08
JRT
136=head1 DESCRIPTION
137
138Conway does make specific recommendations for whitespace and
139curly-braces in your code, but the most important thing is to adopt a
140consistent layout, regardless of the specifics. And the easiest way
141to do that is to use L<Perl::Tidy>. This policy will complain if
142you're code hasn't been run through Perl::Tidy.
143
6c893fc9 144=head1 CONFIGURATION
02558daa 145
6c893fc9
ES
146This policy can be configured to tell Perl::Tidy to use a particular
147F<perltidyrc> file or no configuration at all. By default, Perl::Tidy is told
148to look in its default location for configuration. Perl::Critic can be told to
149tell Perl::Tidy to use a specific configuration file by putting an entry in a
150F<.perlcriticrc> file like this:
02558daa
CD
151
152 [CodeLayout::RequireTidyCode]
153 perltidyrc = /usr/share/perltidy.conf
154
155As a special case, setting C<perltidyrc> to the empty string tells
156Perl::Tidy not to load any configuration file at all and just use
157Perl::Tidy's own default style.
158
159 [CodeLayout::RequireTidyCode]
8e2aefdb 160 perltidyrc =
02558daa 161
59b05e08
JRT
162=head1 NOTES
163
df249cc5
JRT
164L<Perl::Tidy> is not included in the Perl::Critic distribution. The
165latest version of Perl::Tidy can be downloaded from CPAN. If
166Perl::Tidy is not installed, this policy is silently ignored.
59b05e08
JRT
167
168=head1 SEE ALSO
169
170L<Perl::Tidy>
171
59b05e08
JRT
172=head1 AUTHOR
173
174Jeffrey Ryan Thalhammer <thaljef@cpan.org>
175
176=head1 COPYRIGHT
177
20dfddeb 178Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
59b05e08
JRT
179
180This program is free software; you can redistribute it and/or modify
181it under the same terms as Perl itself. The full text of this license
182can be found in the LICENSE file included with this module.
183
184=cut
737d3b65
CD
185
186# Local Variables:
187# mode: cperl
188# cperl-indent-level: 4
189# fill-column: 78
190# indent-tabs-mode: nil
191# c-indentation-style: bsd
192# End:
96fed375 193# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :