Skip to content

Commit

Permalink
Adds support for Perl::Tidy::Sweetened
Browse files Browse the repository at this point in the history
  • Loading branch information
mvgrimes committed Dec 23, 2014
1 parent 9955a18 commit e2084f6
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -127,6 +127,7 @@ You can call this API from your own program instead of executing `tidyall`.
- George Hartzell <georgewh@gene.com>
- Gregory Oschwald <goschwald@maxmind.com>
- Joe Crotty <joe.crotty@returnpath.net>
- Mark Grimes <mgrimes@cpan.org>
- Olaf Alders <olaf@wundersolutions.com>
- Pedro Melo <melo@simplicidade.org>

Expand Down
3 changes: 2 additions & 1 deletion bin/tidyall
Expand Up @@ -236,7 +236,8 @@ distribution comes with plugins for:
=item *
Perl: L<perlcritic|Code::TidyAll::Plugin::PerlCritic>,
L<perltidy|Code::TidyAll::Plugin::PerlTidy>
L<perltidy|Code::TidyAll::Plugin::PerlTidy>,
L<perltidy-sweet|Code::TidyAll::Plugin::PerlTidy::Sweetened>
=item *
Expand Down
1 change: 1 addition & 0 deletions cpanfile
Expand Up @@ -59,6 +59,7 @@ on 'develop' => sub {
requires "Mason::Tidy" => "0";
requires "Mason::Tidy::App" => "0";
requires "Perl::Tidy" => "0";
requires "Perl::Tidy::Sweetened" => "0";
requires "Pod::Checker" => "0";
requires "Pod::Spell" => "0";
requires "Pod::Tidy" => "0";
Expand Down
2 changes: 2 additions & 0 deletions dist.ini
Expand Up @@ -13,6 +13,7 @@ prereqs_skip = JSON
prereqs_skip = Mason::Tidy
prereqs_skip = Mason::Tidy::App
prereqs_skip = Perl::Tidy
prereqs_skip = Perl::Tidy::Sweetened
prereqs_skip = Pod::Checker
prereqs_skip = Pod::Spell
prereqs_skip = Pod::Tidy
Expand All @@ -36,6 +37,7 @@ JSON = 0
Mason::Tidy = 0
Mason::Tidy::App = 0
Perl::Tidy = 0
Perl::Tidy::Sweetened = 0
Pod::Checker = 0
Pod::Spell = 0
Pod::Tidy = 0
Expand Down
81 changes: 81 additions & 0 deletions lib/Code/TidyAll/Plugin/PerlTidySweet.pm
@@ -0,0 +1,81 @@
package Code::TidyAll::Plugin::PerlTidySweet;

use Capture::Tiny qw(capture_merged);
use Perl::Tidy::Sweetened;
use Moo;
extends 'Code::TidyAll::Plugin';

sub transform_source {
my ( $self, $source ) = @_;

# perltidy reports errors in two different ways.
# Argument/profile errors are output and an error_flag is returned.
# Syntax errors are sent to errorfile or stderr, depending on the
# the setting of -se/-nse (aka --standard-error-output). These flags
# might be hidden in other bundles, e.g. -pbp. Be defensive and
# check both.
my ( $output, $error_flag, $errorfile, $stderr, $destination );
$output = capture_merged {
$error_flag = Perl::Tidy::Sweetened::perltidy(
argv => $self->argv,
source => \$source,
destination => \$destination,
stderr => \$stderr,
errorfile => \$errorfile
);
};
die $stderr if $stderr;
die $errorfile if $errorfile;
die $output if $error_flag;
print STDERR $output if defined($output);
return $destination;
}

1;

__END__
=pod
=head1 NAME
Code::TidyAll::Plugin::PerlTidySweet - use perltidy-sweet with tidyall
=head1 SYNOPSIS
# In configuration:
; Configure in-line
;
[PerlTidySweet]
select = lib/**/*.pm
argv = --noll
; or refer to a .perltidyrc in the same directory
;
[PerlTidySweet]
select = lib/**/*.pm
argv = --profile=$ROOT/.perltidyrc
=head1 DESCRIPTION
Runs L<perltidy-sweet>, a Perl tidier based on Perl::Tidy that also
supports new syntactic sugar as provided by L<Method::Signature::Simple>,
L<MooseX::Method::Signatures> and the p5-mop.
=head1 INSTALLATION
Install perltidy-sweet from CPAN.
cpanm Perl::Tidy::Sweet
=head1 CONFIGURATION
=over
=item argv
Arguments to pass to perltidy-sweet
=back
38 changes: 38 additions & 0 deletions t/lib/Test/Code/TidyAll/Plugin/PerlTidySweet.pm
@@ -0,0 +1,38 @@
package Test::Code::TidyAll::Plugin::PerlTidySweet;

use Test::Class::Most parent => 'Test::Code::TidyAll::Plugin';

sub test_main : Tests {
my $self = shift;

my $source = 'if ( $foo) {\nmy $bar = $baz;\n}\n';
$self->tidyall(
source => $source,
expect_tidy => 'if ($foo) {\n my $bar = $baz;\n}\n'
);
$self->tidyall(
conf => { argv => '-bl' },
source => $source,
expect_tidy => 'if ($foo)\n{\n my $bar = $baz;\n}\n'
);
$self->tidyall(
source => 'if ($foo) {\n my $bar = $baz;\n}\n',
expect_ok => 1
);
$self->tidyall(
source => 'method foo ($x,$y){\nmy $x=$self->x;}\n',
expect_tidy => 'method foo ($x,$y) {\n my $x = $self->x;\n}\n',
);
$self->tidyall(
source => 'if ($foo) {\n my $bar = $baz;\n',
expect_error => qr/Final nesting depth/
);
$self->tidyall(
conf => { argv => '--badoption' },
source => $source,
expect_error => qr/Unknown option: badoption/
);
}

1;
1;
6 changes: 5 additions & 1 deletion tidyall.ini
Expand Up @@ -2,6 +2,10 @@
argv = -noll -l=100
select = {bin,lib,t}/**/{tidyall,*.{pl,pm,t}}

[PerlTidySweet]
argv = -noll -l=100
select = {bin,lib,t}/**/{tidyall,*.{pl,pm,t}}

[PodTidy]
select = {bin,lib}/**/{tidyall,*.{pl,pm,pod}}

Expand All @@ -22,4 +26,4 @@ select = .gitignore .ispell* **/.gitignore
select = {bin,lib,t}/**/*.{pl,pm,t}

[Perl::AlignMooseAttributes]
select = {bin,lib,t}/**/*.{pl,pm,t}
select = {bin,lib,t}/**/*.{pl,pm,t}
4 changes: 4 additions & 0 deletions xt/author/Plugin-PerlTidySweet.t
@@ -0,0 +1,4 @@
#!/usr/bin/perl
use lib 't/lib';
use Test::Code::TidyAll::Plugin::PerlTidySweet;
Test::Code::TidyAll::Plugin::PerlTidySweet->runtests;

0 comments on commit e2084f6

Please sign in to comment.