| 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, 2023 -- 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::UseVERSION 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 Text::Treesitter 0.07; # child_by_field_name | ||
| 15 | |||
| 16 | use App::perl::distrolint::Config; | ||
| 17 | |||
| 18 | use constant DESC => "check that every perl file contains 'use VERSION'"; | ||
| 19 | use constant SORT => 10; | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | C<App::perl::distrolint::Check::UseVERSION> - check that every perl file contains C<use VERSION> | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This checks that every Perl source code file contains a C<use VERSION> | ||
| 28 | declaration as its first significant statement. A C<package> statement is | ||
| 29 | permitted before this, but no other code is allowed. | ||
| 30 | |||
| 31 | =head1 CONFIGURATION | ||
| 32 | |||
| 33 | =for highlighter language=ini | ||
| 34 | |||
| 35 | The following extra configuration may be added to the C<[check UseVERSION]> | ||
| 36 | section of F<distrolint.ini>: | ||
| 37 | |||
| 38 | =head2 min_version | ||
| 39 | |||
| 40 | min_version = v5.NN | ||
| 41 | |||
| 42 | If present, the C<use VERSION> declaration at the start of every checked Perl | ||
| 43 | source file must be at least this given version. If a version number lower is | ||
| 44 | found then the check fails. | ||
| 45 | |||
| 46 | The minimum version part of the check does not apply to F<Build.PL> or | ||
| 47 | F<Makefile.PL> as those files should remain runnable by any version of Perl. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | method run ( $app ) | ||
| 52 | { | ||
| 53 | return $self->run_for_each_perl_file( check_file => ); | ||
| 54 | } | ||
| 55 | |||
| 56 | method check_file ( $file ) | ||
| 57 | { | ||
| 58 | my $tree = $self->parse_perl_file( $file ); | ||
| 59 | |||
| 60 | my $min_version = App::perl::distrolint::Config->check_config( | ||
| 61 | $self, "min_version", undef | ||
| 62 | ); | ||
| 63 | defined $min_version and $min_version = version->new( $min_version ); | ||
| 64 | |||
| 65 | my $ok = 1; | ||
| 66 | my $has_use_version; | ||
| 67 | |||
| 68 | # Not using walk_each_statement because we only care about toplevel | ||
| 69 | # statements | ||
| 70 | |||
| 71 | foreach my $node ( $tree->root_node->child_nodes ) { | ||
| 72 | next if $node->is_extra; | ||
| 73 | |||
| 74 | if( $node->is_named and $node->type eq "use_version_statement" ) { | ||
| 75 | my $version_node = $node->child_by_field_name( 'version' ); | ||
| 76 | |||
| 77 | if( defined $min_version ) { | ||
| 78 | my $version = version->new( $version_node->text ); | ||
| 79 | |||
| 80 | if( $version lt $min_version and not( | ||
| 81 | $file eq "Build.PL" or $file eq "Makefile.PL" ) ) { | ||
| 82 | App->diag( App->format_file( $file, $version_node->start_row + 1 ), " has a 'use VERSION' of only $version" ); | ||
| 83 | $ok = 0; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | $has_use_version = 1; | ||
| 88 | next; | ||
| 89 | } | ||
| 90 | # We permit package statements | ||
| 91 | elsif( $node->is_named and $node->type eq "package_statement" ) { | ||
| 92 | next; | ||
| 93 | } | ||
| 94 | |||
| 95 | if( !$has_use_version ) { | ||
| 96 | App->diag( App->format_file( $file, $node->start_row + 1 ), " has a statement before use VERSION" ); | ||
| 97 | $ok = 0; | ||
| 98 | last; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | return $ok; | ||
| 103 | } | ||
| 104 | |||
| 105 | =head1 AUTHOR | ||
| 106 | |||
| 107 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | 0x55AA; | ||