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