Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Community::Prototypes can detect explicit :prototype #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 40 additions & 22 deletions lib/Perl/Critic/Policy/Community/Prototypes.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,46 @@ sub default_themes { 'community' }
sub applies_to { 'PPI::Document' }

sub violates {
my ($self, $elem) = @_;

# Check if signatures are enabled
my $includes = $elem->find('PPI::Statement::Include') || [];
foreach my $include (@$includes) {
next unless $include->type eq 'use';
return () if $include->pragma eq 'feature' and $include =~ m/\bsignatures\b/;
return () if $include->pragma eq 'experimental' and $include =~ m/\bsignatures\b/;
return () if $include->module eq 'Mojo::Base' and $include =~ m/-signatures\b/;
return () if $include->module eq 'Mojolicious::Lite' and $include =~ m/-signatures\b/;
return () if exists $self->{_signature_enablers}{$include->module};
}

my $prototypes = $elem->find('PPI::Token::Prototype') || [];
my @violations;
foreach my $prototype (@$prototypes) {
# Empty prototypes and prototypes containing & can be useful
next if $prototype->prototype eq '' or $prototype->prototype =~ /&/;
push @violations, $self->violation(DESC, EXPL, $prototype);
}

return @violations;
my ($self, $elem) = @_;

my $use_signatures;

# Check if signatures are enabled
my $includes = $elem->find('PPI::Statement::Include') || [];
foreach my $include (@$includes) {
next unless $include->type eq 'use';
$use_signatures = 1 if $include->pragma eq 'feature' and $include =~ m/\bsignatures\b/;
$use_signatures = 1 if $include->pragma eq 'experimental' and $include =~ m/\bsignatures\b/;
$use_signatures = 1 if $include->module eq 'Mojo::Base' and $include =~ m/-signatures\b/;
$use_signatures = 1 if $include->module eq 'Mojolicious::Lite' and $include =~ m/-signatures\b/;
$use_signatures = 1 if exists $self->{_signature_enablers}{ $include->module };
}

my @violations;

if ( !$use_signatures ) {
my $prototypes = $elem->find('PPI::Token::Prototype') || [];
foreach my $prototype (@$prototypes) {

# Empty prototypes and prototypes containing & can be useful
next if $prototype->prototype eq '' or $prototype->prototype =~ /&/;
push @violations, $self->violation( DESC, EXPL, $prototype );
}
}

my $subs = $elem->find('PPI::Statement::Sub') || [];

foreach my $sub (@$subs) {
my $attributes = $sub->find('PPI::Token::Attribute') || [];
foreach my $attr (@$attributes) {
my $c = $attr->content;
next unless defined $c && $c =~ qr{^\Qprototype(\E};
push @violations, $self->violation( DESC, EXPL, $attr );
last;
}
}

return @violations;
}

1;
Expand Down
20 changes: 20 additions & 0 deletions t/Community/Prototypes.run
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,23 @@ sub foo () { ... }
## cut

sub foo (&;@) { ... }

## name ExplicitPrototypes
## failures 1
## cut

sub foo : prototype($$) { ... }

## name ExplicitPrototypesWithSig
## failures 1
## cut

use experimental 'signatures';
sub foo : prototype($$) { ... }

## name MojoSignaturePrototypes
## failures 1
## cut

use Mojo::Base -base, -signatures;
sub bar : prototype($@) { ... }