| 1 | # You may distribute under the terms of either the GNU General Public License | ||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||
| 3 | # | ||
| 4 | # (C) Paul Evans, 2026 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | 1 | ▶ | use v5.36; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Object::Pad 0.807; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | class App::perl::distrolint::Check::SubSignatures 0.09; | ||
| 10 | |||
| 11 | 1 | ▶ | apply App::perl::distrolint::CheckRole::EachFile; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | apply App::perl::distrolint::CheckRole::TreeSitterPerl; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | use List::Util 1.29 qw( any first ); | ||
| 15 | |||
| 16 | use Text::Treesitter 0.07; # child_by_field_name | ||
| 17 | |||
| 18 | use constant DESC => "check that Perl subroutines use signatures if enabled"; | ||
| 19 | use constant SORT => 25; | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | C<App::perl::distrolint::Check::SubSignatures> - check that Perl subroutines use signatures | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This checks that for every Perl source file in F<lib/>, that any C<sub> or | ||
| 28 | C<method> declarations made inside a scope that has the C<signatures> feature | ||
| 29 | enabled are actually made using signatures. That said, it fails if a | ||
| 30 | subroutine declaration is found that does not have a signature, if it lies | ||
| 31 | inside a scope that specifically enables the feature. | ||
| 32 | |||
| 33 | The C<signatures> feature is considered enabled by a C<use VERSION> | ||
| 34 | declaration of a version C<v5.36> or later, or by a C<use feature> declaration | ||
| 35 | that specifically lists the C<signatures> feature. | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | method run ( $app ) | ||
| 40 | { | ||
| 41 | return $self->run_for_each_perl_file( check_file => ); | ||
| 42 | } | ||
| 43 | |||
| 44 | my $QUERY = <<'EOF'; | ||
| 45 | (use_version_statement) | ||
| 46 | @use_version | ||
| 47 | |||
| 48 | (use_statement | ||
| 49 | module: _ @module (#eq? @module "feature")) | ||
| 50 | @use_module | ||
| 51 | |||
| 52 | (subroutine_declaration_statement (signature) ? @signature) | ||
| 53 | @sub | ||
| 54 | |||
| 55 | (method_declaration_statement (signature) ? @signature) | ||
| 56 | @method | ||
| 57 | |||
| 58 | (anonymous_subroutine_expression (signature) ? @signature) | ||
| 59 | @sub | ||
| 60 | |||
| 61 | (anonymous_method_expression (signature) ? @signature) | ||
| 62 | @method | ||
| 63 | EOF | ||
| 64 | |||
| 65 | my sub proto_looks_like_signature ( $proto ) | ||
| 66 | { | ||
| 67 | # prototype must be wrapped in (...) | ||
| 68 | $proto =~ s/^\((.*)\)$/$1/ or | ||
| 69 | return 0; | ||
| 70 | |||
| 71 | # trim | ||
| 72 | $proto =~ s/^\s+//; | ||
| 73 | $proto =~ s/\s+$//; | ||
| 74 | |||
| 75 | # Empty string would count as a zero-arg signature | ||
| 76 | return 1 if $proto eq ''; | ||
| 77 | |||
| 78 | # A single '$' would count as a single-arg unnamed signature param | ||
| 79 | # A single '@' or '%' would count as a single slurpy unnamed signature param | ||
| 80 | return 1 if $proto eq '$' or $proto eq '@' or $proto eq '%'; | ||
| 81 | |||
| 82 | return 0; | ||
| 83 | } | ||
| 84 | |||
| 85 | method check_file ( $file ) | ||
| 86 | { | ||
| 87 | my $tree = $self->parse_perl_file( $file ); | ||
| 88 | |||
| 89 | my $ok = 1; | ||
| 90 | $self->walk_each_scoped_query_match( $QUERY, $tree->root_node, method ( $capture, $context ) { | ||
| 91 | my $node; | ||
| 92 | if( $node = $capture->{use_version} ) { | ||
| 93 | my $version = version->new( $node->child_by_field_name( 'version' )->text ); | ||
| 94 | $context->{use_feature_signatures} = ( $version ge v5.36 ); | ||
| 95 | } | ||
| 96 | elsif( $node = $capture->{use_module} ) { | ||
| 97 | my $sense = ( $node->child_nodes )[0]->type eq "use"; | ||
| 98 | my @features = $self->extract_use_module_imports( $node ); | ||
| 99 | |||
| 100 | $context->{use_feature_signatures} = $sense | ||
| 101 | if any { $_ eq "signatures" } @features; | ||
| 102 | } | ||
| 103 | elsif( $node = $capture->{sub} or $node = $capture->{method} ) { | ||
| 104 | my $kw = ( $capture->{sub} ) ? "sub" : "method"; | ||
| 105 | my $has_signature = defined $capture->{signature}; | ||
| 106 | # tree-sitter-perl can't tell the difference between prototypes | ||
| 107 | # and signatures | ||
| 108 | if( $context->{use_feature_signatures} and not $has_signature ) { | ||
| 109 | my $proto_node = first { $_->type eq "prototype" } $node->child_nodes; | ||
| 110 | |||
| 111 | $has_signature = proto_looks_like_signature( $proto_node->text ) if $proto_node; | ||
| 112 | } | ||
| 113 | |||
| 114 | if( $context->{use_feature_signatures} and !$has_signature ) { | ||
| 115 | App->diag( App->format_file( $file, $node->start_row + 1 ), " declares a $kw without signature" ); | ||
| 116 | $ok = 0; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | else { | ||
| 120 | my @names = sort keys %$capture; | ||
| 121 | die "TODO: Unsure how to handle captures <@names>\n"; | ||
| 122 | } | ||
| 123 | |||
| 124 | return 1; # check all of them | ||
| 125 | } ); | ||
| 126 | |||
| 127 | return $ok; | ||
| 128 | } | ||
| 129 | |||
| 130 | =head1 AUTHOR | ||
| 131 | |||
| 132 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | 0x55AA; | ||