Skip to content

Commit

Permalink
Provide stubbed visitor implementation
Browse files Browse the repository at this point in the history
Part of #889
  • Loading branch information
kpdecker committed Nov 6, 2014
1 parent 632fadc commit a7000f8
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
60 changes: 58 additions & 2 deletions lib/handlebars/compiler/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,64 @@ Visitor.prototype = {
constructor: Visitor,

accept: function(object) {
return this[object.type](object);
}
return object && this[object.type] && this[object.type](object);
},

program: function(program) {
var statements = program.statements,
i, l;

for(i=0, l=statements.length; i<l; i++) {
this.accept(statements[i]);
}
},

block: function(block) {
this.accept(block.mustache);
this.accept(block.program);
this.accept(block.inverse);
},

mustache: function(mustache) {
this.accept(mustache.sexpr);
},

sexpr: function(sexpr) {
var params = sexpr.params, paramStrings = [], hash;

this.accept(sexpr.id);
for(var i=0, l=params.length; i<l; i++) {
this.accept(params[i]);
}
this.accept(sexpr.hash);
},

hash: function(hash) {
var pairs = hash.pairs;

for(var i=0, l=pairs.length; i<l; i++) {
this.accept(pairs[i][1]);
}
},

partial: function(partial) {
this.accept(partial.partialName);
this.accept(partial.context);
this.accept(partial.hash);
},
PARTIAL_NAME: function(partialName) {},

DATA: function(data) {
this.accept(data.id);
},

STRING: function(string) {},
NUMBER: function(number) {},
BOOLEAN: function(bool) {},
ID: function(id) {},

content: function(content) {},
comment: function(comment) {}
};

export default Visitor;
48 changes: 48 additions & 0 deletions spec/visitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*global Handlebars */

describe('Visitor', function() {
if (!Handlebars.Visitor) {
return;
}

function ast_for(template) {
var ast = Handlebars.parse(template);
return Handlebars.print(ast);
}

it('should provide coverage', function() {
// Simply run the thing and make sure it does not fail and that all of the
// stub methods are executed
var visitor = new Handlebars.Visitor();
visitor.accept(Handlebars.parse('{{#foo (bar 1 "1" true) foo=@data}}{{!comment}}{{> bar }} {{/foo}}'));
});

it('should traverse to stubs', function() {
var visitor = new Handlebars.Visitor();

visitor.PARTIAL_NAME = function(partialName) {
equal(partialName.name, 'bar');
};

visitor.STRING = function(string) {
equal(string.string, '2');
};
visitor.NUMBER = function(number) {
equal(number.stringModeValue, 1);
};
visitor.BOOLEAN = function(bool) {
equal(bool.stringModeValue, true);
};
visitor.ID = function(id) {
equal(id.original, 'foo.bar');
};
visitor.content = function(content) {
equal(content.string, ' ');
};
visitor.comment = function(comment) {
equal(comment.comment, 'comment');
};

visitor.accept(Handlebars.parse('{{#foo.bar (foo.bar 1 "2" true) [email protected]}}{{!comment}}{{> bar }} {{/foo.bar}}'));
});
});

0 comments on commit a7000f8

Please sign in to comment.