distrolint/Check/VersionVar.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod n/a totaltotal 0.0% 0 / 60 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
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::VersionVar 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 no perl file sets 'our \$VERSION'";
17 use constant SORT => 12;
18  
19 =head1 NAME
20  
21 C<App::perl::distrolint::Check::VersionVar> - check that no file sets C<our $VERSION>
22  
23 =head1 DESCRIPTION
24  
25 This checks that no Perl source file contains a statement that would directly
26 affect a package variable called C<$VERSION>; in effect ensuring that any
27 module version numbers are set only with the new C<package NAME VERSION>
28 syntax of Perl v5.14.
29 =cut
30  
31 method run ( $app )
32 {
33 return $self->run_for_each_perl_file( check_file => );
34 }
35  
36 method check_file ( $file )
37 {
38 my $tree = $self->parse_perl_file( $file );
39  
40 return $self->walk_each_statement( $tree->root_node, check_statement => $file );
41 }
42  
43 method check_statement ( $node, $file )
44 {
45 my ( $expr ) = $node->child_nodes;
46 return 1 unless $expr->type eq "assignment_expression";
47  
48 my $lhs = $expr->child_by_field_name( "left" );
49  
50 my @vars;
51  
52 if( $lhs->type eq "variable_declaration" ) {
53 if( my $var = $lhs->try_child_by_field_name( "variable" ) ) {
54 @vars = ( $var );
55 }
56 elsif( my $varlist = $lhs->try_child_by_field_name( 'variables' ) ) {
57 foreach my $var ( $varlist->child_nodes ) {
58 push @vars, $var if $var->type eq "scalar";
59 }
60 }
61 else {
62 return 1;
63 }
64 }
65 elsif( $lhs->type eq "scalar" ) {
66 @vars = ( $lhs );
67 }
68 else {
69 return 1;
70 }
71  
72 foreach my $var ( @vars ) {
73 next unless $var->text eq '$VERSION';
74  
75 App->diag( App->format_file( $file, $node->start_row + 1 ), " has an assignment to ", App->format_literal( '$VERSION' ) );
76 return 0;
77 }
78  
79 return 1;
80 }
81  
82 =head1 AUTHOR
83  
84 Paul Evans <leonerd@leonerd.org.uk>
85  
86 =cut
87  
88 0x55AA;