Skip to content

Commit

Permalink
Merge pull request #42 from josegomezr/fix_hash_key_quote_check
Browse files Browse the repository at this point in the history
Improve detection of quoted keys in `::Policy::HashKeyQuotes`
  • Loading branch information
mergify[bot] authored Feb 6, 2024
2 parents fbd66e7 + 92a99ac commit 1b5d5a8
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/perlcritic/Perl/Critic/Policy/HashKeyQuotes.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ our $VERSION = '0.0.1';

sub default_severity { return $SEVERITY_HIGH }
sub default_themes { return qw(openqa) }
# we only want the check quoted expressions
sub applies_to { return qw(PPI::Token::Quote::Single PPI::Token::Quote::Double) }

# check that hashes are not overly using quotes
# (os-autoinst coding style)
sub violates ($self, $elem, $document) {
#we only want the check hash keys
return if !is_hash_key($elem);

my $c = $elem->content;
# special characters
return if $c =~ m/[- \/<>.=_:\\\$\|]/;

# skip anything that's not a hash key
return () unless is_hash_key($elem);

my $k = $elem->literal;
# skip anything that has a special symbol in the content
return () unless $k =~ m/^\w+$/;

# report violation
my $desc = q{Hash key with quotes};
my $expl = q{Avoid useless quotes};
my $expl = qq{Avoid useless quotes for key "$k"};
return $self->violation($desc, $expl, $elem);
}

Expand Down

0 comments on commit 1b5d5a8

Please sign in to comment.