Login
RT #77510: Left curlys as literals in regexps are deprecated.
[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 13
bf159007 14use English qw(-no_match_vars);
776c9cd4
ES
15use Readonly;
16
17use Perl::Tidy qw< >;
18
005d2cb9 19use Perl::Critic::Utils qw{ :booleans :characters :severities };
59b05e08
JRT
20use base 'Perl::Critic::Policy';
21
73c61a84 22our $VERSION = '1.117';
59b05e08 23
6036a254 24#-----------------------------------------------------------------------------
dff08b70 25
c680a9c9
ES
26Readonly::Scalar my $DESC => q{Code is not tidy};
27Readonly::Scalar my $EXPL => [ 33 ];
59b05e08 28
6036a254 29#-----------------------------------------------------------------------------
59b05e08 30
b42b8ea3
ES
31sub supported_parameters {
32 return (
33 {
34 name => 'perltidyrc',
35 description => 'The Perl::Tidy configuration file to use, if any.',
36 default_string => undef,
37 },
38 );
39}
40
c680a9c9
ES
41sub default_severity { return $SEVERITY_LOWEST }
42sub default_themes { return qw(core pbp cosmetic) }
43sub applies_to { return 'PPI::Document' }
dff08b70 44
6036a254 45#-----------------------------------------------------------------------------
02558daa 46
005d2cb9
ES
47sub initialize_if_enabled {
48 my ($self, $config) = @_;
85da2bf2 49
776c9cd4 50 # Set configuration if defined
fd5bd7b5
JRT
51 if (defined $self->{_perltidyrc} && $self->{_perltidyrc} eq $EMPTY) {
52 $self->{_perltidyrc} = \$EMPTY;
02558daa
CD
53 }
54
005d2cb9 55 return $TRUE;
02558daa
CD
56}
57
6036a254 58#-----------------------------------------------------------------------------
59b05e08
JRT
59
60sub violates {
61 my ( $self, $elem, $doc ) = @_;
bf159007 62
05bf01cd 63 # Perl::Tidy seems to produce slightly different output, depending
871ec032 64 # on the trailing whitespace in the input. As best I can tell,
fd9d73d0
JRT
65 # Perl::Tidy will truncate any extra trailing newlines, and if the
66 # input has no trailing newline, then it adds one. But when you
67 # re-run it through Perl::Tidy here, that final newline gets lost,
68 # which causes the policy to insist that the code is not tidy.
69 # This only occurs when Perl::Tidy is writing the output to a
70 # scalar, but does not occur when writing to a file. I may
71 # investigate further, but for now, this seems to do the trick.
871ec032 72
02558daa 73 my $source = $doc->serialize();
a0dcf06f 74 $source =~ s{ \s+ \Z}{\n}xms;
871ec032 75
44b74300 76 # Remove the shell fix code from the top of program, if applicable
c0631e45 77 ## no critic (ProhibitComplexRegexes)
03261933 78 my $shebang_re = qr< [#]! [^\015\012]+ [\015\012]+ >xms;
7a52e1ec 79 my $shell_re = qr<eval [ ] 'exec [ ] [^\015\012]* [ ] \$0 [ ] \$[{]1[+]"\$@"}'
67ab80cc 80 [ \t]*[\012\015]+ [ \t]* if [^\015\012]+ [\015\012]+ >xms;
44b74300
CD
81 $source =~ s/\A ($shebang_re) $shell_re /$1/xms;
82
59b05e08 83 my $dest = $EMPTY;
59b05e08
JRT
84 my $stderr = $EMPTY;
85
871ec032 86
6bf9b465
JRT
87 # Perl::Tidy gets confused if @ARGV has arguments from
88 # another program. Also, we need to override the
89 # stdout and stderr redirects that the user may have
90 # configured in their .perltidyrc file.
c0631e45 91 local @ARGV = qw(-nst -nse);
ee99f507 92
d5fc0818 93 # Trap Perl::Tidy errors, just in case it dies
49622277 94 my $eval_worked = eval {
0a6f07d0
AL
95 Perl::Tidy::perltidy(
96 source => \$source,
97 destination => \$dest,
98 stderr => \$stderr,
02558daa 99 defined $self->{_perltidyrc} ? (perltidyrc => $self->{_perltidyrc}) : (),
d5fc0818 100 );
c713b566 101 1;
d5fc0818
JRT
102 };
103
49622277 104 if ($stderr or not $eval_worked) {
59b05e08 105 # Looks like perltidy had problems
c680a9c9 106 return $self->violation( 'perltidy had errors!!', $EXPL, $elem );
59b05e08
JRT
107 }
108
47639f94 109 if ( $source ne $dest ) {
c680a9c9 110 return $self->violation( $DESC, $EXPL, $elem );
59b05e08
JRT
111 }
112
113 return; #ok!
114}
115
1161;
117
6036a254 118#-----------------------------------------------------------------------------
6bf9b465 119
59b05e08
JRT
120__END__
121
122=pod
123
3c8a7f56
ES
124=for stopwords perltidy
125
59b05e08
JRT
126=head1 NAME
127
11f53956
ES
128Perl::Critic::Policy::CodeLayout::RequireTidyCode - Must run code through L<perltidy|perltidy>.
129
59b05e08 130
af93c316
ES
131=head1 AFFILIATION
132
11f53956
ES
133This Policy is part of the core L<Perl::Critic|Perl::Critic>
134distribution.
af93c316
ES
135
136
59b05e08
JRT
137=head1 DESCRIPTION
138
139Conway does make specific recommendations for whitespace and
140curly-braces in your code, but the most important thing is to adopt a
141consistent layout, regardless of the specifics. And the easiest way
11f53956
ES
142to do that is to use L<Perl::Tidy|Perl::Tidy>. This policy will
143complain if you're code hasn't been run through Perl::Tidy.
144
59b05e08 145
6c893fc9 146=head1 CONFIGURATION
02558daa 147
6c893fc9 148This policy can be configured to tell Perl::Tidy to use a particular
11f53956
ES
149F<perltidyrc> file or no configuration at all. By default, Perl::Tidy
150is told to look in its default location for configuration.
151Perl::Critic can be told to tell Perl::Tidy to use a specific
152configuration file by putting an entry in a F<.perlcriticrc> file like
153this:
02558daa 154
11f53956
ES
155 [CodeLayout::RequireTidyCode]
156 perltidyrc = /usr/share/perltidy.conf
02558daa
CD
157
158As a special case, setting C<perltidyrc> to the empty string tells
159Perl::Tidy not to load any configuration file at all and just use
160Perl::Tidy's own default style.
161
11f53956
ES
162 [CodeLayout::RequireTidyCode]
163 perltidyrc =
164
02558daa 165
59b05e08
JRT
166=head1 SEE ALSO
167
11f53956
ES
168L<Perl::Tidy|Perl::Tidy>
169
59b05e08 170
59b05e08
JRT
171=head1 AUTHOR
172
03887e5e 173Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
59b05e08 174
11f53956 175
59b05e08
JRT
176=head1 COPYRIGHT
177
53b8903f 178Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
59b05e08
JRT
179
180This program is free software; you can redistribute it and/or modify
181it under the same terms as Perl itself. The full text of this license
182can be found in the LICENSE file included with this module.
183
184=cut
737d3b65
CD
185
186# Local Variables:
187# mode: cperl
188# cperl-indent-level: 4
189# fill-column: 78
190# indent-tabs-mode: nil
191# c-indentation-style: bsd
192# End:
96fed375 193# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :