react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / handlebars / dist / cjs / handlebars / compiler / code-gen.js
80728 views"use strict";1var isArray = require("../utils").isArray;23try {4var SourceMap = require('source-map'),5SourceNode = SourceMap.SourceNode;6} catch (err) {7/* istanbul ignore next: tested but not covered in istanbul due to dist build */8SourceNode = function(line, column, srcFile, chunks) {9this.src = '';10if (chunks) {11this.add(chunks);12}13};14/* istanbul ignore next */15SourceNode.prototype = {16add: function(chunks) {17if (isArray(chunks)) {18chunks = chunks.join('');19}20this.src += chunks;21},22prepend: function(chunks) {23if (isArray(chunks)) {24chunks = chunks.join('');25}26this.src = chunks + this.src;27},28toStringWithSourceMap: function() {29return {code: this.toString()};30},31toString: function() {32return this.src;33}34};35}363738function castChunk(chunk, codeGen, loc) {39if (isArray(chunk)) {40var ret = [];4142for (var i = 0, len = chunk.length; i < len; i++) {43ret.push(codeGen.wrap(chunk[i], loc));44}45return ret;46} else if (typeof chunk === 'boolean' || typeof chunk === 'number') {47// Handle primitives that the SourceNode will throw up on48return chunk+'';49}50return chunk;51}525354function CodeGen(srcFile) {55this.srcFile = srcFile;56this.source = [];57}5859CodeGen.prototype = {60prepend: function(source, loc) {61this.source.unshift(this.wrap(source, loc));62},63push: function(source, loc) {64this.source.push(this.wrap(source, loc));65},6667merge: function() {68var source = this.empty();69this.each(function(line) {70source.add([' ', line, '\n']);71});72return source;73},7475each: function(iter) {76for (var i = 0, len = this.source.length; i < len; i++) {77iter(this.source[i]);78}79},8081empty: function(loc) {82loc = loc || this.currentLocation || {start:{}};83return new SourceNode(loc.start.line, loc.start.column, this.srcFile);84},85wrap: function(chunk, loc) {86if (chunk instanceof SourceNode) {87return chunk;88}8990loc = loc || this.currentLocation || {start:{}};91chunk = castChunk(chunk, this, loc);9293return new SourceNode(loc.start.line, loc.start.column, this.srcFile, chunk);94},9596functionCall: function(fn, type, params) {97params = this.generateList(params);98return this.wrap([fn, type ? '.' + type + '(' : '(', params, ')']);99},100101quotedString: function(str) {102return '"' + (str + '')103.replace(/\\/g, '\\\\')104.replace(/"/g, '\\"')105.replace(/\n/g, '\\n')106.replace(/\r/g, '\\r')107.replace(/\u2028/g, '\\u2028') // Per Ecma-262 7.3 + 7.8.4108.replace(/\u2029/g, '\\u2029') + '"';109},110111objectLiteral: function(obj) {112var pairs = [];113114for (var key in obj) {115if (obj.hasOwnProperty(key)) {116var value = castChunk(obj[key], this);117if (value !== 'undefined') {118pairs.push([this.quotedString(key), ':', value]);119}120}121}122123var ret = this.generateList(pairs);124ret.prepend('{');125ret.add('}');126return ret;127},128129130generateList: function(entries, loc) {131var ret = this.empty(loc);132133for (var i = 0, len = entries.length; i < len; i++) {134if (i) {135ret.add(',');136}137138ret.add(castChunk(entries[i], this, loc));139}140141return ret;142},143144generateArray: function(entries, loc) {145var ret = this.generateList(entries, loc);146ret.prepend('[');147ret.add(']');148149return ret;150}151};152153exports["default"] = CodeGen;154155