line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Critic::Policy::RegularExpressions::RequireBracesForMultiline; |
2
|
|
|
|
|
|
|
|
3
|
40
|
|
|
40
|
|
26009
|
use 5.010001; |
|
40
|
|
|
|
|
216
|
|
4
|
40
|
|
|
40
|
|
263
|
use strict; |
|
40
|
|
|
|
|
126
|
|
|
40
|
|
|
|
|
847
|
|
5
|
40
|
|
|
40
|
|
225
|
use warnings; |
|
40
|
|
|
|
|
119
|
|
|
40
|
|
|
|
|
924
|
|
6
|
40
|
|
|
40
|
|
240
|
use Readonly; |
|
40
|
|
|
|
|
165
|
|
|
40
|
|
|
|
|
2072
|
|
7
|
|
|
|
|
|
|
|
8
|
40
|
|
|
40
|
|
311
|
use English qw(-no_match_vars); |
|
40
|
|
|
|
|
145
|
|
|
40
|
|
|
|
|
299
|
|
9
|
40
|
|
|
40
|
|
15217
|
use Carp; |
|
40
|
|
|
|
|
156
|
|
|
40
|
|
|
|
|
2662
|
|
10
|
|
|
|
|
|
|
|
11
|
40
|
|
|
40
|
|
373
|
use Perl::Critic::Utils qw{ :booleans :severities }; |
|
40
|
|
|
|
|
159
|
|
|
40
|
|
|
|
|
2027
|
|
12
|
|
|
|
|
|
|
|
13
|
40
|
|
|
40
|
|
6972
|
use parent 'Perl::Critic::Policy'; |
|
40
|
|
|
|
|
172
|
|
|
40
|
|
|
|
|
273
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '1.146'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Readonly::Scalar my $DESC => q<Use '{' and '}' to delimit multi-line regexps>; |
20
|
|
|
|
|
|
|
Readonly::Scalar my $EXPL => [242]; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Readonly::Array my @EXTRA_BRACKETS => qw{ () [] <> }; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub supported_parameters { |
27
|
|
|
|
|
|
|
return ( |
28
|
|
|
|
|
|
|
{ |
29
|
94
|
|
|
94
|
0
|
1978
|
name => 'allow_all_brackets', |
30
|
|
|
|
|
|
|
description => |
31
|
|
|
|
|
|
|
q[In addition to allowing '{}', allow '()', '[]', and '{}'.], |
32
|
|
|
|
|
|
|
behavior => 'boolean', |
33
|
|
|
|
|
|
|
}, |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
78
|
|
|
78
|
1
|
346
|
sub default_severity { return $SEVERITY_LOWEST } |
38
|
84
|
|
|
84
|
1
|
424
|
sub default_themes { return qw( core pbp cosmetic ) } |
39
|
34
|
|
|
34
|
1
|
164
|
sub applies_to { return qw(PPI::Token::Regexp::Match |
40
|
|
|
|
|
|
|
PPI::Token::Regexp::Substitute |
41
|
|
|
|
|
|
|
PPI::Token::QuoteLike::Regexp) } |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub initialize_if_enabled { |
46
|
51
|
|
|
51
|
1
|
254
|
my ( $self, $config ) = @_; |
47
|
|
|
|
|
|
|
|
48
|
51
|
|
|
|
|
230
|
my %delimiters = ( q<{}> => 1 ); |
49
|
51
|
100
|
|
|
|
240
|
if ( $self->{_allow_all_brackets} ) { |
50
|
1
|
|
|
|
|
10
|
@delimiters{ @EXTRA_BRACKETS } = (1) x @EXTRA_BRACKETS; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
51
|
|
|
|
|
201
|
$self->{_allowed_delimiters} = \%delimiters; |
54
|
|
|
|
|
|
|
|
55
|
51
|
|
|
|
|
221
|
return $TRUE; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub violates { |
61
|
24
|
|
|
24
|
1
|
43
|
my ( $self, $elem, undef ) = @_; |
62
|
|
|
|
|
|
|
|
63
|
24
|
|
|
|
|
69
|
my $re = $elem->get_match_string(); |
64
|
24
|
100
|
|
|
|
350
|
return if $re !~ m/\n/xms; |
65
|
|
|
|
|
|
|
|
66
|
20
|
|
|
|
|
46
|
my ($match_delim) = $elem->get_delimiters(); |
67
|
20
|
100
|
|
|
|
359
|
return if $self->{_allowed_delimiters}{$match_delim}; |
68
|
|
|
|
|
|
|
|
69
|
4
|
|
|
|
|
22
|
return $self->violation( $DESC, $EXPL, $elem ); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
__END__ |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=pod |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 NAME |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Perl::Critic::Policy::RegularExpressions::RequireBracesForMultiline - Use C<{> and C<}> to delimit multi-line regexps. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 AFFILIATION |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
This Policy is part of the core L<Perl::Critic|Perl::Critic> |
88
|
|
|
|
|
|
|
distribution. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 DESCRIPTION |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Long regular expressions are hard to read. A good practice is to use |
94
|
|
|
|
|
|
|
the C<x> modifier and break the regex into multiple lines with |
95
|
|
|
|
|
|
|
comments explaining the parts. But, with the usual C<//> delimiters, |
96
|
|
|
|
|
|
|
the beginning and end can be hard to match, especially in a C<s///> |
97
|
|
|
|
|
|
|
regexp. Instead, try using C<{}> characters to delimit your |
98
|
|
|
|
|
|
|
expressions. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Compare these: |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
s/ |
103
|
|
|
|
|
|
|
<a \s+ href="([^"]+)"> |
104
|
|
|
|
|
|
|
(.*?) |
105
|
|
|
|
|
|
|
</a> |
106
|
|
|
|
|
|
|
/link=$1, text=$2/xms; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
vs. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
s{ |
111
|
|
|
|
|
|
|
<a \s+ href="([^"]+)"> |
112
|
|
|
|
|
|
|
(.*?) |
113
|
|
|
|
|
|
|
</a> |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
{link=$1, text=$2}xms; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Is that an improvement? Marginally, but yes. The curly braces lead |
118
|
|
|
|
|
|
|
the eye better. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 CONFIGURATION |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
There is one option for this policy, C<allow_all_brackets>. If this |
124
|
|
|
|
|
|
|
is true, then, in addition to allowing C<{}>, the other matched pairs |
125
|
|
|
|
|
|
|
of C<()>, C<[]>, and C<< <> >> are allowed. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 CREDITS |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Initial development of this policy was supported by a grant from the |
131
|
|
|
|
|
|
|
Perl Foundation. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 AUTHOR |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Chris Dolan <cdolan@cpan.org> |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 COPYRIGHT |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Copyright (c) 2007-2011 Chris Dolan. Many rights reserved. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
144
|
|
|
|
|
|
|
it under the same terms as Perl itself. The full text of this license |
145
|
|
|
|
|
|
|
can be found in the LICENSE file included with this module |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=cut |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# Local Variables: |
150
|
|
|
|
|
|
|
# mode: cperl |
151
|
|
|
|
|
|
|
# cperl-indent-level: 4 |
152
|
|
|
|
|
|
|
# fill-column: 78 |
153
|
|
|
|
|
|
|
# indent-tabs-mode: nil |
154
|
|
|
|
|
|
|
# c-indentation-style: bsd |
155
|
|
|
|
|
|
|
# End: |
156
|
|
|
|
|
|
|
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : |