Skip to content

Commit

Permalink
fix a bug where TTerse does not create new scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed Dec 19, 2012
1 parent 3bf785a commit 76e6450
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 7 deletions.
33 changes: 26 additions & 7 deletions lib/Text/Xslate/Syntax/TTerse.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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;
}

Expand Down
61 changes: 61 additions & 0 deletions t/900_bugs/031_yappo.t
Original file line number Diff line number Diff line change
@@ -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.