Permalink
Browse files

fix a bug where TTerse does not create new scopes

  • Loading branch information...
1 parent 3bf785a commit 76e64504e138c25cda6e9ca6b546429bc5f5deaa @gfx gfx committed Dec 19, 2012
Showing with 87 additions and 7 deletions.
  1. +26 −7 lib/Text/Xslate/Syntax/TTerse.pm
  2. +61 −0 t/900_bugs/031_yappo.t
@@ -204,6 +204,10 @@ sub std_if {
my($parser, $symbol, $expr) = @_;
my $if = $symbol->clone(arity => "if");
+ my $is_modifier = defined $expr;
+
+ $parser->new_scope() unless $is_modifier; # whole if block
+
my $cond = $parser->expression(0);
if($symbol->id eq 'UNLESS') {
@@ -214,12 +218,17 @@ sub std_if {
}
$if->first($cond);
- if(defined $expr) { # statement modifier
+ if($is_modifier) {
$if->second([ $expr ]);
return $if;
}
- $if->second( $parser->statements() );
+ # then block
+ {
+ $parser->new_scope();
+ $if->second( $parser->statements() );
+ $parser->pop_scope();
+ }
my $t = $parser->token;
@@ -231,7 +240,13 @@ sub std_if {
my $elsif = $t->clone(arity => "if");
$elsif->first( $parser->expression(0) );
- $elsif->second( $parser->statements() );
+
+ {
+ $parser->new_scope();
+ $elsif->second( $parser->statements() );
+ $parser->pop_scope();
+ }
+
$if->third([$elsif]);
$if = $elsif;
$t = $parser->token;
@@ -246,13 +261,17 @@ sub std_if {
Carp::carp(sprintf "%s: Parsing ELSE-IF sequense as ELSIF, but it is likely to be a misuse of ELSE-IF. Please insert semicolon as ELSE; IF, or write it in the same line (around input line %d)", ref $parser, $t->line);
}
- $if->third( $t->id eq "IF"
- ? [$parser->statement()]
- : $parser->statements());
+ {
+ $parser->new_scope();
+ $if->third( $t->id eq "IF"
+ ? [$parser->statement()]
+ : $parser->statements());
+ $parser->pop_scope();
+ }
}
-
$parser->advance("END");
+ $parser->pop_scope();
return $top_if;
}
@@ -0,0 +1,61 @@
+#!perl
+# contributed by Yappo https://gist.github.com/4336310
+use strict;
+use warnings;
+use Test::More;
+
+use Text::Xslate;
+
+{
+ my $tx = Text::Xslate->new({
+ syntax => 'TTerse'
+ });
+ is eval {
+ $tx->render_string(<<'TMPL', { yappo => 'hoge' });
+[% SET yappo = "" -%]
+[% IF true -%]
+[% yappo = 'osawa' -%]
+[% yappo -%]
+[% ELSIF 0 -%]
+[% END -%]
+TMPL
+ }, "osawa";
+ is $@, '';
+}
+
+{
+ my $tx = Text::Xslate->new({
+ syntax => 'TTerse'
+ });
+ is eval {
+ $tx->render_string(<<'TMPL', { yappo => 'hoge' });
+[% SET yappo = 'fuga' -%]
+[% IF true -%]
+[% yappo = 'seiidaishogun' -%]
+[% ELSIF false -%]
+[% yappo = 'osawa' -%]
+[% END -%]
+[% yappo -%]
+TMPL
+ }, "seiidaishogun";
+ is $@, '';
+}
+
+{
+ my $tx = Text::Xslate->new({
+ syntax => 'TTerse'
+ });
+ is eval {
+ $tx->render_string(<<'TMPL', { yappo => 'hoge' });
+[% IF true -%]
+[% yappo = 'seiidaishogun' -%]
+[% ELSIF false -%]
+[% yappo = 'osawa' -%]
+[% END -%]
+[% yappo -%]
+TMPL
+ }, "hoge";
+ is $@, '';
+}
+
+done_testing;

0 comments on commit 76e6450

Please sign in to comment.