Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for Perl::Tidy::Sweetened
- Loading branch information
Showing
8 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/perl | ||
use lib 't/lib'; | ||
use Test::Code::TidyAll::Plugin::PerlTidySweet; | ||
Test::Code::TidyAll::Plugin::PerlTidySweet->runtests; |