react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / handlebars / lib / handlebars / compiler / visitor.js
80713 viewsimport Exception from "../exception";1import AST from "./ast";23function Visitor() {4this.parents = [];5}67Visitor.prototype = {8constructor: Visitor,9mutating: false,1011// Visits a given value. If mutating, will replace the value if necessary.12acceptKey: function(node, name) {13var value = this.accept(node[name]);14if (this.mutating) {15// Hacky sanity check:16if (value && (!value.type || !AST[value.type])) {17throw new Exception('Unexpected node type "' + value.type + '" found when accepting ' + name + ' on ' + node.type);18}19node[name] = value;20}21},2223// Performs an accept operation with added sanity check to ensure24// required keys are not removed.25acceptRequired: function(node, name) {26this.acceptKey(node, name);2728if (!node[name]) {29throw new Exception(node.type + ' requires ' + name);30}31},3233// Traverses a given array. If mutating, empty respnses will be removed34// for child elements.35acceptArray: function(array) {36for (var i = 0, l = array.length; i < l; i++) {37this.acceptKey(array, i);3839if (!array[i]) {40array.splice(i, 1);41i--;42l--;43}44}45},4647accept: function(object) {48if (!object) {49return;50}5152if (this.current) {53this.parents.unshift(this.current);54}55this.current = object;5657var ret = this[object.type](object);5859this.current = this.parents.shift();6061if (!this.mutating || ret) {62return ret;63} else if (ret !== false) {64return object;65}66},6768Program: function(program) {69this.acceptArray(program.body);70},7172MustacheStatement: function(mustache) {73this.acceptRequired(mustache, 'path');74this.acceptArray(mustache.params);75this.acceptKey(mustache, 'hash');76},7778BlockStatement: function(block) {79this.acceptRequired(block, 'path');80this.acceptArray(block.params);81this.acceptKey(block, 'hash');8283this.acceptKey(block, 'program');84this.acceptKey(block, 'inverse');85},8687PartialStatement: function(partial) {88this.acceptRequired(partial, 'name');89this.acceptArray(partial.params);90this.acceptKey(partial, 'hash');91},9293ContentStatement: function(/* content */) {},94CommentStatement: function(/* comment */) {},9596SubExpression: function(sexpr) {97this.acceptRequired(sexpr, 'path');98this.acceptArray(sexpr.params);99this.acceptKey(sexpr, 'hash');100},101PartialExpression: function(partial) {102this.acceptRequired(partial, 'name');103this.acceptArray(partial.params);104this.acceptKey(partial, 'hash');105},106107PathExpression: function(/* path */) {},108109StringLiteral: function(/* string */) {},110NumberLiteral: function(/* number */) {},111BooleanLiteral: function(/* bool */) {},112113Hash: function(hash) {114this.acceptArray(hash.pairs);115},116HashPair: function(pair) {117this.acceptRequired(pair, 'value');118}119};120121export default Visitor;122123124