| 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::StrictAndWarnings 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 constant DESC => "check that every perl file sets strict and warnings"; | ||
| 17 | use constant SORT => 11; | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | C<App::perl::distrolint::Check::StrictAndWarnings> - check that every perl file sets the C<strict> and C<warnings> pragmata | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This checks that for every Perl source code file, there are no significant | ||
| 26 | statements before both the C<strict> and C<warnings> pragmata are in effect. | ||
| 27 | |||
| 28 | A "significant statement" is considered to be most statements, with the | ||
| 29 | exception of any C<package> or C<use> statement, as these typically appear | ||
| 30 | right at the top of the file. | ||
| 31 | |||
| 32 | Either a C<use strict> or a C<use VERSION> with a version number set to v5.12 | ||
| 33 | or higher are considered to enable C<strict>. | ||
| 34 | |||
| 35 | Either a C<use warning> or a C<use VERSION> with a version number set to v5.35 | ||
| 36 | or higher are considered to enable C<warnings>. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | method run ( $app ) | ||
| 41 | { | ||
| 42 | return $self->run_for_each_perl_file( check_file => ); | ||
| 43 | } | ||
| 44 | |||
| 45 | method check_file ( $file ) | ||
| 46 | { | ||
| 47 | my $tree = $self->parse_perl_file( $file ); | ||
| 48 | |||
| 49 | my $ok = 1; | ||
| 50 | my $has_strict; | ||
| 51 | my $has_warnings; | ||
| 52 | |||
| 53 | # Not using walk_each_statement because we only care about toplevel | ||
| 54 | # statements | ||
| 55 | |||
| 56 | foreach my $node ( $tree->root_node->child_nodes ) { | ||
| 57 | next if $node->is_extra; | ||
| 58 | |||
| 59 | if( $node->is_named and $node->type eq "use_version_statement" ) { | ||
| 60 | my $version = version->new( $node->child_by_field_name( "version" )->text ); | ||
| 61 | |||
| 62 | $has_strict = 1 if $version ge v5.12; | ||
| 63 | $has_warnings = 1 if $version ge v5.35; | ||
| 64 | |||
| 65 | next; | ||
| 66 | } | ||
| 67 | # We permit package statements | ||
| 68 | elsif( $node->is_named and $node->type eq "package_statement" ) { | ||
| 69 | next; | ||
| 70 | } | ||
| 71 | elsif( $node->is_named and $node->type eq "use_statement" ) { | ||
| 72 | my $module = $node->child_by_field_name( "module" )->text; | ||
| 73 | |||
| 74 | # TODO: Even use strict (); would keep this happy | ||
| 75 | $has_strict = 1, next if $module eq "strict"; | ||
| 76 | $has_warnings = 1, next if $module eq "warnings"; | ||
| 77 | |||
| 78 | # We permit any other use statement | ||
| 79 | next; | ||
| 80 | } | ||
| 81 | |||
| 82 | if( !$has_strict or !$has_warnings ) { | ||
| 83 | my $line = $node->start_row + 1; | ||
| 84 | |||
| 85 | App->diag( App->format_file( $file, $line ), " has a statement before use strict" ) if !$has_strict; | ||
| 86 | App->diag( App->format_file( $file, $line ), " has a statement before use warnings" ) if !$has_warnings; | ||
| 87 | $ok = 0; | ||
| 88 | last; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | return $ok; | ||
| 93 | } | ||
| 94 | |||
| 95 | =head1 AUTHOR | ||
| 96 | |||
| 97 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | 0x55AA; | ||