distrolint/Check/PostfixDeref.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« distrolint/Check/DeprecatedFeatures.pm ↑ Summary distrolint/Check/StrictAndWarnings.pm »
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, 2026 -- 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::PostfixDeref 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 List::Util 1.29 qw( any first );
15  
16 use Text::Treesitter 0.07; # child_by_field_name
17  
18 use constant DESC => "check that postfix deref is used if enabled";
19 use constant SORT => 25;
20  
21 =head1 NAME
22  
23 C<App::perl::distrolint::Check::PostfixDeref> - check that postfix dereference syntax is used
24  
25 =head1 DESCRIPTION
26  
27 This checks that for every Perl source file in F<lib/>, that any dereference
28 expression made inside a scope that has the C<postderef> feature enabled is
29 actually written using postfix dereference syntax. That said, it fails if a
30 dereference expression using the older circumfix-style (such as C<@{ ... }>)
31 is found, if it lies inside a scope that specifically enables the feature.
32  
33 This checker does not complain about circumfix dereference operators being
34 applied to string literal expressions, such as C<@{"This::Module::ISA"}>.
35 While technically it could be written using postfix-deref, this style is often
36 used for symbolic references, and the distinct visual style helps distinguish
37 them from regular data structure operations.
38  
39 =cut
40  
41 method run ( $app )
42 {
43 return $self->run_for_each_perl_file( check_file => );
44 }
45  
46 my $QUERY = <<'EOF';
47 (use_version_statement)
48 @use_version
49  
50 (use_statement
51 module: _ @module (#eq? @module "feature"))
52 @use_module
53  
54 [
55 (scalar (varname (block (expression_statement) @expr)))
56 (array (varname (block (expression_statement) @expr)))
57 (hash (varname (block (expression_statement) @expr)))
58 ] @deref
59 EOF
60  
61 method check_file ( $file )
62 {
63 my $tree = $self->parse_perl_file( $file );
64  
65 my $ok = 1;
66 $self->walk_each_scoped_query_match( $QUERY, $tree->root_node, method ( $capture, $context ) {
67 my $node;
68 if( $node = $capture->{use_version} ) {
69 my $version = version->new( $node->child_by_field_name( 'version' )->text );
70 # 'use VERSION' doesn't actually enable the feature, but if the code is
71 # asking for v5.24 or above, then the feature will always be available
72 $context->{use_feature_postderef} = ( $version ge v5.24 );
73 }
74 elsif( $node = $capture->{use_module} ) {
75 my $sense = ( $node->child_nodes )[0]->type eq "use";
76 my @features = $self->extract_use_module_imports( $node );
77  
78 $context->{use_feature_postderef} = $sense
79 if any { $_ eq "postderef" } @features;
80 }
81 elsif( $node = $capture->{deref} ) {
82 my $type = uc $node->type;
83 my ( $expr ) = $capture->{expr}->child_nodes;
84 # If the expression is a string-literal or interpolated
85 # string-literal, we think that's a symbolic reference and exempt
86 my $is_symref = $expr->type eq "string_literal" ||
87 $expr->type eq "interpolated_string_literal";
88 if( $context->{use_feature_postderef} and !$is_symref ) {
89 App->diag( App->format_file( $file, $node->start_row + 1 ), " dereferences $type without postfix-deref" );
90 $ok = 0;
91 }
92 }
93 else {
94 warn "TODO: Check <@{[ sort keys %$capture ]}> while use=$context->{use_feature_postderef}\n";
95 }
96  
97 return 1; # check all
98 } );
99  
100 return $ok;
101 }
102  
103 =head1 AUTHOR
104  
105 Paul Evans <leonerd@leonerd.org.uk>
106  
107 =cut
108  
109 0x55AA;
« distrolint/Check/DeprecatedFeatures.pm ↑ Summary distrolint/Check/StrictAndWarnings.pm »