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