| 1 | package Perl::Critic::Policy::RegularExpressions::PreventUselessMetacharacterEscapes; | ||
| 2 | $Perl::Critic::Policy::RegularExpressions::PreventUselessMetacharacterEscapes::VERSION = '1.000'; | ||
| 3 | # ABSTRACT: \Q...\E is for the variable you are interpolating, not for the text around it. | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings FATAL => 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use re '/aa'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 0 | ▶ | use Readonly; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | use Perl::Critic::Utils qw{ :severities :classification :ppi }; | ||
| 15 | use parent qw{Perl::Critic::Policy}; | ||
| 16 | |||
| 17 | |||
| 18 | Readonly::Scalar my $DESC => q{\Q...\E wraps literal text, not just interpolation}; | ||
| 19 | Readonly::Scalar my $EXPL => q{Put \Q...\E around the interpolated value alone; escape your own text yourself}; | ||
| 20 | |||
| 21 | # $name, @name, ${name}, and whatever subscripts or arrows follow -- the parts | ||
| 22 | # of a span whose contents the author cannot see and therefore has to quote. | ||
| 23 | Readonly::Scalar my $INTERPOLATION_RX => qr/ | ||
| 24 | [\$\@] # a sigil | ||
| 25 | (?: \{ \w+ \} | \w+ ) # ${name} or name | ||
| 26 | (?: # then any run of subscripts or arrows | ||
| 27 | -> (?: \[ [^\]]* \] | \{ [^\}]* \} ) | ||
| 28 | | \[ [^\]]* \] | ||
| 29 | | \{ [^\}]* \} | ||
| 30 | )* | ||
| 31 | /x; | ||
| 32 | |||
| 33 | |||
| 34 | sub supported_parameters { | ||
| 35 | return ( | ||
| 36 | { | ||
| 37 | name => 'allow_whitespace', | ||
| 38 | description => 'Allow literal whitespace inside \Q...\E.', | ||
| 39 | default_string => '0', | ||
| 40 | behavior => 'boolean', | ||
| 41 | } | ||
| 42 | ); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub default_severity { return $SEVERITY_MEDIUM } | ||
| 46 | sub default_themes { return qw(bugs maintenance) } | ||
| 47 | |||
| 48 | sub applies_to { | ||
| 49 | return qw{ | ||
| 50 | PPI::Token::Regexp::Match | ||
| 51 | PPI::Token::Regexp::Substitute | ||
| 52 | PPI::Token::QuoteLike::Regexp | ||
| 53 | }; | ||
| 54 | } | ||
| 55 | |||
| 56 | # What each \Q...\E in this string holds, with the interpolations taken out. | ||
| 57 | # A span with no \E runs to the end, which is what perl does with it. | ||
| 58 | sub _literal_remainders { | ||
| 59 | my ( $self, $string ) = @_; | ||
| 60 | |||
| 61 | return () if !defined $string || index( $string, '\\Q' ) < 0; | ||
| 62 | |||
| 63 | my @remainders; | ||
| 64 | while ( $string =~ m/\\Q(.*?)(?:\\E|\z)/gs ) { | ||
| 65 | my $body = $1; | ||
| 66 | |||
| 67 | $body =~ s/$INTERPOLATION_RX//g; | ||
| 68 | $body =~ s/\s+//g if $self->{_allow_whitespace}; | ||
| 69 | |||
| 70 | push @remainders, $body if length $body; | ||
| 71 | } | ||
| 72 | |||
| 73 | return @remainders; | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | sub violates { | ||
| 78 | my ( $self, $elem, undef ) = @_; | ||
| 79 | |||
| 80 | foreach my $half (qw{get_match_string get_substitute_string}) { | ||
| 81 | next if !$elem->can($half); | ||
| 82 | |||
| 83 | my $string = eval { $elem->$half }; | ||
| 84 | return $self->violation( $DESC, $EXPL, $elem ) if $self->_literal_remainders($string); | ||
| 85 | } | ||
| 86 | |||
| 87 | return; | ||
| 88 | } | ||
| 89 | |||
| 90 | 1; | ||
| 91 | |||
| 92 | __END__ | ||