Skip to content

Commit

Permalink
make else clause also work in async for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
jlongster committed Sep 30, 2014
1 parent 357e41e commit da13ddc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
10 changes: 8 additions & 2 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,12 @@ var Compiler = Object.extend({
this.emitLine(this.buffer + ' += ' + output + ';');
}

if (node.else_) {
this.emitLine('if (!' + arr + '.length) {');
this.compile(node.else_, frame);
this.emitLine('}');
}

this.emitLine('frame = frame.pop();');
},

Expand Down Expand Up @@ -1085,9 +1091,9 @@ var Compiler = Object.extend({
});

// var c = new Compiler();
// var src = '{% macro foo() %}{% include "include.html" %}{% endmacro %} This is my template {{ foo() }}';
// var src = '{% asyncEach i in arr %}{{ i }}{% else %}empty{% endeach %}';
// var ast = transformer.transform(parser.parse(src));
//nodes.printNodes(ast);
// nodes.printNodes(ast);
// c.compile(ast);
// var tmpl = c.getCode();
// console.log(tmpl);
Expand Down
7 changes: 3 additions & 4 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ var Parser = Object.extend({
node.body = this.parseUntilBlocks(endBlock, 'else');

if(this.skipSymbol('else')) {
this.advanceAfterBlockEnd('else');
node.else_ = this.parseUntilBlocks(endBlock);
this.advanceAfterBlockEnd('else');
node.else_ = this.parseUntilBlocks(endBlock);
}

this.advanceAfterBlockEnd();
Expand Down Expand Up @@ -1153,8 +1153,7 @@ var Parser = Object.extend({
// console.log(util.inspect(t));
// }

// var p = new Parser(lexer.lex('hello {% foo %} {{ "hi" | bar }} {% endfoo %} end'));
// p.extensions = [new FooExtension()];
// var p = new Parser(lexer.lex('{% if not x %}foo{% endif %}'));
// var n = p.parseAsRoot();
// nodes.printNodes(n);

Expand Down
3 changes: 2 additions & 1 deletion src/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ function convertStatements(ast) {
node.colno,
node.arr,
node.name,
node.body
node.body,
node.else_
);
}
}
Expand Down
10 changes: 4 additions & 6 deletions tests/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,11 @@
equal('{% ' + block + ' i in arr %}{{ i }}{% ' + end + ' %}',
{ arr: [1, 2, 3, 4, 5] }, '12345');

if (block == 'for') {
equal('{% ' + block + ' i in arr %}{{ i }}{% else %}empty{% ' + end + ' %}',
{ arr: [1, 2, 3, 4, 5] }, '12345');
equal('{% ' + block + ' i in arr %}{{ i }}{% else %}empty{% ' + end + ' %}',
{ arr: [1, 2, 3, 4, 5] }, '12345');

equal('{% ' + block + ' i in arr %}{{ i }}{% else %}empty{% ' + end + ' %}',
{ arr: [] }, 'empty');
}
equal('{% ' + block + ' i in arr %}{{ i }}{% else %}empty{% ' + end + ' %}',
{ arr: [] }, 'empty');

equal('{% ' + block + ' a, b, c in arr %}' +
'{{ a }},{{ b }},{{ c }}.{% ' + end + ' %}',
Expand Down

0 comments on commit da13ddc

Please sign in to comment.