Login
Fixed bug caused by odd behavior in Perl::Tidy (I think).
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / CodeLayout / RequireTidyCode.pm
CommitLineData
dff08b70
JRT
1#######################################################################
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6########################################################################
7
59b05e08
JRT
8package Perl::Critic::Policy::CodeLayout::RequireTidyCode;
9
10use strict;
11use warnings;
bf159007 12use English qw(-no_match_vars);
59b05e08
JRT
13use Perl::Critic::Utils;
14use Perl::Critic::Violation;
15use base 'Perl::Critic::Policy';
16
c3166b47 17our $VERSION = '0.15';
59b05e08
JRT
18$VERSION = eval $VERSION; ## no critic
19
dff08b70
JRT
20#----------------------------------------------------------------------------
21
59b05e08 22my $desc = q{Code is not tidy};
5dc3db7a 23my $expl = [ 33 ];
59b05e08
JRT
24
25#----------------------------------------------------------------------------
26
6bf9b465 27sub default_severity { return $SEVERITY_LOWEST }
dff08b70
JRT
28sub applies_to { return 'PPI::Document' }
29
30#----------------------------------------------------------------------------
59b05e08
JRT
31
32sub violates {
33 my ( $self, $elem, $doc ) = @_;
bf159007
JRT
34
35 # If Perl::Tidy is missing, silently pass this test
36 eval { require Perl::Tidy; };
37 return if $EVAL_ERROR;
59b05e08 38
871ec032
JRT
39 # Perl::Tidy seems to produce slightly different output, depeding
40 # on the trailing whitespace in the input. As best I can tell,
41 # Perl::Tidy will truncate more than 3 trailing newlines down to
42 # just 2, and if the input has no trailing newline, then it adds
43 # one. But when you re-run it through Perl::Tidy here, that
44 # trailing newline gets lost, which causes the policy to insist
45 # that the code is not tidy. This only occurs when Perl::Tidy is
46 # writing the output to a scalar, but does not occur when writing
47 # to a file. I may investigate further, but for now, this seems
48 # to do the trick.
49
50 my $source = "$doc";
51 my $last_2_chars = substr $source, -2, 2;
52 if( $last_2_chars eq qq{\n\n} ) {
53 chomp $source;
54 };
55
56
59b05e08 57 my $dest = $EMPTY;
59b05e08
JRT
58 my $stderr = $EMPTY;
59
871ec032 60
6bf9b465
JRT
61 # Perl::Tidy gets confused if @ARGV has arguments from
62 # another program. Also, we need to override the
63 # stdout and stderr redirects that the user may have
64 # configured in their .perltidyrc file.
65 local @ARGV = qw(-nst -nse); ## no critic
ee99f507 66
d5fc0818
JRT
67 # Trap Perl::Tidy errors, just in case it dies
68 eval {
69 Perl::Tidy::perltidy(
70 source => \$source,
71 destination => \$dest,
72 stderr => \$stderr,
73 );
74 };
75
76 if ($stderr || $EVAL_ERROR) {
59b05e08
JRT
77
78 # Looks like perltidy had problems
79 $desc = q{perltidy had errors!!};
80 }
81
871ec032 82
47639f94 83 if ( $source ne $dest ) {
6bf9b465
JRT
84 my $sev = $self->get_severity();
85 return Perl::Critic::Violation->new( $desc, $expl, $doc, $sev );
59b05e08
JRT
86 }
87
88 return; #ok!
89}
90
911;
92
6bf9b465
JRT
93#----------------------------------------------------------------------------
94
59b05e08
JRT
95__END__
96
97=pod
98
99=head1 NAME
100
101Perl::Critic::Policy::CodeLayout::RequireTidyCode
102
103=head1 DESCRIPTION
104
105Conway does make specific recommendations for whitespace and
106curly-braces in your code, but the most important thing is to adopt a
107consistent layout, regardless of the specifics. And the easiest way
108to do that is to use L<Perl::Tidy>. This policy will complain if
109you're code hasn't been run through Perl::Tidy.
110
111=head1 NOTES
112
113Since L<Perl::Tidy> is not widely deployed, this is the only policy in
114the L<Perl::Critic> distribution that is not enabled by default. To
115enable it, put this line in your F<.perlcriticrc> file:
116
117 [CodeLayout::RequireTidyCode]
118
119=head1 SEE ALSO
120
121L<Perl::Tidy>
122
123
124=head1 AUTHOR
125
126Jeffrey Ryan Thalhammer <thaljef@cpan.org>
127
128=head1 COPYRIGHT
129
c3c88e54 130Copyright (c) 2005-2006 Jeffrey Ryan Thalhammer. All rights reserved.
59b05e08
JRT
131
132This program is free software; you can redistribute it and/or modify
133it under the same terms as Perl itself. The full text of this license
134can be found in the LICENSE file included with this module.
135
136=cut