Subject: | ProhibitCommentedOutCode doesn't let you configure your own regex |
I'm copying this from the bug reported at
http://code.google.com/p/perl-critic-bangs/issues/detail?id=1&can=2&q=
Reported by acmoore, Jan 25, 2007
Here's a bug report that I received via email. I'll make a patch and
ask
petdance for a svn commit bit or something.
-Andy
From: oysteto <redacted>
Subject: Possible bug in Bangs::ProhibitCommentedOutCode
To: amoore <redacted>
Date: Sun, 17 Dec 2006 13:37:24 +0100 (CET)
Hi
I tried to use the Bangs::ProhibitCommentedOutCode module, but came
across
some problems when configuring it with in .perlcriticrc.
The problem was that changing the following line in the .perlcriticrc
file
did not have any effect:
coderegex = qr(\$[A-Za-z_].*=/)
After looking in the source I found that the line should rather be
commentedcoderegex = qr(\$[A-Za-z_].*=/)
Changing the line still did not have the desired effect and I think
that
is due to that qr(\$[A-Za-z_].*=/) is interpreted as a string when it
comes from the config file and not as a regex. Changing line 20 in
ProhibitCommentedOutCode.pm to
$self->{_commentedcoderegex} = qr/$config{commentedcoderegex}/;
and the config line to
commentedcoderegex = \$[A-Za-z_].*=
fixed the problem for me. I am not sure it will work when not using a
config file though.
Best Regards
Øystein Torget
Subject: | bangsdiff.txt |
Index: t/commented-out-code.t
===================================================================
--- t/commented-out-code.t (revision 9)
+++ t/commented-out-code.t (working copy)
@@ -1,6 +1,6 @@
use strict;
use warnings;
-use Test::More tests => 1;
+use Test::More tests => 3;
use Perl::Critic::Config;
use Perl::Critic;
@@ -21,4 +21,39 @@
is( pcritique($policy, \$code), 1, $policy);
}
+# pass in the default regex used to look for commented code. This
+# should behave just as though no extra configuration were provided.
+DEFAULTPROFILE: {
+ my $code = <<'END_PERL';
+my $one = 1;
+my $two = '# $foo = "bar"';
+# my $three = 'three';
+# $four is an important variable.
+END_PERL
+ my $policy = 'Bangs::ProhibitCommentedOutCode';
+ my $config = { commentedcoderegex => q(\$[A-Za-z_].*=) };
+
+ is( pcritique( $policy, \$code, $config ), 1, $policy);
+}
+
+
+# To demonstrate that the config file works, change the regex used to
+# look for commented code to only look for variables named 'bang'
+# Bug submitted by Oystein Torget
+CHANGEPROFILE: {
+ my $code = <<'END_PERL';
+my $one = 1;
+my $two = '# $foo = "bar"';
+# my $three = 'three';
+# my $bang = 'three';
+# $four is an important variable.
+END_PERL
+
+ my $policy = 'Bangs::ProhibitCommentedOutCode';
+ my $config = { commentedcoderegex => q(\$bang.*=) };
+
+ is( pcritique( $policy, \$code, $config ), 1, $policy);
+}
+
+
Index: lib/Perl/Critic/Policy/Bangs/ProhibitCommentedOutCode.pm
===================================================================
--- lib/Perl/Critic/Policy/Bangs/ProhibitCommentedOutCode.pm (revision 9)
+++ lib/Perl/Critic/Policy/Bangs/ProhibitCommentedOutCode.pm (working copy)
@@ -17,7 +17,7 @@
# Set commentedcode regex from configuration, if defined.
if ( defined $config{commentedcoderegex} ) {
- $self->{_commentedcoderegex} = $config{commentedcoderegex};
+ $self->{_commentedcoderegex} = qr/$config{commentedcoderegex}/;
}
return $self;
@@ -65,12 +65,12 @@
does that by looking for variable assignments in code as represented
by the regular expression: qr/\$[A-Za-z_].*=/ found in a comment. To
change that regex, pass one into the constructor as a key-value pair,
-where the key is 'coderegex' and the value is a qr() constructed
+where the key is 'commentedcoderegex' and the value is a qr() constructed
regex. Or specify them in your F<.perlcriticrc> file
like this:
[Bangs::ProhibitCommentedOutCode]
- coderegex = qr(\$[A-Za-z_].*=/)
+ commentedcoderegex = \$[A-Za-z_].*=/
=head1 AUTHOR