Skip to content

Commit

Permalink
Added not-followed-by and not-preceded-by so we can check stuff like …
Browse files Browse the repository at this point in the history
…no whitespace before comma
  • Loading branch information
claesjac committed Jan 16, 2012
1 parent bcf5588 commit 48bc63c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
26 changes: 26 additions & 0 deletions lib/Pg/Checksource/RuleBuilder/TokenRule.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ sub build {
};
}

if ($desc->{not_preceded_by}) {
my @prev_types = @{$desc->{not_preceded_by}};

push @checks, sub {
my $pt = $_[1]->previous;
return -1 unless $pt;
my $ptt = $pt->type;
return 0 if any { ref $_ eq "Regexp" ? $ptt =~ $_ : $ptt eq $_ } @prev_types;

1;
};
}

if ($desc->{followed_by}) {
my @follow_types = @{$desc->{followed_by}};

Expand All @@ -44,6 +57,19 @@ sub build {
1;
};
}

if ($desc->{not_followed_by}) {
my @follow_types = @{$desc->{not_followed_by}};

push @checks, sub {
my $ft = $_[1]->following;
return -1 unless $ft;
my $ftt = $ft->type;
return 0 if any { ref $_ eq "Regexp" ? $ftt =~ $_ : $ftt eq $_ } @follow_types;

1;
};
}

if ($desc->{exclude_ci}) {
my %exclude = map { lc($_) => 1 } @{$desc->{exclude_ci}};
Expand Down
2 changes: 2 additions & 0 deletions lib/Pg/Checksource/RuleGrammar.pm
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ sub parse_token_rule {
$self->any_of(
sub { $self->expect("type:"); $rule->{types} = $self->parse_variable_or_list; },
sub { $self->expect("preceded-by:"); $rule->{'preceded_by'} = $self->parse_variable_or_list; },
sub { $self->expect("not-preceded-by:"); $rule->{'not_preceded_by'} = $self->parse_variable_or_list; },
sub { $self->expect("followed-by:"); $rule->{'followed_by'} = $self->parse_variable_or_list; },
sub { $self->expect("not-followed-by:"); $rule->{'not_followed_by'} = $self->parse_variable_or_list; },
sub { $self->expect("exclude-ci:"); $rule->{'exclude_ci'} = $self->parse_variable_or_list; },
sub { $self->expect("only-ci:"); $rule->{'only_ci'} = $self->parse_variable_or_list; },
sub { $self->expect("matches:"); $rule->{matches} = $self->token_string; }
Expand Down
5 changes: 5 additions & 0 deletions rules/whitespace.pcs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ token "whitespace-after-operator" {
token "whitespace-after-comma" {
type: COMMA;
followed-by: WHITESPACE;
};

token "no-whitespace-before-comma" {
type: COMMA;
not-preceded-by: WHITESPACE;
};
2 changes: 1 addition & 1 deletion test.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
select COUNT(*), x*y,SUM(CASE WHEN Foo IS NOT NULL THEN 1 ELSE 0 END)
select COUNT(*) , x*y,SUM(CASE WHEN Foo IS NOT NULL THEN 1 ELSE 0 END)
FROM Bar
WHERE Quax = $1 and Datestamp>=now() -'1 hour'::interval;

0 comments on commit 48bc63c

Please sign in to comment.