我们如何使用Perl :: Tidy或Perl :: Critic来收集旁注?

Nik*_*nko 5 perl comments perl-critic perl-tidy

我的部门目前正在解决一些通用代码的最佳实践,这是我们想有所加强,为开发人员提供Perl::TidyPerl::Critic配置.

现在我们遇到了附带评论的问题.附注是这样的:

my $counter = 0;  # Reset counter
Run Code Online (Sandbox Code Playgroud)

我们宁愿根本没有侧面评论,因为在大多数情况下,它们可以写在有问题的代码之上,在那里它们更容易阅读.如果可能的话,一个Perl::Tidy解决方案将是完美的,这会将一个侧面评论移到它上面的一行,第二个最好的将是一个Perl::Critic政策(我在CPAN没有找到)和第三个最好的,最后一个将是开发人员在进行代码审查时要注意将这些意见指出.

是否可以用Perl::Tidy或实施Perl::Critic

Cha*_*ens 17

我认为这对你有用(如果我明白你想要的话):

package Perl::Critic::Policy::CodeLayout::NoSideComments;

use strict;
use warnings;

use Readonly;

use Perl::Critic::Utils qw{ :severities :classification :ppi };
use parent 'Perl::Critic::Policy';

our $VERSION = 20090904;

Readonly::Scalar my $DESC => "side comments are not allowed";
Readonly::Scalar my $EXPL => "put the comment above the line, not next to it";

sub supported_parameters { return                       }
sub default_severity     { return 5                     }
sub default_themes       { return qw( custom )          }
sub applies_to           { return 'PPI::Token::Comment' }

sub violates {
    my ($self, $elem) = @_;

    #look backwards until you find whitespace that contains a 
    #newline (good) or something other than whitespace (error)

    my $prev = $elem->previous_sibling;
    while ($prev) {
        return $self->violation( $DESC, $EXPL, $elem )
            unless $prev->isa("PPI::Token::Whitespace");
        return if $prev->content =~ /\n/;
        $prev = $prev->previous_sibling;
    }

    #catch # after a block start, but leave the #! line alone
    return $self->violation( $DESC, $EXPL, $elem )
        unless $elem->parent->isa("PPI::Document");
    return;
}

1;
Run Code Online (Sandbox Code Playgroud)