« blib/lib/Perl/Lint/Policy/RegularExpressions/RequireExtendedFormatting.pm
↑ Summary
blib/lib/Perl/Lint/Policy/Subroutines/ProhibitAmpersandSigils.pm »
| 1 | package Perl::Lint::Policy::RegularExpressions::RequireLineBoundaryMatching; | ||
| 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 => 'Regular expression without "/m" flag', | ||
| 9 | EXPL => [237], | ||
| 10 | }; | ||
| 11 | |||
| 12 | sub evaluate { | ||
| 13 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 14 | |||
| 15 | my @violations; | ||
| 16 | |||
| 17 | my $depth = 0; | ||
| 18 | my $is_non_target_reg = 0; | ||
| 19 | |||
| 20 | my $enabled_re_m_depth = -1; # use negative value as default | ||
| 21 | my @enabled_re_m_depths; | ||
| 22 | |||
| 23 | my $disable_re_m_depth = -1; # use negative value as default | ||
| 24 | my @disable_re_m_depths; | ||
| 25 | |||
| 26 | for (my $i = 0; my $token = $tokens->[$i]; $i++) { | ||
| 27 | my $token_type = $token->{type}; | ||
| 28 | my $token_data = $token->{data}; | ||
| 29 | |||
| 30 | my $next_token = $tokens->[$i+1]; | ||
| 31 | my $next_token_type = $next_token->{type}; | ||
| 32 | my $next_token_data = $next_token->{data}; | ||
| 33 | |||
| 34 | if (!$is_non_target_reg && $token_type == REG_DELIM) { | ||
| 35 | if ( | ||
| 36 | defined $next_token_type && | ||
| 37 | ( | ||
| 38 | $next_token_type == SEMI_COLON || # when any regex options don't exist | ||
| 39 | ($next_token_type == REG_OPT && $next_token_data !~ /m/) # when the `m` regex option doesn't exist | ||
| 40 | ) | ||
| 41 | ) { | ||
| 42 | if ( | ||
| 43 | !($enabled_re_m_depth >= 0 && $depth >= $enabled_re_m_depth) || | ||
| 44 | ($disable_re_m_depth >= 0 && $disable_re_m_depth > $enabled_re_m_depth) | ||
| 45 | ) { | ||
| 46 | push @violations, { | ||
| 47 | filename => $file, | ||
| 48 | line => $token->{line}, | ||
| 49 | description => DESC, | ||
| 50 | explanation => EXPL, | ||
| 51 | policy => __PACKAGE__, | ||
| 52 | }; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | next; | ||
| 57 | } | ||
| 58 | |||
| 59 | # Ignore regexes which are unnecessary to check | ||
| 60 | # XXX more? | ||
| 61 | if ( | ||
| 62 | $token_type == REG_ALL_REPLACE || | ||
| 63 | $token_type == REG_LIST || | ||
| 64 | $token_type == REG_QUOTE || | ||
| 65 | $token_type == REG_EXEC | ||
| 66 | ) { | ||
| 67 | $is_non_target_reg = 1; | ||
| 68 | next; | ||
| 69 | } | ||
| 70 | |||
| 71 | if ($token_type == SEMI_COLON) { | ||
| 72 | $is_non_target_reg = 0; | ||
| 73 | next; | ||
| 74 | } | ||
| 75 | |||
| 76 | # Represent block scope hierarchy | ||
| 77 | if ($token_type == LEFT_BRACE) { | ||
| 78 | $depth++; | ||
| 79 | next; | ||
| 80 | } | ||
| 81 | if ($token_type == RIGHT_BRACE) { | ||
| 82 | if ($enabled_re_m_depth == $depth) { | ||
| 83 | pop @enabled_re_m_depths; | ||
| 84 | $enabled_re_m_depth = $enabled_re_m_depths[-1] // -1; | ||
| 85 | } | ||
| 86 | if ($disable_re_m_depth == $depth) { | ||
| 87 | pop @disable_re_m_depths; | ||
| 88 | $disable_re_m_depth = $disable_re_m_depths[-1] // -1; | ||
| 89 | } | ||
| 90 | $depth--; | ||
| 91 | next; | ||
| 92 | } | ||
| 93 | |||
| 94 | # for | ||
| 95 | # `use re qw{/m}` | ||
| 96 | # `use re '/m'` | ||
| 97 | if ($token_type == USED_NAME && $token_data eq 're') { | ||
| 98 | for ($i++; $token = $tokens->[$i]; $i++) { | ||
| 99 | $token_type = $token->{type}; | ||
| 100 | $token_data = $token->{data}; | ||
| 101 | if ($token_type == SEMI_COLON) { | ||
| 102 | last; | ||
| 103 | } | ||
| 104 | if ( | ||
| 105 | ($token_type == RAW_STRING || $token_type == STRING || $token_type == REG_EXP) && | ||
| 106 | $token_data =~ /m/ | ||
| 107 | ) { | ||
| 108 | push @enabled_re_m_depths, $depth; | ||
| 109 | $enabled_re_m_depth = $depth; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | next; | ||
| 114 | } | ||
| 115 | |||
| 116 | # for | ||
| 117 | # `no re qw{/m}` | ||
| 118 | # `no re '/m'` | ||
| 119 | if ( | ||
| 120 | $token_type == BUILTIN_FUNC && | ||
| 121 | $token_data eq 'no' && | ||
| 122 | $next_token_type == KEY && | ||
| 123 | $next_token_data eq 're' | ||
| 124 | ) { | ||
| 125 | for ($i++; $token = $tokens->[$i]; $i++) { | ||
| 126 | $token_type = $token->{type}; | ||
| 127 | $token_data = $token->{data}; | ||
| 128 | if ($token_type == SEMI_COLON) { | ||
| 129 | last; | ||
| 130 | } | ||
| 131 | if ( | ||
| 132 | ($token_type == RAW_STRING || $token_type == STRING || $token_type == REG_EXP) && | ||
| 133 | $token_data =~ /m/ | ||
| 134 | ) { | ||
| 135 | push @disable_re_m_depths, $depth; | ||
| 136 | $disable_re_m_depth = $depth; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | next; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | return \@violations; | ||
| 145 | } | ||
| 146 | |||
| 147 | 1; | ||
| 148 | |||