From 48bc63c8b67fe58eb3da1f3c1851fbbcac2b8251 Mon Sep 17 00:00:00 2001 From: Claes Jakobsson Date: Tue, 17 Jan 2012 00:12:23 +0100 Subject: [PATCH] Added not-followed-by and not-preceded-by so we can check stuff like no whitespace before comma --- lib/Pg/Checksource/RuleBuilder/TokenRule.pm | 26 +++++++++++++++++++++ lib/Pg/Checksource/RuleGrammar.pm | 2 ++ rules/whitespace.pcs | 5 ++++ test.sql | 2 +- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/Pg/Checksource/RuleBuilder/TokenRule.pm b/lib/Pg/Checksource/RuleBuilder/TokenRule.pm index 8b3f698..770afa7 100644 --- a/lib/Pg/Checksource/RuleBuilder/TokenRule.pm +++ b/lib/Pg/Checksource/RuleBuilder/TokenRule.pm @@ -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}}; @@ -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}}; diff --git a/lib/Pg/Checksource/RuleGrammar.pm b/lib/Pg/Checksource/RuleGrammar.pm index 0059b93..afe77ff 100644 --- a/lib/Pg/Checksource/RuleGrammar.pm +++ b/lib/Pg/Checksource/RuleGrammar.pm @@ -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; } diff --git a/rules/whitespace.pcs b/rules/whitespace.pcs index 9124c1b..7f8f79b 100644 --- a/rules/whitespace.pcs +++ b/rules/whitespace.pcs @@ -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; }; \ No newline at end of file diff --git a/test.sql b/test.sql index b250d93..df97cb6 100644 --- a/test.sql +++ b/test.sql @@ -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; \ No newline at end of file