react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / handlebars / dist / cjs / handlebars / compiler / visitor.js
80728 views"use strict";1var Exception = require("../exception")["default"];2var AST = require("./ast")["default"];34function Visitor() {5this.parents = [];6}78Visitor.prototype = {9constructor: Visitor,10mutating: false,1112// Visits a given value. If mutating, will replace the value if necessary.13acceptKey: function(node, name) {14var value = this.accept(node[name]);15if (this.mutating) {16// Hacky sanity check:17if (value && (!value.type || !AST[value.type])) {18throw new Exception('Unexpected node type "' + value.type + '" found when accepting ' + name + ' on ' + node.type);19}20node[name] = value;21}22},2324// Performs an accept operation with added sanity check to ensure25// required keys are not removed.26acceptRequired: function(node, name) {27this.acceptKey(node, name);2829if (!node[name]) {30throw new Exception(node.type + ' requires ' + name);31}32},3334// Traverses a given array. If mutating, empty respnses will be removed35// for child elements.36acceptArray: function(array) {37for (var i = 0, l = array.length; i < l; i++) {38this.acceptKey(array, i);3940if (!array[i]) {41array.splice(i, 1);42i--;43l--;44}45}46},4748accept: function(object) {49if (!object) {50return;51}5253if (this.current) {54this.parents.unshift(this.current);55}56this.current = object;5758var ret = this[object.type](object);5960this.current = this.parents.shift();6162if (!this.mutating || ret) {63return ret;64} else if (ret !== false) {65return object;66}67},6869Program: function(program) {70this.acceptArray(program.body);71},7273MustacheStatement: function(mustache) {74this.acceptRequired(mustache, 'path');75this.acceptArray(mustache.params);76this.acceptKey(mustache, 'hash');77},7879BlockStatement: function(block) {80this.acceptRequired(block, 'path');81this.acceptArray(block.params);82this.acceptKey(block, 'hash');8384this.acceptKey(block, 'program');85this.acceptKey(block, 'inverse');86},8788PartialStatement: function(partial) {89this.acceptRequired(partial, 'name');90this.acceptArray(partial.params);91this.acceptKey(partial, 'hash');92},9394ContentStatement: function(/* content */) {},95CommentStatement: function(/* comment */) {},9697SubExpression: function(sexpr) {98this.acceptRequired(sexpr, 'path');99this.acceptArray(sexpr.params);100this.acceptKey(sexpr, 'hash');101},102PartialExpression: function(partial) {103this.acceptRequired(partial, 'name');104this.acceptArray(partial.params);105this.acceptKey(partial, 'hash');106},107108PathExpression: function(/* path */) {},109110StringLiteral: function(/* string */) {},111NumberLiteral: function(/* number */) {},112BooleanLiteral: function(/* bool */) {},113114Hash: function(hash) {115this.acceptArray(hash.pairs);116},117HashPair: function(pair) {118this.acceptRequired(pair, 'value');119}120};121122exports["default"] = Visitor;123124