|
| 1 | +use v6; |
| 2 | +use Test; |
| 3 | +use lib 'lib'; |
| 4 | + |
| 5 | +=begin overview |
| 6 | +
|
| 7 | +Check for duplicate words in documentation. |
| 8 | +
|
| 9 | +Ignore case, ignore implicit code, and explicit C<=begin code> blocks. |
| 10 | +
|
| 11 | +Unless we're on a line with a pod marker, save the last word and compare it to |
| 12 | +the next line as well. |
| 13 | +
|
| 14 | +Allow a few well known duplicates, like 'long long' |
| 15 | +
|
| 16 | +=end overview |
| 17 | + |
| 18 | +my @files; |
| 19 | +my $safe-dups = Set.new(<method long>); # Allow these dupes |
| 20 | + |
| 21 | +if @*ARGS { |
| 22 | + @files = @*ARGS; |
| 23 | +} else { |
| 24 | + for qx<git ls-files>.lines -> $file { |
| 25 | + next unless $file ~~ / '.' ('pod6'|'md') $/; |
| 26 | + push @files, $file; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +plan +@files; |
| 31 | + |
| 32 | +enum file-type <pod6 plain>; |
| 33 | + |
| 34 | +for @files -> $file { |
| 35 | + my $type = $file ~~ / '.pod6' $/ ?? pod6 !! plain; |
| 36 | + |
| 37 | + my @dupes; |
| 38 | + my $line-num = 0; |
| 39 | + my $last-word = ""; # Keep track of the last word on a line (unless it's a pod directive) |
| 40 | + my $in-code = False; |
| 41 | + for $file.IO.lines -> $line is copy { |
| 42 | + $line-num++; |
| 43 | + next if $type == pod6 && $line ~~ /^ ' ' /; |
| 44 | + my $is-pod = $line ~~ /^ '=' /; |
| 45 | + if !$in-code && $line ~~ /^ '=begin code' / { |
| 46 | + $in-code = True; |
| 47 | + } elsif $in-code && $line ~~ /^ '=end code' / { |
| 48 | + $in-code = False; |
| 49 | + } |
| 50 | + |
| 51 | + $line = $last-word ~ " " ~ $line; |
| 52 | + $last-word = ""; |
| 53 | + |
| 54 | + next if $in-code; |
| 55 | + |
| 56 | + my @line-dupes = ($line ~~ m:g/:i << (<alpha>+) >> \s+ << $0 >> /).map(~*[0]); |
| 57 | + for @line-dupes -> $dupe { |
| 58 | + next if $safe-dups ∋ ~$dupe[0]; |
| 59 | + @dupes.push: "“" ~ $dupe[0] ~ "” on line $line-num"; |
| 60 | + } |
| 61 | + |
| 62 | + next if $is-pod; |
| 63 | + $line ~~ m/ << (<alpha>+) \s* $/; |
| 64 | + if ?$/ { |
| 65 | + $last-word = ~$0; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + my $message = "$file has duplicate words"; |
| 70 | + if @dupes { |
| 71 | + is @dupes.join("\n"), '', $message; |
| 72 | + } else { |
| 73 | + pass $message; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +# vim: expandtab shiftwidth=4 ft=perl6 |
0 commit comments