« blib/lib/Perl/Lint/Policy/Variables/ProhibitPackageVars.pm
↑ Summary
blib/lib/Perl/Lint/Policy/NamingConventions/Capitalization.pm »
| 1 | package Perl::Lint; | ||
| 2 | 8 | ▶ | use 5.010001; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 8 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 8 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 8 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 8 | ▶ | use Compiler::Lexer; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Module::Pluggable; | ||
| 8 | use Module::Load; | ||
| 9 | |||
| 10 | our $VERSION = "0.23"; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my ($class, $args) = @_; | ||
| 14 | |||
| 15 | my @ignores; | ||
| 16 | |||
| 17 | if (my $ignores = $args->{ignore}) { | ||
| 18 | if (ref $ignores ne 'ARRAY') { | ||
| 19 | Carp::croak "`ignore` must be array reference"; | ||
| 20 | } | ||
| 21 | |||
| 22 | push @ignores, map {"Perl::Lint::Policy::$_"} @$ignores; | ||
| 23 | } | ||
| 24 | |||
| 25 | if (my $filters = $args->{filter}) { | ||
| 26 | if (ref $filters ne 'ARRAY') { | ||
| 27 | Carp::croak "`filter` must be array reference"; | ||
| 28 | } | ||
| 29 | |||
| 30 | for my $filter (@$filters) { | ||
| 31 | my $filter_package = "Perl::Lint::Filter::$filter"; | ||
| 32 | load $filter_package; | ||
| 33 | |||
| 34 | push @ignores, map {"Perl::Lint::Policy::$_"} @{$filter_package->filter}; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | Module::Pluggable->import( | ||
| 39 | search_path => 'Perl::Lint::Policy', | ||
| 40 | require => 1, | ||
| 41 | inner => 0, | ||
| 42 | except => [@ignores], | ||
| 43 | ); | ||
| 44 | my @site_policies = plugins(); # Exported by Module::Pluggable | ||
| 45 | |||
| 46 | # TODO add mechanism to add extend policies | ||
| 47 | |||
| 48 | bless { | ||
| 49 | args => $args, | ||
| 50 | site_policies => \@site_policies, | ||
| 51 | }, $class; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub lint { | ||
| 55 | my ($self, $files) = @_; | ||
| 56 | |||
| 57 | my @files = ($files); # when scalar value | ||
| 58 | if (my $ref = ref $files) { | ||
| 59 | if ($ref ne 'ARRAY') { | ||
| 60 | Carp::croak("Argument of files expects scalar value or array reference"); | ||
| 61 | } | ||
| 62 | @files = @$files; | ||
| 63 | } | ||
| 64 | |||
| 65 | my @violations; | ||
| 66 | for my $file (@files) { | ||
| 67 | open my $fh, '<', $file or die "Cannnot open $file: $!"; | ||
| 68 | my $src = do { local $/; <$fh> }; | ||
| 69 | |||
| 70 | push @violations, @{$self->_lint($src, $file)}; | ||
| 71 | } | ||
| 72 | |||
| 73 | return \@violations; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub lint_string { | ||
| 77 | my ($self, $src) = @_; | ||
| 78 | |||
| 79 | return $self->_lint($src); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub _lint { | ||
| 83 | my ($self, $src, $file) = @_; | ||
| 84 | |||
| 85 | if (!defined $src || $src eq '') { | ||
| 86 | return []; | ||
| 87 | } | ||
| 88 | |||
| 89 | my $args = $self->{args}; | ||
| 90 | |||
| 91 | my $lexer = Compiler::Lexer->new($file); | ||
| 92 | my $tokens = $lexer->tokenize($src); | ||
| 93 | |||
| 94 | # list `no lint` lines | ||
| 95 | # TODO improve performance | ||
| 96 | my %no_lint_lines = (); | ||
| 97 | my %used_no_lint_lines = (); | ||
| 98 | my $line_num = 1; | ||
| 99 | for my $line (split /\r?\n/, $src) { | ||
| 100 | $line =~ s/"(?:[^"]*?#[^"]*?)*"//g; | ||
| 101 | $line =~ s/'(?:[^']*?#[^']*?)*'//g; | ||
| 102 | $line =~ s/\[(?:[^\[]*?#[^\]]*?)*\]//g; | ||
| 103 | if ($line =~ /(#.+)?##\s*no\slint(?:\s+(?:qw)?([(][^)]*[)]|"[^"]*"|'[^']*'))?/) { | ||
| 104 | next if $1; # Already commented out at before | ||
| 105 | |||
| 106 | my $annotations = {}; | ||
| 107 | if ($2) { | ||
| 108 | my $annotation = substr $2, 1, -1; | ||
| 109 | $annotations = {map {$_ => 1} grep {$_} split /[, ]/, $annotation}; | ||
| 110 | } | ||
| 111 | $no_lint_lines{$line_num} = $annotations; | ||
| 112 | } | ||
| 113 | $line_num++; | ||
| 114 | } | ||
| 115 | |||
| 116 | my $prohibit_useless_no_lint_policy; | ||
| 117 | |||
| 118 | my @violations; | ||
| 119 | for my $policy (@{$self->{site_policies}}) { | ||
| 120 | if ($policy eq 'Perl::Lint::Policy::Miscellanea::ProhibitUselessNoLint') { | ||
| 121 | $prohibit_useless_no_lint_policy = $policy; | ||
| 122 | next; | ||
| 123 | } | ||
| 124 | |||
| 125 | if ($policy eq 'Perl::Lint::Policy::Miscellanea::ProhibitUnrestrictedNoLint') { | ||
| 126 | push @violations, @{$policy->evaluate($file, $tokens, $src, $args, \%no_lint_lines)}; | ||
| 127 | next; | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | for my $violation (@{$policy->evaluate($file, $tokens, $src, $args)}) { | ||
| 132 | my $violation_line = $violation->{line}; | ||
| 133 | my $no_lint = $no_lint_lines{$violation_line}; | ||
| 134 | if (!$no_lint || (keys %$no_lint > 0 && !$no_lint->{(split /::/, $violation->{policy})[-1]})) { | ||
| 135 | push @violations, $violation; | ||
| 136 | } | ||
| 137 | $used_no_lint_lines{$violation_line} = 1; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | if ($prohibit_useless_no_lint_policy) { | ||
| 142 | push @violations, | ||
| 143 | @{$prohibit_useless_no_lint_policy->evaluate($file, \%no_lint_lines, \%used_no_lint_lines)}; | ||
| 144 | } | ||
| 145 | |||
| 146 | return \@violations; | ||
| 147 | } | ||
| 148 | |||
| 149 | 1; | ||
| 150 | |||
| 151 | __END__ | ||