| 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 | use v5.36; | ||
| 7 | use Object::Pad 0.807; | ||
| 8 | |||
| 9 | class App::perl::distrolint::Check::UseUTF8 0.09; | ||
| 10 | |||
| 11 | apply App::perl::distrolint::CheckRole::EachFile; | ||
| 12 | apply App::perl::distrolint::CheckRole::TreeSitterPerl; | ||
| 13 | |||
| 14 | use constant DESC => "check that every perl file sets 'use utf8' if required"; | ||
| 15 | use constant SORT => 11; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | C<App::perl::distrolint::Check::UseUTF8> - check that every perl file sets the C<utf8> pragma if required | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This checks that for every Perl source code file, if it contains any non-ASCII | ||
| 24 | characters then a C<use utf8> statement is in effect. | ||
| 25 | |||
| 26 | The contents of comments and POD sections are ignored, as any non-ASCII | ||
| 27 | characters here will not change meaning in the presence or absence of the | ||
| 28 | C<utf8> pragma. | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | method run ( $app ) | ||
| 33 | { | ||
| 34 | return $self->run_for_each_perl_file( check_file => ); | ||
| 35 | } | ||
| 36 | |||
| 37 | my $EXTRAS_QUERY = <<'EOF'; | ||
| 38 | (comment) @comment | ||
| 39 | (pod) @pod | ||
| 40 | EOF | ||
| 41 | |||
| 42 | method check_file ( $file ) | ||
| 43 | { | ||
| 44 | my $tree = $self->parse_perl_file( $file ); | ||
| 45 | |||
| 46 | my $use_utf8_at; | ||
| 47 | |||
| 48 | my $all_source = $tree->root_node->text; | ||
| 49 | |||
| 50 | # Not using walk_each_statement because we only care about toplevel | ||
| 51 | # statements | ||
| 52 | |||
| 53 | foreach my $node ( $tree->root_node->child_nodes ) { | ||
| 54 | next if $node->is_extra; | ||
| 55 | |||
| 56 | if( $node->is_named and $node->type eq "use_statement" ) { | ||
| 57 | my $module = $node->child_by_field_name( "module" )->text; | ||
| 58 | |||
| 59 | $use_utf8_at = $node->start_byte, next if $module eq "utf8"; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | $self->walk_each_query_match( $EXTRAS_QUERY, $tree->root_node, method ( $captures ) { | ||
| 64 | my ( $node ) = values %$captures; | ||
| 65 | |||
| 66 | my $len = $node->end_byte - $node->start_byte; | ||
| 67 | use bytes; | ||
| 68 | substr( $all_source, $node->start_byte, $len ) = "-" x $len; | ||
| 69 | } ); | ||
| 70 | |||
| 71 | if( !defined $use_utf8_at and | ||
| 72 | $all_source =~ m/[^[:ascii:]]/ ) { | ||
| 73 | App->diag( App->format_file( $file ), " contains non-ASCII characters but no ", App->format_literal( "use utf8" ) ); | ||
| 74 | return 0; | ||
| 75 | } | ||
| 76 | |||
| 77 | if( defined $use_utf8_at and | ||
| 78 | do { use bytes; substr( $all_source, 0, $use_utf8_at ) } =~ m/[^[:ascii:]]/ ) { | ||
| 79 | App->diag( App->format_file( $file ), " contains non-ASCII characters before the ", App->format_literal( "use utf8" ) ); | ||
| 80 | return 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | return 1; | ||
| 84 | } | ||
| 85 | |||
| 86 | =head1 AUTHOR | ||
| 87 | |||
| 88 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | 0x55AA; |