« blib/lib/Perl/Lint/Policy/RegularExpressions/ProhibitUselessTopic.pm
↑ Summary
blib/lib/Perl/Lint/Policy/RegularExpressions/RequireDotMatchAnything.pm »
| 1 | package Perl::Lint::Policy::RegularExpressions::RequireBracesForMultiline; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Perl::Lint::Constants::Type; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use parent "Perl::Lint::Policy"; | ||
| 6 | |||
| 7 | use constant { | ||
| 8 | DESC => 'Use "{" and "}" to delimit multi-line regexps', | ||
| 9 | EXPL => [242], | ||
| 10 | }; | ||
| 11 | |||
| 12 | sub evaluate { | ||
| 13 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 14 | |||
| 15 | my $brackets_regex = qr/\A\{\Z/; | ||
| 16 | if ($args->{require_braces_for_multiline}->{allow_all_brackets}) { | ||
| 17 | $brackets_regex = qr/\A[\{\(\[\<]\Z/; | ||
| 18 | } | ||
| 19 | |||
| 20 | my @violations; | ||
| 21 | for (my $i = 0, my $token_type; my $token = $tokens->[$i]; $i++) { | ||
| 22 | $token_type = $token->{type}; | ||
| 23 | |||
| 24 | if ($token_type == REG_EXP || $token_type == REG_REPLACE_FROM) { | ||
| 25 | if (my @backslashes = $token->{data} =~ /(\\*)\n/g) { | ||
| 26 | my $reg_delim_token = $tokens->[$i-1]; | ||
| 27 | if ($reg_delim_token->{data} !~ $brackets_regex) { | ||
| 28 | for my $backslash (@backslashes) { | ||
| 29 | if (!$backslash || length($backslash) % 2 == 0) { | ||
| 30 | push @violations, { | ||
| 31 | filename => $file, | ||
| 32 | line => $token->{line}, | ||
| 33 | description => DESC, | ||
| 34 | explanation => EXPL, | ||
| 35 | policy => __PACKAGE__, | ||
| 36 | }; | ||
| 37 | last; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | return \@violations; | ||
| 46 | } | ||
| 47 | |||
| 48 | 1; | ||
| 49 | |||