Module::Checkstyle::Check::Whitespace - Make sure whitespace is at correct places


Module-Checkstyle documentation Contained in the Module-Checkstyle distribution.

Index


Code Index:

NAME

Top

Module::Checkstyle::Check::Whitespace - Make sure whitespace is at correct places

CONFIGURATION DIRECTIVES

Top

Whitespace after comma

Checks that there is whitespace after a comma, for example as in my ($foo, $bar);. Enable it by setting after-comma to true.

after-comma = true

Whitespace before comma

Checks that there is whitespace before a comma, for example as in my ($foo ,$bar);. Enable it by setting before-comma to 1.

before-comma = true

Whitespace after fat comma

Checks that there is whitespace after a fat comma (=>), for example as in call(arg=> 1). Enable it by setting after-fat-comma to true.

after-fat-comma = true

Whitespace before fat comma

Checks that there is whitespace before a fat comma (=>), for example as in call(arg =>1). Enable it by setting before-fat-comma to true.

before-fat-comma = true

Whitespace after control keyword in compound statements

Checks that there is whitespace after a control-flow keyword in a compound statement. This means an if, elsif, else, while, for, foreach and continue (if (EXPR) { .. }) but not when they are used as statement modifiers (... if EXPR). For information on compound statements read Compound Statements in perlsyn. Enable this check by setting after-compound to a true value.

after-compound = true

SEE ALSO

Top

Writing configuration files. Format in Module::Checkstyle::Config

Module::Checkstyle


Module-Checkstyle documentation Contained in the Module-Checkstyle distribution.

package Module::Checkstyle::Check::Whitespace;

use strict;
use warnings;

use Carp qw(croak);
use Readonly;

use Module::Checkstyle::Util qw(:problem :args);

use base qw(Module::Checkstyle::Check);

# The directives we provide
Readonly my $AFTER_COMMA        => 'after-comma';
Readonly my $BEFORE_COMMA       => 'before-comma';
Readonly my $AFTER_FAT_COMMA    => 'after-fat-comma';
Readonly my $BEFORE_FAT_COMMA   => 'before-fat-comma';
Readonly my $AFTER_COMPOUND     => 'after-compound';

sub register {
    return (
            'PPI::Token::Operator'     => \&handle_operator,
            'PPI::Statement::Compound' => \&handle_compound,
        );
}

sub new {
    my ($class, $config) = @_;
    
    my $self = $class->SUPER::new($config);
    
    # Keep configuration local
    foreach ($AFTER_COMMA, $BEFORE_COMMA, $AFTER_FAT_COMMA, $BEFORE_FAT_COMMA, $AFTER_COMPOUND) {
        $self->{$_} = as_true($config->get_directive($_));
    }

    return $self;
}

sub handle_operator {
    my ($self, $operator, $file) = @_;

    my @problems;

    push @problems, $self->_handle_comma($operator, $file);
    push @problems, $self->_handle_fat_comma($operator, $file);
    
    return @problems;
}

sub _handle_comma {
    my ($self, $operator, $file) = @_;

    my @problems;

    if ($operator->content() eq ',') {
        if ($self->{$AFTER_COMMA}) {
            # Next sibling should be whitespace
            my $sibling = $operator->next_sibling();
            if ($sibling && ref $sibling && !$sibling->isa('PPI::Token::Whitespace')) {
                push @problems, new_problem($self->config, $AFTER_COMMA,
                                             qq(Missing whitespace after comma (,)),
                                             $operator, $file);
            }
        }
        
        if ($self->{$BEFORE_COMMA}) {
            # Previous sibling should be whitespace
            my $sibling = $operator->previous_sibling();
            if ($sibling && ref $sibling && !$sibling->isa('PPI::Token::Whitespace')) {
                push @problems, new_problem($self->config, $BEFORE_COMMA,
                                             qq(Missing whitespace before comma (,)),
                                             $operator, $file);
            }
        }
    }

    return @problems;
}

sub _handle_fat_comma {
    my ($self, $operator, $file) = @_;

    my @problems;
    
    if ($operator->content() eq '=>') {
        if ($self->{$AFTER_FAT_COMMA}) {
            # Next sibling should be whitespace
            my $sibling = $operator->next_sibling();
            if ($sibling && ref $sibling && !$sibling->isa('PPI::Token::Whitespace')) {
                push @problems, new_problem($self->config, $AFTER_FAT_COMMA,
                                             qq(Missing whitespace after fat comma (=>)),
                                             $operator, $file);
            }
        }

        if ($self->{$BEFORE_FAT_COMMA}) {
            # Previous sibling should be whitespace
            my $sibling = $operator->previous_sibling();
            if ($sibling && ref $sibling && !$sibling->isa('PPI::Token::Whitespace')) {
                push @problems, new_problem($self->config, $BEFORE_FAT_COMMA,
                                             qq(Missing whitespace before fat comma (=>)),
                                             $operator, $file);
            }
        }
    }

    return @problems;
}

sub handle_compound {
    my ($self, $compound, $file) = @_;

    my @problems;

    if ($self->{$AFTER_COMPOUND}) {
        my @children = $compound->schildren();

      CHECK_COMPOUND_BLOCK:
        foreach my $child (@children) {
            next CHECK_COMPOUND_BLOCK if !$child->isa('PPI::Token::Word');
            my $word = $child->content();
            my $sibling = $child->next_sibling();
            if (defined $sibling && ref $sibling && !$sibling->isa('PPI::Token::Whitespace')) {
                push @problems, new_problem($self->config, $AFTER_COMPOUND,
                                             qq('$word' is not followed by whitespace),
                                             $child, $file);
            }
        }
    }

    return @problems;
}

1;
__END__