Login
Fix tests that broke as a result of Config now throwing an
[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
10use strict;
11use warnings;
c680a9c9
ES
12use Readonly;
13
bf159007 14use English qw(-no_match_vars);
005d2cb9 15use Perl::Critic::Utils qw{ :booleans :characters :severities };
59b05e08
JRT
16use base 'Perl::Critic::Policy';
17
0f68e4b5 18our $VERSION = '1.083_001';
59b05e08 19
6036a254 20#-----------------------------------------------------------------------------
dff08b70 21
c680a9c9
ES
22Readonly::Scalar my $DESC => q{Code is not tidy};
23Readonly::Scalar my $EXPL => [ 33 ];
59b05e08 24
6036a254 25#-----------------------------------------------------------------------------
59b05e08 26
b42b8ea3
ES
27sub supported_parameters {
28 return (
29 {
30 name => 'perltidyrc',
31 description => 'The Perl::Tidy configuration file to use, if any.',
32 default_string => undef,
33 },
34 );
35}
36
c680a9c9
ES
37sub default_severity { return $SEVERITY_LOWEST }
38sub default_themes { return qw(core pbp cosmetic) }
39sub applies_to { return 'PPI::Document' }
dff08b70 40
6036a254 41#-----------------------------------------------------------------------------
02558daa 42
005d2cb9
ES
43sub initialize_if_enabled {
44 my ($self, $config) = @_;
85da2bf2 45
613f4745
CD
46 # workaround for Test::Without::Module v0.11
47 local $EVAL_ERROR = undef;
48
005d2cb9
ES
49 # If Perl::Tidy is missing, bow out.
50 eval { require Perl::Tidy; };
51 return $FALSE if $EVAL_ERROR;
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)
e25c095f
CD
81 my $shebang_re = qr{[#]![^\015\012]+[\015\012]+}xms;
82 my $shell_re = qr{eval [ ] 'exec [ ] [^\015\012]* [ ] \$0 [ ] \${1[+]"\$@"}'
6a4d1045 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
JRT
96 # Trap Perl::Tidy errors, just in case it dies
97 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
JRT
103 );
104 };
105
106 if ($stderr || $EVAL_ERROR) {
59b05e08
JRT
107
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
JRT
130
131=head1 DESCRIPTION
132
133Conway does make specific recommendations for whitespace and
134curly-braces in your code, but the most important thing is to adopt a
135consistent layout, regardless of the specifics. And the easiest way
136to do that is to use L<Perl::Tidy>. This policy will complain if
137you're code hasn't been run through Perl::Tidy.
138
6c893fc9 139=head1 CONFIGURATION
02558daa 140
6c893fc9
ES
141This policy can be configured to tell Perl::Tidy to use a particular
142F<perltidyrc> file or no configuration at all. By default, Perl::Tidy is told
143to look in its default location for configuration. Perl::Critic can be told to
144tell Perl::Tidy to use a specific configuration file by putting an entry in a
145F<.perlcriticrc> file like this:
02558daa
CD
146
147 [CodeLayout::RequireTidyCode]
148 perltidyrc = /usr/share/perltidy.conf
149
150As a special case, setting C<perltidyrc> to the empty string tells
151Perl::Tidy not to load any configuration file at all and just use
152Perl::Tidy's own default style.
153
154 [CodeLayout::RequireTidyCode]
8e2aefdb 155 perltidyrc =
02558daa 156
59b05e08
JRT
157=head1 NOTES
158
df249cc5
JRT
159L<Perl::Tidy> is not included in the Perl::Critic distribution. The
160latest version of Perl::Tidy can be downloaded from CPAN. If
161Perl::Tidy is not installed, this policy is silently ignored.
59b05e08
JRT
162
163=head1 SEE ALSO
164
165L<Perl::Tidy>
166
59b05e08
JRT
167=head1 AUTHOR
168
169Jeffrey Ryan Thalhammer <thaljef@cpan.org>
170
171=head1 COPYRIGHT
172
20dfddeb 173Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
59b05e08
JRT
174
175This program is free software; you can redistribute it and/or modify
176it under the same terms as Perl itself. The full text of this license
177can be found in the LICENSE file included with this module.
178
179=cut
737d3b65
CD
180
181# Local Variables:
182# mode: cperl
183# cperl-indent-level: 4
184# fill-column: 78
185# indent-tabs-mode: nil
186# c-indentation-style: bsd
187# End:
96fed375 188# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :