« blib/lib/Perl/Lint/Policy/Variables/ProhibitUnusedVariables.pm
↑ Summary
blib/lib/Perl/Lint/Filter/LikePerlCritic/Brutal.pm »
| 1 | package Perl::Lint::Policy::Variables::RequireLocalizedPunctuationVars; | ||
| 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 B::Keywords; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use List::MoreUtils qw/apply uniq/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Perl::Lint::Constants::Type; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use parent "Perl::Lint::Policy"; | ||
| 8 | |||
| 9 | use constant { | ||
| 10 | DESC => 'Magic variable "%s" should be assigned as "local"', | ||
| 11 | EXPL => [81, 82], | ||
| 12 | }; | ||
| 13 | |||
| 14 | my %var_token_types = ( | ||
| 15 | &VAR => 1, | ||
| 16 | &ARRAY_VAR => 1, | ||
| 17 | &HASH_VAR => 1, | ||
| 18 | &GLOBAL_VAR => 1, | ||
| 19 | &GLOBAL_ARRAY_VAR => 1, | ||
| 20 | &GLOBAL_HASH_VAR => 1, | ||
| 21 | |||
| 22 | &PROGRAM_ARGUMENT => 1, | ||
| 23 | &LIBRARY_DIRECTORIES => 1, | ||
| 24 | &INCLUDE => 1, | ||
| 25 | &ENVIRONMENT => 1, | ||
| 26 | &SIGNAL => 1, | ||
| 27 | &SPECIFIC_VALUE => 1, | ||
| 28 | &ARRAY_SIZE => 1, | ||
| 29 | ); | ||
| 30 | |||
| 31 | my @globals = ( | ||
| 32 | @B::Keywords::Arrays, | ||
| 33 | @B::Keywords::Hashes, | ||
| 34 | @B::Keywords::Scalars, | ||
| 35 | ); | ||
| 36 | push @globals, map { "*$_" } grep { substr($_, 0, 1) ne '*' } @B::Keywords::Filehandles; | ||
| 37 | my %globals = map { $_ => 1 } @globals; | ||
| 38 | |||
| 39 | sub evaluate { | ||
| 40 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 41 | |||
| 42 | my @exemptions = qw/$_ $ARG @_/; | ||
| 43 | if (my $this_policies_arg = $args->{require_localized_punctuation_vars}) { | ||
| 44 | push @exemptions, split(/\s+/, $this_policies_arg->{allow} || ''); | ||
| 45 | } | ||
| 46 | my %exemptions = map { $_ => 1 } @exemptions; | ||
| 47 | |||
| 48 | my @violations; | ||
| 49 | for (my $i = 0, my $token_type, my $variable; my $token = $tokens->[$i]; $i++) { | ||
| 50 | $token_type = $token->{type}; | ||
| 51 | |||
| 52 | if ($token_type == LOCAL_DECL) { | ||
| 53 | $token = $tokens->[++$i]; | ||
| 54 | |||
| 55 | last if !$token; | ||
| 56 | if ($token->{type} == LEFT_PAREN) { | ||
| 57 | my $lpnum = 1; | ||
| 58 | for ($i++; $token = $tokens->[$i]; $i++) { | ||
| 59 | $token_type = $token->{type}; | ||
| 60 | |||
| 61 | if ($token_type == LEFT_PAREN) { | ||
| 62 | $lpnum++; | ||
| 63 | } | ||
| 64 | elsif ($token_type == RIGHT_PAREN) { | ||
| 65 | last if --$lpnum <= 0; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | next; | ||
| 71 | } | ||
| 72 | |||
| 73 | $variable = $token->{data}; | ||
| 74 | if ($token_type == MOD) { | ||
| 75 | $token = $tokens->[++$i]; | ||
| 76 | $token_type = $token->{type}; | ||
| 77 | |||
| 78 | last if !$token; | ||
| 79 | if ($token_type == NOT) { | ||
| 80 | $variable .= $token->{data}; | ||
| 81 | $token_type = VAR; # XXX | ||
| 82 | } | ||
| 83 | elsif ($token_type == BIT_XOR) { | ||
| 84 | $variable .= $token->{data}; | ||
| 85 | |||
| 86 | $token = $tokens->[++$i]; | ||
| 87 | $token_type = $token->{type}; | ||
| 88 | |||
| 89 | last if !$token; | ||
| 90 | |||
| 91 | if ($token_type == KEY) { | ||
| 92 | $variable .= $token->{data}; | ||
| 93 | $token_type = VAR; # XXX | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | elsif ($token_type == SPECIFIC_VALUE) { | ||
| 98 | $token = $tokens->[$i+1]; | ||
| 99 | next if !$token; | ||
| 100 | |||
| 101 | if ($token->{type} == KEY) { | ||
| 102 | $i++; | ||
| 103 | $variable .= $token->{data}; | ||
| 104 | $token_type = SPECIFIC_VALUE; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | elsif ($token_type == GLOB) { | ||
| 108 | $token = $tokens->[++$i]; | ||
| 109 | |||
| 110 | last if !$token; | ||
| 111 | $token_type = $token->{type}; | ||
| 112 | if ( | ||
| 113 | $token_type == KEY || | ||
| 114 | $token_type == TYPE_STDIN || | ||
| 115 | $token_type == TYPE_STDOUT || | ||
| 116 | $token_type == TYPE_STDERR | ||
| 117 | ) { | ||
| 118 | $variable .= $token->{data}; | ||
| 119 | $token_type = VAR; # XXX | ||
| 120 | } | ||
| 121 | } ## fall through | ||
| 122 | |||
| 123 | if ($var_token_types{$token_type}) { | ||
| 124 | my $line = $token->{line}; | ||
| 125 | |||
| 126 | my $before_token = $tokens->[$i-1]; | ||
| 127 | if ($before_token && $before_token->{type} == ASSIGN) { | ||
| 128 | next; | ||
| 129 | } | ||
| 130 | |||
| 131 | $token = $tokens->[++$i]; | ||
| 132 | last if !$token; | ||
| 133 | $token_type = $token->{type}; | ||
| 134 | |||
| 135 | if ($token_type == LEFT_BRACKET) { | ||
| 136 | my $lbnum = 1; | ||
| 137 | for ($i++; $token = $tokens->[$i]; $i++) { | ||
| 138 | $token_type = $token->{type}; | ||
| 139 | if ($token_type == LEFT_BRACKET) { | ||
| 140 | $lbnum++; | ||
| 141 | } | ||
| 142 | elsif ($token_type == RIGHT_BRACKET) { | ||
| 143 | last if --$lbnum <= 0; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | $token = $tokens->[++$i]; | ||
| 147 | |||
| 148 | substr($variable, 0, 1) = '@'; | ||
| 149 | } | ||
| 150 | elsif ($token_type == LEFT_BRACE) { | ||
| 151 | my $lbnum = 1; | ||
| 152 | for ($i++; $token = $tokens->[$i]; $i++) { | ||
| 153 | $token_type = $token->{type}; | ||
| 154 | if ($token_type == LEFT_BRACE) { | ||
| 155 | $lbnum++; | ||
| 156 | } | ||
| 157 | elsif ($token_type == RIGHT_BRACE) { | ||
| 158 | last if --$lbnum <= 0; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | $token = $tokens->[++$i]; | ||
| 162 | |||
| 163 | substr($variable, 0, 1) = '%'; | ||
| 164 | } | ||
| 165 | |||
| 166 | last if !$token; | ||
| 167 | |||
| 168 | if ($token->{type} == RIGHT_PAREN) { | ||
| 169 | $token = $tokens->[++$i]; | ||
| 170 | last if !$token; | ||
| 171 | } | ||
| 172 | |||
| 173 | next if $token->{type} != ASSIGN; | ||
| 174 | |||
| 175 | if ($globals{$variable} && !$exemptions{$variable}) { | ||
| 176 | push @violations, { | ||
| 177 | filename => $file, | ||
| 178 | line => $line, | ||
| 179 | description => DESC, | ||
| 180 | explanation => EXPL, | ||
| 181 | policy => __PACKAGE__, | ||
| 182 | }; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | return \@violations; | ||
| 188 | } | ||
| 189 | |||
| 190 | 1; | ||
| 191 | |||