File Coverage

blib/lib/Code/TidyAll/Plugin/PerlTidy.pm
Criterion Covered Total %
statement 27 27 100.0
branch 7 10 70.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 42 45 93.3


line stmt bran cond sub pod time code
1             package Code::TidyAll::Plugin::PerlTidy;
2              
3 1     1   571 use strict;
  1         2  
  1         35  
4 1     1   6 use warnings;
  1         3  
  1         34  
5              
6 1     1   5 use Capture::Tiny qw(capture_merged);
  1         3  
  1         73  
7 1     1   7 use Perl::Tidy;
  1         3  
  1         123  
8              
9 1     1   8 use Moo;
  1         2  
  1         8  
10              
11             extends 'Code::TidyAll::Plugin';
12              
13             our $VERSION = '0.83';
14              
15             sub transform_source {
16 6     6 1 19 my ( $self, $source ) = @_;
17              
18             # This bit of insanity is needed because if some other code calls
19             # Getopt::Long::Configure() to change some options, then everything can go
20             # to hell. Internally perltidy() tries to use Getopt::Long without
21             # resetting the configuration defaults, leading to very confusing
22             # errors. See https://rt.cpan.org/Ticket/Display.html?id=118558
23 6         32 Getopt::Long::ConfigDefaults();
24              
25             # perltidy reports errors in two different ways.
26             # Argument/profile errors are output and an error_flag is returned.
27             # Syntax errors are sent to errorfile or stderr, depending on the
28             # the setting of -se/-nse (aka --standard-error-output). These flags
29             # might be hidden in other bundles, e.g. -pbp. Be defensive and
30             # check both.
31 6         114 my ( $output, $error_flag, $errorfile, $stderr, $destination );
32              
33             # Add --encode-output-strings (-eos) for PT releases in 2022 and later to
34             # tell perltidy that we want encoded character strings returned. See
35             # https://github.com/houseabsolute/perl-code-tidyall/issues/84
36             # https://github.com/perltidy/perltidy/issues/83
37 6         20 my $argv = $self->argv;
38 6 50       38 $argv .= ' --encode-output-strings' if $Perl::Tidy::VERSION > 20220101;
39              
40             $output = capture_merged {
41 6     6   6681 $error_flag = Perl::Tidy::perltidy(
42             argv => $self->argv,
43             source => \$source,
44             destination => \$destination,
45             stderr => \$stderr,
46             errorfile => \$errorfile
47             );
48 6         165 };
49 6 100       530948 die $stderr if $stderr;
50 5 100       35 die $errorfile if $errorfile;
51 4 50       15 die $output if $error_flag;
52 4 50       18 print STDERR $output if defined($output);
53 4         28 return $destination;
54             }
55              
56             1;
57              
58             # ABSTRACT: Use perltidy with tidyall
59              
60             __END__
61              
62             =pod
63              
64             =encoding UTF-8
65              
66             =head1 NAME
67              
68             Code::TidyAll::Plugin::PerlTidy - Use perltidy with tidyall
69              
70             =head1 VERSION
71              
72             version 0.83
73              
74             =head1 SYNOPSIS
75              
76             # In configuration:
77              
78             ; Configure in-line
79             ;
80             [PerlTidy]
81             select = lib/**/*.pm
82             argv = --noll
83              
84             ; or refer to a .perltidyrc in the same directory
85             ;
86             [PerlTidy]
87             select = lib/**/*.pm
88             argv = --profile=$ROOT/.perltidyrc
89              
90             =head1 DESCRIPTION
91              
92             Runs L<perltidy>, a Perl tidier.
93              
94             =head1 INSTALLATION
95              
96             Install perltidy from CPAN.
97              
98             cpanm perltidy
99              
100             =head1 CONFIGURATION
101              
102             This plugin accepts the following configuration options:
103              
104             =head2 argv
105              
106             Arguments to pass to C<perltidy>.
107              
108             If you are using C<Perl::Tidy> version 20220101 or newer, than the
109             C<--encode-output-strings> flag will be appended to whatever you supply. In
110             this case, you should ensure that you are I<not> passing a
111             C<--character-encoding> (C<-enc>) or C<-utf8> flag to perltidy as well.
112              
113             =head1 SUPPORT
114              
115             Bugs may be submitted at L<https://github.com/houseabsolute/perl-code-tidyall/issues>.
116              
117             =head1 SOURCE
118              
119             The source code repository for Code-TidyAll can be found at L<https://github.com/houseabsolute/perl-code-tidyall>.
120              
121             =head1 AUTHORS
122              
123             =over 4
124              
125             =item *
126              
127             Jonathan Swartz <swartz@pobox.com>
128              
129             =item *
130              
131             Dave Rolsky <autarch@urth.org>
132              
133             =back
134              
135             =head1 COPYRIGHT AND LICENSE
136              
137             This software is copyright (c) 2011 - 2022 by Jonathan Swartz.
138              
139             This is free software; you can redistribute it and/or modify it under
140             the same terms as the Perl 5 programming language system itself.
141              
142             The full text of the license can be found in the
143             F<LICENSE> file included with this distribution.
144              
145             =cut