react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / handlebars / dist / cjs / handlebars / compiler / printer.js
80728 views"use strict";1var Visitor = require("./visitor")["default"];23function print(ast) {4return new PrintVisitor().accept(ast);5}67exports.print = print;function PrintVisitor() {8this.padding = 0;9}1011exports.PrintVisitor = PrintVisitor;PrintVisitor.prototype = new Visitor();1213PrintVisitor.prototype.pad = function(string) {14var out = "";1516for(var i=0,l=this.padding; i<l; i++) {17out = out + " ";18}1920out = out + string + "\n";21return out;22};2324PrintVisitor.prototype.Program = function(program) {25var out = '',26body = program.body,27i, l;2829if (program.blockParams) {30var blockParams = 'BLOCK PARAMS: [';31for(i=0, l=program.blockParams.length; i<l; i++) {32blockParams += ' ' + program.blockParams[i];33}34blockParams += ' ]';35out += this.pad(blockParams);36}3738for(i=0, l=body.length; i<l; i++) {39out = out + this.accept(body[i]);40}4142this.padding--;4344return out;45};4647PrintVisitor.prototype.MustacheStatement = function(mustache) {48return this.pad('{{ ' + this.SubExpression(mustache) + ' }}');49};5051PrintVisitor.prototype.BlockStatement = function(block) {52var out = "";5354out = out + this.pad('BLOCK:');55this.padding++;56out = out + this.pad(this.SubExpression(block));57if (block.program) {58out = out + this.pad('PROGRAM:');59this.padding++;60out = out + this.accept(block.program);61this.padding--;62}63if (block.inverse) {64if (block.program) { this.padding++; }65out = out + this.pad('{{^}}');66this.padding++;67out = out + this.accept(block.inverse);68this.padding--;69if (block.program) { this.padding--; }70}71this.padding--;7273return out;74};7576PrintVisitor.prototype.PartialStatement = function(partial) {77var content = 'PARTIAL:' + partial.name.original;78if(partial.params[0]) {79content += ' ' + this.accept(partial.params[0]);80}81if (partial.hash) {82content += ' ' + this.accept(partial.hash);83}84return this.pad('{{> ' + content + ' }}');85};8687PrintVisitor.prototype.ContentStatement = function(content) {88return this.pad("CONTENT[ '" + content.value + "' ]");89};9091PrintVisitor.prototype.CommentStatement = function(comment) {92return this.pad("{{! '" + comment.value + "' }}");93};9495PrintVisitor.prototype.SubExpression = function(sexpr) {96var params = sexpr.params, paramStrings = [], hash;9798for(var i=0, l=params.length; i<l; i++) {99paramStrings.push(this.accept(params[i]));100}101102params = "[" + paramStrings.join(", ") + "]";103104hash = sexpr.hash ? " " + this.accept(sexpr.hash) : "";105106return this.accept(sexpr.path) + " " + params + hash;107};108109PrintVisitor.prototype.PathExpression = function(id) {110var path = id.parts.join('/');111return (id.data ? '@' : '') + 'PATH:' + path;112};113114115PrintVisitor.prototype.StringLiteral = function(string) {116return '"' + string.value + '"';117};118119PrintVisitor.prototype.NumberLiteral = function(number) {120return "NUMBER{" + number.value + "}";121};122123PrintVisitor.prototype.BooleanLiteral = function(bool) {124return "BOOLEAN{" + bool.value + "}";125};126127PrintVisitor.prototype.Hash = function(hash) {128var pairs = hash.pairs;129var joinedPairs = [];130131for (var i=0, l=pairs.length; i<l; i++) {132joinedPairs.push(this.accept(pairs[i]));133}134135return 'HASH{' + joinedPairs.join(', ') + '}';136};137PrintVisitor.prototype.HashPair = function(pair) {138return pair.key + '=' + this.accept(pair.value);139};140141