react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / handlebars / dist / amd / handlebars / compiler / code-gen.js
80738 viewsdefine(1["../utils","exports"],2function(__dependency1__, __exports__) {3"use strict";4var isArray = __dependency1__.isArray;56try {7var SourceMap = require('source-map'),8SourceNode = SourceMap.SourceNode;9} catch (err) {10/* istanbul ignore next: tested but not covered in istanbul due to dist build */11SourceNode = function(line, column, srcFile, chunks) {12this.src = '';13if (chunks) {14this.add(chunks);15}16};17/* istanbul ignore next */18SourceNode.prototype = {19add: function(chunks) {20if (isArray(chunks)) {21chunks = chunks.join('');22}23this.src += chunks;24},25prepend: function(chunks) {26if (isArray(chunks)) {27chunks = chunks.join('');28}29this.src = chunks + this.src;30},31toStringWithSourceMap: function() {32return {code: this.toString()};33},34toString: function() {35return this.src;36}37};38}394041function castChunk(chunk, codeGen, loc) {42if (isArray(chunk)) {43var ret = [];4445for (var i = 0, len = chunk.length; i < len; i++) {46ret.push(codeGen.wrap(chunk[i], loc));47}48return ret;49} else if (typeof chunk === 'boolean' || typeof chunk === 'number') {50// Handle primitives that the SourceNode will throw up on51return chunk+'';52}53return chunk;54}555657function CodeGen(srcFile) {58this.srcFile = srcFile;59this.source = [];60}6162CodeGen.prototype = {63prepend: function(source, loc) {64this.source.unshift(this.wrap(source, loc));65},66push: function(source, loc) {67this.source.push(this.wrap(source, loc));68},6970merge: function() {71var source = this.empty();72this.each(function(line) {73source.add([' ', line, '\n']);74});75return source;76},7778each: function(iter) {79for (var i = 0, len = this.source.length; i < len; i++) {80iter(this.source[i]);81}82},8384empty: function(loc) {85loc = loc || this.currentLocation || {start:{}};86return new SourceNode(loc.start.line, loc.start.column, this.srcFile);87},88wrap: function(chunk, loc) {89if (chunk instanceof SourceNode) {90return chunk;91}9293loc = loc || this.currentLocation || {start:{}};94chunk = castChunk(chunk, this, loc);9596return new SourceNode(loc.start.line, loc.start.column, this.srcFile, chunk);97},9899functionCall: function(fn, type, params) {100params = this.generateList(params);101return this.wrap([fn, type ? '.' + type + '(' : '(', params, ')']);102},103104quotedString: function(str) {105return '"' + (str + '')106.replace(/\\/g, '\\\\')107.replace(/"/g, '\\"')108.replace(/\n/g, '\\n')109.replace(/\r/g, '\\r')110.replace(/\u2028/g, '\\u2028') // Per Ecma-262 7.3 + 7.8.4111.replace(/\u2029/g, '\\u2029') + '"';112},113114objectLiteral: function(obj) {115var pairs = [];116117for (var key in obj) {118if (obj.hasOwnProperty(key)) {119var value = castChunk(obj[key], this);120if (value !== 'undefined') {121pairs.push([this.quotedString(key), ':', value]);122}123}124}125126var ret = this.generateList(pairs);127ret.prepend('{');128ret.add('}');129return ret;130},131132133generateList: function(entries, loc) {134var ret = this.empty(loc);135136for (var i = 0, len = entries.length; i < len; i++) {137if (i) {138ret.add(',');139}140141ret.add(castChunk(entries[i], this, loc));142}143144return ret;145},146147generateArray: function(entries, loc) {148var ret = this.generateList(entries, loc);149ret.prepend('[');150ret.add(']');151152return ret;153}154};155156__exports__["default"] = CodeGen;157});158159