Skip to content

Commit

Permalink
handle nested subs
Browse files Browse the repository at this point in the history
  • Loading branch information
plicease committed Dec 27, 2024
1 parent 0557542 commit be74c65
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Revision history for {{$dist->name}}

{{$NEXT}}
- (ProhibitSignaturesAndAtUnderscore) Do not detect @_ inside nested sub (gh#10, gh#11)

0.08 2024-12-27 09:48:41 -0700
- Documentation fixes and improvements.
- More accurately detect signatures for Policy Plicease::ProhibitSignaturesAndAtUnderscore (MITHALDU++, gh#9)
- (ProhibitSignaturesAndAtUnderscore) More accurately detect signatures (MITHALDU++, gh#9)

0.07 2024-05-29 20:01:09 -0600
- (ProhibitUnicodeDigitInRegexp) Treat digit character class /[:digit:]/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ sub violates {
foreach my $sub (@$subs) {
next unless( $PPI::Document::VERSION > 1.279 ?
@{$sub->find('PPI::Structure::Signature')} : defined $sub->prototype );
my $symbols = $sub->find('PPI::Token::Symbol') || [];
foreach my $symbol (@$symbols) {
next unless $symbol->symbol eq '@_';

foreach my $symbol ( _recurse($sub->schildren) ) {
push @violations, $self->violation(DESC, EXPL, $symbol);
}
}
Expand All @@ -97,4 +96,23 @@ sub violates {
return @violations;
}

# since PPI doesn't detect anonymous subroutines...
# look to ignore a PPI::Token::Word with `sub` followed by sibling PPI::Structure::Block

sub _recurse {
my @ret;
my(@children) = @_;
for my $i (0..$#children) {
next if $children[$i]->isa('PPI::Statement::Sub');
next if $i >= 1 && $children[$i]->isa('PPI::Structure::Block') && $children[$i-1]->isa('PPI::Token::Word') && $children[$i-1]->literal eq 'sub';

if($children[$i]->isa('PPI::Token::Symbol') && $children[$i]->symbol eq '@_') {
push @ret, $children[$i];
} elsif($children[$i]->can('schildren')) {
push @ret, _recurse($children[$i]->schildren);
}
}
return @ret;
}

1;
20 changes: 20 additions & 0 deletions t/Plicease/ProhibitSignaturesAndAtUnderscore.run
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,23 @@ sub foo ($a) {
sub foo ($$) {
my @x = @_;
}

## name code ref inside sub with sig
## failures 0
## cut

use experimental qw( signatures );

sub foo ($a) {
return sub { return @_ }
}

## name code ref inside sub with sig
## failures 0
## cut

use experimental qw( signatures );

sub foo ($a) {
sub bar { return @_ }
}

0 comments on commit be74c65

Please sign in to comment.