File Coverage

blib/lib/Perl/Tidy/Sweetened.pm
Criterion Covered Total %
statement 25 25 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 0 1 0.0
total 35 36 97.2


line stmt bran cond sub pod time code
1             package Perl::Tidy::Sweetened;
2              
3             # ABSTRACT: Tweaks to Perl::Tidy to support some syntactic sugar
4              
5 13     13   899144 use 5.010;
  13         151  
6 13     13   121 use strict;
  13         61  
  13         394  
7 13     13   67 use warnings;
  13         30  
  13         442  
8 13     13   16514 use Perl::Tidy qw();
  13         4933324  
  13         740  
9              
10             our $VERSION = '1.19';
11              
12 13     13   6253 use Perl::Tidy::Sweetened::Pluggable;
  13         51  
  13         752  
13 13     13   6038 use Perl::Tidy::Sweetened::Keyword::Block;
  13         38  
  13         513  
14 13     13   5880 use Perl::Tidy::Sweetened::Variable::Twigils;
  13         53  
  13         5040  
15              
16             our $plugins = Perl::Tidy::Sweetened::Pluggable->new();
17              
18             # Create a subroutine filter for:
19             # sub foo ($i, $j) {}
20             $plugins->add_filter(
21             Perl::Tidy::Sweetened::Keyword::Block->new(
22             keyword => 'sub',
23             marker => 'SUB',
24             replacement => 'sub',
25             clauses => ['PAREN'],
26             ) );
27              
28             # Create a subroutine filter for:
29             # func foo (Int $i) returns (Bool) {}
30             # where both the parameter list and the returns type are optional
31             $plugins->add_filter(
32             Perl::Tidy::Sweetened::Keyword::Block->new(
33             keyword => 'func',
34             marker => 'FUNC',
35             replacement => 'sub',
36             clauses => [ 'PAREN?', '(returns \s* PAREN)?' ],
37             ) );
38              
39             # Create a subroutine filter for:
40             # fun foo (Int $i) {}
41             # where the parameter list is optional
42             $plugins->add_filter(
43             Perl::Tidy::Sweetened::Keyword::Block->new(
44             keyword => 'fun',
45             marker => 'FUN',
46             replacement => 'sub',
47             clauses => ['PAREN?'],
48             ) );
49              
50             # Create a subroutine filter for:
51             # method foo (Int $i) returns (Bool) {}
52             # where both the parameter list and the returns type are optional
53             $plugins->add_filter(
54             Perl::Tidy::Sweetened::Keyword::Block->new(
55             keyword => 'method',
56             marker => 'METHOD',
57             replacement => 'sub',
58             clauses =>
59             [ 'PAREN?', '(returns \s* PAREN)?', '(\b(?:is|but|does) \s+ \w+)?' ],
60             ) );
61              
62             # Create a subroutine filter for:
63             # around foo (Int $i) returns (Bool) {}
64             # before foo (Int $i) returns (Bool) {}
65             # after foo (Int $i) returns (Bool) {}
66             # where both the parameter list and the returns type are optional
67             $plugins->add_filter(
68             Perl::Tidy::Sweetened::Keyword::Block->new(
69             keyword => $_,
70             marker => uc($_),
71             replacement => 'sub',
72             clauses => ['PAREN?'],
73             ) ) for qw(around before after);
74              
75             # Create a subroutine filter for:
76             # classmethod foo (Int $i) {}
77             # where the parameter list is optional
78             $plugins->add_filter(
79             Perl::Tidy::Sweetened::Keyword::Block->new(
80             keyword => 'classmethod',
81             marker => 'CLASSMETHOD',
82             replacement => 'sub',
83             clauses => ['PAREN?'],
84             ) );
85              
86             # Create a subroutine filter for:
87             # objectmethod foo (Int $i) {}
88             # where the parameter list is optional
89             $plugins->add_filter(
90             Perl::Tidy::Sweetened::Keyword::Block->new(
91             keyword => 'objectmethod',
92             marker => 'OBJECTMETHOD',
93             replacement => 'sub',
94             clauses => ['PAREN?'],
95             ) );
96              
97             # Create a subroutine filter for:
98             # class Foo extends Bar {
99             # class Foo with Bar, Baz {
100             # where both the extends and with are optional
101             $plugins->add_filter(
102             Perl::Tidy::Sweetened::Keyword::Block->new(
103             keyword => 'class',
104             marker => 'CLASS',
105             replacement => 'package',
106             clauses =>
107             [ '(with(\s+\w+)*)?', '(extends \s+ [\w|:]+)?', '(is(\s+\w+)*)?', ],
108             ) );
109              
110             # Create a twigil filter for:
111             # $!variable_name
112             $plugins->add_filter(
113             Perl::Tidy::Sweetened::Variable::Twigils->new(
114             twigil => '$!',
115             marker => 'TWG_BANG',
116             ) );
117              
118             sub perltidy {
119 94     94 0 188822 my %args = @_;
120 94         522 $plugins->add_args( $args{argv} );
121              
122             return Perl::Tidy::perltidy(
123 94     94   2914755 prefilter => sub { $plugins->prefilter( $_[0] ) },
124 94     94   1209640 postfilter => sub { $plugins->postfilter( $_[0] ) },
125             @_
126 94         785 );
127             }
128              
129             1;
130              
131             __END__
132              
133             =pod
134              
135             =head1 NAME
136              
137             Perl::Tidy::Sweetened - Tweaks to Perl::Tidy to support some syntactic sugar
138              
139             =head1 VERSION
140              
141             version 1.19
142              
143             =head1 STATUS
144              
145             =for html <a href="https://travis-ci.org/mvgrimes/Perl-Tidy-Sweetened"><img src="https://travis-ci.org/mvgrimes/Perl-Tidy-Sweetened.svg?branch=master" alt="Build Status"></a>
146             <a href="https://metacpan.org/pod/Perl::Tidy::Sweetened"><img alt="CPAN version" src="https://badge.fury.io/pl/Perl-Tidy-Sweetened.svg" /></a>
147              
148             =head1 DESCRIPTION
149              
150             There are a number of modules on CPAN that allow users to write their classes
151             with a more "modern" syntax. These tools eliminate the need to shift off
152             C<$self>, can support type checking and offer other improvements.
153             Unfortunately, they can break the support tools that the Perl community has
154             come to rely on. This module attempts to work around those issues.
155              
156             The module uses
157             L<Perl::Tidy>'s C<prefilter> and C<postfilter> hooks to support C<method> and
158             C<func> keywords, including the (possibly multi-line) parameter lists. This is
159             quite an ugly hack, but it is the recommended method of supporting these new
160             keywords (see the 2010-12-17 entry in the Perl::Tidy
161             L<CHANGES|https://metacpan.org/source/SHANCOCK/Perl-Tidy-20120714/CHANGES>
162             file). B<The resulting formatted code will leave the parameter lists untouched.>
163              
164             C<Perl::Tidy::Sweetened> attempts to support the syntax outlined in the
165             following modules, but most of the new syntax styles should work:
166              
167             =over
168              
169             =item * p5-mop
170              
171             =item * Method::Signatures::Simple
172              
173             =item * MooseX::Method::Signatures
174              
175             =item * MooseX::Declare
176              
177             =item * Moops
178              
179             =item * perl 5.20 signatures
180              
181             =item * Kavorka
182              
183             =back
184              
185             =head1 THANKS
186              
187             The idea and much of original code taken from Jonathan Swartz'
188             L<blog|http://www.openswartz.com/2010/12/19/perltidy-and-method-happy-together/>.
189              
190             =head1 SEE ALSO
191              
192             L<Perl::Tidy>
193              
194             =head1 AUTHOR
195              
196             Mark Grimes <mgrimes@cpan.org>
197              
198             =head1 SOURCE
199              
200             Source repository is at L<https://github.com/mvgrimes/Perl-Tidy-Sweetened>.
201              
202             =head1 BUGS
203              
204             Please report any bugs or feature requests on the bugtracker website L<https://github.com/mvgrimes/Perl-Tidy-Sweetened/issues>
205              
206             When submitting a bug or request, please include a test-file or a
207             patch to an existing test-file that illustrates the bug or desired
208             feature.
209              
210             =head1 COPYRIGHT AND LICENSE
211              
212             This software is copyright (c) 2023 by Mark Grimes <mgrimes@cpan.org>.
213              
214             This is free software; you can redistribute it and/or modify it under
215             the same terms as the Perl 5 programming language system itself.
216              
217             =cut