-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to filter Vale JSON output using rule precedence
Signed-off-by: Jack Baldry <[email protected]>
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use JSON; | ||
|
||
sub print_usage { | ||
print "Reads JSON formatted error messages from standard input and filters them based on rule precedence.\n"; | ||
print "\n"; | ||
print "Options:\n"; | ||
print " --help Show this help message\n"; | ||
} | ||
|
||
if ($#ARGV >= 0 ) { | ||
print_usage(); | ||
|
||
exit 2; | ||
} | ||
|
||
my %rules_precedence = ( | ||
'Grafana.ProductPossessives' => 1, | ||
'Grafana.WordList' => 2, | ||
'Grafana.Spelling' => 3, | ||
); | ||
|
||
my %filtered_errors; | ||
|
||
while (my $line = <STDIN>) { | ||
chomp $line; | ||
my $error = decode_json($line); | ||
|
||
# Each error has the rule name in square brackets at the beginning of the message. | ||
# For example: {"message": "[Grafana.Spelling] ..." ...} | ||
my ($rule_name) = $error->{message} =~ /^\[(.*?)\]/; | ||
$error->{rule_name} = $rule_name; | ||
|
||
# Locations are unique by PATH:LINE:COLUMN. | ||
my $location_key = join(':', $error->{location}{path}, $error->{location}{range}{start}{line}, $error->{location}{range}{start}{column}); | ||
|
||
if (!exists $filtered_errors{$location_key} || $rules_precedence{$error->{rule_name}} < $rules_precedence{$filtered_errors{$location_key}->{rule_name}}) { | ||
$filtered_errors{$location_key} = $error; | ||
} | ||
} | ||
|
||
foreach my $error (values %filtered_errors) { | ||
delete $error->{rule_name}; | ||
print encode_json($error) . "\n"; | ||
} |