« blib/lib/Perl/Lint/Policy/Modules/ProhibitAutomaticExportation.pm
↑ Summary
blib/lib/Perl/Lint/Policy/Modules/ProhibitExcessMainComplexity.pm »
| 1 | package Perl::Lint::Policy::Modules::ProhibitConditionalUseStatements; | ||
| 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 Perl::Lint::Keywords; | ||
| 6 | use parent "Perl::Lint::Policy"; | ||
| 7 | |||
| 8 | use constant { | ||
| 9 | DESC => 'Conditional "use" statement', | ||
| 10 | EXPL => 'Use "require" to conditionally include a module', | ||
| 11 | }; | ||
| 12 | |||
| 13 | sub evaluate { | ||
| 14 | my ($class, $file, $tokens, $args) = @_; | ||
| 15 | |||
| 16 | my @violations; | ||
| 17 | my $is_in_cond = 0; | ||
| 18 | my $is_in_if = 0; | ||
| 19 | my $is_in_do = 0; | ||
| 20 | my $is_in_BEGIN = 0; | ||
| 21 | my $left_brace_num = 0; | ||
| 22 | my $is_in_illegal_do = 0; | ||
| 23 | my $previous_violation; | ||
| 24 | for (my $i = 0, my $next_token, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) { | ||
| 25 | $next_token = $tokens->[$i+1]; | ||
| 26 | $token_type = $token->{type}; | ||
| 27 | $token_data = $token->{data}; | ||
| 28 | |||
| 29 | if ( | ||
| 30 | $token_type == UNLESS_STATEMENT || | ||
| 31 | $token_type == ELSE_STATEMENT || | ||
| 32 | $token_type == ELSIF_STATEMENT || | ||
| 33 | $token_type == WHILE_STATEMENT || | ||
| 34 | $token_type == UNTIL_STATEMENT || | ||
| 35 | $token_type == FOR_STATEMENT || | ||
| 36 | $token_type == FOREACH_STATEMENT || | ||
| 37 | $token_type == CONTINUE || | ||
| 38 | ($token_type == BUILTIN_FUNC && $token_data eq 'eval') | ||
| 39 | ) { | ||
| 40 | $is_in_cond = 1; | ||
| 41 | next; | ||
| 42 | } | ||
| 43 | elsif ( | ||
| 44 | ($token_type == AND || $token_type == OR || $token_type == ALPHABET_OR || $token_type == ALPHABET_AND) && $next_token->{type} == DO | ||
| 45 | ) { | ||
| 46 | $is_in_illegal_do = 1; | ||
| 47 | $i++; | ||
| 48 | $next_token = undef; | ||
| 49 | } | ||
| 50 | elsif ($token_type == DO) { | ||
| 51 | $is_in_do = 1; | ||
| 52 | } | ||
| 53 | elsif ($token_type == IF_STATEMENT) { | ||
| 54 | $is_in_if = 1; | ||
| 55 | } | ||
| 56 | elsif ($token_type == MOD_WORD && $token_data eq 'BEGIN') { | ||
| 57 | $is_in_BEGIN = 1; | ||
| 58 | } | ||
| 59 | elsif ($token_type == LEFT_BRACE) { | ||
| 60 | $left_brace_num++; | ||
| 61 | } | ||
| 62 | elsif ($token_type == RIGHT_BRACE) { | ||
| 63 | if (--$left_brace_num == 0) { | ||
| 64 | my $next_token_type = $next_token->{type}; | ||
| 65 | if ( | ||
| 66 | $is_in_do && $next_token_type && | ||
| 67 | ( | ||
| 68 | $next_token_type eq IF_STATEMENT || | ||
| 69 | $next_token_type eq FOR_STATEMENT || | ||
| 70 | $next_token_type eq WHILE_STATEMENT || | ||
| 71 | $next_token_type eq UNTIL_STATEMENT || | ||
| 72 | $next_token_type eq UNLESS_STATEMENT || | ||
| 73 | $next_token_type eq FOREACH_STATEMENT | ||
| 74 | ) | ||
| 75 | ) { | ||
| 76 | push @violations, $previous_violation if $previous_violation; | ||
| 77 | } | ||
| 78 | $is_in_cond = 0; | ||
| 79 | $is_in_BEGIN = 0; | ||
| 80 | $is_in_if = 0; | ||
| 81 | $is_in_do = 0; | ||
| 82 | $is_in_illegal_do = 0; | ||
| 83 | $previous_violation = undef; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | elsif ($is_in_cond || $is_in_illegal_do || $is_in_do || ($is_in_BEGIN && $is_in_if)) { | ||
| 87 | if ($token_type == USE_DECL) { | ||
| 88 | my $next_token_type = $next_token->{type}; | ||
| 89 | if ( | ||
| 90 | $next_token_type == NAMESPACE || | ||
| 91 | ($next_token_type == USED_NAME && !is_perl_pragma($next_token->{data})) | ||
| 92 | ) { | ||
| 93 | $previous_violation = { | ||
| 94 | filename => $file, | ||
| 95 | line => $token->{line}, | ||
| 96 | description => DESC, | ||
| 97 | explanation => EXPL, | ||
| 98 | policy => __PACKAGE__, | ||
| 99 | }; | ||
| 100 | if (!$is_in_do) { | ||
| 101 | push @violations, $previous_violation; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | return \@violations; | ||
| 109 | } | ||
| 110 | |||
| 111 | # TODO support post conditional notation | ||
| 112 | |||
| 113 | 1; | ||
| 114 | |||