Skip to content

Commit

Permalink
Improve sanity checks in compiler and visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
kpdecker committed Aug 22, 2015
1 parent 729d9fd commit 2571dd8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/handlebars/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ Compiler.prototype = {
},

accept: function(node) {
/* istanbul ignore next: Sanity code */
if (!this[node.type]) {
throw new Exception('Unknown type: ' + node.type, node);
}

this.sourceNode.unshift(node);
let ret = this[node.type](node);
this.sourceNode.shift();
Expand Down
10 changes: 8 additions & 2 deletions lib/handlebars/compiler/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ Visitor.prototype = {
acceptKey: function(node, name) {
let value = this.accept(node[name]);
if (this.mutating) {
// Hacky sanity check:
if (value && typeof value.type !== 'string') {
// Hacky sanity check: This may have a few false positives for type for the helper
// methods but will generally do the right thing without a lot of overhead.
if (value && !Visitor.prototype[value.type]) {
throw new Exception('Unexpected node type "' + value.type + '" found when accepting ' + name + ' on ' + node.type);
}
node[name] = value;
Expand Down Expand Up @@ -49,6 +50,11 @@ Visitor.prototype = {
return;
}

/* istanbul ignore next: Sanity code */
if (!this[object.type]) {
throw new Exception('Unknown type: ' + object.type, object);
}

if (this.current) {
this.parents.unshift(this.current);
}
Expand Down

0 comments on commit 2571dd8

Please sign in to comment.