Skip to content

Commit

Permalink
Add script to filter Vale JSON output using rule precedence
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Baldry <[email protected]>
  • Loading branch information
jdbaldry committed Jan 9, 2025
1 parent 7ed0517 commit abbef5c
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions scripts/inhibit-rules.pl
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";
}

0 comments on commit abbef5c

Please sign in to comment.