| 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, 2024 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.36; | ||
| 7 | use Object::Pad 0.807; | ||
| 8 | |||
| 9 | class App::perl::distrolint::Check::PerlVersionVar 0.09; | ||
| 10 | |||
| 11 | apply App::perl::distrolint::CheckRole::EachFile; | ||
| 12 | apply App::perl::distrolint::CheckRole::TreeSitterPerl; | ||
| 13 | |||
| 14 | use Text::Treesitter 0.07; # child_by_field_name | ||
| 15 | |||
| 16 | use constant DESC => "check that perl files use \$] correctly"; | ||
| 17 | use constant SORT => 12; | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | C<App::perl::distrolint::Check::PerlVersionVar> - check for bad use of C<$]> | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | The special Perl variable C<$]> contains the Perl version number, formatted as | ||
| 26 | a regular floating-point integer. Comparisons on this variable should be done | ||
| 27 | using numerical operators (C<< >= >> etc) rather than stringy ones | ||
| 28 | (C<ge> etc). This checker ensures any comparisons use the former kind, and not | ||
| 29 | the latter. | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | method run ( $app ) | ||
| 34 | { | ||
| 35 | return $self->run_for_each_perl_file( check_file => ); | ||
| 36 | } | ||
| 37 | |||
| 38 | # We can't do "either left or right" in tree-sitter queries, but we can do two | ||
| 39 | # separate queries that match each case | ||
| 40 | my $QUERY = <<'EOF'; | ||
| 41 | (relational_expression | ||
| 42 | left: _ @left (#eq? @left "$]") | ||
| 43 | operator: _ @operator (#any-of? @operator "eq" "ne" "lt" "le" "ge" "gt" "cmp")) | ||
| 44 | @expression | ||
| 45 | |||
| 46 | (relational_expression | ||
| 47 | operator: _ @operator (#any-of? @operator "eq" "ne" "lt" "le" "ge" "gt" "cmp") | ||
| 48 | right: _ @right (#eq? @right "$]")) | ||
| 49 | @expression | ||
| 50 | EOF | ||
| 51 | |||
| 52 | method check_file ( $file ) | ||
| 53 | { | ||
| 54 | my $tree = $self->parse_perl_file( $file ); | ||
| 55 | |||
| 56 | return 0 unless $self->walk_each_query_match( $QUERY, $tree->root_node, method ( $captures ) { | ||
| 57 | my $node = $captures->{expression}; | ||
| 58 | |||
| 59 | App->diag( App->format_file( $file, $node->start_row + 1 ), " applies stringy comparison operator to ", App->format_literal( '$]' ) ); | ||
| 60 | } ); | ||
| 61 | } | ||
| 62 | |||
| 63 | =head1 AUTHOR | ||
| 64 | |||
| 65 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | 0x55AA; |