blib/lib/Perl/Lint/Policy/RegularExpressions/RequireExtendedFormatting.pm

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