| 1 | package Perl::Critic::Policy::RegularExpressions::ProhibitEscapedMetacharacters; | ||||||||||
| 2 | |||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 40 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 40 | ▶ | use Perl::Critic::Utils qw( :severities hashify ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 40 | our $VERSION = '1.156'; | |||||||||
| 13 | |||||||||||
| 14 | #----------------------------------------------------------------------------- | ||||||||||
| 15 | |||||||||||
| 16 | 40 | Readonly::Scalar my $DESC => q{Use character classes for literal metachars instead of escapes}; | |||||||||
| 17 | 40 | Readonly::Scalar my $EXPL => [247]; | |||||||||
| 18 | |||||||||||
| 19 | 40 | Readonly::Hash my %REGEXP_METACHARS => hashify(split / /xms, '{ } ( ) . * + ? |'); | |||||||||
| 20 | |||||||||||
| 21 | #----------------------------------------------------------------------------- | ||||||||||
| 22 | |||||||||||
| 23 | 90 | ▶ | sub supported_parameters { return qw() } | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 24 | 74 | ▶ | sub default_severity { return $SEVERITY_LOWEST } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 25 | 83 | ▶ | sub default_themes { return qw( core pbp cosmetic ) } | ||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 26 | 31 | ▶ | sub applies_to { return qw(PPI::Token::Regexp::Match | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 27 | PPI::Token::Regexp::Substitute | ||||||||||
| 28 | PPI::Token::QuoteLike::Regexp) } | ||||||||||
| 29 | |||||||||||
| 30 | #----------------------------------------------------------------------------- | ||||||||||
| 31 | |||||||||||
| 32 | 1 | ▶ | sub violates { | ||||||||
|
Subroutine
violates: called CC=9 SCAR=41.6
Pod
documented
| |||||||||||
| 33 | 1 | my ( $self, $elem, $document ) = @_; | |||||||||
| 34 | |||||||||||
| 35 | # optimization: don't bother parsing the regexp if there are no escapes | ||||||||||
| 36 | 1 | ▶ | return if $elem !~ m/\\/xms; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | my $re = $document->ppix_regexp_from_element( $elem ) or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 0 | ▶ | $re->failures() and return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | ▶ | my $qr = $re->regular_expression() or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | |||||||||||
| 42 | 0 | ▶ | my $exacts = $qr->find( 'PPIx::Regexp::Token::Literal' ) or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | foreach my $exact( @{ $exacts } ) { | |||||||||
| 44 | 0 | ▶ | $exact->content() =~ m/ \\ ( . ) /xms or next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | ▶ | return $self->violation( $DESC, $EXPL, $elem ) if $REGEXP_METACHARS{$1}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 0 | return; # OK | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 40 | 1; | |||||||||
| 52 | |||||||||||
| 53 | __END__ | ||||||||||