Login
Self-compliance with ProhibitUnrestrictedNoCritic
[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
173667ce 19our $VERSION = '1.093_01';
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();
a0dcf06f 77 $source =~ s{ \s+ \Z}{\n}xms;
871ec032 78
44b74300 79 # Remove the shell fix code from the top of program, if applicable
c0631e45 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.
c0631e45 94 local @ARGV = qw(-nst -nse);
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
3c8a7f56
ES
127=for stopwords perltidy
128
59b05e08
JRT
129=head1 NAME
130
11f53956
ES
131Perl::Critic::Policy::CodeLayout::RequireTidyCode - Must run code through L<perltidy|perltidy>.
132
59b05e08 133
af93c316
ES
134=head1 AFFILIATION
135
11f53956
ES
136This Policy is part of the core L<Perl::Critic|Perl::Critic>
137distribution.
af93c316
ES
138
139
59b05e08
JRT
140=head1 DESCRIPTION
141
142Conway does make specific recommendations for whitespace and
143curly-braces in your code, but the most important thing is to adopt a
144consistent layout, regardless of the specifics. And the easiest way
11f53956
ES
145to do that is to use L<Perl::Tidy|Perl::Tidy>. This policy will
146complain if you're code hasn't been run through Perl::Tidy.
147
59b05e08 148
6c893fc9 149=head1 CONFIGURATION
02558daa 150
6c893fc9 151This policy can be configured to tell Perl::Tidy to use a particular
11f53956
ES
152F<perltidyrc> file or no configuration at all. By default, Perl::Tidy
153is told to look in its default location for configuration.
154Perl::Critic can be told to tell Perl::Tidy to use a specific
155configuration file by putting an entry in a F<.perlcriticrc> file like
156this:
02558daa 157
11f53956
ES
158 [CodeLayout::RequireTidyCode]
159 perltidyrc = /usr/share/perltidy.conf
02558daa
CD
160
161As a special case, setting C<perltidyrc> to the empty string tells
162Perl::Tidy not to load any configuration file at all and just use
163Perl::Tidy's own default style.
164
11f53956
ES
165 [CodeLayout::RequireTidyCode]
166 perltidyrc =
167
02558daa 168
59b05e08
JRT
169=head1 NOTES
170
11f53956
ES
171L<Perl::Tidy|Perl::Tidy> is not included in the Perl::Critic
172distribution. The latest version of Perl::Tidy can be downloaded from
173CPAN. If Perl::Tidy is not installed, this policy is silently
174ignored.
175
59b05e08
JRT
176
177=head1 SEE ALSO
178
11f53956
ES
179L<Perl::Tidy|Perl::Tidy>
180
59b05e08 181
59b05e08
JRT
182=head1 AUTHOR
183
184Jeffrey Ryan Thalhammer <thaljef@cpan.org>
185
11f53956 186
59b05e08
JRT
187=head1 COPYRIGHT
188
20dfddeb 189Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
59b05e08
JRT
190
191This program is free software; you can redistribute it and/or modify
192it under the same terms as Perl itself. The full text of this license
193can be found in the LICENSE file included with this module.
194
195=cut
737d3b65
CD
196
197# Local Variables:
198# mode: cperl
199# cperl-indent-level: 4
200# fill-column: 78
201# indent-tabs-mode: nil
202# c-indentation-style: bsd
203# End:
96fed375 204# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :