« blib/lib/Perl/Lint/Policy/ValuesAndExpressions/ProhibitNoisyQuotes.pm
↑ Summary
blib/lib/Perl/Lint/Policy/ValuesAndExpressions/ProhibitSpecialHeredocTerminator.pm »
| 1 | package Perl::Lint::Policy::ValuesAndExpressions::ProhibitQuotesAsQuotelikeOperatorDelimiters; | ||
| 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 | EXPL => 'Using quotes as delimiters for quote-like operators obfuscates code', | ||
| 9 | }; | ||
| 10 | |||
| 11 | my %TARGET_REGS = ( | ||
| 12 | ®_QUOTE => 1, | ||
| 13 | ®_DOUBLE_QUOTE => 1, | ||
| 14 | ®_LIST => 1, | ||
| 15 | ®_EXEC => 1, | ||
| 16 | ®_DECL => 1, | ||
| 17 | ®_MATCH => 1, | ||
| 18 | |||
| 19 | ®_ALL_REPLACE => 1, | ||
| 20 | ); | ||
| 21 | |||
| 22 | # TODO operator which has `replace from` and `replace to` doesn't work certainly. | ||
| 23 | # It maybe caused by Compiler::Lexer's bug. | ||
| 24 | |||
| 25 | sub evaluate { | ||
| 26 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 27 | |||
| 28 | my %target_regs_allows_single_quote = ( | ||
| 29 | 'qx' => 1, | ||
| 30 | 'qr' => 1, | ||
| 31 | 'm' => 1, | ||
| 32 | ); | ||
| 33 | if (defined (my $single_quote_allowed_operators = $args->{single_quote_allowed_operators})) { | ||
| 34 | %target_regs_allows_single_quote = (); | ||
| 35 | for my $allow_delim (split qr/\s+/, $single_quote_allowed_operators) { | ||
| 36 | $target_regs_allows_single_quote{$allow_delim} = 1; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | my %target_regs_allows_double_quote = (); | ||
| 41 | if (defined (my $double_quote_allowed_operators = $args->{double_quote_allowed_operators})) { | ||
| 42 | for my $allow_delim (split qr/\s+/, $double_quote_allowed_operators) { | ||
| 43 | $target_regs_allows_double_quote{$allow_delim} = 1; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | my %target_regs_allows_back_quote = (); | ||
| 48 | if (defined (my $back_quote_allowed_operators = $args->{back_quote_allowed_operators})) { | ||
| 49 | for my $allow_delim (split qr/\s+/, $back_quote_allowed_operators) { | ||
| 50 | $target_regs_allows_back_quote{$allow_delim} = 1; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | my @violations; | ||
| 55 | my $next_token; | ||
| 56 | for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) { | ||
| 57 | $token_type = $token->{type}; | ||
| 58 | $token_data = $token->{data}; | ||
| 59 | |||
| 60 | if ($TARGET_REGS{$token_type}) { | ||
| 61 | $next_token = $tokens->[++$i]; | ||
| 62 | if ($next_token && $next_token->{type} == REG_DELIM) { | ||
| 63 | my $next_token_data= $next_token->{data}; | ||
| 64 | |||
| 65 | my $desc = ''; | ||
| 66 | if ($next_token_data eq q<'> && !$target_regs_allows_single_quote{$token_data}) { | ||
| 67 | $desc= 'Single-quote used as quote-like operator delimiter'; | ||
| 68 | } | ||
| 69 | elsif ($next_token_data eq q<"> && !$target_regs_allows_double_quote{$token_data}) { | ||
| 70 | $desc = 'Double-quote used as quote-like operator delimiter'; | ||
| 71 | } | ||
| 72 | elsif ($next_token_data eq q<`> && !$target_regs_allows_back_quote{$token_data}) { | ||
| 73 | $desc = 'Back-quote (back-tick) used as quote-like operator delimiter'; | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | next; | ||
| 77 | } | ||
| 78 | |||
| 79 | push @violations, { | ||
| 80 | filename => $file, | ||
| 81 | line => $next_token->{line}, | ||
| 82 | description => $desc, | ||
| 83 | explanation => EXPL, | ||
| 84 | policy => __PACKAGE__, | ||
| 85 | }; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | return \@violations; | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; | ||
| 94 | |||