Skip to content

Commit 33d068e

Browse files
mvgrimesautarch
authored andcommitted
Adds support for Perl::Tidy::Sweetened
1 parent 1eec20d commit 33d068e

File tree

10 files changed

+151
-1
lines changed

10 files changed

+151
-1
lines changed

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
- The JSON plugin now uses JSON::MaybeXS instead of JSON.pm. Patch by Greg
44
Oschwald. GitHub #37.
55

6+
- Added a new plugin, PerlTidySweet, which uses perltidy-sweet instead of
7+
perltidy.
8+
69

710
0.27 2015-07-04
811

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ You can call this API from your own program instead of executing `tidyall`.
127127
- George Hartzell <[email protected]>
128128
- Gregory Oschwald <[email protected]>
129129
- Joe Crotty <[email protected]>
130+
- Mark Grimes <[email protected]>
130131
- Olaf Alders <[email protected]>
131132
- Pedro Melo <[email protected]>
132133

bin/tidyall

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ distribution comes with plugins for:
238238
=item *
239239
240240
Perl: L<perlcritic|Code::TidyAll::Plugin::PerlCritic>,
241-
L<perltidy|Code::TidyAll::Plugin::PerlTidy>
241+
L<perltidy|Code::TidyAll::Plugin::PerlTidy>,
242+
L<perltidy-sweet|Code::TidyAll::Plugin::PerlTidy::Sweetened>
242243
243244
=item *
244245

cpanfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ on 'develop' => sub {
6464
requires "Perl::Critic" => "1.123";
6565
requires "Perl::Critic::Policy::Moose::RequireMakeImmutable" => "0";
6666
requires "Perl::Tidy" => "20140711";
67+
requires "Perl::Tidy::Sweetened" => "1.00";
6768
requires "Pod::Checker" => "0";
6869
requires "Pod::Spell" => "0";
6970
requires "Pod::Tidy" => "0";

dist.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ prereqs_skip = JSON
1111
prereqs_skip = Mason::Tidy
1212
prereqs_skip = Mason::Tidy::App
1313
prereqs_skip = Perl::Tidy
14+
prereqs_skip = Perl::Tidy::Sweetened
1415
prereqs_skip = Pod::Checker
1516
prereqs_skip = Pod::Spell
1617
prereqs_skip = Pod::Tidy
@@ -47,6 +48,7 @@ Mason::Tidy::App = 0
4748
Perl::Critic = 0
4849
Perl::Critic::Policy::Moose::RequireMakeImmutable = 0
4950
Perl::Tidy = 0
51+
Perl::Tidy::Sweetened = 1.00
5052
Pod::Checker = 0
5153
Pod::Spell = 0
5254
Pod::Tidy = 0
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package Code::TidyAll::Plugin::PerlTidySweet;
2+
3+
use Capture::Tiny qw(capture_merged);
4+
use Perl::Tidy::Sweetened 1.00;
5+
use Moo;
6+
extends 'Code::TidyAll::Plugin';
7+
8+
our $VERSION = '0.28';
9+
10+
sub transform_source {
11+
my ( $self, $source ) = @_;
12+
13+
# perltidy reports errors in two different ways.
14+
# Argument/profile errors are output and an error_flag is returned.
15+
# Syntax errors are sent to errorfile or stderr, depending on the
16+
# the setting of -se/-nse (aka --standard-error-output). These flags
17+
# might be hidden in other bundles, e.g. -pbp. Be defensive and
18+
# check both.
19+
my ( $output, $error_flag, $errorfile, $stderr, $destination );
20+
$output = capture_merged {
21+
$error_flag = Perl::Tidy::Sweetened::perltidy(
22+
argv => $self->argv,
23+
source => \$source,
24+
destination => \$destination,
25+
stderr => \$stderr,
26+
errorfile => \$errorfile
27+
);
28+
};
29+
die $stderr if $stderr;
30+
die $errorfile if $errorfile;
31+
die $output if $error_flag;
32+
print STDERR $output if defined($output);
33+
return $destination;
34+
}
35+
36+
1;
37+
38+
# ABSTRACT: Use perltidy-sweet with tidyall
39+
40+
__END__
41+
42+
=pod
43+
44+
=head1 SYNOPSIS
45+
46+
# In configuration:
47+
48+
; Configure in-line
49+
;
50+
[PerlTidySweet]
51+
select = lib/**/*.pm
52+
argv = --noll
53+
54+
; or refer to a .perltidyrc in the same directory
55+
;
56+
[PerlTidySweet]
57+
select = lib/**/*.pm
58+
argv = --profile=$ROOT/.perltidyrc
59+
60+
=head1 DESCRIPTION
61+
62+
Runs L<perltidy-sweet>, a Perl tidier based on Perl::Tidy that also supports
63+
new syntactic sugar as provided by L<Method::Signature::Simple>,
64+
L<MooseX::Method::Signatures> and the p5-mop.
65+
66+
=head1 INSTALLATION
67+
68+
Install perltidy-sweet from CPAN.
69+
70+
cpanm Perl::Tidy::Sweet
71+
72+
=head1 CONFIGURATION
73+
74+
=over
75+
76+
=item argv
77+
78+
Arguments to pass to perltidy-sweet
79+
80+
=back
81+
82+
=cut
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Test::Code::TidyAll::Plugin::PerlTidySweet;
2+
3+
use Test::Class::Most parent => 'Test::Code::TidyAll::Plugin';
4+
5+
sub test_main : Tests {
6+
my $self = shift;
7+
8+
my $source = 'if ( $foo) {\nmy $bar = $baz;\n}\n';
9+
$self->tidyall(
10+
conf => { argv => '-npro' },
11+
source => $source,
12+
expect_tidy => 'if ($foo) {\n my $bar = $baz;\n}\n'
13+
);
14+
$self->tidyall(
15+
conf => { argv => '-npro -bl' },
16+
source => $source,
17+
expect_tidy => 'if ($foo)\n{\n my $bar = $baz;\n}\n'
18+
);
19+
$self->tidyall(
20+
conf => { argv => '-npro' },
21+
source => 'if ($foo) {\n my $bar = $baz;\n}\n',
22+
expect_ok => 1
23+
);
24+
$self->tidyall(
25+
source => 'method foo ($x,$y){\nmy $x=$self->x;}\n',
26+
expect_tidy => 'method foo ($x,$y) {\n my $x = $self->x;\n}\n',
27+
);
28+
$self->tidyall(
29+
source => 'if ($foo) {\n my $bar = $baz;\n',
30+
expect_error => qr/Final nesting depth/
31+
);
32+
$self->tidyall(
33+
conf => { argv => '--badoption' },
34+
source => $source,
35+
expect_error => qr/Unknown option: badoption/
36+
);
37+
}
38+
39+
1;

tidyall.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ ignore = t/00-report-prereqs.t
66
ignore = t/author-*
77
ignore = t/release-*
88

9+
[PerlTidySweet]
10+
argv = --pro=$ROOT/perltidyrc
11+
except_modes = dzil
12+
select = {bin,lib,t}/**/{tidyall,*.{pl,pm,t}}
13+
ignore = t/00-report-prereqs.t
14+
ignore = t/author-*
15+
ignore = t/release-*
16+
917
[PodTidy]
1018
select = {bin,lib}/**/{tidyall,*.{pl,pm,pod}}
1119
ignore = t/00-report-prereqs.t

xt/author/Plugin-PerlTidySweet.t

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/perl
2+
3+
use Test::More;
4+
5+
plan skip_all => 'This plugin requires Perl 5.10+'
6+
unless $] >= 5.010;
7+
8+
use lib 't/lib';
9+
use Test::Code::TidyAll::Plugin::PerlTidySweet;
10+
Test::Code::TidyAll::Plugin::PerlTidySweet->runtests;

xt/author/self-tidyall.t

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use warnings;
44
use Test::Code::TidyAll;
55
use Test::More;
66

7+
plan skip_all => 'This plugin requires Perl 5.10+'
8+
unless $] >= 5.010;
9+
710
tidyall_ok( verbose => 1 );
811

912
done_testing();

0 commit comments

Comments
 (0)