Login
Reorder methods in Documentation::PodSpelling to make violates()
[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);
bbf4108c 15use Perl::Critic::Utils qw{ :characters :severities };
59b05e08
JRT
16use base 'Perl::Critic::Policy';
17
e858ba92 18our $VERSION = 1.06;
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
CD
33
34sub new {
6e7d6c9f
CD
35 my ($class, @args) = @_;
36 my $self = $class->SUPER::new(@args);
85da2bf2 37
6e7d6c9f 38 my %config = @args;
02558daa
CD
39
40 #Set configuration if defined
85da2bf2 41 $self->{_perltidyrc} = $config{perltidyrc};
fd5bd7b5
JRT
42 if (defined $self->{_perltidyrc} && $self->{_perltidyrc} eq $EMPTY) {
43 $self->{_perltidyrc} = \$EMPTY;
02558daa
CD
44 }
45
46 return $self;
47}
48
6036a254 49#-----------------------------------------------------------------------------
59b05e08
JRT
50
51sub violates {
52 my ( $self, $elem, $doc ) = @_;
bf159007
JRT
53
54 # If Perl::Tidy is missing, silently pass this test
55 eval { require Perl::Tidy; };
56 return if $EVAL_ERROR;
59b05e08 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
CD
71 # Remove the shell fix code from the top of program, if applicable
72 my $shebang_re = qr/\#![^\015\012]+[\015\012]+/xms;
73 my $shell_re = qr/eval [ ] 'exec [ ] [^\015\012]* [ ] \$0 [ ] \${1\+"\$@"}'
74 [ \t]*[\012\015]+ [ \t]*if[^\015\012]+[\015\012]+/xms;
75 $source =~ s/\A ($shebang_re) $shell_re /$1/xms;
76
59b05e08 77 my $dest = $EMPTY;
59b05e08
JRT
78 my $stderr = $EMPTY;
79
871ec032 80
6bf9b465
JRT
81 # Perl::Tidy gets confused if @ARGV has arguments from
82 # another program. Also, we need to override the
83 # stdout and stderr redirects that the user may have
84 # configured in their .perltidyrc file.
85 local @ARGV = qw(-nst -nse); ## no critic
ee99f507 86
d5fc0818
JRT
87 # Trap Perl::Tidy errors, just in case it dies
88 eval {
0a6f07d0
AL
89 Perl::Tidy::perltidy(
90 source => \$source,
91 destination => \$dest,
92 stderr => \$stderr,
02558daa 93 defined $self->{_perltidyrc} ? (perltidyrc => $self->{_perltidyrc}) : (),
d5fc0818
JRT
94 );
95 };
96
97 if ($stderr || $EVAL_ERROR) {
59b05e08
JRT
98
99 # Looks like perltidy had problems
c680a9c9 100 return $self->violation( 'perltidy had errors!!', $EXPL, $elem );
59b05e08
JRT
101 }
102
47639f94 103 if ( $source ne $dest ) {
c680a9c9 104 return $self->violation( $DESC, $EXPL, $elem );
59b05e08
JRT
105 }
106
107 return; #ok!
108}
109
1101;
111
6036a254 112#-----------------------------------------------------------------------------
6bf9b465 113
59b05e08
JRT
114__END__
115
116=pod
117
118=head1 NAME
119
120Perl::Critic::Policy::CodeLayout::RequireTidyCode
121
122=head1 DESCRIPTION
123
124Conway does make specific recommendations for whitespace and
125curly-braces in your code, but the most important thing is to adopt a
126consistent layout, regardless of the specifics. And the easiest way
127to do that is to use L<Perl::Tidy>. This policy will complain if
128you're code hasn't been run through Perl::Tidy.
129
6c893fc9 130=head1 CONFIGURATION
02558daa 131
6c893fc9
ES
132This policy can be configured to tell Perl::Tidy to use a particular
133F<perltidyrc> file or no configuration at all. By default, Perl::Tidy is told
134to look in its default location for configuration. Perl::Critic can be told to
135tell Perl::Tidy to use a specific configuration file by putting an entry in a
136F<.perlcriticrc> file like this:
02558daa
CD
137
138 [CodeLayout::RequireTidyCode]
139 perltidyrc = /usr/share/perltidy.conf
140
141As a special case, setting C<perltidyrc> to the empty string tells
142Perl::Tidy not to load any configuration file at all and just use
143Perl::Tidy's own default style.
144
145 [CodeLayout::RequireTidyCode]
8e2aefdb 146 perltidyrc =
02558daa 147
59b05e08
JRT
148=head1 NOTES
149
df249cc5
JRT
150L<Perl::Tidy> is not included in the Perl::Critic distribution. The
151latest version of Perl::Tidy can be downloaded from CPAN. If
152Perl::Tidy is not installed, this policy is silently ignored.
59b05e08
JRT
153
154=head1 SEE ALSO
155
156L<Perl::Tidy>
157
59b05e08
JRT
158=head1 AUTHOR
159
160Jeffrey Ryan Thalhammer <thaljef@cpan.org>
161
162=head1 COPYRIGHT
163
0d5b2dca 164Copyright (c) 2005-2007 Jeffrey Ryan Thalhammer. All rights reserved.
59b05e08
JRT
165
166This program is free software; you can redistribute it and/or modify
167it under the same terms as Perl itself. The full text of this license
168can be found in the LICENSE file included with this module.
169
170=cut
737d3b65
CD
171
172# Local Variables:
173# mode: cperl
174# cperl-indent-level: 4
175# fill-column: 78
176# indent-tabs-mode: nil
177# c-indentation-style: bsd
178# End:
345c7562 179# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab :