| 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-2026 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.36; | ||
| 7 | use Object::Pad 0.807; | ||
| 8 | |||
| 9 | class App::perl::distrolint::Check::Pod 0.09; | ||
| 10 | |||
| 11 | apply App::perl::distrolint::CheckRole::EachFile; | ||
| 12 | apply App::perl::distrolint::CheckRole::TreeSitterPerl; | ||
| 13 | |||
| 14 | use Text::Treesitter 0.10; # parse_string_range | ||
| 15 | |||
| 16 | use constant DESC => "check that every perl file contains some Pod"; | ||
| 17 | use constant SORT => 30; | ||
| 18 | |||
| 19 | use List::Util qw( any first ); | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | C<App::perl::distrolint::Check::Pod> - check that every Perl source file contains documentation | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This checks that Perl source code files contain at least one block of Pod. | ||
| 28 | |||
| 29 | Unit tests (named F<*.t>) and build-time generated source files (F<*.PL>) are | ||
| 30 | exempt from this check. Files in the F<examples/> directory are also skipped. | ||
| 31 | |||
| 32 | Additionally checks that each of the following C<=head1> sections appear: | ||
| 33 | |||
| 34 | =for highlighter language=pod | ||
| 35 | |||
| 36 | =head1 NAME | ||
| 37 | =head1 DESCRIPTION | ||
| 38 | =head1 AUTHOR | ||
| 39 | |||
| 40 | =for highlighter | ||
| 41 | |||
| 42 | Additional checks are applied to the contents of various C<=head> sections. | ||
| 43 | |||
| 44 | =head1 CONFIGURATION | ||
| 45 | |||
| 46 | =for highlighter language=ini | ||
| 47 | |||
| 48 | The following extra configuration may be added to the C<[check Pod]> | ||
| 49 | section of F<distrolint.ini>: | ||
| 50 | |||
| 51 | =head2 require_highlighter_directive | ||
| 52 | |||
| 53 | require_highlighter_directive = true | ||
| 54 | |||
| 55 | If true, prints a note (not a failure) if any verbatim paragraphs are found | ||
| 56 | before a C<=for highlighter language=...> directive. Defaults to false. | ||
| 57 | |||
| 58 | =head2 require_name_code_wrapped | ||
| 59 | |||
| 60 | require_name_code_wrapped = false | ||
| 61 | |||
| 62 | If true, the C<=head1 NAME> section must name the module wrapped in a | ||
| 63 | C<CE<gt>...E<lt>> wrapping. If false, this is optional (but still permitted). | ||
| 64 | Defaults to true. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | method run ( $app ) | ||
| 69 | { | ||
| 70 | return $self->run_for_each_perl_file( check_file => ); | ||
| 71 | } | ||
| 72 | |||
| 73 | my $HEAD_QUERY = <<'EOF'; | ||
| 74 | (command_paragraph | ||
| 75 | (command) @command | ||
| 76 | _ @content) @para | ||
| 77 | (plain_paragraph | ||
| 78 | _ @content) @para | ||
| 79 | (verbatim_paragraph | ||
| 80 | _ @content) @para | ||
| 81 | EOF | ||
| 82 | |||
| 83 | method check_file ( $file ) | ||
| 84 | { | ||
| 85 | # .t and .PL files don't need Pod | ||
| 86 | return 1 if $file =~ m/\.t$|\.PL$/; | ||
| 87 | # Examples probably not either | ||
| 88 | return 1 if $file =~ m/examples\//; | ||
| 89 | # Anything in t/ is probably internal library or whatever | ||
| 90 | return 1 if $file =~ m(^t/); | ||
| 91 | |||
| 92 | my $tree = $self->parse_perl_file( $file ); | ||
| 93 | |||
| 94 | my @pod_nodes; | ||
| 95 | |||
| 96 | $self->walk_each_query_match( '(pod) @pod', $tree->root_node, method ( $captures ) { | ||
| 97 | push @pod_nodes, $captures->{pod}; | ||
| 98 | } ); | ||
| 99 | |||
| 100 | unless( @pod_nodes ) { | ||
| 101 | App->diag( App->format_file( $file ), " has no Pod" ); | ||
| 102 | return 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | my $require_highlighter_directive = | ||
| 106 | App::perl::distrolint::Config->check_config( $self, "require_highlighter_directive", 0 ); | ||
| 107 | |||
| 108 | state $TSPOD //= Text::Treesitter->new( lang_name => "pod" ); | ||
| 109 | |||
| 110 | my $text = $tree->text; | ||
| 111 | |||
| 112 | my @head1_titles; | ||
| 113 | my $last_head1; | ||
| 114 | my %nodes_per_head1; | ||
| 115 | |||
| 116 | my $ok = 1; | ||
| 117 | |||
| 118 | my $highlighter; | ||
| 119 | |||
| 120 | foreach my $pod ( @pod_nodes ) { | ||
| 121 | my $podtree = $TSPOD->parse_string_range( $text, | ||
| 122 | node => $pod, | ||
| 123 | ); | ||
| 124 | |||
| 125 | # We can't use ->walk_each_query_match because that presumes a query | ||
| 126 | # based on t-s-perl | ||
| 127 | state $QUERY = Text::Treesitter::Query->new( | ||
| 128 | $TSPOD->lang, $HEAD_QUERY | ||
| 129 | ); | ||
| 130 | |||
| 131 | my $qc = Text::Treesitter::QueryCursor->new; | ||
| 132 | $qc->exec( $QUERY, $podtree->root_node ); | ||
| 133 | |||
| 134 | while( my $captures = $qc->next_match_captures ) { | ||
| 135 | my $paratype = $captures->{para}->type; | ||
| 136 | |||
| 137 | my $contentnode = $captures->{content}; | ||
| 138 | my $content = $contentnode->text; | ||
| 139 | $content =~ s/\n// unless $contentnode->type eq "verbatim_paragraph"; | ||
| 140 | |||
| 141 | my $command = $captures->{command} ? $captures->{command}->text : undef; | ||
| 142 | if( defined $command and $command eq "=head1" ) { | ||
| 143 | push @head1_titles, $content; | ||
| 144 | $last_head1 = $content; | ||
| 145 | # Take the final word if it's "... METHODS" or "... FUNCTIONS" | ||
| 146 | $last_head1 = $1 if $last_head1 =~ m/\b(METHODS|FUNCTIONS)$/; | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | push $nodes_per_head1{$last_head1 // ""}->@*, $captures->{para}; | ||
| 150 | |||
| 151 | if( $content =~ m/^\s*TODO\s*$/ ) { | ||
| 152 | App->note( App->format_file( $file, $contentnode->start_row + 1 ), " contains a TODO paragraph in Pod" ); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | if( ( $command // "" ) eq "=head2" and | ||
| 157 | my $meth = $self->can( "check_head2_$last_head1" ) ) { | ||
| 158 | $ok &= $meth->( $self, $file, $contentnode ); | ||
| 159 | } | ||
| 160 | |||
| 161 | if( ( $command // "" ) eq "=for" and $content =~ s/^highlighter// ) { | ||
| 162 | $content =~ s/^\s+//; | ||
| 163 | my @args = split m/\s+/, $content; | ||
| 164 | $args[0] = "language=$args[0]" if @args and $args[0] !~ m/=/; | ||
| 165 | |||
| 166 | $highlighter = ""; # empty string is still defined | ||
| 167 | m/^language=(.*)$/ and $highlighter = $1, last | ||
| 168 | for @args; | ||
| 169 | } | ||
| 170 | |||
| 171 | if( $paratype eq "verbatim_paragraph" ) { | ||
| 172 | if( $require_highlighter_directive and !defined $highlighter ) { | ||
| 173 | App->note( App->format_file( $file, $contentnode->start_row + 1 ), " verbatim paragraph without highlighter config" ); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | foreach my $title (qw( NAME DESCRIPTION AUTHOR )) { | ||
| 180 | unless( any { $_ eq $title } @head1_titles ) { | ||
| 181 | App->diag( App->format_file( $file ), " is missing a '=head1 $title' Pod section" ); | ||
| 182 | return 0; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | foreach my $title ( sort keys %nodes_per_head1 ) { | ||
| 187 | my $meth = $self->can( "check_nodes_$title" ) or | ||
| 188 | next; | ||
| 189 | $meth->( $self, $file, ( $nodes_per_head1{$title} // [] )->@* ) or | ||
| 190 | return 0; | ||
| 191 | } | ||
| 192 | |||
| 193 | return $ok; | ||
| 194 | } | ||
| 195 | |||
| 196 | =head2 Checks on C<NAME> | ||
| 197 | |||
| 198 | After a C<=head1 NAME> there should be exactly one paragraph, and its content | ||
| 199 | should match C<NAME - text>, where C<NAME> should match the module name | ||
| 200 | implied by the file's path, optionally wrapped in C<CE<lt>...E<gt>> formatting. | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | method check_nodes_NAME ( $file, @nodes ) | ||
| 205 | { | ||
| 206 | if( @nodes > 1 ) { | ||
| 207 | App->diag( App->format_file( $file ), " has more than one paragraph under =head1 NAME" ); | ||
| 208 | return 0; | ||
| 209 | } | ||
| 210 | |||
| 211 | my $content = $nodes[0]->text =~ s/\n/ /gr; | ||
| 212 | my $line = $nodes[0]->start_row + 1; | ||
| 213 | |||
| 214 | unless( $content =~ m/^(.*) - (.*)$/ ) { | ||
| 215 | App->diag( App->format_file( $file, $line ), " =head1 NAME section does not look like Package::Name - description" ); | ||
| 216 | return 0; | ||
| 217 | } | ||
| 218 | my ( $pkgname, $description ) = ( $1, $2 ); | ||
| 219 | unless( $pkgname =~ s/^C<(.*)>$/$1/ ) { | ||
| 220 | if( App::perl::distrolint::Config->check_config( $self, "require_name_code_wrapped", 1 ) ) { | ||
| 221 | App->diag( App->format_file( $file, $line ), " =head1 NAME is not wrapped in C<...> tag" ); | ||
| 222 | return 0; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | $file =~ m{^lib/(.*).pm$} or return 1; | ||
| 227 | my $pkgname_from_file = $1 =~ s{/}{::}gr; | ||
| 228 | |||
| 229 | unless( $pkgname eq $pkgname_from_file ) { | ||
| 230 | App->diag( App->format_file( $file, $line ), " =head1 NAME section should start C<$pkgname_from_file> - ..." ); | ||
| 231 | return 0; | ||
| 232 | } | ||
| 233 | |||
| 234 | return 1; | ||
| 235 | } | ||
| 236 | |||
| 237 | =head2 Checks on C<FUNCTIONS> and C<METHODS> | ||
| 238 | |||
| 239 | For every C<=head2> inside C<=head1 FUNCTIONS> or C<=head1 METHODS>, the text | ||
| 240 | is checked to ensure it is a bareword function/method name, optionally | ||
| 241 | followed by other clarifying text after whitespace. | ||
| 242 | |||
| 243 | After every C<=head2> the next paragraph must be a verbatim paragraph, | ||
| 244 | presumed to contain the function's minsynopsis code. The contents of this | ||
| 245 | are also checked, to see that the first line looks like an example calling | ||
| 246 | the named function or method, that ends in a semicolon. | ||
| 247 | |||
| 248 | The function name can optionally be preceeded by a variable assignment to | ||
| 249 | indicate the return value (C<$var = ...> or C<($list, $of, @vars) = ...>), | ||
| 250 | optionally prefixed with C<my>. It can optionally be preceeded by a variable | ||
| 251 | containing the invocant name and a method call arrow (C<< $var->... >>). It | ||
| 252 | can optionally be followed by any other text in parentheses, to indicate the | ||
| 253 | arguments passed. It can optionally use an C<await> expression, used to | ||
| 254 | indicate it is a L<Future>-returning asynchronous function or method. | ||
| 255 | |||
| 256 | E.g. | ||
| 257 | |||
| 258 | =for highlighter language=perl | ||
| 259 | |||
| 260 | funcname; | ||
| 261 | funcname(@args); | ||
| 262 | $self->methodname(@args); | ||
| 263 | $result = funcname(args, here); | ||
| 264 | my ($return, $values) = Some::Package->methodname(some, more, args); | ||
| 265 | my $response = await $client->call; | ||
| 266 | |||
| 267 | =cut | ||
| 268 | |||
| 269 | method check_head2_FUNCTIONS ( $file, $node ) { $self->_check_head2_func( $file, FUNCTIONS => $node ) } | ||
| 270 | method check_head2_METHODS ( $file, $node ) { $self->_check_head2_func( $file, METHODS => $node ) } | ||
| 271 | |||
| 272 | method _check_head2_func ( $file, $head1_title, $node ) | ||
| 273 | { | ||
| 274 | my $text = $node->text; | ||
| 275 | if( $text !~ m/^(\w+)(?:\s+.*)?$/ ) { | ||
| 276 | App->diag( | ||
| 277 | App->format_file( $file, $node->start_row + 1 ), | ||
| 278 | " $head1_title should be =head2 barename; is ", | ||
| 279 | App->format_literal( $text ) ); | ||
| 280 | return 0; | ||
| 281 | } | ||
| 282 | |||
| 283 | return 1; | ||
| 284 | } | ||
| 285 | |||
| 286 | method check_nodes_FUNCTIONS ( $file, @nodes ) { $self->_check_nodes_func( $file, FUNCTIONS => @nodes ); } | ||
| 287 | method check_nodes_METHODS ( $file, @nodes ) { $self->_check_nodes_func( $file, METHODS => @nodes ); } | ||
| 288 | |||
| 289 | method _check_nodes_func ( $file, $head1_title, @nodes ) | ||
| 290 | { | ||
| 291 | my $ok = 1; | ||
| 292 | |||
| 293 | my $last_head2; | ||
| 294 | |||
| 295 | while( @nodes ) { | ||
| 296 | my $node = shift @nodes; | ||
| 297 | my $type = $node->type; | ||
| 298 | |||
| 299 | my $contentnode = first { $_->type eq "content" } $node->child_nodes; | ||
| 300 | |||
| 301 | if( $type eq "command_paragraph" and $node->child_by_field_name( "command" )->text eq "=head2" ) { | ||
| 302 | $last_head2 = $contentnode; | ||
| 303 | my $funcname = ( split m/\s+/, $last_head2->text )[0]; | ||
| 304 | |||
| 305 | # Having just switched to a new head2 we immediately expect a verbatim paragraph | ||
| 306 | |||
| 307 | # Though it's possible we might have more head2s first to give multiple headings | ||
| 308 | while( @nodes and $nodes[0]->type eq "command_paragraph" and | ||
| 309 | $nodes[0]->child_by_field_name( "command" )->text eq "=head2" ) { | ||
| 310 | shift @nodes; | ||
| 311 | } | ||
| 312 | |||
| 313 | $node = shift @nodes; | ||
| 314 | unless( $node and $node->type eq "verbatim_paragraph" ) { | ||
| 315 | App->diag( App->format_file( $file, $last_head2->start_row + 1 ), | ||
| 316 | " =head2 $funcname section should be followed by a verbatim paragraph" ); | ||
| 317 | $ok = 0; | ||
| 318 | next; | ||
| 319 | } | ||
| 320 | |||
| 321 | my $minisynopsis = $node->text; | ||
| 322 | $minisynopsis =~ s/^\s+//gm; | ||
| 323 | |||
| 324 | # Trim trailing comments | ||
| 325 | $minisynopsis =~ s/\s*#.*$//gm; | ||
| 326 | # Trim leading blank lines | ||
| 327 | $minisynopsis =~ s/^\n+//; | ||
| 328 | |||
| 329 | my $VAR = qr/[\$\@\%]\w+/; | ||
| 330 | my $VARS = qr/\(\s*$VAR(?:,\s*$VAR)*(?:,\s*\.\.\.)?\s*\)/; | ||
| 331 | my $INVOCANT = qr/(?:\$\w+|(?:\w+::)*\w+)/; | ||
| 332 | my $match = $minisynopsis =~ m/\A | ||
| 333 | (?:(?:my\s+)?(?:$VAR | $VARS)\s+=\s+)? # var or (var...) = | ||
| 334 | (?:await\s+)? | ||
| 335 | (?:$INVOCANT->)? | ||
| 336 | \Q$funcname\E | ||
| 337 | (?:\(.*\))? # (args) | ||
| 338 | /x; | ||
| 339 | if( !$match ) { | ||
| 340 | App->diag( App->format_file( $file, $node->start_row + 1 ), | ||
| 341 | " minisynopsis should look like [[my] VAR(S) =] [await] [VAR->] $funcname [(ARGS...)];" ); | ||
| 342 | $ok = 0; | ||
| 343 | next; | ||
| 344 | } | ||
| 345 | |||
| 346 | if( $minisynopsis =~ m/->get;$/ ) { | ||
| 347 | App->diag( App->format_file( $file, $node->start_row + 1 ), | ||
| 348 | " minisynopsis should use 'await' expression rather than trailing ->get call" ); | ||
| 349 | } | ||
| 350 | |||
| 351 | if( $minisynopsis !~ m/\A.*;$/sm ) { | ||
| 352 | App->diag( App->format_file( $file, $node->start_row + 1 ), | ||
| 353 | " minisynopsis first line should end with ';'" ); | ||
| 354 | $ok = 0; | ||
| 355 | next; | ||
| 356 | } | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | return $ok; | ||
| 361 | } | ||
| 362 | |||
| 363 | =head1 AUTHOR | ||
| 364 | |||
| 365 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 366 | |||
| 367 | =cut | ||
| 368 | |||
| 369 | 0x55AA; |