« blib/lib/Perl/Lint/Policy/Variables/ProhibitPerl4PackageNames.pm
↑ Summary
blib/lib/Perl/Lint/Policy/Variables/ProtectPrivateVars.pm »
| 1 | package Perl::Lint::Policy::Variables::ProhibitPunctuationVars; | ||
| 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 Compiler::Lexer; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use List::Util qw/any/; | ||
| 6 | use Perl::Lint::Constants::Type; | ||
| 7 | use parent "Perl::Lint::Policy"; | ||
| 8 | |||
| 9 | use constant { | ||
| 10 | DESC => 'Magic punctuation variable %s used', | ||
| 11 | EXPL => [79], | ||
| 12 | }; | ||
| 13 | |||
| 14 | my %var_token_types = ( | ||
| 15 | &VAR => 1, | ||
| 16 | &ARRAY_VAR => 1, | ||
| 17 | &HASH_VAR => 1, | ||
| 18 | |||
| 19 | &GLOBAL_VAR => 1, | ||
| 20 | &GLOBAL_ARRAY_VAR => 1, | ||
| 21 | &GLOBAL_HASH_VAR => 1, | ||
| 22 | ); | ||
| 23 | |||
| 24 | my %expands_regexp_token_types = ( | ||
| 25 | ®_EXEC => 1, | ||
| 26 | ®_DECL => 1, | ||
| 27 | ®_DOUBLE_QUOTE => 1, | ||
| 28 | ); | ||
| 29 | |||
| 30 | my %special_variable_token_types = ( | ||
| 31 | &SPECIFIC_VALUE => 1, | ||
| 32 | &ARRAY_SIZE => 1, | ||
| 33 | ); | ||
| 34 | |||
| 35 | my %magic_variables = ( | ||
| 36 | '$1' => 1, '$2' => 1, '$3' => 1, | ||
| 37 | '$4' => 1, '$5' => 1, '$6' => 1, | ||
| 38 | '$7' => 1, '$8' => 1, '$9' => 1, | ||
| 39 | '$_' => 1, '$&' => 1, '$`' => 1, | ||
| 40 | '$+' => 1, '@+' => 1, '@*' => 1, | ||
| 41 | '%+' => 1, '$*' => 1, '$.' => 1, | ||
| 42 | '$/' => 1, '$|' => 1, '$(' => 1, | ||
| 43 | '$"' => 1, '$;' => 1, '$%' => 1, | ||
| 44 | '$=' => 1, '$-' => 1, '@-' => 1, | ||
| 45 | '%-' => 1, '$)' => 1, '$~' => 1, | ||
| 46 | '$^' => 1, '$:' => 1, '$?' => 1, | ||
| 47 | '$!' => 1, '%!' => 1, '$@' => 1, | ||
| 48 | '$$' => 1, '$<' => 1, '$>' => 1, | ||
| 49 | '$0' => 1, '$[' => 1, '$]' => 1, | ||
| 50 | '@_' => 1, | ||
| 51 | |||
| 52 | q{$'} => 1, | ||
| 53 | |||
| 54 | '$^L' => 1, '$^A' => 1, '$^E' => 1, | ||
| 55 | '$^C' => 1, '$^D' => 1, '$^F' => 1, | ||
| 56 | '$^H' => 1, '$^I' => 1, '$^M' => 1, | ||
| 57 | '$^N' => 1, '$^O' => 1, '$^P' => 1, | ||
| 58 | '$^R' => 1, '$^S' => 1, '$^T' => 1, | ||
| 59 | '$^V' => 1, '$^W' => 1, '$^X' => 1, | ||
| 60 | '%^H' => 1, | ||
| 61 | |||
| 62 | '$\\' => 1, | ||
| 63 | '$::|' => 1, | ||
| 64 | '$}' => 1, | ||
| 65 | '$,' => 1, | ||
| 66 | '$#' => 1, | ||
| 67 | '$#+' => 1, | ||
| 68 | '$#-' => 1, | ||
| 69 | ); | ||
| 70 | |||
| 71 | my %ignore_for_interpolation = ( | ||
| 72 | q{$'} => 1, | ||
| 73 | q{$$} => 1, | ||
| 74 | q{$#} => 1, | ||
| 75 | q{$:} => 1, | ||
| 76 | ); | ||
| 77 | |||
| 78 | sub evaluate { | ||
| 79 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 80 | |||
| 81 | my $string_mode = ''; | ||
| 82 | my %exempt_vars = ( | ||
| 83 | '$_' => 1, '@_' => 1, '$]' => 1, | ||
| 84 | '$1' => 1, '$2' => 1, '$3' => 1, | ||
| 85 | '$4' => 1, '$5' => 1, '$6' => 1, | ||
| 86 | '$7' => 1, '$8' => 1, '$9' => 1, | ||
| 87 | ); | ||
| 88 | |||
| 89 | if (my $this_policies_arg = $args->{prohibit_punctuation_vars}) { | ||
| 90 | $string_mode = $this_policies_arg->{string_mode} || ''; | ||
| 91 | if ($string_mode eq 'thorough') { | ||
| 92 | %exempt_vars = (); | ||
| 93 | } | ||
| 94 | |||
| 95 | for my $exempt_var (split(/\s+/, $this_policies_arg->{allow} || '')) { | ||
| 96 | $exempt_vars{$exempt_var} = 1; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | my $lexer_for_str = Compiler::Lexer->new; | ||
| 101 | |||
| 102 | my @violations; | ||
| 103 | for ( | ||
| 104 | my $i = 0, my $token_type, my $token_data, my $is_ref = 0, my $is_raw_heredoc_tag = 0; | ||
| 105 | my $token = $tokens->[$i]; | ||
| 106 | $i++ | ||
| 107 | ) { | ||
| 108 | $token_type = $token->{type}; | ||
| 109 | $token_data = $token->{data}; | ||
| 110 | |||
| 111 | if ($special_variable_token_types{$token_type}) { | ||
| 112 | if ($is_ref) { | ||
| 113 | $is_ref = 0; | ||
| 114 | next; | ||
| 115 | } | ||
| 116 | |||
| 117 | if ($exempt_vars{$token_data}) { | ||
| 118 | next; | ||
| 119 | } | ||
| 120 | |||
| 121 | if (! $magic_variables{$token_data}) { | ||
| 122 | next; | ||
| 123 | } | ||
| 124 | |||
| 125 | push @violations, { | ||
| 126 | filename => $file, | ||
| 127 | line => $token->{line}, | ||
| 128 | description => sprintf(DESC, $token_data), | ||
| 129 | explanation => EXPL, | ||
| 130 | policy => __PACKAGE__, | ||
| 131 | }; | ||
| 132 | next; | ||
| 133 | } | ||
| 134 | |||
| 135 | if ($var_token_types{$token_type}) { | ||
| 136 | if ($is_ref) { | ||
| 137 | $is_ref = 0; | ||
| 138 | next; | ||
| 139 | } | ||
| 140 | |||
| 141 | if ($exempt_vars{$token_data}) { | ||
| 142 | next; | ||
| 143 | } | ||
| 144 | |||
| 145 | if (! $magic_variables{$token_data}) { | ||
| 146 | next; | ||
| 147 | } | ||
| 148 | |||
| 149 | if (substr($token_data, 1, 1) =~ /\A[^a-zA-Z]\Z/) { | ||
| 150 | push @violations, { | ||
| 151 | filename => $file, | ||
| 152 | line => $token->{line}, | ||
| 153 | description => sprintf(DESC, $token_data), | ||
| 154 | explanation => EXPL, | ||
| 155 | policy => __PACKAGE__, | ||
| 156 | }; | ||
| 157 | } | ||
| 158 | next; | ||
| 159 | } | ||
| 160 | |||
| 161 | if ($token_type == REF) { | ||
| 162 | $is_ref = 1; | ||
| 163 | next; | ||
| 164 | } | ||
| 165 | |||
| 166 | if ($token_type == HERE_DOCUMENT_RAW_TAG) { | ||
| 167 | $is_raw_heredoc_tag = 1; | ||
| 168 | next; | ||
| 169 | } | ||
| 170 | |||
| 171 | if ($token_type == HERE_DOCUMENT_END) { | ||
| 172 | $is_raw_heredoc_tag = 0; | ||
| 173 | next; | ||
| 174 | } | ||
| 175 | |||
| 176 | if ($expands_regexp_token_types{$token_type}) { | ||
| 177 | $i += 2; | ||
| 178 | $token = $tokens->[$i]; | ||
| 179 | if ($token->{type} != REG_EXP) { # when content is empty | ||
| 180 | next; | ||
| 181 | } | ||
| 182 | $token_data = $token->data; | ||
| 183 | $token_type = STRING; | ||
| 184 | } # fall through | ||
| 185 | |||
| 186 | if ( | ||
| 187 | $token_type == STRING || | ||
| 188 | $token_type == EXEC_STRING | ||
| 189 | # ($token_type == HERE_DOCUMENT && $is_raw_heredoc_tag) | ||
| 190 | ) { | ||
| 191 | if ($string_mode eq 'disable') { | ||
| 192 | next; | ||
| 193 | } | ||
| 194 | |||
| 195 | my $parts = $lexer_for_str->tokenize($token_data); | ||
| 196 | my $ref_count = 0; | ||
| 197 | for (my $j = 0, my $part_type, my $used_var; my $part = $parts->[$j]; $j++) { | ||
| 198 | $part_type = $part->{type}; | ||
| 199 | $used_var = $part->{data}; | ||
| 200 | |||
| 201 | if ($part_type == REF) { | ||
| 202 | $ref_count++; | ||
| 203 | next; | ||
| 204 | } | ||
| 205 | |||
| 206 | if ($ref_count % 2 != 0) { | ||
| 207 | $ref_count = 0; | ||
| 208 | next; | ||
| 209 | } | ||
| 210 | |||
| 211 | if ($part_type == SPECIFIC_VALUE) { | ||
| 212 | if ($used_var eq '$:') { | ||
| 213 | $part = $parts->[$j+1]; | ||
| 214 | |||
| 215 | if ($part && $part->{type} == COLON) { | ||
| 216 | $part = $parts->[$j+2]; | ||
| 217 | if ($part && $part->{type} == BIT_OR) { | ||
| 218 | $used_var = '$::|'; | ||
| 219 | } | ||
| 220 | else { | ||
| 221 | next; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | } | ||
| 225 | # TODO | ||
| 226 | # elsif ($used_var eq q{$'}) { | ||
| 227 | # $part = $parts->[$j+1]; | ||
| 228 | # if ($part && $part->{type} == KEY) { | ||
| 229 | # # next; | ||
| 230 | # } | ||
| 231 | # } | ||
| 232 | } | ||
| 233 | elsif ($part_type != ARRAY_SIZE) { | ||
| 234 | if (!$var_token_types{$part_type}) { | ||
| 235 | next; | ||
| 236 | } | ||
| 237 | |||
| 238 | $part = $parts->[++$j]; | ||
| 239 | if ($part) { | ||
| 240 | if ($used_var eq '$') { | ||
| 241 | if ($part->{type} == RIGHT_BRACE) { | ||
| 242 | $used_var = '$}'; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | elsif ($used_var eq '@') { | ||
| 246 | if ($part->{type} == MUL) { | ||
| 247 | $used_var = '@*'; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | elsif ($used_var eq '%-') { | ||
| 251 | if ($part->{type} == INT) { # for formatting. e.g. "%-04f" | ||
| 252 | next; | ||
| 253 | } | ||
| 254 | } | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | if ($exempt_vars{$used_var}) { | ||
| 259 | next; | ||
| 260 | } | ||
| 261 | |||
| 262 | if ($string_mode eq 'simple' && $ignore_for_interpolation{$used_var}) { | ||
| 263 | next; | ||
| 264 | } | ||
| 265 | |||
| 266 | if ($magic_variables{$used_var}) { | ||
| 267 | push @violations, { | ||
| 268 | filename => $file, | ||
| 269 | line => $token->{line}, | ||
| 270 | description => sprintf(DESC, $used_var), | ||
| 271 | explanation => EXPL, | ||
| 272 | policy => __PACKAGE__, | ||
| 273 | }; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | next; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | return \@violations; | ||
| 282 | } | ||
| 283 | |||
| 284 | 1; | ||
| 285 | |||